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

0 Members and 1 Guest are viewing this topic.

Offline Forever

  • Contributor
  • ***
  • Posts: 120
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

  • Contributor
  • ***
  • Posts: 120
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: 4
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
  • Senior Contributor
  • ****
  • Posts: 225
    • Copperwood WX
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 »

Offline giordolo

  • Member
  • *
  • Posts: 4
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
  • Senior Contributor
  • ****
  • Posts: 225
    • Copperwood WX
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


Offline azchrisf

  • Cobra Weather Dominator Operator
  • Senior Contributor
  • ****
  • Posts: 225
    • Copperwood WX
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 »

Offline Forever

  • Contributor
  • ***
  • Posts: 120
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
  • Senior Contributor
  • ****
  • Posts: 225
    • Copperwood WX
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)?

Offline i_fiorentino

  • Forecaster
  • *****
  • Posts: 556
  • 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
  • Senior Contributor
  • ****
  • Posts: 225
    • Copperwood WX
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.

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Senior Contributor
  • ****
  • Posts: 225
    • Copperwood WX
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 »

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1196
    • Support site Leuven Template and scripts
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 »
Vantage VUE with a WLIP connected to a Meteobridge used for https://weer.sluispark.be/
Same VUE+WLIP uploads to WL.com(2.0)  used for https://sluispark.be/weather28/
Vantage VP2 with a USB logger/Meteobridge for https://www.weerstation-herent.be/
Envoy with a WLIP uploads to WL.com(1.0) as a backup / test for the same site.

Offline i_fiorentino

  • Forecaster
  • *****
  • Posts: 556
  • 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