Skip to content

Commit 2717b7e

Browse files
authored
Merge pull request #2535 from makermelissa/purple-air-update
It uses Secrets verses settings.toml which is the new method but as this is an update it should be ok
2 parents 3250637 + 4a58ad4 commit 2717b7e

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Purple_Air_Display/code.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
import terminalio
1313
from adafruit_matrixportal.matrixportal import MatrixPortal
1414

15+
# Get wifi details and more from a secrets.py file
16+
try:
17+
from secrets import secrets
18+
except ImportError:
19+
print("WiFi secrets are kept in secrets.py, please add them there!")
20+
raise
21+
1522
def aqi_transform(val):
1623
aqi = pm_to_aqi(val) # derive Air Quality Index from Particulate Matter 2.5 value
1724
return "AQI: %d" % aqi
@@ -32,16 +39,19 @@ def message_transform(val): # picks message based on thresholds
3239
return "Unknown"
3340

3441
SENSOR_ID = 3085 # Poughkeepsie # 30183 LA outdoor / 37823 oregon / 21441 NYC
35-
SENSOR_REFRESH_PERIOD = 30 # seconds
36-
DATA_SOURCE = "https://www.purpleair.com/json?show=" + str(SENSOR_ID)
42+
SENSOR_REFRESH_PERIOD = 300 # seconds
43+
DATA_SOURCE = f"https://api.purpleair.com/v1/sensors/{SENSOR_ID}?fields=pm2.5_10minute"
3744
SCROLL_DELAY = 0.02
38-
DATA_LOCATION = ["results", 0, "PM2_5Value"] # navigate the JSON response
45+
DATA_LOCATION = ["sensor", "stats", "pm2.5_10minute"] # navigate the JSON response
3946

4047
# --- Display setup ---
4148
matrixportal = MatrixPortal(
4249
status_neopixel=board.NEOPIXEL,
4350
debug=True,
4451
url=DATA_SOURCE,
52+
headers={"X-API-Key": secrets["purple_air_api_key"], # purpleair.com
53+
"Accept": "application/json"
54+
},
4555
json_path=(DATA_LOCATION, DATA_LOCATION),
4656
)
4757

0 commit comments

Comments
 (0)