-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
584 lines (393 loc) · 17.1 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import time
import uftpd
import uasyncio as asyncio
import json
from microdot_asyncio import Microdot, redirect, send_file, Response, Request
from machine import Pin, RTC, ADC, SoftI2C as I2C, Timer
from htu21d import *
from device import *
DEVICE1_TIMER = 20 #timer in seconds, switch off after this period
DEVICE2_TIMER = 10 #timer in seconds, switch off after this period
DEVICE2_PERIOD_TIMER = 18000 #in seconds, period for pump activation (device2)
DEBUG = False
rtc = RTC()
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=400000) #setup I2C
display = SSD1306_I2C(128, 32, i2c) #init oled driver
Request.max_content_length = 1024 * 1024
Request.max_body_length = 1024 * 1024
def getRtcTime():
date_str = "{2:02d}.{1:02d}.{0:4d}".format(*rtc.datetime())
time_str = "{4:02d}:{5:02d}".format(*rtc.datetime())
return [date_str, time_str]
class stream(): #async generator for server side events
def __init__(self):
self.data = { 'Sensors': {'hum':'0',
'temp':0,
'soil':0},
'Control': {'device1':0,
'device2':0,
'auto':0 },
'Names': {'device1_name': 'noname1',
'device2_name': 'noname2'},
'Time': {0},
'Date': {0}
}
with open("config.json", "r") as json_data: #load config from json file and read names for devices
self.config = json.load(json_data)
self.data['Names']['device1_name'] = device1.name = self.config['device1_name']
self.data['Names']['device2_name'] = device2.name = self.config['device2_name']
if "True" in self.config['auto']:
autoModeCtrl.status = True
else:
autoModeCtrl.status = False
def __aiter__(self):
return self
async def __anext__(self):
self.data['Names']['device1_name'] = device1.name
self.data['Names']['device2_name'] = device2.name
self.data['Control']['auto'] = autoModeCtrl.status
try:
self.temper = round(gy21.temperature)
gy21.LAST_VALUE_TEMP = self.temper
except:
if DEBUG: print('! exception ! TEMP Error')
self.temper = gy21.LAST_VALUE_TEMP
try:
self.hum = round(gy21.humidity)
gy21.LAST_VALUE_HU = self.hum
except:
if DEBUG: print('! exception ! HUM Error')
self.hum = gy21.LAST_VALUE_HU
date, current_time = getRtcTime()
self.soil = soilMeter.read() / 10
self.data['Sensors']['hum'] = self.hum
self.data['Sensors']['temp'] = self.temper
self.data['Sensors']['soil'] = self.soil
self.data['Control']['device1'] = device1.running()
self.data['Control']['device2'] = device2.running()
self.data['Time'] = current_time
self.data['Date'] = date
await asyncio.sleep(1)
return f"data: {json.dumps(self.data)}" + "\n\n"
# setup webserver
app = Microdot()
#
gy21 = HTU21D(22,21) #take temp and hum from sensor # HTU21D(scl, sda)
#setup devices
device1 = motorDevice(12, "device1") #fan
device2 = motorDevice(14, "device2") #pump
#setup soil sensor
soilMeter = ADC(Pin(34), atten=ADC.ATTN_11DB)
#setup automode control from Class
autoModeCtrl = autoModeControl()
#setup timer for display and debounce function
timer = Timer(0) #for display
pumpTimer = Timer(1) #timer for pump
def dispalySwitchOff(timer): #set timer for display off
display.fill(0)
display.show()
def button1_click_func(): #function for button1
if not device2.running() and Pin(13).value() == 1:
try:
temper = round(gy21.temperature)
gy21.LAST_VALUE_TEMP = temper
except:
temper = gy21.LAST_VALUE_TEMP
try:
hum = round(gy21.humidity)
gy21.LAST_VALUE_HU = hum
except:
hum = gy21.LAST_VALUE_HU
if button1.screen == 0:
display.fill(0)
display.text(f'auto mode: {autoModeCtrl.status}', 0, 0, 1)
display.text(f'temp, C: {temper}', 0, 12, 1)
display.text(f'hum, RH: {hum}', 0, 24, 1)
display.show()
button1.screen = 1 #set screen number
elif button1.screen == 1:
display.fill(0)
display.text(f'Watering Status', 0, 0, 1)
display.text(f'Soil: {autoModeCtrl.soilData} / {autoModeCtrl.th_soil}', 0, 12, 1)
if autoModeCtrl.soilData > autoModeCtrl.th_soil:
display.text(f'Need Water!', 0, 24, 1)
else:
display.text(f'Soil is OK', 0, 24, 1)
display.show()
button1.screen = 0 #set screen number
timer.init(mode=Timer.ONE_SHOT, period=5000, callback=dispalySwitchOff)
button1 = Button(Pin(13, Pin.IN, Pin.PULL_DOWN), callback=button1_click_func) #instatiate button object from device.py
device1.stop()
device2.stop()
async def writeLog(): # write log for temp, humid and soil. Send data to /chart. Need to have empty .json file at first start.
# json format:
# data = {
# "temper": [],
# "humid": [],
# "soil": []
# "time": []
# }
while True:
with open ("log2.json", "r") as f:
data = json.load(f)
date, current_time = getRtcTime()
try:
temper = round(gy21.temperature)
gy21.LAST_VALUE_TEMP = temper
except:
temper = gy21.LAST_VALUE_TEMP
try:
hum = round(gy21.humidity)
gy21.LAST_VALUE_HU = hum
except:
hum = gy21.LAST_VALUE_HU
soil = soilMeter.read()
# write last value to the last index
data["temper"].pop(0)
data["temper"].append(int(temper))
data["humid"].pop(0)
data["humid"].append(int(hum))
data["soil"].pop(0)
data["soil"].append(int(soil /10 ))
data["time"].pop(0)
data["time"].append(current_time)
#data_result = json.dumps(data)
with open ("log2.json", "w") as f:
json.dump(data, f)
await asyncio.sleep(3200)
async def emergencyDeviceControl(): #counter for emerg shutdown DEVICE1 or DEVICE2
device1.isTimerSet = False
device2.isTimerSet = False
while True:
if device1.running():
time_now = time.time()
if DEBUG: print('Device1 autooff in: ', DEVICE1_TIMER - round(time_now - device1.startTime))
if time_now - device1.startTime > DEVICE1_TIMER:
if DEBUG: print('time reached')
device1.stop()
if device2.running():
time_now = time.time()
if DEBUG: print('Device2 autooff in: ', DEVICE2_TIMER - round(time_now - device2.startTime))
if time_now - device2.startTime > DEVICE2_TIMER:
if DEBUG: print('time reached')
device2.stop()
await asyncio.sleep(2)
async def deviceControl(): #control for device1 and device2. Can use auto control or manual
write_request = True
last_max_temper = 0
last_max_hum = 0
with open ("config.json", "r") as file:
config = json.load(file)
# set thresholds for autowork sensors from config file
# set auto mode from config file
# humid is list [low_value, high_value]
th_temper = int(config["temper"])
th_humid_low = int(config["humid"][0])
th_humid_high = int(config["humid"][1])
autoModeCtrl.th_soil = int(config["soil"])
if "ON" or "True" in config['auto']:
autoModeCtrl.status = True
else:
autoModeCtrl.status = False
while True:
try:
temper = round(gy21.temperature)
gy21.LAST_VALUE_TEMP = temper
except:
if DEBUG: print('! exception ! TEMP Error')
temper = gy21.LAST_VALUE_TEMP
try:
hum = round(gy21.humidity)
gy21.LAST_VALUE_HU = hum
except:
if DEBUG: print('! exception ! HUM Error')
hum = gy21.LAST_VALUE_HU
if temper < th_temper:
last_max_temper = 0
if hum < th_humid_low:
last_max_hum = 0
with open ("log2.json", "r") as f:
data = json.load(f)
soil_data_len = len([i for i in data["soil"] if i>0])
if soil_data_len > 0:
soilData = int(sum(data["soil"][:-4:-1]) / len(data["soil"][:-4:-1]))
autoModeCtrl.soilData = soilData #pass value to class
autoMode = autoModeCtrl.status
if autoMode:
if temper > th_temper:
if DEBUG: print("Temp exceed threshold")
if temper > last_max_temper:
last_max_temper = temper
#write_request = True
# if not device1.running():
# device1.start()
#if write_request:
#with open("log.txt", "w") as output:
# output.write(f'Temp: {temper} {getRtcTime()} \n')
#if temper > last_max_temper:
# write_request = True
#else:
# write_request = False
if temper < th_temper:
#device1.stop()
if DEBUG: print("Temp below threshold")
#write_request = True
if hum > th_humid_high:
if hum > last_max_hum:
last_max_hum = hum
write_request = True
if DEBUG: print("Humidity exceed threshold")
if not device1.running():
device1.start()
# if write_request:
# with open("log.txt", "w") as output:
# output.write(f'Hum: {hum} {getRtcTime()} \n')
# if hum > last_max_hum:
# write_request = True
# else:
# write_request = False
if hum < th_humid_low: # or temper < th_temper :
if DEBUG: print("Humidity below threshold")
device1.stop()
#write_request = True
if soilData > autoModeCtrl.th_soil:
time_now = time.time()
if DEBUG: print("raw soil sensor", soilMeter.read(), "ALERT NEED WATER !!!!")
if time_now - device2.periodStartTime < DEVICE2_PERIOD_TIMER:
if DEBUG: print('auto device2, waiting to start', DEVICE2_PERIOD_TIMER - (time_now - device2.periodStartTime))
elif time_now - device2.periodStartTime > DEVICE2_PERIOD_TIMER:
if DEBUG: print("starting")
device2.periodStartTime = time.time()
device2.start()
with open("log.txt", "w") as output:
output.write(f'Last time device2 started: {getRtcTime()} \n')
else:
write_request = True
await asyncio.sleep(5)
@app.route('/progress', methods=["GET", "POST"]) #sending ServerSideEvents to browser
async def progress(request):
return stream(), 200, {"Content-Type": "text/event-stream"}
# return 'OK'
@app.route('/')
async def index(response):
return send_file("index.html")
@app.route('/static/<path:path>')
async def static(request, path):
if '..' in path:
# directory traversal is not allowed
return 'Not found', 404
return send_file('static/' + path)
@app.route('/config.json', methods=['GET', 'POST'] ) #here we save and load config data to server
async def config(request):
global DEVICE2_TIMER
global DEVICE2_PERIOD_TIMER
params = request.args #request args from GET
# config.json?save=1&h_temper=30&th_humid_low=60&th_humid_high=70&th_soil=190&th_lamp=OFF&dev_name1=test1&dev_name2=test2
if "save" in params:
config_data = {
"auto": str(autoModeCtrl.status),
"temper": params.get("h_temper") if params.get("h_temper") is not None else 0,
"humid": [params.get('th_humid_low') if params.get('th_humid_low') is not None else 0, \
params.get('th_humid_high') if params.get('th_humid_high') is not None else 0],
"soil": int(params.get("th_soil")) if params.get("th_soil") is not None else 180,
"lamp": params.get("th_lamp") if params.get("th_lamp") is not None else 0,
"device1_name": params.get("dev_name1") if params.get("dev_name1") is not None else "noname1",
"device2_name": params.get("dev_name2") if params.get("dev_name1") is not None else "noname2",
"dev_time":params.get('dev_time'),
"dev_date":params.get('dev_date'),
"dev2_runtime":int(params.get('run_time')) if params.get('run_time') is not None else DEVICE2_TIMER,
"dev2_period":int(params.get('period_time')) if params.get('period_time') is not None else DEVICE2_PERIOD_TIMER
}
device1.name, device2.name = params.get("dev_name1"), params.get("dev_name2")
autoModeCtrl.th_soil = int(config_data['soil'])
if params.get('run_time') is not None:
DEVICE2_TIMER = int(params.get('run_time'))
DEVICE2_PERIOD_TIMER = int(params.get('period_time')) * 60
if params.get('dev_time') is not None:
time = params.get('dev_time')
date = params.get('dev_date')
year = int(date[0:4])
month = int(date[5:7])
day = int(date[8::])
hour = int(time[0:2])
minute = int(time[3:5])
rtc.datetime((year, month, day, 0, hour, minute, 0, 0))
else:
date, time = getRtcTime()
config_data['dev_time']=time
config_data['dev_date']=f'{date[-4::]}-{date[3:5]}-{date[0:2]}' #convert to datetime notation yy-mm-dt
config_data['last_time'] = device2.lastTime # last time device was active
with open ("config.json", "w+") as file:
json.dump(config_data, file)
return send_file('config.json')
else:
with open("config.json", "r") as file:
config_data = json.load(file)
config_data['last_time'] = device2.lastTime
return config_data
@app.route('/control', methods=['GET', 'POST'])
async def control(request):
params = request.args #request args from GET
if 'wifi' in params: #fetch(`/config.json?wifi=${wifi_mode}&ssid=${params['ssid']}&ssid_pass=${params['ssid_pass']}`);
with open('boot_ini.json', 'r') as input:
settings = json.load(input)
settings['DEFAULT_WIFI_MODE'] = params.get('wifi')
if len(params.get('ssid')) > 0:
settings['SSID'] = params.get('ssid')
if len(params.get('ssid_pass')):
settings['SSID_PWD'] = params.get('ssid_pass')
with open('boot_ini.json', 'w') as input:
json.dump(settings, input)
if "auto_mode" not in params:
return Response(body="wrong parameters passed")
if "ON" in params.get('auto_mode'):
autoModeCtrl.status = True
if "OFF"in params.get('auto_mode'):
autoModeCtrl.status = False
if params['device1'] == 'ON':
device1.start()
if params['device1'] == 'OFF':
device1.stop()
if params['device2'] == 'ON':
device2.start()
if params['device2'] == 'OFF':
device2.stop()
return Response(body=params, headers = {"Content-Security-Policy-Report-Only" : "default-src 'self'"})
@app.route('/log.txt')
async def log(request):
return send_file('log.txt')
@app.route('/boot_ini.json')
async def log(request):
return send_file('boot_ini.json')
@app.route('/javascript2.js')
async def log(request):
return send_file('javascript2.js')
@app.route('/index2.html')
async def log(request):
return send_file('index2.html')
@app.route('/favicon.ico')
async def favicon(request):
return send_file('/static/favicon.ico')
@app.route('/chart', methods=['GET', 'POST']) #send log with data for charts. Data prepared by "writeLog()"
async def chart(request):
return send_file("log2.json")
@app.route('/log2.json')
async def log(request):
return send_file('log2.json')
@app.route('/shutdown')
async def shutdown(request):
request.app.shutdown()
return 'The server is shutting down...'
async def main():
task1 = asyncio.create_task(app.start_server(port=80, debug=False))
task2 = asyncio.create_task(writeLog())
task3 = asyncio.create_task(deviceControl())
task4 = asyncio.create_task(emergencyDeviceControl())
await task1
await task2
await task3
await task4
try:
asyncio.run(main())
finally:
asyncio.new_event_loop() # Clear retained state