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

0 Members and 1 Guest are viewing this topic.

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Senior Contributor
  • ****
  • Posts: 239
    • Copperwood WX
Purpleair PHP Retrieval script
« on: December 30, 2017, 10:12:07 AM »
This PHP script provides 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 script is released under the Creative Commons Attribution-ShareAlike license.
Modify and redistribute as you want, but you must leave the credits intact, and if you make modifications, please share with your fellow members. Make sure to identify the version of the script you are sharing, as new ones can be released and of course it becomes confusing.

Enjoy!

NOTE:
If you are not familiar with the setup and install of PHP scripts, find someone who is. Web scripts and programs are not for beginners or the inexperienced.

Cavaets
weatherbee has noted that the JSON file appears to be two minutes behind the Purpleair site. This was in the first version of the script, since then changes have been made and it should display exactly what is being displayed on the PA website.

Current Version
The current version of the script attached to this message is v1.5 FINAL released 2/22/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.

For the Thingspeak IDs for PM10 values, these are listed in the JSON file as well. You will need the values THINGSPEAK_PRIMARY_ID and THINGSPEAK_PRIMARY_ID_READ_KEY from both sensors (A and B). A will have a set of those, and B will have a set of those. If you are using the All Data option, you will also need the THINGSPEAK_SECONDARY_ID and THINGSPEAK_SECONDARY_ID_READ_KEY again from both sensors.

Caching Mode - OPTIONAL
Purpleair and Thingspeak do not like it (and will block you) if you make too many requests in a short period of time to their website. What the limits are I have no idea, but small weather sites unless they are high traffic shouldn't have an issue and is a non-issue. But if you want to decrease loading time anyways, you can enable the Caching mode of the script (Version 1.5). It will download the data and store it locally on the server for the period of time you set. Once that period is over it will re-request new data - just like any other cache. You can automate the process by enabling this option and CRON'ing the script.

All Data Mode - OPTIONAL
Version 1.5 has an option that will request all data from the sensor - you can use this to write additional HTML for different sensors, etc. However, it can take up to 15-20 seconds for all this data to be requested. It is recommended you use caching mode and CRON'ing the script when using this option. The All Data option returns the particles per deciliter and temp/hum of the sensor.

SQL Logging Mode - OPTIONAL
You can use the script in a CRON-type configuration at a predetermined interval to log the Purpleair data to a MySQL database without displaying anything, or you can do it with displaying the data as usual. In phpMyAdmin, create a database for the data, then attached is a SQL dump file you can import to phpMyAdmin that will create the tables for you. Make sure you import this under the database you just created. THIS IS NOT NECESSARY UNLESS YOU WANT TO USE THE SQL LOGGING MODE.
« Last Edit: March 12, 2018, 07:50:39 AM by azchrisf »

Offline weatherbee

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

  • Cobra Weather Dominator Operator
  • Senior Contributor
  • ****
  • Posts: 239
    • 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

  • Contributor
  • ***
  • Posts: 127
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: 1287
    • My PWS at Leuven Belgium Europe
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

Development and maintenance of Leuven-Template&Scripts  at: https://support.leuven-template.eu/
My PWS at home https://weer.sluispark.be/
And at my daughters house https://www.weerstation-herent.be/

Offline Forever

  • Contributor
  • ***
  • Posts: 127
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: 163
    • 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: 1287
    • My PWS at Leuven Belgium Europe
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


Development and maintenance of Leuven-Template&Scripts  at: https://support.leuven-template.eu/
My PWS at home https://weer.sluispark.be/
And at my daughters house https://www.weerstation-herent.be/

Offline weatherbee

  • Senior Contributor
  • ****
  • Posts: 163
    • 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: 277
  • #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


Offline azchrisf

  • Cobra Weather Dominator Operator
  • Senior Contributor
  • ****
  • Posts: 239
    • 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

  • Contributor
  • ***
  • Posts: 127
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: 277
  • #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 »

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Senior Contributor
  • ****
  • Posts: 239
    • 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

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

  • Cobra Weather Dominator Operator
  • Senior Contributor
  • ****
  • Posts: 239
    • 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

  • Cobra Weather Dominator Operator
  • Senior Contributor
  • ****
  • Posts: 239
    • 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

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

  • Cobra Weather Dominator Operator
  • Senior Contributor
  • ****
  • Posts: 239
    • 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

  • Cobra Weather Dominator Operator
  • Senior Contributor
  • ****
  • Posts: 239
    • 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

  • Cobra Weather Dominator Operator
  • Senior Contributor
  • ****
  • Posts: 239
    • 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

Offline azchrisf

  • Cobra Weather Dominator Operator
  • Senior Contributor
  • ****
  • Posts: 239
    • Copperwood WX
Re: Purpleair PHP Retrieval script
« Reply #22 on: January 20, 2018, 09:13:40 AM »
Would anyone be interested if I added a MySQL logging function?
I'd add both a mode when the script is loaded, and a CRON-type function that wouldn't display the HTML but just pull the values from PA's site and log them (good for doing your own graphs, logs, etc.).

Offline Toxic

  • Senior Contributor
  • ****
  • Posts: 277
  • #conligwx
    • conligwx.org
Re: Purpleair PHP Retrieval script
« Reply #23 on: January 20, 2018, 09:43:41 AM »
Would anyone be interested if I added a MySQL logging function?
I'd add both a mode when the script is loaded, and a CRON-type function that wouldn't display the HTML but just pull the values from PA's site and log them (good for doing your own graphs, logs, etc.).
Sounds like a good idea. Would be interested in this myself.

Sent from my ONEPLUS A5010 using Tapatalk


Offline azchrisf

  • Cobra Weather Dominator Operator
  • Senior Contributor
  • ****
  • Posts: 239
    • Copperwood WX
Re: Purpleair PHP Retrieval script
« Reply #24 on: January 29, 2018, 04:07:35 AM »
Releasing Version 1.3a today.
Changes:

// --
// Version 1.3
// * Added option of logging of sensor data to MySQL DB
// * Added PM10 data from Thingspeak
// --
// Version 1.3a
// * Added option to show values of ug/m3 in 2 decimal places if value is less than 1 (ex. instead of 0 you will see 0.35 or whatever the value)

 

anything