# 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()