C2D communications

Alex Fang 230 Reputation points
2023-08-04T14:58:54.4066667+00:00

I am reading this article: https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-c2d-guidance

  1. Am I correct to understand that if I wanted to do something like arming/disarming a device or turning a device on/off, I would use a direct method?
  2. In the case that I wish to set a device operating temperature threshold so that it doesn't operate when it's above 70 degrees, I would use the Twin's desired properties?
  3. I am somewhat confused by what I can do with cloud-to-device messages. The article says it's one-way notifications to the device app, but what are some examples of this would be used?
  4. Whatever I choose to use, am I correct to understand that the device app would have to be configured to implement the direct methods that I create on the backend app?
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,258 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sander van de Velde | MVP 35,971 Reputation points MVP
    2023-08-04T17:34:14.93+00:00

    Hello @Alex Fang,

    Let's examine each statement.

    One: yes, you can control the device with one or more direct methods. Each method has a name and a body. In the device, you listen for incoming messages (part of the logic) and after handling the method, a response can be given. Direct methods can time out. The device needs to be connected while a Direct method is called. Synchronous usage.

    Two, yes, desired properties are typically used to set configuration settings for a device. When set while a device is connected, it will get an event. After reading the (new) desired properties, it can report back how it reacted to the desired properties (an 'echo' of the same properties is a common solution). If the device wakes up later than the moment the settings were set, it can read out the latest desired properties. This way you can compare large groups of devices and check which devices have picked up the desired properties and which have not. Asynchronous usage.

    Three: yes, Cloud messages are the odd one. These are a bit like direct methods (without a response) but these are queued. A device can read cloud messages (it actually get all message still in the queue). The device has to confirm the retrieval of each message, otherwise, it stays in the queue. Asynchronous usage.

    Four: You are probably programming your device with custom code like Python or C#. So yes, next to sending messages to the cloud, you also need to program how to handle incoming direct methods, desired properties, or cloud methods. The SDKs available in multiple programming languages take a lot of the heavy lifting away.

    Check out the free learning modules for more details.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 17,521 Reputation points
    2023-08-04T17:36:29.7433333+00:00

    Hi @Alex Fang Greetings! Thank you for posting this question here.

    Your understanding on Direct Methods and Twins is correct.

    Direct Methods are typically used for communications that require immediate confirmation of the result and you would also get a result notification from the device to the cloud informing the method execution result.

    Twin desired properties are one-way communications from the cloud to the device, where the cloud sends a desired property to the device and the device updates its reported property to match the desired property.

    Cloud-two-device messages

    These are also one way communication from cloud to device application. But the difference between Twin desired properties and cloud to device messages is that we are sending the messages from cloud to a device application such as mobile app in this scenario. Consider an example where you have a smart camera monitoring you have and sending data to Azure IoT Hub. If there is an intruder, you would use cloud-to-device message to send a notification to a mobile application from the cloud.

    Kindly note that the direct method can accept a maximum payload of size 128kb for the request as well as response. The allowed size of Twin desired properties is capped at 32kb. Cloud to device messages allow a maximum message size of 64kb. Please factor in the protocol limitations of each of these approaches and the frequency limits at which each of these approaches are capped and choose the appropriate approach. Please refer the Details comparison table of the article Cloud-to-device communications guidance for additional information on this.

    Whatever I choose to use, am I correct to understand that the device app would have to be configured to implement the direct methods that I create on the backend app

    Yes, you are correct. Assuming that you would want to create a Web/mobile Application, you would have to create a custom end point that can be called from your application which in turn triggers the direct method. Let me know if you need any assistance implementing this. We would be glad to help you.

    Hope this answers your questions. Please let us know if you need any additional clarification.


    If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.

    0 comments No comments

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.