From 5edff46b87c2fedb204217454be880913eb60697 Mon Sep 17 00:00:00 2001 From: Michael Lynch Date: Thu, 19 Jan 2017 21:36:40 -0500 Subject: [PATCH] Connecting to real components --- greenpithumb/fake_sensors.py | 41 -------------------------- greenpithumb/greenpithumb.py | 20 ++++++++----- greenpithumb/wiring_config.ini.example | 4 +-- 3 files changed, 14 insertions(+), 51 deletions(-) delete mode 100644 greenpithumb/fake_sensors.py diff --git a/greenpithumb/fake_sensors.py b/greenpithumb/fake_sensors.py deleted file mode 100644 index 708ed38..0000000 --- a/greenpithumb/fake_sensors.py +++ /dev/null @@ -1,41 +0,0 @@ -"""Collection of fake sensosrs to use until we switch to real hardware.""" - -# TODO(mtlynch): Delete this once we move to real hardware. - -import random - -import adc -from dht11 import dht11 - - -class FakeAdc(adc.Adc): - """Fake ADC to use (temporarily) until we switch to real hardware. - - Fake implementation of an ADC. Generates random readings. - """ - - def __init__(self, light_start, moisture_start): - self._pin_values = { - adc.PIN_LIGHT_SENSOR: light_start, - adc.PIN_MOISTURE_SENSOR: moisture_start, - } - - def read_pin(self, pin): - self._pin_values[pin] += random.randint(-5, 5) - return self._pin_values[pin] - - -class FakeDht11(object): - - def __init__(self, temperature_start, humidity_start): - self._temperature = temperature_start - self._humidity = humidity_start - - def read(self): - self._temperature += random.randint(-5, 5) - self._humidity += random.randint(-5, 5) - - return dht11.DHT11Result( - error_code=dht11.DHT11Result.ERR_NO_ERROR, - temperature=self._temperature, - humidity=self._humidity) diff --git a/greenpithumb/greenpithumb.py b/greenpithumb/greenpithumb.py index 4a3a09c..a659dfd 100644 --- a/greenpithumb/greenpithumb.py +++ b/greenpithumb/greenpithumb.py @@ -1,7 +1,10 @@ import argparse import time -import fake_sensors +import Adafruit_DHT + +import clock +import dht11 import humidity_sensor import light_sensor import moisture_sensor @@ -13,13 +16,14 @@ class SensorHarness(object): """Simple container for GreenPiThumbs that polls their values and prints.""" def __init__(self, wiring_config): - # TODO(mtlynch): Hook wiring_config up to components that depend on it. - self._adc = fake_sensors.FakeAdc( - light_start=500.0, moisture_start=600.0) - self._light_sensor = light_sensor.LightSensor(self._adc) - self._moisture_sensor = moisture_sensor.MoistureSensor(self._adc) - self._dht11 = fake_sensors.FakeDht11( - temperature_start=35.0, humidity_start=150.0) + local_clock = clock.LocalClock() + self._light_sensor = light_sensor.LightSensor( + self._adc, wiring_config.adc_channels.light_sensor) + self._moisture_sensor = moisture_sensor.MoistureSensor( + self._adc, wiring_config.adc_channels.soil_moisture_sensor) + self._dht11 = dht11.CachingDHT11( + lambda: Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, wiring_config.gpio_pins.dht11), + local_clock) self._temperature_sensor = temperature_sensor.TemperatureSensor( self._dht11) self._humidity_sensor = humidity_sensor.HumiditySensor(self._dht11) diff --git a/greenpithumb/wiring_config.ini.example b/greenpithumb/wiring_config.ini.example index b7d9de9..75ef2b8 100644 --- a/greenpithumb/wiring_config.ini.example +++ b/greenpithumb/wiring_config.ini.example @@ -13,5 +13,5 @@ mcp3008_cs_shdn: 25 # adc_channels section maps which GreenPiThumb component is connected to which # channel of the MCP3008 ADC. [adc_channels] -soil_moisture_sensor: 0 -light_sensor: 6 +soil_moisture_sensor: 7 +light_sensor: 0