-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.py
86 lines (77 loc) · 2.03 KB
/
mqtt.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
from umqtt.simple import MQTTClient
from lcd import puts, set_pos
import ubinascii
import machine
import time
import display_climate
import gc
# Default MQTT server to connect to
SERVER = "192.168.0.69"
CLIENT_ID = ubinascii.hexlify(machine.unique_id())
temp_tn = b"sensors/greenhouse/temperature"
rh_tn = b"sensors/greenhouse/humidity/relative"
light_tn = b"sensors/greenhouse/light"
vpd_tn = b"sensors/greenhouse/vapor_pressure_difference"
store = { temp_tn: 0,
rh_tn: 0,
light_tn: 0,
vpd_tn: 0
}
new_data = False
current_line = 0
def sub_cb(topic, msg):
global new_data, store
if topic not in store:
print("unexpected subscription message from topic '%s'" % (topic))
else:
store[topic] = float(msg.decode())
print((topic, store[topic]))
new_data = True
def puts_scroll(msg):
global current_line
set_pos(0,current_line)
puts(msg)
if current_line == 0:
current_line = 1
else:
current_line = 0
def reset():
puts_scroll("reseting in 3s...")
time.sleep(3)
import machine
machine.reset()
def main():
global new_data, store
puts_scroll("Starting LCD...")
display_climate.init()
try:
puts_scroll("Starting MQTT...")
c = MQTTClient(CLIENT_ID, SERVER)
c.set_callback(sub_cb)
puts_scroll("Connecting...")
c.connect()
puts_scroll("%s OK" % (SERVER,))
for topic in store.keys():
puts_scroll("Sub'ing:%s" % (topic[-24:-16].decode(),))
puts_scroll("'%s'" % (topic[-16:].decode())) # only show last 16 chars bc the LCD is small
c.subscribe(topic)
puts_scroll("..sub'd.")
time.sleep_ms(300)
puts_scroll("Init complete")
except Exception as exc:
puts_scroll(repr(exc))
reset()
try:
while 1:
# c.wait_msg() # blocking
c.check_msg() # non-blocking
if new_data:
display_climate.update_display(store[temp_tn], store[rh_tn], store[light_tn], store[vpd_tn])
new_data = False
gc.collect()
time.sleep_ms(500)
except Exception as exc:
puts_scroll(repr(exc))
time.sleep(3)
finally:
reset()