Ejemplo n.º 1
0
    def __init__(self, db_path='/home/pi/.greenhouse/greenhouse.db'):
        """
        db_path defaults to /home/pi/.greenhouse/greenhouse.db
        """
        self.db = GreenhouseDatabase(db_path)

        self.darkness_level = 0.01
    def __init__(self, db_path='/home/pi/.greenhouse/greenhouse.db'):
        """
        db_path defaults to /home/pi/.greenhouse/greenhouse.db
        """
        self.db = GreenhouseDatabase(db_path)

        self.darkness_level = 0.01
    def __init__(self, db_path='/home/pi/.greenhouse/greenhouse.db'):
        """
        db_path defaults to /home/pi/.greenhouse/greenhouse.db
        """
        self.db = GreenhouseDatabase(db_path)

        self.target_temperature_lower = 20
        self.target_temperature_upper = 30

        self.target_humidity_lower = 60
        self.target_humidity_upper = 85

        self.target_soil = 60

        self.target_light = 60

        self.status_colours = {
            self.SENSOR_LOW: 'blue',
            self.SENSOR_OK: 'green',
            self.SENSOR_HIGH: 'red',
        }

        self._setup_gpio()
    def __init__(self, db_path="/home/pi/.greenhouse/greenhouse.db"):
        """
        db_path defaults to /home/pi/.greenhouse/greenhouse.db
        """
        self.db = GreenhouseDatabase(db_path)

        self.target_temperature_lower = 20
        self.target_temperature_upper = 30

        self.target_humidity_lower = 60
        self.target_humidity_upper = 85

        self.target_soil = 60

        self.target_light = 60

        self.status_colours = {self.SENSOR_LOW: "blue", self.SENSOR_OK: "green", self.SENSOR_HIGH: "red"}

        self._setup_gpio()
Ejemplo n.º 5
0
class Greenhouse(object):
    DHT_SENSOR = Adafruit_DHT.DHT22
    DHT = 19
    SOIL = 26
    LIGHT = 18

    def __init__(self, db_path='/home/pi/.greenhouse/greenhouse.db'):
        """
        db_path defaults to /home/pi/.greenhouse/greenhouse.db
        """
        self.db = GreenhouseDatabase(db_path)

        self.darkness_level = 0.01

    @property
    def temperature(self):
        humidity, temperature = self._get_humidity_and_temperature()
        return temperature

    @property
    def humidity(self):
        humidity, temperature = self._get_humidity_and_temperature()
        return humidity

    @property
    def soil(self):
        return self._get_average_soil_moisture(5)

    @property
    def light(self):
        return self._get_average_light_level(5)

    def _get_humidity_and_temperature(self):
        humidity, temperature = Adafruit_DHT.read_retry(sensor=self.DHT_SENSOR,
                                                        pin=self.DHT,
                                                        retries=5)
        return (humidity, temperature)

    def _get_soil_moisture(self):
        time_taken = self._time_charging_soil_capacitor()
        totally_wet_time = 4E-3
        totally_dry_time = 0.9
        moisture = (math.log(time_taken / totally_dry_time) /
                    math.log(totally_wet_time / totally_dry_time))
        return max(0, min(1, moisture)) * 100

    def _time_charging_soil_capacitor(self):
        pin = self.SOIL
        GPIO.setup(pin, GPIO.OUT, GPIO.PUD_OFF)
        GPIO.output(pin, GPIO.LOW)
        sleep(0.1)
        start_time = time()
        GPIO.setup(pin, GPIO.IN)
        max_time = 1
        while GPIO.input(pin) == GPIO.LOW and time() - start_time < max_time:
            pass
        end_time = time()
        time_taken = end_time - start_time
        return time_taken

    def _get_light_level(self):
        time_taken = self._time_charging_light_capacitor()
        value = 100 * time_taken / self.darkness_level
        return 100 - value

    def _time_charging_light_capacitor(self):
        pin = self.LIGHT
        GPIO.setup(pin, GPIO.OUT)
        GPIO.output(pin, GPIO.LOW)
        sleep(0.1)
        GPIO.setup(pin, GPIO.IN)
        start_time = time()
        end_time = time()
        while (GPIO.input(pin) == GPIO.LOW
               and time() - start_time < self.darkness_level):
            end_time = time()
        time_taken = end_time - start_time
        return min(time_taken, self.darkness_level)

    def _get_average_soil_moisture(self, num):
        values = [self._get_soil_moisture() for n in range(num)]
        average_value = sum(values) / len(values)
        return average_value

    def _get_average_light_level(self, num):
        values = [self._get_light_level() for n in range(num)]
        average_value = sum(values) / len(values)
        return average_value

    def _get_timestamp(self):
        dt = datetime.now()
        dt_date = str(dt.date())
        dt_time = str(dt.time())
        timestamp = "%s %s" % (dt_date, dt_time[:8])
        return timestamp

    def record_sensor_values(self):
        """
        Save sensor readings to database
        """
        timestamp = self._get_timestamp()
        temperature = self.temperature
        humidity = self.humidity
        soil = self.soil
        light = self.light

        values = (timestamp, temperature, humidity, soil, light)

        self.db.record_sensor_values(values)

    def export_to_csv(self, file_path='/home/pi/greenhouse.csv'):
        """
        Export sensor data from database and save as CSV file in file_path
        Defaults to /home/pi/greenhouse.csv
        """
        self.db.export_to_csv(file_path)
class GreenhouseIndicator(object):
    LED_COLOURS = [
        'white',
        'red',
        'blue',
        'green',
    ]

    LEDS = {
        'white': [13, 9, 27],
        'red': [16, 11, 23],
        'blue': [20, 6, 22],
        'green': [21, 12, 25],
    }

    SENSOR_LOW = 'low'
    SENSOR_OK = 'ok'
    SENSOR_HIGH = 'high'

    def __init__(self, db_path='/home/pi/.greenhouse/greenhouse.db'):
        """
        db_path defaults to /home/pi/.greenhouse/greenhouse.db
        """
        self.db = GreenhouseDatabase(db_path)

        self.target_temperature_lower = 20
        self.target_temperature_upper = 30

        self.target_humidity_lower = 60
        self.target_humidity_upper = 85

        self.target_soil = 60

        self.target_light = 60

        self.status_colours = {
            self.SENSOR_LOW: 'blue',
            self.SENSOR_OK: 'green',
            self.SENSOR_HIGH: 'red',
        }

        self._setup_gpio()

    @property
    def temperature(self):
        return self.db.get_sensor_value('temperature')

    @property
    def humidity(self):
        return self.db.get_sensor_value('humidity')

    @property
    def soil(self):
        return self.db.get_sensor_value('soil')

    @property
    def light(self):
        return self.db.get_sensor_value('light')

    @property
    def temperature_status(self):
        lower = self.target_temperature_lower
        upper = self.target_temperature_upper

        if lower <= self.temperature <= upper:
            return self.SENSOR_OK
        elif self.temperature < lower:
            return self.SENSOR_LOW
        elif self.temperature > upper:
            return self.SENSOR_HIGH

    @property
    def humidity_status(self):
        lower = self.target_humidity_lower
        upper = self.target_humidity_upper

        if lower <= self.humidity <= upper:
            return self.SENSOR_OK
        elif self.humidity < lower:
            return self.SENSOR_LOW
        elif self.humidity > upper:
            return self.SENSOR_HIGH

    @property
    def soil_status(self):
        if self.soil > self.target_soil:
            return self.SENSOR_OK
        else:
            return self.SENSOR_LOW

    @property
    def light_status(self):
        if self.light >= self.target_light:
            return self.SENSOR_OK
        else:
            return self.SENSOR_LOW

    def _setup_gpio(self):
        for colour in self.LEDS:
            for led in self.LEDS[colour]:
                GPIO.setup(led, GPIO.OUT)
                GPIO.output(led, False)

    def _turn_led_on_or_off(self, colour, index, on_or_off):
        led = self.LEDS[colour][index]
        GPIO.output(led, on_or_off)

    def _turn_led_on(self, colour, index):
        self._turn_led_on_or_off(colour, index, on_or_off=True)

    def _turn_led_off(self, colour, index):
        self._turn_led_on_or_off(colour, index, on_or_off=False)

    def _turn_colour_leds_on_or_off(self, colour, on_or_off):
        leds = self.LEDS[colour]
        for led in range(len(leds)):
            if on_or_off:
                self._turn_led_on(colour, led)
            else:
                self._turn_led_off(colour, led)

    def _turn_colour_leds_on(self, colour):
        self._turn_colour_leds_on_or_off(colour=colour, on_or_off=True)

    def _turn_colour_leds_off(self, colour):
        self._turn_colour_leds_on_or_off(colour=colour, on_or_off=False)

    def _turn_index_leds_on_or_off(self, index, on_or_off):
        for colour in self.LEDS:
            if on_or_off:
                self._turn_led_on(colour, index)
            else:
                self._turn_led_off(colour, index)

    def _turn_index_leds_on(self, index):
        self._turn_index_leds_on_or_off(index=index, on_or_off=True)

    def _turn_index_leds_off(self, index):
        self._turn_index_leds_on_or_off(index=index, on_or_off=False)

    def _turn_all_leds_on_or_off(self, on_or_off):
        for colour in self.LEDS:
            if on_or_off:
                self._turn_colour_leds_on(colour)
            else:
                self._turn_colour_leds_off(colour)

    def _turn_all_leds_on(self):
        self._turn_all_leds_on_or_off(on_or_off=True)

    def _turn_all_leds_off(self):
        self._turn_all_leds_on_or_off(on_or_off=False)

    def turn_leds_on(self, colour=None, index=None):
        """
        Turn LEDs on
        - if colour given, only that colour
        - if index given, only that index
        - if both given, only that LED
        - if neither given, all LEDs

        e.g. turn_leds_on()
        e.g. turn_leds_on(colour='red')
        e.g. turn_leds_on(index=0)
        e.g. turn_leds_on(colour='red', index=0)
        """
        if colour and index is not None:
            self._turn_led_on(colour, index)
        elif colour:
            self._turn_colour_leds_on(colour)
        elif index is not None:
            self._turn_index_leds_on(index)
        else:
            self._turn_all_leds_on()

    def turn_leds_off(self, colour=None, index=None):
        """
        Turn LEDs off
        - if colour given, only that colour
        - if index given, only that index
        - if both given, only that LED
        - if neither given, all LEDs

        e.g. turn_leds_off()
        e.g. turn_leds_off(colour='red')
        e.g. turn_leds_off(index=0)
        e.g. turn_leds_off(colour='red', index=0)
        """
        if colour and index is not None:
            self._turn_led_off(colour, index)
        elif colour:
            self._turn_colour_leds_off(colour)
        elif index is not None:
            self._turn_index_leds_off(index)
        else:
            self._turn_all_leds_off()

    def show_status_on_leds(self):
        """
        Use LEDs to indicate sensor statuses according to self.status_colours
        """
        sensor_statuses = [
            self.temperature_status,
            self.humidity_status,
            self.soil_status,
            self.light_status,
        ]

        for status in sensor_statuses:
            colour = self.status_colours[status]
            self.turn_leds_on(colour)
            sleep(2)
            self.turn_leds_off(colour)
            sleep(0.1)
class Greenhouse(object):
    DHT_SENSOR = Adafruit_DHT.DHT22
    DHT = 19
    SOIL = 26
    LIGHT = 18

    def __init__(self, db_path='/home/pi/.greenhouse/greenhouse.db'):
        """
        db_path defaults to /home/pi/.greenhouse/greenhouse.db
        """
        self.db = GreenhouseDatabase(db_path)

        self.darkness_level = 0.01

    @property
    def temperature(self):
        humidity, temperature = self._get_humidity_and_temperature()
        return temperature

    @property
    def humidity(self):
        humidity, temperature = self._get_humidity_and_temperature()
        return humidity

    @property
    def soil(self):
        return self._get_average_soil_moisture(5)

    @property
    def light(self):
        return self._get_average_light_level(5)

    def _get_humidity_and_temperature(self):
        humidity, temperature = Adafruit_DHT.read_retry(
            sensor=self.DHT_SENSOR,
            pin=self.DHT,
            retries=5
        )
        return (humidity, temperature)

    def _get_soil_moisture(self):
        time_taken = self._time_charging_soil_capacitor()
        totally_wet_time = 4E-3
        totally_dry_time = 0.9
        moisture = (
            math.log(time_taken / totally_dry_time) /
            math.log(totally_wet_time / totally_dry_time)
        )
        return max(0, min(1, moisture)) * 100

    def _time_charging_soil_capacitor(self):
        pin = self.SOIL
        GPIO.setup(pin, GPIO.OUT, GPIO.PUD_OFF)
        GPIO.output(pin, GPIO.LOW)
        sleep(0.1)
        start_time = time()
        GPIO.setup(pin, GPIO.IN)
        max_time = 1
        while GPIO.input(pin) == GPIO.LOW and time() - start_time < max_time:
            pass
        end_time = time()
        time_taken = end_time - start_time
        return time_taken

    def _get_light_level(self):
        time_taken = self._time_charging_light_capacitor()
        value = 100 * time_taken / self.darkness_level
        return 100 - value

    def _time_charging_light_capacitor(self):
        pin = self.LIGHT
        GPIO.setup(pin, GPIO.OUT)
        GPIO.output(pin, GPIO.LOW)
        sleep(0.1)
        GPIO.setup(pin, GPIO.IN)
        start_time = time()
        end_time = time()
        while (
            GPIO.input(pin) == GPIO.LOW and
            time() - start_time < self.darkness_level
        ):
            end_time = time()
        time_taken = end_time - start_time
        return min(time_taken, self.darkness_level)

    def _get_average_soil_moisture(self, num):
        values = [self._get_soil_moisture() for n in range(num)]
        average_value = sum(values) / len(values)
        return average_value

    def _get_average_light_level(self, num):
        values = [self._get_light_level() for n in range(num)]
        average_value = sum(values) / len(values)
        return average_value

    def _get_timestamp(self):
        dt = datetime.now()
        dt_date = str(dt.date())
        dt_time = str(dt.time())
        timestamp = "%s %s" % (dt_date, dt_time[:8])
        return timestamp

    def record_sensor_values(self):
        """
        Save sensor readings to database
        """
        timestamp = self._get_timestamp()
        temperature = self.temperature
        humidity = self.humidity
        soil = self.soil
        light = self.light

        values = (timestamp, temperature, humidity, soil, light)

        self.db.record_sensor_values(values)

    def export_to_csv(self, file_path='/home/pi/greenhouse.csv'):
        """
        Export sensor data from database and save as CSV file in file_path
        Defaults to /home/pi/greenhouse.csv
        """
        self.db.export_to_csv(file_path)