class DhtSensor(GenericSensor): def __init__(self, iniSectionName): try: cfg = Config.DhtSensorConfig(iniSectionName) self.cfg = cfg self.temperature = None self.humidity = None self.error = None # set up DHTXX Sensor object self.sensor = DHTXX(pin=cfg.pin, sensorType=eval(cfg.dht_type), scale=eval(cfg.scale)) except Exception as ex: # Handle other exceptions print(type(ex)) print(ex.args) print(ex) raise (ex) def read_raw(self): return self.read() def read(self): dhtVal = self.sensor.read_and_retry() if dhtVal.is_valid(): self.temperature = str(round(dhtVal.temperature, 1)) self.humidity = str(round(dhtVal.humidity, 1)) self.error = None return dhtVal else: print("Error: %d" % resdhtValult.error_code) self.temperature = None self.humidity = None self.error = dhtVal.error_code return {"Error": dhtVal.error_code} def readObj(self): # Need to override because we're returning two abjects dhtVal = self.read() if dhtVal.is_valid(): sensorResultDict1 = { "description": self.cfg.description_temperature, "sort": int(self.cfg.sort_temperature), "type": self.cfg.type_temperature, "field_name": self.cfg.field_name_temperature, "format": self.cfg.format_temperature, "reading": self.temperature } sensorResultDict2 = { "description": self.cfg.description_humidity, "sort": int(self.cfg.sort_humidity), "type": self.cfg.type_humidity, "field_name": self.cfg.field_name_humidity, "format": self.cfg.format_humidity, "reading": self.humidity } return [sensorResultDict1, sensorResultDict2]
class DhtSensor(GenericSensor): def __init__(self, iniSectionName): try: cfg = Config.DhtSensorConfig(iniSectionName) self.cfg = cfg self.temperature = None self.humidity = None self.error = None # set up DHTXX Sensor object self.sensor = DHTXX(pin=cfg.pin, sensorType=eval(cfg.dht_type), scale=eval(cfg.scale)) except Exception as ex: # Handle other exceptions print(type(ex)) print(ex.args) print(ex) raise (ex) def read_raw(self): return self.read() def read(self): dhtVal = self.sensor.read_and_retry() if dhtVal.is_valid(): self.temperature = str(round(dhtVal.temperature, 1)) self.humidity = str(round(dhtVal.humidity, 1)) self.error = None return dhtVal else: print("Error: %d" % resdhtValult.error_code) self.temperature = None self.humidity = None self.error = dhtVal.error_code return {"Error": dhtVal.error_code}
import RPi.GPIO as GPIO from dhtxx import DHTXX from time import sleep # initialize GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) # read data using GPIO #16 pin for a DHT22 sensor with results in FAHRENHEIT instance = DHTXX(pin=16, sensorType=DHTXX.DHT22, scale=DHTXX.FAHRENHEIT) print("Reading Sensor...") result = instance.read_and_retry() if result.is_valid(): print("Temperature: %-3.1f F" % result.temperature) print("Humidity: %-3.1f %%" % result.humidity) else: print("Error: %d" % result.error_code) GPIO.cleanup()
if not path.exists(RPGARDEN_LOG_FILE): with open(RPGARDEN_LOG_FILE, mode='w') as logFile: fieldnames = ['log_date', 'temperature', 'humidity',\ 'soil_moisture','light'] logFile_writer = csv.writer(logFile) logFile_writer.writerow(fieldnames) # just read the sensors once and log it. try: moistureVal = moistureSensor.read() print("Soil Moisture: %.2f" % moistureVal) photoVal = photoSensor.read() print("Light Sensor: %.2f" % photoVal) dhtVal = dhtSensor.read_and_retry() if dhtVal.is_valid(): print("Temperature: %-3.1f F" % dhtVal.temperature) print("Humidity: %-3.1f %%" % dhtVal.humidity) else: print("Error: %d" % resdhtValult.error_code) with open(RPGARDEN_LOG_FILE, mode='a') as logFile: logFile_writer = csv.writer(logFile) logFile_writer.writerow([datetime.datetime.now(), \ round(dhtVal.temperature,2), \ round(dhtVal.humidity,2),\ round(moistureVal,2), \ round(photoVal,2)]) print("\n")