-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
243 lines (193 loc) · 7.01 KB
/
app.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import esp
import machine
import utime as time
from dht import DHT22
from machine import I2C
from machine import Pin, Timer
from micropython import const, schedule
import hass
import model
import mqtt
from bme280 import BME280
from config import *
from controller import Controller
from display import init as init_display
from display import sys_status_view
from hass import ThermostatAPI as HassThermostatAPI
from model import SensorSample, LocalChanges
from sht31 import SHT31
from sys_status import instance as sys_status
from tsl2561 import TSL2561
dht_sensor = None
DHT_TIM_ID = const(1)
dht_tim = None
# battery voltage
V_TIM_ID = const(2)
v_tim = None
v_adc = machine.ADC(0)
last_light_sensor_sample_ts = 0
current_display_brightness = 255
LIGHT_SENSOR_SAMPLE_INTERVAL = const(1000)
def main():
global dht_sensor
global dht_tim
global v_tim
try:
i2c = I2C(scl=Pin(PIN_I2C_SCL), sda=Pin(PIN_I2C_SDA))
init_display(i2c)
except OSError as e:
print("Can not initialize display!", repr(e))
time.sleep(10)
machine.reset()
return
from display import instance as display
display.render(sys_status_view)
while 1:
try:
dht_sensor = MultiSensor(pin=PIN_DHT, i2c=i2c)
dht_sensor.sample() # test sensor
sys_status.set_sensor(True)
display.render(sys_status_view)
break
except OSError as e:
print("Sensor failure", repr(e))
time.sleep(3)
hass_api = hass.API(HASS_BASE_URL, api_password=HASS_PASSWORD)
hass_thermo = HassThermostatAPI(hass_api, HASS_THERMOSTAT_ID)
cur_state = hass_thermo.get_state()
while cur_state is None:
print("[HASS] Connecting to the server...")
time.sleep_ms(500)
cur_state = hass_thermo.get_state()
print("[HASS] Connected")
sys_status.set_hass_api(True)
display.render(sys_status_view)
model.init(cur_state)
mqtt.init(mqtt_msg_dispatch)
sys_status.set_mqtt(True)
display.render(sys_status_view)
time.sleep(1)
sys_status.boot = False
display.render(sys_status_view)
t_sensor_mqtt = mqtt.HassMQTTTemperatureSensor(mapper=lambda x: x.t)
t_sensor_mqtt.register({})
h_sensor_mqtt = mqtt.HassMQTTHumiditySensor(mapper=lambda x: x.h)
h_sensor_mqtt.register({})
v_sensor_mqtt = mqtt.HassMQTTVoltageSensor()
v_sensor_mqtt.register({})
dht_tim = Timer(DHT_TIM_ID)
sensor_update = dht_updater(t_sensor_mqtt, h_sensor_mqtt, Sensor2Model())
sensor_update(None)
dht_tim.init(period=SENSOR_SAMPLE_INTERVAL * 1000, mode=Timer.PERIODIC,
callback=sensor_update)
v_tim = Timer(V_TIM_ID)
v_update = voltage_updater(v_sensor_mqtt)
v_update(None)
v_tim.init(period=30 * 1000, mode=Timer.PERIODIC,
callback=v_update)
controller = Controller(hass_thermo_api=hass_thermo,
thermostat_model=model.instance,
local_changes=LocalChanges(max_temp=float(cur_state['attributes']['max_temp']),
min_temp=float(cur_state['attributes']['min_temp'])))
if LIGHT_SLEEP_ENABLED:
esp.sleep_type(esp.SLEEP_LIGHT)
lux_sensor = TSL2561(i2c=i2c)
lux_sensor.active(True)
time.sleep_ms(500)
while 1:
adjust_display_brightness(lux_sensor)
mqtt.loop()
controller.loop()
class MultiSensor:
__slots__ = ['dht', 'bme', 'sht', 'prev_sample']
def __init__(self, pin: int, i2c: I2C):
self.dht = DHT22(Pin(pin))
self.bme = BME280(i2c=i2c, address=BME280_I2C_ADDR)
self.sht = SHT31(i2c, addr=SHT31_I2C_ADDR)
self.prev_sample = SensorSample(-1000, -1000, -1000)
def sample(self):
try:
self.dht.measure()
dht_result = SensorSample(self.dht.temperature(), self.dht.humidity(), -1000)
print("[SENSOR] DHT22: T = {} {}, H = {} % RH".format(dht_result.t, TEMPERATURE_UNIT, dht_result.h))
except OSError as e:
print("[SENSOR] DHT22 is not available")
dht_result = None
if SENSOR_MAIN == "dht":
raise e
try:
_sht_result = self.sht.get_temp_humi()
sht_result = SensorSample(round(_sht_result[0], 1), round(_sht_result[1], 1), -1000)
print("[SENSOR] SHT31: T = {} {}, H = {} % RH".format(sht_result.t, TEMPERATURE_UNIT, sht_result.h))
except OSError as e:
print("[SENSOR] SHT31 is not available")
sht_result = None
if SENSOR_MAIN == "sht":
raise e
try:
bme_result = self.bme.sample()
bme_result.t = round(bme_result.t, 1)
bme_result.h = round(bme_result.h, 1)
print(
"[SENSOR] BME280: T = {} {}, H = {} % RH, P = {} hPa".format(bme_result.t, TEMPERATURE_UNIT,
bme_result.h,
bme_result.p))
except OSError as e:
print("[SENSOR] BME280 is not available")
bme_result = None
if SENSOR_MAIN == "bme":
raise e
if SENSOR_MAIN == "dht":
result = dht_result
if SENSOR_MAIN == "sht":
result = sht_result
if SENSOR_MAIN == "bme":
result = bme_result
if bme_result is not None:
result.p = bme_result.p
self.prev_sample = result
return result
class Sensor2Model:
def on_next(self, x: SensorSample):
model.instance.update_sensor_sample(x)
def dht_updater(*conns):
def f(_timer):
schedule(sensor_push_sample, conns)
return f
def sensor_push_sample(conns):
try:
result = dht_sensor.sample()
except OSError as e:
print("[SENSOR]", repr(e))
sys_status.set_sensor(False)
result = None # actually impossible
if result is not None:
for s in conns:
s.on_next(result)
def mqtt_msg_dispatch(topic, msg):
model.instance.update_by_mqtt(topic.decode(), msg.decode())
def voltage_updater(sink):
def f(_timer):
schedule(push_voltage, sink)
return f
def push_voltage(sink):
raw = v_adc.read()
v = raw / 1024 * BATTERY_VOLTAGE_ADC_SCALE
print("[BATTERY] voltage = {0:.2f}v".format(v))
sink.on_next(v)
def adjust_display_brightness(sensor: TSL2561):
from display import instance as display
global last_light_sensor_sample_ts
global current_display_brightness
now = time.ticks_ms()
if now - last_light_sensor_sample_ts > LIGHT_SENSOR_SAMPLE_INTERVAL:
lux = sensor.read()
if lux >= 12:
b = 255
else:
b = 0
if abs(current_display_brightness - b) > 10:
current_display_brightness = b
display.set_brightness(b)
print("[LIGHT] {} lux, set display brightness to {}".format(lux, b))
last_light_sensor_sample_ts = now