Last active
January 1, 2022 20:47
-
-
Save DaveKT/ba4de3dd00d4abcf052ebd710ef84ada to your computer and use it in GitHub Desktop.
Proof of concept for data capture from a personal Purple Air sensor. This script allows the purple air owner to capture data for custom application.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Author: David Kolet-Tassara | |
| # Date: December 31, 2021 | |
| # Purpose: POC for data capture from a personal purple air sensor. Data downloads are available but this script | |
| # allows the owner to capture for use automatically for custom applications. | |
| # Requirements | |
| # Purple Air JSON URL: purpleair json URL provided in their welcome message when you sign up your sensor. | |
| # AQL Library: https://pypi.org/project/python-aqi/ Used to convert purple air readings to AQI | |
| import aqi | |
| import json | |
| import sqlite3 | |
| import time | |
| from urllib.request import urlopen | |
| url = "https://www.purpleair.com/json?show=xxxxxxx&key=xxxxxxxxxxx" # sensor and key data removed. Paste your URL here. | |
| response = urlopen(url) | |
| jsondata = json.loads(response.read()) | |
| sensorA = float(jsondata["results"][0]["pm2_5_cf_1"]) | |
| sensorB = float(jsondata["results"][1]["pm2_5_cf_1"]) | |
| avg = str((sensorA+sensorB)/2) | |
| aqindex = (aqi.to_iaqi(aqi.POLLUTANT_PM25, avg, algo=aqi.ALGO_EPA)) | |
| recordutc = int(time.time()) | |
| datacreated = int(jsondata["results"][0]["LastSeen"]) | |
| temp_f = float(jsondata["results"][0]["temp_f"]) | |
| pressure = float(jsondata["results"][0]["pressure"]) | |
| humidity = float(jsondata["results"][0]["humidity"]) | |
| # print(recordutc) | |
| # print(datacreated) | |
| # print(temp_f) | |
| # print(pressure) | |
| # print(humidity) | |
| # print(sensorA) | |
| # print(sensorB) | |
| # print(aqindex) | |
| # I created the SQL database ahead of time to capture data. Use the data as you see fit. | |
| sql = f'INSERT INTO readings VALUES ({recordutc}, {datacreated}, {temp_f}, {pressure}, {humidity}, {sensorA}, {sensorB}, {aqindex})' | |
| # print(sql) | |
| con = sqlite3.connect('purple.sqlite') | |
| cur = con.cursor() | |
| cur.execute(sql) | |
| con.commit() | |
| con.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment