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

0 Members and 1 Guest are viewing this topic.

Offline Forever

  • Senior Contributor
  • ****
  • Posts: 209
Re: Purpleair PHP Retrieval script
« Reply #25 on: January 30, 2018, 11:31:51 AM »
Thanks for the update.

I was not able to get the SQL to work.

Any change you could make a SQL Import file? I'm not getting an error but I may have setup something wrong.

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #26 on: January 30, 2018, 12:35:17 PM »
I screwed up with the SQL statement, I fixed it and also attached a SQL dump file to create the tables for you. See directions I posted in original post.

Sorry 'bout that.
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 #27 on: January 30, 2018, 06:56:38 PM »
Thanks for fixing the SQL.

There are two new issues now.
The Combine HTML broke the color and format on channel b.
There are 3 lines of code I had to remove to get the page to load.
echo getAQIColorGradedRGBA(32.1,1);
echo getAQIColorRGB(48);
echo getAQIColorTonedTR(48);

Offline Forever

  • Senior Contributor
  • ****
  • Posts: 209
Re: Purpleair PHP Retrieval script
« Reply #28 on: January 30, 2018, 07:21:27 PM »
Does anyone have a list of what values are in all of the ThingSpeak fields?

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #29 on: January 30, 2018, 09:29:21 PM »
I'm being a total idiot today - I uploaded the testing version of the script I'm trying things in.
I uploaded the CORRECT file that fixes both those problems.

Purpleair sent me this that shows what fields are what -

https://docs.google.com/document/d/15ijz94dXJ-YAZLi9iZ_RaBwrZ4KtYeCy08goGBwnbCU/edit?usp=sharing

I barely could figure out how to work with that REST API, and the best I did was a fluke and I got the PM10 value.
So by all means play with it and if you can get it to work let me know! :D
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 #30 on: January 30, 2018, 10:37:37 PM »
Thanks again.

It seems to be working well.
http://www.weathercat.net/wxpurpleair.php

Thanks for the link, the file will be very useful. I'll see what I can do with it this weekend.

Offline Forever

  • Senior Contributor
  • ****
  • Posts: 209
Re: Purpleair PHP Retrieval script
« Reply #31 on: January 31, 2018, 11:18:34 PM »
It looking like the css code for channel A when both channels are shown was removed.

Now when you show both channels channel A is showing the color for the combination of both channels rather than the channel by it's self.

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #32 on: February 01, 2018, 01:47:09 AM »
Yes I combined the CSS code, but I made sure a 2nd one was reference, so Hmmm...I see it's referencing the AQI values correct, so I'm not sure why.
« Last Edit: February 01, 2018, 01:52:16 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 #33 on: February 01, 2018, 02:28:36 AM »
Currently there are only 2 sets of CSS code. You need 3, one for A, B And A+B.

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #34 on: February 01, 2018, 02:53:43 AM »
Yeah I see the problem.
I'll update the thing in a few hours.

Thanks for letting me know :-)
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 weatherbee

  • Senior Contributor
  • ****
  • Posts: 210
    • Sherman Ct. Weather
Re: Purpleair PHP Retrieval script
« Reply #35 on: February 01, 2018, 07:30:33 AM »
I noticed that for PM10 that the PPMLevel is calculated the same as PM2.5.  For particles over 12 µg/m3 goes to medium level but PM10 doesn't go to medium level till it's over 50 µg/m3.  You can see the PM10 levels change at https://aqicn.org/calculator/.
Tom

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #36 on: February 01, 2018, 09:28:01 AM »
Alright I uploaded 1.4 - I fixed the CSS problem, and I verified that it is infact putting out the correct LOW, MEDIUM, etc. for PM10.
Keep in mind I am using the values from Purpleair's site/scripts, not the US EPA standard. If you want them different then by all means you can change them in your script. But I would be careful, the way the Purpleair sensor measures things and does the math could be totally different than the way EPA does things for certain reasons and you could be giving an erronous value. Or the other way around - who knows, not my job :-P

I also added Channel B to the PM10 - so make sure you re-read the thread, it requires the keys from Channel B too.

I don't plan to add it to the script but on my site you can see I added a PM10 AQI number. Feel free to customize the script.
If you want to do this all you have to do is copy the AQI cell at top and change it to aqiFromPM($pm10val).
« Last Edit: February 01, 2018, 09:34:13 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 ConligWX

  • Forecaster
  • *****
  • Posts: 836
  • #conligwx
    • conligwx.org
Re: Purpleair PHP Retrieval script
« Reply #37 on: February 01, 2018, 10:21:33 AM »
tried to get this working but ended up as a blank screen on my saratoga template.  1.3 worked, but 1.4 will not show. just curious,  looking at your code should you not wrap all php code (that is within the html table code) with

Code: [Select]
<?php
phpcode
....
?>

rather than no php opening and closing tags?

example:
Code: [Select]
<tr>
<td colspan="7" style="height: 48px" class="auto-style13">Short-term PM<sub>2.5</sub> is '. GetPPMLevel($aqst) . ' at ' . $aqst . ' &#181;g/m3 - '. $MessageComment . '</td></tr> ';
if (empty($TSID)) {
} else {
echo '
<tr><td colspan="7" style="height: 48px" class="auto-style13">Short-term PM<sub>10</sub> is '. GetPPMLevel($pm10val) . ' at ' . $pm10val . ' &#181;g/m3 </td> ';
}
echo ' </tr>
« Last Edit: February 01, 2018, 10:24:47 AM by Toxic »
Regards Simon
Davis Vantage Pro2 Plus (6162UK) • Daytime FARS • WeatherLink Live • AirLink • PurpleAir PA-II-SD • CumulusMX •


Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #38 on: February 01, 2018, 10:53:13 AM »
You can do it different ways....It's running fine on my PHP (http://copperwoodwx.com/purpleair.php)
Make sure you have entered BOTH keys for the Thingspeak, that could kill it, also try removing this line:

Code: [Select]
if ($TSID == "" || $TSID2 = "")
{
echo "No Thingspeak credentials entered.";
die;
}

Give me a link to your site too and check the PHP error log.
« Last Edit: February 01, 2018, 10:59:40 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 ConligWX

  • Forecaster
  • *****
  • Posts: 836
  • #conligwx
    • conligwx.org
Re: Purpleair PHP Retrieval script
« Reply #39 on: February 01, 2018, 11:07:45 AM »
I've recreated the file again managed to get the template working now.  think it was an extra ;' somewhere since i had added the AQI categories table below the values.

https://www.conligwx.org/wxairquality.php
Regards Simon
Davis Vantage Pro2 Plus (6162UK) • Daytime FARS • WeatherLink Live • AirLink • PurpleAir PA-II-SD • CumulusMX •


Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #40 on: February 01, 2018, 11:45:11 AM »
I found some bugs too, at least with my PHP version.
Uploaded 1.4b.

Sorry for all the changes and updates, I know it's a PITA to update so frequently.
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 #41 on: February 01, 2018, 06:22:15 PM »
weatherbee I re-read your post, I think I may have misunderstood.
I don't have any values for PM10, nor does Purpleair, and EPA standard is over 24 hours...so I am unsure what to use?
« Last Edit: February 01, 2018, 06:29:33 PM 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 weatherbee

  • Senior Contributor
  • ****
  • Posts: 210
    • Sherman Ct. Weather
Re: Purpleair PHP Retrieval script
« Reply #42 on: February 01, 2018, 07:20:42 PM »
azchrisf
Quote
so I am unsure what to use?
I am too.  I think I'll have to reseach more on PM10 before I know how to handle it.
Tom

Offline Forever

  • Senior Contributor
  • ****
  • Posts: 209
Re: Purpleair PHP Retrieval script
« Reply #43 on: February 01, 2018, 11:25:09 PM »
It's hard to test with the site going up and down but I think the PM10 is outputting 0 or a value lower than it should be for channel B. I see to be getting a value about half what I should be getting.
« Last Edit: February 01, 2018, 11:27:41 PM by Forever »

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #44 on: February 02, 2018, 02:40:56 AM »
Make sure your running 1.4b, I fixed a bug that caused that.
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 #45 on: February 02, 2018, 07:43:35 AM »
I just checked and I have the 1.4b version and this is still an issue. It looks like there is an extra /2 in the code.

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #46 on: February 02, 2018, 08:57:48 AM »
Can't be...?
This is running the raw 1.4b version, and it works fine:
http://copperwoodwx.com/purpleair.php
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 #47 on: February 02, 2018, 10:31:08 AM »
All I know is when I go to the IP address of my PurpleAir the PM10 displayed there is 2 X higher then what shows in the script.

If I remove the extra /2 in the code the numbers match. I removed this in the code that shows the value if it's less than zero if I remember correctly.

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Forecaster
  • *****
  • Posts: 455
Re: Purpleair PHP Retrieval script
« Reply #48 on: February 03, 2018, 07:58:48 AM »
If you are refering to this line:

Code: [Select]
if ($pm10 + $pm102 < "1.0") {
$pm10val = round(($pm10 + $pm102)/2,2);
} else {
$pm10val = round(($pm10 + $pm102)/2,0);
}
} else {
...
$pm10val = round(($pm10 + $pm102)/2,0);

Then no, it is supposed to be there. The first two in the if block are for the decimal ON, deciding whether it's less than 1 or not, and the 3rd one under the else block is for when the decimal option is OFF. All the ,0 and ,2 do is tell it how many decimal places to use.

If the PM10 ug/m3 level is below 0 (ex. 0.35), it's just going to show 0 like it always did. That's why
I added the option to show the decimal place. Some people may want to see it others don't.

I'd like to know what your PM10 value is when this problem is happening from both your sensor and what it shows.

weatherbee - Consider that the sensor is providing a particle ug/m3 reading instantaneous.

From all appearances, it appears Purpleair is using the 24hr formula for an instantaneous read on the PM 2.5 AQI according to published tables for the math, and I also tested it with the EPA calculator for PM10.

If you want to, I personally am, but do not want to add it to the script because I am unsure if it is correct, here is the code I came up with:

Code: [Select]
function aqiFromPM10 ($pm10num) {
if (is_nan($pm10num)) {
return "-";
}
if (isset($pm10num) == false) {
return "Error: No value";
}
if ($pm10num < 0.0) {
return $pm10num;
}
if ($pm10num > 1000.0) {
return "Polluted beyond belief";
}
if ($pm10num > 505) {
return calcAQI($pm10num, 500.0, 401.0, 604.0, 505.0);
} else if ($pm10num > 425) {
    return calcAQI($pm10num, 400.0, 301.0, 504.0, 425.0);
  } else if ($pm10num > 355) {
    return calcAQI($pm10num, 300.0, 201.0, 424.0, 355.0);
  } else if ($pm10num > 255) {
    return calcAQI($pm10num, 200.0, 151.0, 354.0, 255.0);
  } else if ($pm10num > 155) {
    return calcAQI($pm10num, 150.0, 101.0, 254.0, 155.0);
  } else if ($pm10num > 55) {
    return calcAQI($pm10num, 100.0, 51.0, 154.0, 55.0);
  } else if ($pm10num >= 0.0) {
    return calcAQI($pm10num, 50.0, 0.0, 54.0, 0.0);
  } else {
    return "-";
  }
}

and also

Code: [Select]
function GetPPMLevel($pmlevel, $pmtype)
{
if ($pmtype = "PM25") {
if ($pmlevel <= 30) {
return "LOW";
} else if ($pmlevel >=30) {
return "MODERATE";
} else if ($pmlevel >=70) {
return "HIGH";
} else if ($pmlevel >=200) {
return "VERY HIGH";
} else if ($pmlevel > 500) {
return "EXTREME";
} else {
return "UNKNOWN";
}
} elseif ($pmtype = "PM10") {
      if ($type = "PM10") {
if ($pmlevel <= 54) {
return "LOW";
} else if ($pmlevel >=55) {
return "MODERATE";
} else if ($pmlevel >=155) {
return "HIGH";
} else if ($pmlevel >=255) {
return "VERY HIGH";
} else if ($pmlevel >= 355) {
return "EXTREME";
} else {
return "UNKNOWN";
}
}
}
}

When using that and Purpleair's existing code, the values mirror exactly the EPA calc's. So if you think it's correct, I'll add it, otherwise I'll leave it out.
« Last Edit: February 03, 2018, 08:02:32 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 #49 on: February 03, 2018, 12:57:12 PM »
Looking at this again the /2 should be there but the script is not pulling a value for channel b.

I made a small script to just pull the PM10 data and I do have data for both channels.

I'm not sure why the script will not output data for channel b.

 

anything