Web Weather > Weather Website PHP/AJAX scripting

Purpleair PHP Retrieval script

(1/25) > >>

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.


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.

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.

Thanks for the script.  I've incorporated it with my AQI block.
You can see at www.shermanctweather.org.

Looks great! Hope you enjoy it!

Many Thanks for the good work ;-)

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?



[0] Message Index

[#] Next page

Go to full version