Author Topic: Purpleair PHP Retrieval script  (Read 27194 times)

0 Members and 1 Guest are viewing this topic.

Offline Forever

  • Senior Contributor
  • ****
  • Posts: 209
Re: Purpleair PHP Retrieval script
« Reply #75 on: March 11, 2018, 04:01:59 PM »
Yah, the odd think is I can still pull the json file with my web browser but the script fails.

Offline Forever

  • Senior Contributor
  • ****
  • Posts: 209
Re: Purpleair PHP Retrieval script
« Reply #76 on: March 11, 2018, 04:29:57 PM »
Seems to be a problem with the SSL certificate.

The most resent cacert.pem removed some entries causing the issue.
I reverted back to an older version.
« Last Edit: March 11, 2018, 04:33:12 PM by Forever »

Offline giordolo

  • Member
  • *
  • Posts: 5
Re: Purpleair PHP Retrieval script
« Reply #77 on: March 11, 2018, 05:02:00 PM »
Im live without changes
this is happening too often

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #78 on: March 12, 2018, 07:33:05 AM »
Toxic -
This happens in PHP7 because it is more strict about variable declaration and usage. If error reporting has E_ALL, then your going to be seeing that from other scripts as well. It's still something I need to fix but it doesn't really mean anythings broken (as far as I know). I get them from Meteotemplate in my error log as well, if you don't want to see those messages (there mostly for debugging of scripts), change E_ALL to something like E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR  (Show only errors) in your PHP config file.

As for the script not working today, first off, there is nothing I can do. The script doesn't have a mind of its own and decides it's having a bad day - if it was working and then stops working, something broke on your server or outside of the script. Purpleair's servers the last couple months have been a bit flaky here and there. You all need to remember the server and your machine are in separate locations on separate networks with (usually) separate routes - one route could be having issues (the server) while your PC isn't. There has been many famous cases of things like Google getting knocked offline for everyone who had an ISP that used Level3 for the route. What actually happened I have no idea.

Second, giordolo, based on your comments, you seem to be blaming my script for your troubles. If you don't like the script, don't use it and make something better. Instead of only complaining about the lack of something to retrieve the data, I did something about it and wrote one to do it, but that seems to not satisfy you. It took a lot of research and figuring things out too. Now if I am misinterpreting your comments my apologies but the way I read into them I don't see that.

If there is a bona fide problem with the script, I will be happy to fix it and typically tackle it pretty quick.


« Last Edit: March 12, 2018, 07:44:58 AM by azchrisf »
Davis Vantage Pro 2 Plus 6163 w/ 8 Transmitters!
Also doing Soil and Leaf 4x
WU: KAZGLEND106 CWOP: FW1398 (F1398) Purpleair: 98793/LAZGLEND8
My setup:
https://www.wxforum.net/index.php?topic=41867.0

Offline giordolo

  • Member
  • *
  • Posts: 5
Re: Purpleair PHP Retrieval script
« Reply #79 on: March 12, 2018, 08:11:36 AM »
Oh No!
First of all I ask sorry if I have created the misunderstanding: I'm absolutely not blaming your script!
I'm sorry but my english is very poor: trust me, I really appreciate open source projects, I know that behind there is a loto of work and time spent on.
I use your script, I will use your script and I try to let you could improve it managing errors that are happenings.
Unfortunately I could only notify to you the errors; I'm not a developer but only a weather fan that tries to arrange a site in order to have some data :-)
Regarding the point, I dont understand how a script could stop and start to work without an external change; this is the why I was asking if purpleair api or thingspeak api were up during my failures. When I have said "this is happening too often" I was simply referring to the fact that it is happening too often; not blaming the script
I ask sorry again

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #80 on: March 12, 2018, 09:26:00 AM »
Ok no worries then I misunderstood you

Sent from my Pixel using Tapatalk

Davis Vantage Pro 2 Plus 6163 w/ 8 Transmitters!
Also doing Soil and Leaf 4x
WU: KAZGLEND106 CWOP: FW1398 (F1398) Purpleair: 98793/LAZGLEND8
My setup:
https://www.wxforum.net/index.php?topic=41867.0

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #81 on: March 12, 2018, 10:36:42 AM »
People are emailing me wanting local sensor data for upload to the web.
The script runs on PHP, which technically requires a webserver to work correctly. I would have to rewrite the entire script to use the local data. So in other words, this just isn't going to happen.

Now, I could write a small program that runs locally that will then upload via FTP the data, but I am not going to do that for free, I would need to charge for the program since it would take more of my already limited free time plus my professional responsibilities. If anyone else can do it I'd love for you to do that for the benefit of all.

I will be making a video explaining how to configure the script, but something I need to point out - PHP and web servers are not for beginners. You need to learn at least a little on how they work and how to work with them or you will be forever lost. ;-)
« Last Edit: March 12, 2018, 10:39:34 AM by azchrisf »
Davis Vantage Pro 2 Plus 6163 w/ 8 Transmitters!
Also doing Soil and Leaf 4x
WU: KAZGLEND106 CWOP: FW1398 (F1398) Purpleair: 98793/LAZGLEND8
My setup:
https://www.wxforum.net/index.php?topic=41867.0

Offline Forever

  • Senior Contributor
  • ****
  • Posts: 209
Re: Purpleair PHP Retrieval script
« Reply #82 on: March 12, 2018, 11:35:26 AM »
I currently pull the real-time, PM1, PM2.5 and PM10 data directly from my PurpleAir.

The problem is the short-term, 30 minute, 1 hour, 6 hours and other reading are not on the PurpleAir device. The values are calculated on the PurpleAir servers and then made available. The only way to get this kind of data would be to store it in sql and calculate the vales.

Like azchrisf said this is not an easy task.

I can provide a small script to pull just the current value from the local PurpleAir so that people can play around with it.

Code: [Select]
<?php

//Get the sensor data via JSON
$pajsona = @file_get_contents('http://192.168.1.103/json');
$json_resulta=json_decode($pajsonatrue);

//Create our variables for different Sensor results
$pm25a $json_resulta['pm2_5_cf_1'];
$pm25b $json_resulta['pm2_5_cf_1_b'];

$aqia aqiFromPM($pm25a);
$aqib aqiFromPM($pm25b);
$AQI round(($aqia $aqib)/2);

//Function to get AQI number from PPM reading
function aqiFromPM ($pm) {
if (is_nan($pm)) {
return "-";
}
if (isset($pm) == false) {
return "Error: No value";
}
if ($pm 0.0) {
return $pm;
}
if ($pm 1000.0) {
return "-";
}
if ($pm 350.5) {
return calcAQI($pm500.0401.0500.0350.5);
} else if ($pm 250.5) {
    return 
calcAQI($pm400.0301.0350.4250.5);
  } else if (
$pm 150.5) {
    return 
calcAQI($pm300.0201.0250.4150.5);
  } else if (
$pm 55.5) {
    return 
calcAQI($pm200.0151.0150.455.5);
  } else if (
$pm 35.5) {
    return 
calcAQI($pm150.0101.055.435.5);
  } else if (
$pm 12.1) {
    return 
calcAQI($pm100.051.035.412.1);
  } else if (
$pm >= 0.0) {
    return 
calcAQI($pm50.00.012.00.0);
  } else {
    return 
"-";
  }
}

//Function that actually calculates the AQI number
function calcAQI($Cp$Ih$Il$BPh$BPl) {
  
$a = ($Ih $Il);
  
$b = ($BPh $BPl);
  
$c = ($Cp $BPl);
  return 
round(($a/$b) * $c $Il);     
}

//Function that gets the AQI's description
function getAQIDescription($aqinum) {
  if (
$aqinum >= 401) {
  
return 'Hazardous';
  } else if (
$aqinum >= 301) {
  
return 'Hazardous';
  } else if (
$aqinum >= 201) {
  
return 'Very Unhealthy';
  } else if (
$aqinum >= 151) {
  
return 'Unhealthy';
  } else if (
$aqinum >= 101) {
  
return 'Unhealthy for Sensitive Groups';
  } else if (
$aqinum >= 51) {
  
return 'Moderate';
  } else if (
$aqinum >= 0) {
  
return 'Good';
  } else {
  
return 'Unknown';
  }
}

?>

Thinking about this a little more this may not work for others, my server is on my local network so it has access to my PurpleAir.

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #83 on: March 21, 2018, 07:57:35 PM »
Anyone notice that certainty graph they got up there now? How do you think it applies to this script (should I add it)?
Davis Vantage Pro 2 Plus 6163 w/ 8 Transmitters!
Also doing Soil and Leaf 4x
WU: KAZGLEND106 CWOP: FW1398 (F1398) Purpleair: 98793/LAZGLEND8
My setup:
https://www.wxforum.net/index.php?topic=41867.0

Offline i_fiorentino

  • Forecaster
  • *****
  • Posts: 562
  • Davis Vantage Vue, Boltek Stormtr., Mobotix M22-IT
    • Meteopistoia.it
Re: Purpleair PHP Retrieval script
« Reply #84 on: April 15, 2018, 05:47:43 AM »
Hi guys,
thanks for sharing this script!

I've just installed my purple air sensor and installed script here: http://www.meteopistoia.it/custom/purpleair.php

Just a question.
Is there a meteotemplate block to sharing Pm10 / PM2.5?

Thansk in advance.
Regards,


Alessandro

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #85 on: April 15, 2018, 08:02:26 AM »
Meteotemplate has a block available but it does not show AQI, only ug/m3 counts.
Davis Vantage Pro 2 Plus 6163 w/ 8 Transmitters!
Also doing Soil and Leaf 4x
WU: KAZGLEND106 CWOP: FW1398 (F1398) Purpleair: 98793/LAZGLEND8
My setup:
https://www.wxforum.net/index.php?topic=41867.0

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #86 on: April 15, 2018, 08:09:24 AM »
Just wanted to point this out -

From further research, Purpleair is using the math for 24 hour weighted average for instant PM 2.5, which from my understanding, is wrong. You can see they are using 24 hours values in the script (I C/P verbatim except for conversion to PHP from their JS) from https://en.wikipedia.org/wiki/Air_quality_index#Computing_the_AQI. There really is no way I can see you can get an "instant" AQI - all the formulas need 24 hours of data, and NowCast looks like it needs at least 2 or more (2 works when I use their calculator).

Furthermore, the PM10 formula is also a 24 hour weighted average, and that is incorrect as well.

Someone needs to sit down and figure out the proper values for an AQI using an instantaneous read - IF it's even possible, like I said it doesn't seem to be. It's beyond my understanding as many times as I try to figure out the math, etc. for it, but I can see having an analytical personality that the math they are using (and likewise I am using in the script) for what they are doing vs. what it is meant for is wrong.

In order to actually to display the AQI bona-fide as the EPA does it, I would have to re-write the script to log values over a 24 hour period, then every hour do math to give a new AQI.

I'd love to hear comments from you guys.
« Last Edit: April 15, 2018, 08:13:21 AM by azchrisf »
Davis Vantage Pro 2 Plus 6163 w/ 8 Transmitters!
Also doing Soil and Leaf 4x
WU: KAZGLEND106 CWOP: FW1398 (F1398) Purpleair: 98793/LAZGLEND8
My setup:
https://www.wxforum.net/index.php?topic=41867.0

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: Purpleair PHP Retrieval script
« Reply #87 on: April 15, 2018, 09:04:57 AM »
Just wanted to point this out -

From further research, Purpleair is using the math for 24 hour weighted average for instant PM 2.5, which from my understanding, is wrong. You can see they are using 24 hours values in the script (I C/P verbatim except for conversion to PHP from their JS) from https://en.wikipedia.org/wiki/Air_quality_index#Computing_the_AQI. There really is no way I can see you can get an "instant" AQI - all the formulas need 24 hours of data, and NowCast looks like it needs at least 2 or more (2 works when I use their calculator).

Furthermore, the PM10 formula is also a 24 hour weighted average, and that is incorrect as well.

Someone needs to sit down and figure out the proper values for an AQI using an instantaneous read - IF it's even possible, like I said it doesn't seem to be. It's beyond my understanding as many times as I try to figure out the math, etc. for it, but I can see having an analytical personality that the math they are using (and likewise I am using in the script) for what they are doing vs. what it is meant for is wrong.

In order to actually to display the AQI bona-fide as the EPA does it, I would have to re-write the script to log values over a 24 hour period, then every hour do math to give a new AQI.

I'd love to hear comments from you guys.

The health effects of AQ are based on a continuous exposure to the measured value.
If the measured value  xyz remains the same for 24 hours the health effects would be abc

When calculating an AQ-index I display the current ("instant" AQI) with a warning that the health effects for that index are for a 24 hour period.

The Luftdaten and the PurpleAir sensor allow uploading the raw data to ones own website where calculation of "instant" and "24 hour" AQI can easily be done.

But  far more important than using "instant" or "24 hour" values, is the quality of the sensor itself.

After almost 2 months testing the PurpleAir sensor,  it turns out that the measured values (p/um3  or ug/m3)  are always higher then the official stations.

I attached a screenshot, the legenda with the health effects and the index are for Belgium/Europe not the US ones.
Top part is a Luftdaten sensor, always in the same range as the official measuring station. As are most of the 50+ Luftdaten sensors in our city. Check  current Luftdaten page at my website

The Purpleair sensor at the bottom, is at the same location and always measures slightly more in the low AQ  to a lot higher in the high AQ.  Check  current PurpleAir page at my website

There is also a website where we compare our 50+ Luftdaten stations with the nearby official validated measurements. It uses the Dutch language as that is the official language here. https://leuvenair.be/in-detail/grafieken/
I attached a graph comparing my station with the official validated measurements.

Wim
« Last Edit: April 15, 2018, 09:13:17 AM by wvdkuil »

Offline i_fiorentino

  • Forecaster
  • *****
  • Posts: 562
  • Davis Vantage Vue, Boltek Stormtr., Mobotix M22-IT
    • Meteopistoia.it
Re: Purpleair PHP Retrieval script
« Reply #88 on: April 16, 2018, 06:57:18 PM »
Hi guys,
before all, thanks for sharing this script!!  =D&gt; =D&gt;
i would show also pm2.5.

Which is the name of variable to show pm2.5? $aqst?
And what about Pm2.5 30min, 60min, 1hr, and 1week?


Thanks in advance.
Regards,


Alessandro

Offline ALITTLEweird1

  • Mark
  • Global Moderator
  • Forecaster
  • *****
  • Posts: 923
    • North Bend Weather
Re: Purpleair PHP Retrieval script
« Reply #89 on: August 08, 2018, 08:25:56 PM »
Why does the reading using the script show 18, but on the purple air map it shows 55?
"Nature can do without man, but man cannot do without nature."


Software: WeatherDisplay
Hardware: Davis VP2 + VP2 Solar + VP2 UV + Lightning Detector + Logitech Webcam + Soil temp + Soil Moisture

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: Purpleair PHP Retrieval script
« Reply #90 on: August 09, 2018, 07:35:59 AM »
Why does the reading using the script show 18, but on the purple air map it shows 55?
The Purpleair map shows realtime values for PM2.5 for your location here  When I write this: AQHI is 70 for PM2.5 count of 21µg/m3

But one can not calculate the official AQHI based on a realtime value, it should be the 24 hour average value. At his moment your 24 hours value  from the  same Purpleair data is 4.45 µg/m3 with  an correct 24 hours AQHI of 19. I assume that your script also calculates using the 24 hour value.

I put your sensorID 14033 in the demo version of my script at  https://leuven-template.eu/purpleair/_test1.php

You can check all PM2.5 values and the corresponding theoretical / correct AQHI for your sensor and compare those with your script.

Wim
« Last Edit: August 09, 2018, 07:45:28 AM by wvdkuil »

Offline ALITTLEweird1

  • Mark
  • Global Moderator
  • Forecaster
  • *****
  • Posts: 923
    • North Bend Weather
Re: Purpleair PHP Retrieval script
« Reply #91 on: August 09, 2018, 09:14:56 AM »
I was just thinking the number on the purple air map would match the script.
"Nature can do without man, but man cannot do without nature."


Software: WeatherDisplay
Hardware: Davis VP2 + VP2 Solar + VP2 UV + Lightning Detector + Logitech Webcam + Soil temp + Soil Moisture

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: Purpleair PHP Retrieval script
« Reply #92 on: August 09, 2018, 09:58:00 AM »
I was just thinking the number on the purple air map would match the script.
If there are settings for selecting what to display and they are set correct, it should.
It all depends what you want to display.
The map uses "AQHI" but that can not be based on realtime-values.
Check he script you are using if one can select the realtime value and then they should be the same.

Wim

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: Purpleair PHP Retrieval script
« Reply #93 on: August 09, 2018, 10:17:29 AM »
I downloaded this script Chris Formeister. www.copperwoodwx.com/www.cfcsaz.com
I set the sensor settings to yours: 14033 and 14034
No toher changes as by default the script displays the ShortTerm, almost equal to the value on the PurpleAir map

If you are also using that script, check your settings again or post a zip.

Wim
 

Offline ALITTLEweird1

  • Mark
  • Global Moderator
  • Forecaster
  • *****
  • Posts: 923
    • North Bend Weather
Re: Purpleair PHP Retrieval script
« Reply #94 on: August 09, 2018, 10:43:10 AM »
Thanks for checking that Wim. That looks much better.  Do you know where i can get a copy of that script so i can try it?

Thanks
"Nature can do without man, but man cannot do without nature."


Software: WeatherDisplay
Hardware: Davis VP2 + VP2 Solar + VP2 UV + Lightning Detector + Logitech Webcam + Soil temp + Soil Moisture

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: Purpleair PHP Retrieval script
« Reply #95 on: August 09, 2018, 11:39:06 AM »
Thanks for checking that Wim. That looks much better.  Do you know where i can get a copy of that script so i can try it?

Thanks
The link is in the first message of this topic.   https://www.wxforum.net/index.php?topic=33482.0

Wim

Offline giordolo

  • Member
  • *
  • Posts: 5
Re: Purpleair PHP Retrieval script
« Reply #96 on: October 14, 2018, 05:29:23 AM »
Hello, I'm using the script in this way (see attach, renamed in txt because the Forum dont allow php attachments). I'm a beginner and I'm not able to obtain the paga background same as the cell background that contains the current AUI value. Could someone help me please?
many thanks
giordolo

Offline JohnMaino

  • Member
  • *
  • Posts: 1
Re: Purpleair PHP Retrieval script
« Reply #97 on: November 17, 2021, 05:25:40 PM »
I'm posting here instead of starting a new thread since this is a PurpleAir-specific question.

I'm happy grabbing live, real-time data from my PurpleAir PA-II-SD, firmware v6.01 sensor on my local LAN for use with my home-automation setup via http://192.168.1.100/json?live=true. It's working great. No latency or reliability issues compared to using PurpleAir's public cloud API.

I'm concerned that at some point in future, PurpleAir Inc. may (for selfish corporate reasons) simply decide to remove direct access to the live, local-LAN json data. That they'll nix it "just because" with no reason given. The feature is both undocumented and unsupported by PurpleAir so presumably they could turn it off (via a firmware update) at their discretion and without warning. If they did so it would break my home automation setup.

Does anyone have details on how the PurpleAir PA-II unit implements checking for firmware updates? Does it do so in the course of normal push data requests to sensor-logging cloud servers, or does it contact a separate server to check on firmware updates?

If possibly I'd like to use a firewall rule to block firmware-update checks while still allowing the normal 2-minute push sensor-data uploads to the PurpleAir cloud.

Appreciate it if anyone has insight into firmware-update mechanics on the PA-II-SD, v.6.01

Offline 92merc

  • BismarckWeather.net
  • Forecaster
  • *****
  • Posts: 1308
  • BismarckWeather.net
    • BismarckWeather.net
Re: Purpleair PHP Retrieval script
« Reply #98 on: May 17, 2022, 12:54:25 PM »
Bringing up an old thread.  I implemented the script.  But I noticed a little formatting issue.

Main PHP is formatting fine.
https://www.bismarckweather.net/purpleair.php

But I used a simple include PHP into Saratoga's wxnewpage.php template.  The top number with "Good" gets run into the number, unlike the main page.  Is there an easy fix?

https://www.bismarckweather.net/wxpa.php
https://www.BismarckWeather.net
Davis VP2, Cumulus, WeatherDisplay, Blitzortung, Saratoga Scripts, NOAA Stream via PI

Offline 92merc

  • BismarckWeather.net
  • Forecaster
  • *****
  • Posts: 1308
  • BismarckWeather.net
    • BismarckWeather.net
Re: Purpleair PHP Retrieval script
« Reply #99 on: May 19, 2022, 08:51:45 AM »
Looks like this script will need a re-write.  ThingSpeak is going away.  Here is the post from Facebook:

A heads up to anyone using PurpleAir's ThingSpeak data.
We are switching to Google BigQuery for our database and there will be a new API on https://api.purpleair.com to get historic data soon. You will need to migrate your code to this new API as some time around August, ThingSpeak will no longer be available. We will let everyone know here and on the community site as soon as we have more information on the new history end point for sensors.
https://www.BismarckWeather.net
Davis VP2, Cumulus, WeatherDisplay, Blitzortung, Saratoga Scripts, NOAA Stream via PI