2023-10-17 00:10:23 +02:00
|
|
|
import Adafruit_DHT
|
|
|
|
|
|
|
|
class Dht22Sensor:
|
|
|
|
def __init__(self, pin):
|
2023-10-17 00:19:51 +02:00
|
|
|
self.sensor = Adafruit_DHT.AM2302
|
2023-10-17 00:10:23 +02:00
|
|
|
self.pin = pin
|
2023-10-17 17:57:32 +02:00
|
|
|
self.last_read = None
|
2023-10-17 00:10:23 +02:00
|
|
|
|
|
|
|
def read(self):
|
|
|
|
humidity, temperature = Adafruit_DHT.read_retry(self.sensor, self.pin)
|
|
|
|
if humidity is not None and temperature is not None:
|
2023-10-17 17:57:32 +02:00
|
|
|
self.last_read = {'temperature': temperature, 'humidity': humidity}
|
|
|
|
return self.last_read
|
2023-10-17 00:10:23 +02:00
|
|
|
else:
|
2023-10-17 17:57:32 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
def get_last_read(self):
|
|
|
|
if self.last_read is None:
|
|
|
|
self.read()
|
|
|
|
return self.last_read
|