Repository navigation
Expand file tree
/
Copy pathdht22.py
More file actions
63 lines (54 loc) · 1.7 KB
/
Copy pathdht22.py
File metadata and controls
63 lines (54 loc) · 1.7 KB
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
import time
import board
import adafruit_dht
from config import log, CONFIG
from metrics_handler import log_local_temperature_humidity
# Replace D4 with whatever pin data actually is connected
_pin = board.D4
def _create_connection_sensor() -> adafruit_dht.DHT22:
"""
Connect to sensor
:return: Object of DHT22 with connection initialized
"""
return adafruit_dht.DHT22(_pin)
def exit_dht_sensor(dhtDevice: adafruit_dht.DHT22):
"""
Properly exit sensor
"""
dhtDevice.exit()
def get_temperature_humidity() -> (float, float):
"""
Get temperature & humidity
:return: (temperature, humidity)
"""
temp, hum = retrieve_data_sensor()
if CONFIG["metrics"]["record_metrics"]:
log_local_temperature_humidity(temp, hum)
return temp, hum
def retrieve_data_sensor(tries = 5) -> (float, float):
"""
Retrieve data from sensor
:param tries: number of time to accept an error
:return: (temperature, humidity)
"""
dhtDevice = _create_connection_sensor()
nb_try = 0
while nb_try < tries:
time.sleep(1)
try:
temp = float(dhtDevice.temperature)
hum = float(dhtDevice.humidity)
# Sometimes, the sensor will return 25.5, which is incorrect
# See https://forum.arduino.cc/t/arduino-due-dht22-wrong-value/1035449/3
if temp == 25.5 and hum == 25.5:
raise RuntimeError("Wrong value: 25.5")
except RuntimeError as error:
log.warn(error)
nb_try+=1
except Exception as error:
dhtDevice.exit()
raise error
else:
dhtDevice.exit()
return temp, hum
dhtDevice.exit()