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

0 Members and 1 Guest are viewing this topic.

Offline azchrisf

  • Weather Mechanic
  • Contributor
  • ***
  • Posts: 118
    • Copperwood WX
Purpleair PHP Retrieval script
« on: December 30, 2017, 10:12:07 AM »
This script provides basic information retrieval from your Purpleair sensor's JSON file to a basic HTML interface.

This will come in great handy for those of you who wish to add a Purpleair "widget" to your websites like I do, which they STILL have yet to offer after 2 years. I wrote this in less than an hour being a novice, certainly they could...

The HTML needs cleanup as it was generated by MS Expression.

Modify and redistribute as you want, but leave the credits intact please, and if you make modifications, please share with your fellow members.

Enjoy!

Cavaets
weatherbee has noted that the JSON file appears to be two minutes behind the Purpleair site. Please report any observations.

Current Version
The current version of the script attached to this message is v1.2a released 1/14/18.
Changes are listed in the script.

Finding your Sensor IDs:
You can find your sensor's IDs by going to www.purpleair.com/json and searching for your sensor's name. Usually 2 are listed, get both IDs - you will need them. The first is A and the second is B. Or email Purpleair for help with getting your sensor IDs.
« Last Edit: January 14, 2018, 12:29:19 PM by azchrisf »

Offline weatherbee

  • Senior Contributor
  • ****
  • Posts: 160
    • Sherman Ct. Weather
Re: Purpleair PHP Retrieval script
« Reply #1 on: December 30, 2017, 03:59:28 PM »
Thanks for the script.  I've incorporated it with my AQI block.
You can see at www.shermanctweather.org.
Tom

Offline azchrisf

  • Weather Mechanic
  • Contributor
  • ***
  • Posts: 118
    • Copperwood WX
Re: Purpleair PHP Retrieval script
« Reply #2 on: December 31, 2017, 02:46:09 AM »
Looks great! Hope you enjoy it!

Offline WSP

  • Member
  • *
  • Posts: 1
  • PD4MM
    • Weerstation Parkstad
Re: Purpleair PHP Retrieval script
« Reply #3 on: December 31, 2017, 09:57:12 AM »
Many Thanks for the good work ;-)

Offline Forever

  • Senior Member
  • **
  • Posts: 86
Re: Purpleair PHP Retrieval script
« Reply #4 on: January 02, 2018, 11:21:45 PM »
I just got my PurpleAir and setup this script but noticed the AQI on the PurpleAir site and this script do not match.

This there more than one way to calculate AQI?

Is this script calculating the AQI correctly?

http://www.weathercat.net/purpleair.php
https://www.purpleair.com/map?&zoom=15&selected=393264|393266&lat=36.98094&lng=-122.01315899999997&clustersize=30&orderby=L&latr=0.03081979434855242&lngr=0.05776405334472656

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1148
    • Support site Leuven Template and scripts
Re: Purpleair PHP Retrieval script
« Reply #5 on: January 03, 2018, 04:44:49 AM »
I just got my PurpleAir and setup this script but noticed the AQI on the PurpleAir site and this script do not match.

This there more than one way to calculate AQI?

Is this script calculating the AQI correctly?

http://www.weathercat.net/purpleair.php
https://www.purpleair.com/map?&zoom=15&selected=393264|393266&lat=36.98094&lng=-122.01315899999997&clustersize=30&orderby=L&latr=0.03081979434855242&lngr=0.05776405334472656
Yes there are more ways to calculate. But only one "official" for the US.
https://www.airnow.gov/index.cfm?action=airnow.calculator

When I enter the values for PM2.5 Santa Cruz HighSchool https://www.purpleair.com/json?show=4355 there I get the same AQI as at the map:
21 ug/m3 => 70 AQI  Moderate

This purpleair script returns similar results when I test it for Santa Cruz HighSchool
21.58 ug/m3 => AQI 71 Moderate

But your script http://www.weathercat.net/purpleair.php returns lower values but the calculation is correct
14 ug/m3 => AQI 55 Moderate
Those 14 ug/m3 values are different from Santa Cruz HighSchool at the purpleair map which are all in the 20 ug/m3  range.

Check your script and  the number you entered at:
Code: [Select]
$sensorid = '4355';
Wim

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 Forever

  • Senior Member
  • **
  • Posts: 86
Re: Purpleair PHP Retrieval script
« Reply #6 on: January 03, 2018, 07:44:08 AM »
I checked my station ID and it's correct. https://www.purpleair.com/json?show=5650

The vales are much closer today but never seem to be the same.

At this point I guess it could be a rounding issue.


Offline weatherbee

  • Senior Contributor
  • ****
  • Posts: 160
    • Sherman Ct. Weather
Re: Purpleair PHP Retrieval script
« Reply #7 on: January 03, 2018, 07:51:19 AM »
I changed the script a little to pull the time of the file it is using.  I noticed it's about two minutes behind then the script on the map.  Also script pulls data only from channel A and I assume the AQI on the map is the average between channel A and B.
Tom

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1148
    • Support site Leuven Template and scripts
Re: Purpleair PHP Retrieval script
« Reply #8 on: January 03, 2018, 07:55:25 AM »
I changed the script a little to pull the time of the file it is using.  I noticed it's about two minutes behind then the script on the map.  Also script pulls data only from channel A and I assume the AQI on the map is the average between channel A and B.
Tom
Exactly, I was typing that now, When you check the link, https://www.purpleair.com/json?show=5650 you see that the two sensors are reported separate, their PM2.5 are  9.02 and 9.79  which result in an AQI of 38 and 40.

Succes, Wim


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 weatherbee

  • Senior Contributor
  • ****
  • Posts: 160
    • Sherman Ct. Weather
Re: Purpleair PHP Retrieval script
« Reply #9 on: January 03, 2018, 08:13:10 AM »
I also changed the script to pull in both channels and average them together.
Tom

Offline Toxic

  • Senior Contributor
  • ****
  • Posts: 153
  • #conligwx
    • conligwx.org
Re: Purpleair PHP Retrieval script
« Reply #10 on: January 03, 2018, 09:42:46 AM »
Thanks for the script, works nicely though I had   to change some of the html code due to formatting and xHTML validation failures.

Does.anyone have html/PHP code for both sensors?

Sent from my ONEPLUS A5010 using Tapatalk

Regards Simon


Offline azchrisf

  • Weather Mechanic
  • Contributor
  • ***
  • Posts: 118
    • Copperwood WX
Re: Purpleair PHP Retrieval script
« Reply #11 on: January 03, 2018, 10:04:20 AM »
I would appreciate both of you sending me the updates you made, and I'll update the script for everyone.

Sent from my Pixel using Tapatalk


Offline Forever

  • Senior Member
  • **
  • Posts: 86
Re: Purpleair PHP Retrieval script
« Reply #12 on: January 04, 2018, 07:46:19 AM »
I would appreciate both of you sending me the updates you made, and I'll update the script for everyone.

Sent from my Pixel using Tapatalk
Yes please, I would love having the improvements others have made in this script.

Offline Toxic

  • Senior Contributor
  • ****
  • Posts: 153
  • #conligwx
    • conligwx.org
Re: Purpleair PHP Retrieval script
« Reply #13 on: January 05, 2018, 05:12:01 PM »
I would appreciate both of you sending me the updates you made, and I'll update the script for everyone.

Sent from my Pixel using Tapatalk

Once I have my unit and testing with my Website I'll update the code for my Saratoga Script. the only improvements I have done so far is some xHTML validation

« Last Edit: January 05, 2018, 05:19:20 PM by Toxic »
Regards Simon


Offline azchrisf

  • Weather Mechanic
  • Contributor
  • ***
  • Posts: 118
    • Copperwood WX
Re: Purpleair PHP Retrieval script
« Reply #14 on: January 06, 2018, 10:50:07 AM »
I have updated the original post with Version 1.1 of the script.
The following changes were made:

// Version 1.1
// * Added Sensor channel B and averaging for ug/m3 - thanks to weatherbee
// * Addded timestamp option
// * Added averaging option for AQI values (try and see if it shows more acurately to Purpleair's site when turned on)

Not listed in the changes (forgot):

* Changed AQI value to center
* Changed appearance to 100% of width, this way it can be integrated within tables, etc. and auto-adjust for different screen areas

I will integrate Toxic's HTML changes for the next version.
« Last Edit: January 06, 2018, 10:53:24 AM by azchrisf »

Offline Forever

  • Senior Member
  • **
  • Posts: 86
Re: Purpleair PHP Retrieval script
« Reply #15 on: January 06, 2018, 07:44:19 PM »
Thanks for the update. I made a few changes and thought I'd pass them on to see what you think of them.

I fixed the Show Time Stamp option.
I changed the Average API option to show both channels or to average them.
I added the One Week reading.
I added the GetPMComment.

Keep in mind I'm not a programmer so I probably did not do things the most efficient way.

http://www.weathercat.net/wxpurpleair.php

Edit: Made a few small fixes to the file.
« Last Edit: January 13, 2018, 11:52:17 PM by Forever »

Offline azchrisf

  • Weather Mechanic
  • Contributor
  • ***
  • Posts: 118
    • Copperwood WX
Re: Purpleair PHP Retrieval script
« Reply #16 on: January 07, 2018, 12:16:12 AM »
Looks great! I will add that all in.
One thing I'd like to ask after further reflection is that if you make changes, please send them to me via PM, otherwise posting different versions of updates in the thread could confuse other users as to what's up to date or not. If your making cosmetic changes, feel free to post them but code changes please send to me.

Two thing I need to tackle, and need help with:

1. The PA display shows a kind of color transition as you get near another level (ex. Low to Moderate) using a yellow-green type color - same for others. If someone can come up with code for that I'm sure all of us would appreciate it, I have the Purpleair code for it but they use a lot of different functions it seems.

2. For you Weatherbee espicially - how can we get the Meteotemplate block to auto refresh? Once we get this done I'll submit it to Jachym for inclusion on his site for everyone.

Thank you all for helping out with the script - I'm glad we are making something we can all use :-)
« Last Edit: January 10, 2018, 12:33:20 PM by azchrisf »

Offline azchrisf

  • Weather Mechanic
  • Contributor
  • ***
  • Posts: 118
    • Copperwood WX
Re: Purpleair PHP Retrieval script
« Reply #17 on: January 10, 2018, 12:34:00 PM »
Incase anyone wants to work on those transition colors, here is the complete PA javascript file showing what they do.

Offline Forever

  • Senior Member
  • **
  • Posts: 86
Re: Purpleair PHP Retrieval script
« Reply #18 on: January 14, 2018, 12:11:13 AM »
If your are using the ajax dashboard cell template I have modified the get-aqi-rss-with-link.php file to pull data from the json file.

Just add your IDs and pick if you want to show the real time or short term reading.

http://www.weathercat.net/cell.php


Offline azchrisf

  • Weather Mechanic
  • Contributor
  • ***
  • Posts: 118
    • Copperwood WX
Re: Purpleair PHP Retrieval script
« Reply #19 on: January 14, 2018, 12:43:02 AM »
Excellent work - when I get a little more time me and weatherbee will try and submit an official block for Meteotemplate.

www.copperwoodwx.com

Offline azchrisf

  • Weather Mechanic
  • Contributor
  • ***
  • Posts: 118
    • Copperwood WX
Re: Purpleair PHP Retrieval script
« Reply #20 on: January 14, 2018, 10:21:43 AM »
Posted Version 1.2 today -

Changes from the script:

// Version 1.2
// * Changed the Average API option to show both channels or to average them (thanks to Forever)
// * Added the One Week reading (thanks to Forever)
// * Added the GetPMComment (thanks to Forever)
// * Updated GetPMComment to the PurpleAir levels as they specify for the messages rather than US Gov standard
// * Did some Error Handling for connections

Offline azchrisf

  • Weather Mechanic
  • Contributor
  • ***
  • Posts: 118
    • Copperwood WX
Re: Purpleair PHP Retrieval script
« Reply #21 on: January 14, 2018, 12:27:04 PM »
Doing a quick update to 1.2a, Forever made some quick fixes:

// Version 1.2a - all changes thanks to Forever
// * Fixed issue with styling missing in Channel B
// * Cleaned up table structure
// * Fixed typos/punctuation
// * Fixed W3 validation errors

 

anything