|
61 | 61 |
|
62 | 62 | # Define callback methods which are called when events occur |
63 | 63 | # pylint: disable=unused-argument, redefined-outer-name |
64 | | - |
65 | | - |
66 | 64 | def connect(client, userdata, flags, rc): |
67 | 65 | # This function will be called when the client is connected |
68 | 66 | # successfully to the broker. |
@@ -112,21 +110,24 @@ def handle_pump(command): |
112 | 110 | Expected command format: {"pump": 1, "pump_time":3} |
113 | 111 | :param json command: Message from device/commands# |
114 | 112 | """ |
115 | | - print("Pump command received: {} for {} seconds".format( |
116 | | - command['pump'], command['pump_time'])) |
117 | 113 | pump_time = command['pump_time'] |
118 | 114 | pump_status = command['pump'] |
119 | | - if not pump_status: |
| 115 | + if pump_status == 0: |
| 116 | + print("Turning pump off") |
120 | 117 | water_pump.value = False |
121 | 118 | else: |
122 | | - initial = time.monotonic() |
| 119 | + print("Turning pump on for {} seconds...", pump_time) |
| 120 | + start_pump = time.monotonic() |
123 | 121 | while True: |
124 | | - if now - initial > pump_time: |
125 | | - print("Turning pump off...") |
| 122 | + now = time.monotonic() |
| 123 | + if now - start_pump > pump_time: |
| 124 | + # Turn the pump off |
| 125 | + print("Turning pump off") |
126 | 126 | water_pump.value = False |
127 | | - return |
128 | | - water_pump.value = True |
129 | | - print("Done watering!") |
| 127 | + break |
| 128 | + else: |
| 129 | + initial = now |
| 130 | + print("pump off") |
130 | 131 |
|
131 | 132 | # Initialize Google Cloud IoT Core interface |
132 | 133 | google_iot = Cloud_Core(esp, secrets) |
|
0 commit comments