This commit is contained in:
Max 2021-01-03 14:21:02 +00:00
parent 3f775aeb91
commit bb36389d2a
2 changed files with 48 additions and 15 deletions

View File

@ -76,14 +76,14 @@ class Master:
logging.info(f'Got temp from slave: {temp}ºC') logging.info(f'Got temp from slave: {temp}ºC')
if temp: if temp:
return float("{:.1f}".format(float(temp.decode()))) return float("{:.1f}".format(float(temp.decode())))
return False return 0
def get_hmdt(self): def get_hmdt(self):
hmdt = self.com.send_and_wait('iot_g_hmdt|') hmdt = self.com.send_and_wait('iot_g_hmdt|')
logging.info(f'Got hmdt from slave: {hmdt}') logging.info(f'Got hmdt from slave: {hmdt}')
if hmdt: if hmdt:
return int(hmdt.decode()) return int(hmdt.decode())
return False return 0
def run_pump(self, volume): def run_pump(self, volume):
status = self.com.send_and_wait(f'iot_pmp_ctrl|{str(volume)}') status = self.com.send_and_wait(f'iot_pmp_ctrl|{str(volume)}')
@ -102,7 +102,7 @@ if __name__ == "__main__":
@node.route("/") @node.route("/")
def root(): def root():
return "IoT-ICL DE Weateher Master Node running..." return "IoT-ICL DE Weather Master Node running..."
@node.route("/hmdt") @node.route("/hmdt")
def humidity(): def humidity():

View File

@ -45,8 +45,35 @@ class LoRa:
class WateringPump: class WateringPump:
__PINS = {'pwm': board.D20}
__FLOW_RATE = 130
def __init__(self): 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: 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()}') logging.debug(f'Init\'d soil sensor, humidity: {self.sensor.moisture_read()}, temp: {self.sensor.get_temp()}')
def get_temp(self): def get_temp(self):
temp = self.sensor.get_temp() try:
logging.debug(f'Temp: {temp}') temp = self.sensor.get_temp()
return temp logging.debug(f'Temp: {temp}')
return temp
except Exception:
return -50
def get_hmdt(self): def get_hmdt(self):
hmdt = self.sensor.moisture_read() try:
logging.debug(f'Humidity: {hmdt}') hmdt = self.sensor.moisture_read()
return hmdt logging.debug(f'Humidity: {hmdt}')
return hmdt
except Exception:
return -50
class Slave: class SensorNode:
def __init__(self): def __init__(self):
self.com = LoRa() self.com = LoRa()
@ -110,13 +143,13 @@ class Slave:
def pump_control(self, volume): def pump_control(self, volume):
water_qty = int(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) time.sleep(0.5)
self.com.send_message('OK') self.com.send_message('OK')
time.sleep(water_qty) self.pump.dispense(volume)
if __name__ == "__main__": if __name__ == "__main__":
logging.root.setLevel(logging.DEBUG) logging.root.setLevel(logging.DEBUG)
slave = Slave() sensor_node = SensorNode()
slave.wait_for_instructions() sensor_node.wait_for_instructions()