Skip to content

Commit 3241747

Browse files
brentrubrentru
authored andcommitted
redo handle pump, properly parse json
1 parent 41d37ba commit 3241747

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

PyPortal_GCP_IOT_Planter/code.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@
6161

6262
# Define callback methods which are called when events occur
6363
# pylint: disable=unused-argument, redefined-outer-name
64-
65-
6664
def connect(client, userdata, flags, rc):
6765
# This function will be called when the client is connected
6866
# successfully to the broker.
@@ -112,21 +110,24 @@ def handle_pump(command):
112110
Expected command format: {"pump": 1, "pump_time":3}
113111
:param json command: Message from device/commands#
114112
"""
115-
print("Pump command received: {} for {} seconds".format(
116-
command['pump'], command['pump_time']))
117113
pump_time = command['pump_time']
118114
pump_status = command['pump']
119-
if not pump_status:
115+
if pump_status == 0:
116+
print("Turning pump off")
120117
water_pump.value = False
121118
else:
122-
initial = time.monotonic()
119+
print("Turning pump on for {} seconds...", pump_time)
120+
start_pump = time.monotonic()
123121
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")
126126
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")
130131

131132
# Initialize Google Cloud IoT Core interface
132133
google_iot = Cloud_Core(esp, secrets)

0 commit comments

Comments
 (0)