-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht_service.py
40 lines (36 loc) · 1.05 KB
/
dht_service.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
import time
import board
import adafruit_dht
import aqi_gadget_config
import time
device = None
def init ():
global device
# Initial the dht device, with data pin connected to:
device = adafruit_dht.DHT22(board.D18, use_pulseio=True)
return device
def stop ():
global device
device.exit()
def read_packet ():
global device
try:
offsetC = aqi_gadget_config.temp_offset_celsius
temperature_c = device.temperature + offsetC
temperature_f = temperature_c * (9 / 5) + 32
h_factor = 1.0 / (2.0 ** (offsetC / 11.0))
humidity = device.humidity * h_factor
return {
"C" : temperature_c,
"F" : temperature_f,
"H" : humidity,
"time" : time.time(),
}
except RuntimeError as error:
# Errors happen fairly often, DHT's are hard to read, just keep going
print("WARNING: [dht]", error.args[0])
return None
except Exception as error:
stop()
print("ERROR: [dht] bail: ", str(error))
return "FAIL"