Пример #1
0
    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)
Пример #2
0
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]
Пример #3
0
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}
Пример #4
0
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()
Пример #5
0
#Config and Log files
RPGARDEN_CONFIG_FILE = "/home/pi/code/rpgarden/rpgarden.ini"
RPGARDEN_LOG_FILE = "/home/pi/code/rpgarden/logs/datalogTest.csv"

# Set up the SPI Bus, The chip select, and the MCP
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
cs = digitalio.DigitalInOut(CHIP_SELECT_PIN)
mcp = MCP.MCP3008(spi, cs)
moistureSensor = McpSensor(mcp, RPGARDEN_CONFIG_FILE, \
                           "moisture_sensor_1_" + socket.gethostname())
photoSensor = McpSensor(mcp, RPGARDEN_CONFIG_FILE, \
                        "photo_sensor_1_" + socket.gethostname())

# Set up for the DHT sensor
GPIO.setmode(GPIO.BCM)
dhtSensor = DHTXX(pin=16, sensorType=DHTXX.DHT22, \
                  scale=DHTXX.FAHRENHEIT)

# Check if the log file exists, if not, start with a header row
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()