chickenhouse/climate_control/sensors.py

29 lines
884 B
Python
Raw Normal View History

'''Module for sensor implementations'''
try:
import Adafruit_DHT
except ImportError:
raise ImportError('Adafruit_DHT library not found.')
def is_valid(temperature, humidity):
'''Check if temperature and humidity are valid.'''
return True if humidity <= 100.0 and humidity >= 0.0 and \
temperature <= 100.0 and temperature >= -50.0 else False
class Dht22(object):
'''DHT 22 temperature and Humidity sensor class.'''
sensor = 22
pin = int()
def __init__(self, pin):
self.pin = pin
def read(self):
'''Read temperature and humidity.'''
temperature = -200.0
humidity = -1.0
valid = False
while valid is False:
temperature, humidity = Adafruit_DHT.read_retry(self.sensor, self.pin)
valid = is_valid(temperature, humidity)
return temperature, humidity