def __init__(self): IotDevice.__init__(self) IotDevice.set_light(self, 2.4) IotDevice.set_humidity(self, 30.0) IotDevice.set_moisture(self, 4.0) IotDevice.set_temperature(self, 71.1) # spawn a task to update our data __update_task = asyncio.create_task(self.update_loop())
async def turn_valve_on_handler(iot_device: IotDevice, values): # TODO: Cancel any previous timer. if values and type(values) == int: print("Turning device on for {count} secs".format(count=values)) # TODO: Set a timer to turn off device. else: print("Turning device on.") iot_device.turn_valve_on()
async def turn_valve_on_handler(iot_device: IotDevice, values): iot_device.cancel_timer() if values and type(values) == int: duration = values print("Turning device on for {0} secs".format(duration)) _auto_shutoff_timer(iot_device, duration) else: print("Turning device on.") iot_device.turn_valve_on()
def printTelemetry(device: IotDevice): print(device.get_telemetry()) humidity, tempF = device.humidity_and_temperature print("Temperature is {0:.2f}F".format(tempF)) print("Humidity is {0:.2f} out of 100".format(humidity)) print("Light is {0:.2f} out of 10".format(device.light)) print("Moisture is {0:.2f} out of 10".format(device.moisture)) print("Flow is {0:.2f} Liters/minute".format(device.flow)) print("Valve is {0}".format(device.valve))
def __init__(self, gpio_relay, gpio_flow, ip_address=None, humid_temp="DHT22", moisture="I2C"): IotDevice.__init__(self) if gpio_relay == "SIM": self.gpio_relay = None else: if ip_address is not None: self.gpio_relay = LED(gpio_relay, PiGPIOFactory(host=ip_address)) else: self.gpio_relay = LED(gpio_relay) # For now we want to leave the DHT sensor (measures temperature and humidity) # connected to pin 18. if humid_temp == "BME280": i2c = board.I2C() self.ht_sensor = Bme280(i2c) self.ht_sensor.set_sea_level_pressure(1022.2) elif humid_temp == "DHT11": self.ht_sensor = adafruit_dht.DHT11(board.D18) elif humid_temp == "DHT22": self.ht_sensor = adafruit_dht.DHT22(board.D18) else: self.ht_sensor = None # For now we want to leave SCL to pin 3 and SDA to pin 2 for i2c interface. # meaning moisture sensor will need to be connected to these pins if moisture == "SIM": self.moisture_sensor = None else: self.moisture_sensor = Seesaw(busio.I2C(board.D3, board.D2), addr=0x36) if gpio_flow == "SIM": self.gpio_flow = None else: self.gpio_flow = FrequencySignal(gpio_flow)
async def testDevice(device: IotDevice): print("\n\nTesting {0} device.".format(type(device))) printTelemetry(device) print("\nTurning on valve, then waiting 5 seconds...") device.turn_valve_on() await asyncio.sleep(5) printTelemetry(device) if device.get_flow() == 0: print("\033[93mWARNING: *** NO FLOW DETECTED ***\033[0m") print("\nTurning off valve, then waiting 5 seconds...") device.turn_valve_off() await asyncio.sleep(5) printTelemetry(device) if device.get_flow() != 0: print("\033[93mWARNING: *** FLOW DETECTED ***\033[0m")
async def main(): device = IotDevice() #await testDevice(device) device = SimulatedDevice() await testDevice(device)
def _auto_shutoff_handlers(iot_device: IotDevice): iot_device.cancel_timer() print("Auto shutoff, turning device off..") iot_device.turn_valve_off()
def _auto_shutoff_timer(iot_device: IotDevice, duration): timer = Timer(duration, _auto_shutoff_handlers, [iot_device]) iot_device.set_timer(timer) timer.start()
async def turn_valve_off_handler(iot_device: IotDevice, values): iot_device.cancel_timer() print("Turning device off.") iot_device.turn_valve_off()
def update_data(self): IotDevice.set_light(self, random.random() * 10.0) IotDevice.set_moisture(self, random.random() * 10.0) IotDevice.set_humidity(self, random.random() * 100.0) IotDevice.set_temperature(self, 40.0 + random.random() * 40.0)
def turn_valve_off(self): IotDevice.set_flow(self, 0.0) IotDevice.turn_valve_off(self)
def turn_valve_on(self): IotDevice.set_flow(self, 6.2) IotDevice.turn_valve_on(self)
async def turn_valve_off_handler(iot_device: IotDevice, values): # TODO: Cancel any timers from turn_valve_on_handler print("Turning device off.") iot_device.turn_valve_off()