Azure IoT Hub - Send message (C2D) to one device on 2 topics instead of devicebound/#

醉酒当歌 提交于 2019-12-11 17:59:46

问题


As a MQTT client (device), I am using devices/{deviceid}/messages/devicebound/# topic to receive Cloud to Device messages. This device is a gateway for me and there are 3 devices attached to it. I want to send 3 different commands to 1 gateway on 3 different topics, like

devices/{deviceid}/messages/devicebound/device1
devices/{deviceid}/messages/devicebound/device2
devices/{deviceid}/messages/devicebound/device3

My gateway is registered in iot-hub and I use it at {deviceid}.

Is it possible?


回答1:


Based on the official document "Communicate with your IoT hub using the MQTT protocol", it says:

Since IoT Hub is not a general purpose pub-sub messaging broker, it only supports the documented topic names and topic filters.

IoT Hub delivers messages with the Topic Name devices/{device_id}/messages/devicebound/, or devices/{device_id}/messages/devicebound/{property_bag} if there are any message properties.

Here is a solution using system property "messageId" as the filter. So your three topics:

devices/{deviceid}/messages/devicebound/device1 
devices/{deviceid}/messages/devicebound/device2 
devices/{deviceid}/messages/devicebound/device3

would look like this:

devices/{deviceid}/messages/devicebound/%24.mid=device1&%24.to=%2Fdevices%2Ftest1%2Fmessages%2FdeviceBound&iothub-ack=full
devices/{deviceid}/messages/devicebound/%24.mid=device2&%24.to=%2Fdevices%2Ftest1%2Fmessages%2FdeviceBound&iothub-ack=full
devices/{deviceid}/messages/devicebound/%24.mid=device3&%24.to=%2Fdevices%2Ftest1%2Fmessages%2FdeviceBound&iothub-ack=full

This is the test:

Send C2D message from azure device explorer:

Receive C2D message from MQTTBox:



来源:https://stackoverflow.com/questions/48775383/azure-iot-hub-send-message-c2d-to-one-device-on-2-topics-instead-of-devicebo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!