diff --git a/lora_nodes/master/main.py b/lora_nodes/master/main.py index 18b9fe8..a968035 100644 --- a/lora_nodes/master/main.py +++ b/lora_nodes/master/main.py @@ -76,14 +76,14 @@ class Master: logging.info(f'Got temp from slave: {temp}ÂșC') if temp: return float("{:.1f}".format(float(temp.decode()))) - return False + return 0 def get_hmdt(self): hmdt = self.com.send_and_wait('iot_g_hmdt|') logging.info(f'Got hmdt from slave: {hmdt}') if hmdt: return int(hmdt.decode()) - return False + return 0 def run_pump(self, volume): status = self.com.send_and_wait(f'iot_pmp_ctrl|{str(volume)}') @@ -102,7 +102,7 @@ if __name__ == "__main__": @node.route("/") def root(): - return "IoT-ICL DE Weateher Master Node running..." + return "IoT-ICL DE Weather Master Node running..." @node.route("/hmdt") def humidity(): diff --git a/lora_nodes/slave/main.py b/lora_nodes/slave/main.py index 754a4ce..6a4001d 100644 --- a/lora_nodes/slave/main.py +++ b/lora_nodes/slave/main.py @@ -45,8 +45,35 @@ class LoRa: class WateringPump: + __PINS = {'pwm': board.D20} + __FLOW_RATE = 130 + def __init__(self): - pass + self.ctrl_pin = self.__PINS['pwm'] + self.pump = digitalio.DigitalInOut(self.ctrl_pin) + self.pump.direction = digitalio.Direction.OUTPUT + self.pump.value = False + logging.debug(f'Init\'d pump at pin {self.ctrl_pin}, Power state is {self.pump.value}') + + def dispense(self, volume: int): + ''' + Pump flow rate is 130 ml/min at 5V + need to convert vol in ml to time duration in s + target_vol*60/130 = pump dispensing duration in seconds + ''' + flow_rate = self.__FLOW_RATE + run_duration_seconds = volume*60/flow_rate + self.run_for(run_duration_seconds) + + def __run_for(self, duration: float): + start_time = time.time() + end_time = start_time + duration + self.pump.value = True + logging.info('Starting pump') + while time.time() < end_time: + pass + self.pump.value = False + logging.info('Stopping pump') class SoliSensor: @@ -61,17 +88,23 @@ class SoliSensor: logging.debug(f'Init\'d soil sensor, humidity: {self.sensor.moisture_read()}, temp: {self.sensor.get_temp()}') def get_temp(self): - temp = self.sensor.get_temp() - logging.debug(f'Temp: {temp}') - return temp + try: + temp = self.sensor.get_temp() + logging.debug(f'Temp: {temp}') + return temp + except Exception: + return -50 def get_hmdt(self): - hmdt = self.sensor.moisture_read() - logging.debug(f'Humidity: {hmdt}') - return hmdt + try: + hmdt = self.sensor.moisture_read() + logging.debug(f'Humidity: {hmdt}') + return hmdt + except Exception: + return -50 -class Slave: +class SensorNode: def __init__(self): self.com = LoRa() @@ -110,13 +143,13 @@ class Slave: def pump_control(self, volume): water_qty = int(volume) - print(f'Running pump for {water_qty} ml.') + logging.info(f'Running pump for {water_qty} ml.') time.sleep(0.5) self.com.send_message('OK') - time.sleep(water_qty) + self.pump.dispense(volume) if __name__ == "__main__": logging.root.setLevel(logging.DEBUG) - slave = Slave() - slave.wait_for_instructions() + sensor_node = SensorNode() + sensor_node.wait_for_instructions()