-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht22.py
33 lines (28 loc) · 987 Bytes
/
dht22.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
from time import sleep
import board
import adafruit_dht
class Dht22:
def __init__(self) -> None:
self.dhtDevice = adafruit_dht.DHT22(board.D6)
def read(self):
for attempt in range(20):
try:
dhtData = DhtData(self.dhtDevice.temperature, self.dhtDevice.humidity)
self.dhtDevice.exit()
return dhtData
except RuntimeError as error:
print(error.args[0])
sleep(2.0)
continue
except Exception as error:
self.dhtDevice.exit()
raise error
else:
# reach the retry limit
self.dhtDevice.exit()
raise Exception("cannot get dht22 data")
class DhtData:
def __init__(self, temperatureC, humidity) -> None:
self.temperatureC = round(temperatureC, 1)
self.temperatureF = round(temperatureC * (9 / 5) + 32, 1)
self.humidity = round(humidity, 1)