Esempio n. 1
0
class HumiditySensor:
    def __init__(self, spi, pin, conn, pump_pin):
        self.pin = pin
        self.spi = spi
        self.conn = conn
        self.pump = DeviceController(pin=pump_pin)
        self.value = None
        self.status = None
        self.timestamp = None
        self.force = False

    def set_min_max(self, min_humidity, max_humidity):
        self.pump.add_condition(lambda humidity, c_type: min_humidity >
                                humidity and c_type == 'humidity')

    def unset_min_max(self):
        self.pump.reset()

    def set_pump_state(self, state):
        self.pump.set_state(state)

    def get_pump_state(self):
        return self.pump.get_state()

    def read(self):
        # read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
        r = self.spi.xfer2([1, 8 + self.pin << 4, 0])
        self.value = ((r[1] & 3) << 8) + r[2]
        if self.value is not None:
            if self.value > 1023 or self.value < 0:
                self.status = 'INCOHERENT_READ'
            else:
                self.value = round(50 * (cos(pi * self.value / 1023) + 1))
                self.status = 'OK'
                if self.force is False:
                    self.pump.set_duration(5)
                    self.pump.verify_conditions(self.value, 'humidity')
                else:
                    self.pump.set_duration(None)
        else:
            self.status = 'FAILED_TO_RETRIEVE'
        return self.value, self.status

    def save(self):
        self.timestamp = self.conn.save('HUMIDITY', {
            'value': self.value,
            'status': self.status
        })

    def log(self):
        print(
            f"[HumiditySensor] Humidity: {self.value} - Status: {self.status} - Timestamp: {self.timestamp}"
        )

    def run(self, force):
        self.force = force
        self.read()
        self.save()
        self.log()
        return self.get_pump_state()
Esempio n. 2
0
class TemperatureSensor:
    def __init__(self, pin, heater_pin, conn):
        self.pin = pin
        self.retries = 5
        self.heater = DeviceController(pin=heater_pin)
        self.conn = conn
        self.sensor = Adafruit_DHT.DHT22
        self.value = None
        self.status = None
        self.timestamp = None
        self.force = False

    def set_min_max(self, min_temperature, max_temperature):
        self.heater.add_condition(
            lambda temperature, c_type: min_temperature > temperature and c_type == 'temperature')

    def unset_min_max(self):
        self.heater.reset()

    def set_heater_state(self, state):
        self.heater.set_state(state)

    def get_heater_state(self):
        return self.heater.get_state()

    def read(self):
        _, self.value = Adafruit_DHT.read_retry(self.sensor, self.pin, self.retries)
        if self.value is not None:
            self.value = round(self.value, 1)
            if self.value > 80 or self.value < -40:
                self.status = 'INCOHERENT_READ'
            else:
                self.status = 'OK'
                if self.force is False:
                    self.heater.verify_conditions(self.value, 'temperature')
        else:
            self.status = 'FAILED_TO_RETRIEVE'
        return self.value, self.status

    def save(self):
        self.timestamp = self.conn.save('TEMPERATURE', {'value': self.value, 'status': self.status})

    def log(self):
        print(f"[TemperatureSensor] Temperature: {self.value} - Status: {self.status} - Timestamp: {self.timestamp}")

    def run(self, force):
        self.force = force
        self.read()
        self.save()
        self.log()
        return self.get_heater_state()
Esempio n. 3
0
class PhotoSensor:
    def __init__(self, spi, pin, conn, lamp_pin):
        self.spi = spi
        self.pin = pin
        self.conn = conn
        self.lamp = DeviceController(pin=lamp_pin)
        self.value = None
        self.status = None
        self.timestamp = None
        self.force = False

    def set_min_max(self, min_illumination, max_illumination):
        self.lamp.add_condition(lambda photo, c_type: min_illumination > photo and c_type == 'photo')

    def unset_min_max(self):
        self.lamp.reset()

    def set_lamp_state(self, state):
        self.lamp.set_state(state)

    def get_lamp_state(self):
        return self.lamp.get_state()

    def read(self):
        # read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
        r = self.spi.xfer2([1, 8 + self.pin << 4, 0])
        self.value = ((r[1] & 3) << 8) + r[2]
        if self.value is not None:
            if self.value > 1023 or self.value < 0:
                self.status = 'VALUE_OUT_OF_BOUNDS'
            else:
                self.status = 'OK'
                self.value = round(50 * (cos(pi * self.value / 1023) + 1))
                if self.force is False:
                    self.lamp.verify_conditions(self.value, 'photo')
        else:
            self.status = 'CANNOT_READ_LDR'
        return self.value, self.status

    def save(self):
        self.timestamp = self.conn.save('ILLUMINATION', {'value': self.value, 'status': self.status})

    def log(self):
        print(f"[PhotoSensor] Value: {self.value} - Status: {self.status} - Timestamp: {self.timestamp}")

    def run(self, force):
        self.force = force
        self.read()
        self.save()
        self.log()
        return self.get_lamp_state()