-
Notifications
You must be signed in to change notification settings - Fork 0
/
temphumids.py
40 lines (33 loc) · 1.05 KB
/
temphumids.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
from collections import deque
from functools import reduce
from threading import Lock
import logging
from dht11 import dht11
from rpithermostat import sampler
logger = logging.getLogger(__name__)
temphumids = deque([], maxlen=30)
sensor = dht11.DHT11(26)
lock_temphumids = Lock()
measure_thread = None
def get_current_temphumid():
with lock_temphumids:
size = len(temphumids)
avg_t = reduce(lambda tally, b: tally + b.temperature, temphumids, 0) / size
avg_h = reduce(lambda tally, b: tally + b.humidity, temphumids, 0) / size
retval = {'temperature':avg_t, 'humidity':avg_h}
logging.debug("current temphumid %r" % retval)
return retval
def record_temp():
current = sensor.read()
if current.is_valid():
with lock_temphumids:
temphumids.append(current)
else:
# retrying
record_temp()
def start_recording():
global measure_thread
logging.info("Start recording")
record_temp()
measure_thread = sampler.Sampler(1, record_temp)
measure_thread.start()