@@ -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+
9097def 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