-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
149 lines (131 loc) · 3.75 KB
/
code.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import time
import board
import adafruit_ahtx0
import terminalio
import neopixel
import wifi
from displayio import Group
from adafruit_display_text import bitmap_label
from digitalio import DigitalInOut, Direction, Pull
# Configuration
delay = 1
temp_warning_c = 35
temp_critical_c = 50
humid_warning = 45
humid_critical = 55
# Board Setup
i2c = board.I2C()
sensor = adafruit_ahtx0.AHTx0(i2c)
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
pixel.brightness = 0.3
temp_status = "OK" # Can be "OK", "WARN", or "CRIT"
humid_status = "OK"
btn = DigitalInOut(board.BUTTON)
btn.direction = Direction.INPUT
btn.pull = Pull.UP
button_ready = True
show_network = False
# Wifi Setup
# pylint: disable=no-name-in-module,wrong-import-order
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
if "ssid" in secrets:
try:
print("Connecting to {}".format(secrets["ssid"]))
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to {}!".format(secrets["ssid"]))
except:
print("Wifi connection failed!")
raise
else:
print("SSID was not provided. Wifi will not be connected.")
# Sensor Display Setup
main_group = Group()
ok_color = 0x00FF00
warn_color = 0xFFFF00
crit_color = 0xFF0000
temp_c_label = bitmap_label.Label(
font=terminalio.FONT,
text="Temp (C):",
scale=2,
color=ok_color,
)
temp_c_label.anchor_point = (0, 0)
temp_c_label.anchored_position = (10, 10)
main_group.append(temp_c_label)
temp_f_label = bitmap_label.Label(
font=terminalio.FONT,
text="Temp (F):",
scale=2,
color=ok_color,
)
temp_f_label.anchor_point = (0, 0)
temp_f_label.anchored_position = (10, 50)
main_group.append(temp_f_label)
humid_label = bitmap_label.Label(
font=terminalio.FONT,
text="RH:",
scale=2,
color=ok_color,
)
humid_label.anchor_point = (0, 0)
humid_label.anchored_position = (10, 90)
main_group.append(humid_label)
board.DISPLAY.show(main_group)
# Network Display Setup
network_group = Group()
ip_label = bitmap_label.Label(
font=terminalio.FONT,
text="IP: {}".format(wifi.radio.ipv4_address),
scale=2,
)
ip_label.anchor_point = (0, 0)
ip_label.anchored_position = (10, 10)
network_group.append(ip_label)
while (True):
temp_c = round(sensor.temperature, 2)
temp_f = round((temp_c * 1.8) + 32, 2)
humidity = round(sensor.relative_humidity, 2)
if temp_c > temp_critical_c:
temp_status = "CRIT"
temp_c_label.color = crit_color
temp_f_label.color = crit_color
elif temp_c > temp_warning_c:
temp_status = "WARN"
temp_c_label.color = warn_color
temp_f_label.color = warn_color
else:
temp_status = "OK"
temp_c_label.color = ok_color
temp_f_label.color = ok_color
if humidity > humid_critical:
humid_status = "CRIT"
humid_label.color = crit_color
elif humidity > humid_warning:
humid_status = "WARN"
humid_label.color = warn_color
else:
humid_status = "OK"
humid_label.color = ok_color
if temp_status == "CRIT" or humid_status == "CRIT":
pixel.fill((255, 0, 0))
elif temp_status == "WARN" or humid_status == "WARN":
pixel.fill((255, 255, 0))
else:
pixel.fill((0, 255, 0))
temp_c_label.text = "Temp (C): {}".format(temp_c)
temp_f_label.text = "Temp (F): {}".format(temp_f)
humid_label.text = "RH: {}%".format(humidity)
if not btn.value:
if btn.value != button_ready:
show_network = not show_network # Toggle display of status
button_ready = False
else:
button_ready = True
if show_network:
board.DISPLAY.show(network_group)
else:
board.DISPLAY.show(main_group)