Get thing name associated with MQTT subscribed message #345
-
I am creating a Python script that will listen for shadow change events for multiple IoT things, decide if the changes are actionable and if so modify the thing shadow.
The issue I am running into is when my subscription callback is called I do not have any indication which thing triggered the shadow event. I am testing using two IoT things in the backend (e.g. Here is a short snippet of how I am subscribing:
I am getting the following payload in my
Notice there is no reference to the thing name that triggered the update message. I dug into the SDK a bit and didn't see any place where the thing name is associated with a given callback. I believe the thing name can be inferred based on the topic that the message was received on but it looks like the topic is not passed to the final callback for a subscription. Is there a different way to subscribe so that I can associate a thing name and callback? I assume I am taking the wrong approach here but am not sure how to fix it. Thanks for any thoughts/feedback. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
You could always use the something similar to the callback from the pubsub sample and use a wildcard to get all shadow topics. You can look here to find info on the reserved topics. shadow_topic = "$aws/things/#/shadow"
print("Subscribing to topic '{}'...".format(shadow_topic))
subscribe_future, packet_id = mqtt_connection.subscribe(
topic=shadow_topic,
qos=mqtt.QoS.AT_LEAST_ONCE,
callback=on_message_received) This would return the topics that you are looking for. |
Beta Was this translation helpful? Give feedback.
-
You could make your message handlers lambdas that capture the associated thing name internally. |
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
You could make your message handlers lambdas that capture the associated thing name internally.