-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_dht.py
46 lines (36 loc) · 1.27 KB
/
monitor_dht.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
# judascleric 2019
import time
import Adafruit_DHT as dht
class RingBuffer:
def __init__(self, size_max):
self.max = size_max
self.data = []
class __Full:
def append(self, x):
self.data[self.cur] = x
self.cur = (self.cur+1) % self.max
def get(self):
return self.data[self.cur:]+self.data[:self.cur]
def append(self, x):
self.data.append(x)
if len(self.data) == self.max:
self.cur = 0
self.__class__ = self.__Full
def get(self):
return self.data
class DHTMonitor():
def __init__(self):
self.sensor_data = RingBuffer(32000)
self.min_refresh_interval = 2.0 # seconds
self.last_reading = time.time() - self.min_refresh_interval
def read_sensor(self):
elapsed = time.time() - self.last_reading
if elapsed > self.min_refresh_interval:
humidity,temperature = dht.read_retry(dht.AM2302, 4)
self.last_reading = time.time()
data = {
'time': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()),
'temperature': round(temperature * 9/5.0 + 32, 2),
'humidity': round(humidity, 2),
}
self.sensor_data.append(data)