-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwater_system.py
126 lines (97 loc) · 3.27 KB
/
water_system.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
"""Monitor local rainfall and disable garden watering system if required."""
import machine
import esp32
import utime
import clock
import wifi
import config
from file_logger import File
import weather
import watcher
import thingspeak
def run():
"""Main entry point to execute this program."""
sleep_enabled = _sleep_enabled()
try:
File.logger().info('%s - Awake: %s', clock.timestamp(),
machine.wake_reason())
rainfall = False
next_wake = config.RTC_ALARM
battery_volts = _battery_voltage()
if not sleep_enabled:
watcher.disable()
if wifi.connect():
_resetConnectCount()
rain_data = weather.get_rain_data()
rainfall = rain_data.rainfall_occurring()
thingspeak.send(rain_data, battery_volts)
if rainfall:
File.logger().info('%s - System OFF', clock.timestamp())
_system_off()
else:
File.logger().info('%s - System ON', clock.timestamp())
_system_on()
else:
if _incrementConnectCount() > 5:
# Give up trying to connect to WiFi
_resetConnectCount()
else:
File.logger().info('%s - Set one minute sleep, attempts %d',
clock.timestamp(), _getConnectCount())
next_wake = config.SLEEP_ONE_MINUTE
except Exception as ex:
# Catch exceptions so that device goes back to sleep HTTP calls
# fail with exceptions.
File.logger().exc(ex, '%s - Error', clock.timestamp())
finally:
try:
wifi.disconnect()
except Exception as ex:
File.logger().exc(ex, '%s - WIFI disconnect error',
clock.timestamp())
if sleep_enabled:
File.logger().info('%s - Sleeping...', clock.timestamp())
File.close_log()
_sleep_until(next_wake)
else:
File.logger().info('%s - Not sleeping', clock.timestamp())
File.close_log()
def init_ftp():
wifi.connect()
import ftp
def _sleep_until(alarm_time):
_configure_pin_interrupt()
clock.configure_rtc_alarm(alarm_time)
machine.deepsleep()
def _configure_pin_interrupt():
esp32.wake_on_ext1(pins=(config.WAKEUP_PIN,), level=esp32.WAKEUP_ALL_LOW)
def _system_on():
_pulse_relay(config.WATER_ON_PIN)
def _system_off():
_pulse_relay(config.WATER_OFF_PIN)
def _pulse_relay(pin):
pin.value(1)
# 10ms minimum time to alter relay latch as per specification of G6SK-2
utime.sleep_ms(10)
pin.value(0)
def _battery_voltage():
adc = machine.ADC(config.BATTERY_PIN)
sum = 0
for x in range(0, config.ADC_READS):
sum += adc.read_uv() / 1000000
return (sum / config.ADC_READS) * config.RESISTOR_RATIO
def _sleep_enabled():
return config.NO_SLEEP_PIN.value() == 0
def _getConnectCount():
val = machine.RTC().memory()[0]
if val is not None:
return val
else:
machine.RTC().memory(bytes([0]))
return 0
def _incrementConnectCount():
val = _getConnectCount() + 1
machine.RTC().memory(bytes([val]))
return val
def _resetConnectCount():
machine.RTC().memory(bytes([0]))