Skip to content

Commit dd766c8

Browse files
brentrubrentru
authored andcommitted
publish data from loop without qos
1 parent 60346ad commit dd766c8

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

PyPortal_GCP_IOT_Planter/code.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,40 +61,51 @@
6161

6262
# Define callback methods which are called when events occur
6363
# pylint: disable=unused-argument, redefined-outer-name
64+
65+
6466
def connect(client, userdata, flags, rc):
6567
# This function will be called when the client is connected
6668
# successfully to the broker.
6769
print('Connected to Google Cloud IoT!')
68-
print('Flags: {0}\n RC: {1}'.format(flags, rc))
70+
print('Flags: {0}\nRC: {1}'.format(flags, rc))
6971
# Subscribes to commands/# topic
7072
google_mqtt.subscribe_to_all_commands()
7173

74+
7275
def disconnect(client, userdata, rc):
7376
# This method is called when the client disconnects
7477
# from the broker.
7578
print('Disconnected from Google Cloud IoT!')
7679

80+
7781
def subscribe(client, userdata, topic, granted_qos):
7882
# This method is called when the client subscribes to a new topic.
7983
print('Subscribed to {0} with QOS level {1}'.format(topic, granted_qos))
8084

85+
8186
def unsubscribe(client, userdata, topic, pid):
8287
# This method is called when the client unsubscribes from a topic.
8388
print('Unsubscribed from {0} with PID {1}'.format(topic, pid))
8489

90+
8591
def publish(client, userdata, topic, pid):
8692
# This method is called when the client publishes data to a topic.
8793
print('Published to {0} with PID {1}'.format(topic, pid))
8894

95+
8996
def message(client, topic, msg):
9097
# This method is called when the client receives data from a topic.
91-
# Check if command is about the water pump
9298
try:
99+
# Attempt to load a JSON command
93100
msg_dict = json.loads(msg)
94-
handle_pump(msg_dict)
101+
# Handle water-pump commands
102+
if msg_dict['pump_time']:
103+
handle_pump(msg_dict)
95104
except:
105+
# Non-JSON command, print normally
96106
print("Message from {}: {}".format(topic, msg))
97107

108+
98109
def handle_pump(command):
99110
"""Handles command about the planter's
100111
watering pump from Google Core IoT.
@@ -109,15 +120,18 @@ def handle_pump(command):
109120
print("Turning pump on for {} seconds...".format(pump_time))
110121
start_pump = time.monotonic()
111122
while True:
123+
gfx.show_gcp_status('Watering plant...')
112124
cur_time = time.monotonic()
113125
if cur_time - start_pump > pump_time:
114126
# Timer expired, leave the loop
115127
print("Plant watered!")
116128
break
117-
water_pump.value=True
129+
water_pump.value = True
130+
gfx.show_gcp_status('Plant watered!')
118131
print("Turning pump off")
119132
water_pump.value = False
120133

134+
121135
# Initialize Google Cloud IoT Core interface
122136
google_iot = Cloud_Core(esp, secrets)
123137

@@ -163,12 +177,12 @@ def handle_pump(command):
163177
# Display Soil Sensor values on pyportal
164178
temperature = gfx.show_temp(temperature)
165179
gfx.show_water_level(moisture_level)
166-
# TODO: Add water status?
167180
print('Sending data to GCP IoT Core')
168-
gfx.show_gcp_status('Sending data...')
169-
google_mqtt.publish(temperature,'events', qos=1)
170-
google_mqtt.publish(moisture_level,'events', qos=1)
171-
gfx.show_gcp_status('Data sent!')
181+
gfx.show_gcp_status('Publishing data...')
182+
google_mqtt.publish(temperature, "events")
183+
time.sleep(2)
184+
google_mqtt.publish(moisture_level, "events")
185+
gfx.show_gcp_status('Data published!')
172186
print('Data sent!')
173187
# Reset timer
174188
initial = now

0 commit comments

Comments
 (0)