-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathth.py
111 lines (99 loc) · 4.59 KB
/
th.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
from umqtt.simple import MQTTClient
from uwebsrv.microWebSrv import MicroWebSrv
from machine import Pin
from dht import DHT11
from math import sqrt
from time import sleep
class History(object):
def __init__(self, max_history):
self.max_history = max_history
self.items = []
def append(self, elmnt):
self.items.insert(0, elmnt)
if len(self.items) > self.max_history:
self.items.pop()
def __iter__(self):
return iter(self.items)
def __len__(self):
return len(self.items)
t, h = 0, 0
MAX_HISTORY = 20
hist_t, hist_h = History(MAX_HISTORY), History(MAX_HISTORY)
max_t, max_h = -100, -100
min_t, min_h = 100, 100
mean_t, mean_h = 0, 0
var_t, var_h = 0, 0
std_t, std_h= 0, 0
def mean_variance_std(deq):
sum = 0
for elmnt in deq:
sum = sum + elmnt
mean = sum / len(deq)
if len(deq) > 1:
sum = 0
for elmnt in deq:
sum = sum + (elmnt-mean)**2
var = sum / (len(deq) - 1)
return mean, var, sqrt(var)
else:
return mean, 0, 0
server = 'test.mosquitto.org'
client_id = 'ESP32_DHT11_BOULA'
client = MQTTClient(client_id, server)
try:
client.connect()
except:
pass
led = Pin(21, Pin.OUT, Pin.PULL_UP)
@MicroWebSrv.route('/dht')
def _httpHandlerDHTGet(httpClient, httpResponse):
led.value(not led.value())
data = 'Current Temperature: {0}°C - Current Humidity: {1}%<br/>Temperature Maximum: {2}°C - Humidity Maximum: {3}%<br/>Temperature Minimum: {4}°C - Humidity Minimum: {5}%<br/>Temperature Mean: {6:3.4f} - Humidity Mean: {7:3.4f}<br/>Temperature Variance: {8:3.4f} - Humidity Variance: {9:3.4f}<br/>Temperature Standard Deviation: {10:3.4f} - Humidity Standard Deviation: {11:3.4f}<br/>'.format(t, h, max_t, max_h, min_t, min_h, mean_t, mean_h, var_t, var_h, std_t, std_h)
httpResponse.WriteResponseOk(
headers = ({'Cache-Control': 'no-cache'}),
contentType = 'text/event-stream',
contentCharset = 'UTF-8',
content = 'data: {0}\n\n'.format(data))
routeHandlers = [ ( "/dht", "GET", _httpHandlerDHTGet ) ]
srv = MicroWebSrv(routeHandlers=routeHandlers, webPath='/uwebsrv/www/')
srv.Start(threaded=True)
th = DHT11(Pin(0, Pin.IN, Pin.PULL_UP))
while True:
sleep(2)
try:
th.measure()
t, h = th.temperature(), th.humidity()
if isinstance(t, int) and isinstance(h, int):
if t > max_t:
max_t = t
elif t < min_t:
min_t = t
if h > max_h:
max_h = h
elif h < min_h:
min_h = h
hist_t.append(t)
hist_h.append(h)
mean_t, var_t, std_t = mean_variance_std(hist_t)
mean_h, var_h, std_h = mean_variance_std(hist_h)
msg = b'Current Temperature: {0}C\t\tCurrent Humidity: {1}%\nTemperature Maximum: {2}C\t\tHumidity Maximum: {3}%\nTemperature Minimum: {4}C\t\tHumidity Minimum: {5}%\nTemperature Mean: {6:3.4f}\t\tHumidity Mean: {7:3.4f}\nTemperature Variance: {8:3.4f}\t\tHumidity Variance: {9:3.4f}\nTemperature Standard Deviation: {10:3.4f}\tHumidity Standard Deviation: {11:3.4f}\n'.format(t, h, max_t, max_h, min_t, min_h, mean_t, mean_h, var_t, var_h, std_t, std_h)
print(msg)
try:
client.publish(b'cse1/train/temperature/current', b'{0}C'.format(t))
client.publish(b'cse1/train/temperature/max', b'{0}C'.format(max_t))
client.publish(b'cse1/train/temperature/min', b'{0}C'.format(min_t))
client.publish(b'cse1/train/temperature/mean', b'{0:3.4f}'.format(mean_t))
client.publish(b'cse1/train/temperature/variance', b'{0:3.4f}'.format(var_t))
client.publish(b'cse1/train/temperature/std', b'{0:3.4f}'.format(std_t))
client.publish(b'cse1/train/humidity/current', b'{0}%'.format(h))
client.publish(b'cse1/train/humidity/max', b'{0}%'.format(max_h))
client.publish(b'cse1/train/humidity/min', b'{0}%'.format(min_h))
client.publish(b'cse1/train/humidity/mean', b'{0:3.4f}%'.format(mean_h))
client.publish(b'cse1/train/humidity/variance', b'{0:3.4f}%'.format(var_h))
client.publish(b'cse1/train/humidity/std', b'{0:3.4f}%'.format(std_h))
except:
print('Failed to publish the message to the MQTT broker.\n')
else:
print('Invalid sensor reading.')
except OSError:
print('Failed to read sensor.')