-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
60 lines (47 loc) · 1.79 KB
/
main.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
# BLOOM Hub
# Main
# Author: Simon Aschenbrenner
from http import BackendError
from time import ticks_diff, ticks_ms
import constants
import hub
import reset
import sensors
import watering
def main_loop():
"""
Hub will enter this loop after setup and stay in it for eternity if not powercycled or rebooted.
Exception safe, will automatically enter reset.ask() if more than constants.MAX_FAILED_REQUESTS requests to the backend fail.
"""
print("ENTER MAIN LOOP")
hub.led.on()
failed_request_counter = 0
last_backend_call = 0
while True:
try:
sensors.collect()
time_since_last_backend_call = ticks_diff(ticks_ms(), last_backend_call)
if (time_since_last_backend_call > constants.BACKEND_CALL_DELAY) or (time_since_last_backend_call < 0): # overflow protection
if hub.has_user():
print("Hub has user")
watering.water()
sensors.check()
else: # Remote reset happened
print("Hub has no user, remote factory reset")
reset.reset(wlan=True, lora=True)
last_backend_call = ticks_ms()
failed_request_counter = 0
except BackendError as e:
watering.stop_water()
print(e)
failed_request_counter += 1
print("Failed request #", failed_request_counter)
except Exception as e:
print(e)
reset.reset(wlan=False, lora=False) # Reboot
if failed_request_counter >= constants.MAX_FAILED_REQUESTS:
print("Too many failed requests, asking for WLAN reset")
reset.ask(constants.MESSAGE_ERROR_BACKEND, wlan=True, lora=False)
if __name__ == "__main__":
hub.setup()
main_loop()