-
Notifications
You must be signed in to change notification settings - Fork 0
/
traffic-aqi.py
64 lines (47 loc) · 1.06 KB
/
traffic-aqi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import urllib.request
import json
from gpiozero import LED
from gpiozero import Button
from time import sleep
from signal import pause
import datetime
button = Button(22)
add_green = LED(17)
add_red = LED(27)
red = LED(18)
green = LED(23)
yellow = LED(24)
def display_status(aqi_variable):
if aqi_variable < 51:
green.on()
yellow.off()
red.off()
elif 51 <= aqi_variable <= 100:
yellow.on()
green.off()
red.off()
elif aqi_variable > 100:
red.on()
green.off()
yellow.off()
def clear_display():
red.off()
green.off()
yellow.off();
def fetch_show_aqi():
external_data = json.loads(urllib.request.urlopen("http://localhost:81/aqi.json").read())
aqi = external_data["aqi"]
display_status(aqi)
button.when_pressed = fetch_show_aqi
while True:
try:
now = datetime.datetime.now()
if now.hour >= 8:
fetch_show_aqi()
else:
clear_display()
except Exception:
clear_display()
pass
sleep(60)
pause()