-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdht22.py
More file actions
26 lines (23 loc) · 779 Bytes
/
dht22.py
File metadata and controls
26 lines (23 loc) · 779 Bytes
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
# dht22.py
# Helper class that fetches data from our dht22 sensor
# uses adafruit_dht ❤️ https://github.com/adafruit/Adafruit_CircuitPython_DHT
# Pi imports
import adafruit_dht
class DHT22():
# Helper class
def __init__(self, Pin):
self.pin = Pin
self.dht_device = adafruit_dht.DHT22(self.pin)
def fetch_data(self):
temperature = None
humidity = None
retries = 0
while temperature == None or humidity == None:
if retries > 10: # Try 10 times and then return None
break
try:
temperature = self.dht_device.temperature
humidity = self.dht_device.humidity
except:
retries += 1
return (temperature, humidity)