How to update device twin by mqtt client

Chen, Bo 21 Reputation points
2022-10-25T01:56:53.38+00:00

I am going to communicate with IOT hub device twin with mqtt.fx and wondering:
-what's the topics that I can publish to or subscribe from device twin?
-what's the payload like?
Thanks for your kind support.

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,203 questions
0 comments No comments
{count} votes

Accepted answer
  1. AshokPeddakotla-MSFT 34,861 Reputation points
    2022-10-25T03:09:36.87+00:00

    @Chen, Bo Welcome to Microsoft Q&A forum!

    -what's the topics that I can publish to or subscribe from device twin? and what's the payload like?

    When you connect with MQTT, You will be able to send telemetry to the devices/{deviceId}/messages/events/ topic and receive cloud-to-device messages by subscribing to the devices/{deviceId}/messages/devicebound/# topic.

    With MQTT, you can also update a reported property in the Device Twin. You can do that as follows:

    The following sequence describes how a device updates the reported properties in the device twin in IoT Hub:

    1. A device must first subscribe to the $iothub/twin/res/# topic to receive the operation's responses from IoT Hub.
    2. A device sends a message that contains the device twin update to the $iothub/twin/PATCH/properties/reported/?$rid={request-id} topic. This message includes a request ID value.
    3. The service then sends a response message that contains the new ETag value for the reported properties collection on topic $iothub/twin/res/{status}/?$rid={request-id}. This response message uses the same request ID as the request.

    The request message body contains a JSON document that contains new values for reported properties. Each member in the JSON document updates or add the corresponding member in the device twin's document. A member set to null deletes the member from the containing object. For example:

    {  
        "telemetrySendFrequency": "35m",  
        "batteryLevel": 60  
    }  
    

    You will get a 204 status code if Success (no content is returned)

    Please refer the documentation Using the MQTT protocol directly (as a module) for more details.

    253649-image.png

    Do let us know if that helps or have any further queries.

    If the response is helpful, please click "Accept Answer" and upvote it.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.