20 lines
No EOL
599 B
Python
20 lines
No EOL
599 B
Python
import Adafruit_DHT
|
|
|
|
class Dht22Sensor:
|
|
def __init__(self, pin):
|
|
self.sensor = Adafruit_DHT.AM2302
|
|
self.pin = pin
|
|
self.last_read = None
|
|
|
|
def read(self):
|
|
humidity, temperature = Adafruit_DHT.read_retry(self.sensor, self.pin)
|
|
if humidity is not None and temperature is not None:
|
|
self.last_read = {'temperature': temperature, 'humidity': humidity}
|
|
return self.last_read
|
|
else:
|
|
return None
|
|
|
|
def get_last_read(self):
|
|
if self.last_read is None:
|
|
self.read()
|
|
return self.last_read |