Skip to content

Commit bc43bcb

Browse files
gguussJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Subscribes to MQTT topic for configuration updates (#1231)
* Subscribes to MQTT topic for configuration updates * Print is a function.
1 parent c35f6f9 commit bc43bcb

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

iot/api-client/mqtt_example/cloudiot_mqtt_example.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ def on_publish(unused_client, unused_userdata, unused_mid):
8787
print('on_publish')
8888

8989

90+
def on_message(unused_client, unused_userdata, message):
91+
"""Callback when the device receives a message on a subscription."""
92+
payload = str(message.payload)
93+
print('Received message \'{}\' on topic \'{}\' with Qos {}'.format(
94+
payload, message.topic, str(message.qos)))
95+
96+
9097
def get_client(
9198
project_id, cloud_region, registry_id, device_id, private_key_file,
9299
algorithm, ca_certs, mqtt_bridge_hostname, mqtt_bridge_port):
@@ -116,10 +123,17 @@ def get_client(
116123
client.on_connect = on_connect
117124
client.on_publish = on_publish
118125
client.on_disconnect = on_disconnect
126+
client.on_message = on_message
119127

120128
# Connect to the Google MQTT bridge.
121129
client.connect(mqtt_bridge_hostname, mqtt_bridge_port)
122130

131+
# This is the topic that the device will receive configuration updates on.
132+
mqtt_config_topic = '/devices/{}/config'.format(device_id)
133+
134+
# Subscribe to the config topic.
135+
client.subscribe(mqtt_config_topic, qos=1)
136+
123137
# Start the network loop.
124138
client.loop_start()
125139

0 commit comments

Comments
 (0)