@@ -96,24 +96,24 @@ def publish(client, userdata, topic, pid):
9696
9797def message (client , topic , msg ):
9898 # This method is called when the client receives data from a topic.
99+ # Check if command is about the water pump
99100 try :
100- # Check if command is about the water pump
101101 msg_dict = json .loads (msg )
102- if msg_dict ['pump_time' ]:
103- handle_pump (msg_dict )
104- except KeyError :
102+ handle_pump (msg_dict )
103+ except :
105104 print ("Message from {}: {}" .format (topic , msg ))
106105
106+
107107def handle_pump (command ):
108108 """Handles command about the planter's
109109 watering pump from Google Core IoT.
110110 :param json command: Message from device/commands#
111111 """
112112 # Parse the pump command message
113- # Expected format: {"pump ": 1 , "pump_time":3}
113+ # Expected format: {"power ": true , "pump_time":3}
114114 pump_time = command ['pump_time' ]
115- pump_status = command ['pump ' ]
116- if pump_status == 1 :
115+ pump_status = command ['power ' ]
116+ if pump_status :
117117 print ("Turning pump on for {} seconds..." .format (pump_time ))
118118 start_pump = time .monotonic ()
119119 while True :
@@ -122,6 +122,7 @@ def handle_pump(command):
122122 # Timer expired, leave the loop
123123 print ("Plant watered!" )
124124 break
125+ water_pump .value = True
125126 print ("Turning pump off" )
126127 water_pump .value = False
127128
0 commit comments