How do I send a message to my devices in Azure IoT hub repeatedly?

PhangGuanRongEE-8742 20 Reputation points
2023-06-11T15:07:18.1933333+00:00

I have a device that only has RS232 data port, that port is connected to my router through serial communication that is able to connect to my Azure IoT hub devices. The thing is, I need to be able to send a 6 byte command to that device so that it will return me a set of data of the operating parameters. I know there is a send "Message to device" or "Cloud-to-device message feature" but it only sends once. How do I repeatedly send that 6 byte command, is there any programming within Azure IoT Hub or another service perhaps?

Example of the command is: "0xFC 0x74 0x01 0x00 0xFE 0x71"

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

2 answers

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 17,766 Reputation points Moderator
    2023-06-13T00:54:19.6933333+00:00

    Hi @PhangGuanRongEE-8742 Have you looked into the Service Client? The class provides an implementation for the method invokeDeviceMethod(string, DeviceMethodParams) which lets you call the direct method on the device through an SDK.

    Here is a sample implementation of the method.

            // Invoke the direct method on the device, passing the payload.
            private static async Task InvokeMethodAsync(string deviceId, ServiceClient serviceClient)
            {
                var methodInvocation = new CloudToDeviceMethod("SetTelemetryInterval")
                {
                    ResponseTimeout = TimeSpan.FromSeconds(30),
                };
                methodInvocation.SetPayloadJson("10");
    
                Console.WriteLine($"Invoking direct method for device: {deviceId}");
    
                // Invoke the direct method asynchronously and get the response from the simulated device.
                CloudToDeviceMethodResult response = await serviceClient.InvokeDeviceMethodAsync(deviceId, methodInvocation);
    
                Console.WriteLine($"Response status: {response.Status}, payload:\n\t{response.GetPayloadAsJson()}");
    
            }
    

    You can modify the line of the code SetPayloadJson and pass each of these options "0xFC 0x74 0x01 0x00 0xFE 0x71" iteratively by calling this function.

    Please refer to the resource Control a device which provides the implementation in C#, Node.js, Python and Java.

    Hope this helps! Please let us know if this works for you and feel free to comment below with any follow up questions you may have.


  2. chbeier 1,871 Reputation points
    2023-06-14T09:23:04.2333333+00:00

    Hello @PhangGuanRongEE-8742

    You have three options to communicate from the cloud with a client device, what are described in the Cloud-to-device communications guidance.

    You can call a Direct method on a device when it is connected and get immediate feedback if the method was called and if the method throws an error (custom payload besides http status code in the response). You might trigger calling the direct method when the device comes online.

    If you want to send commands to offline devices, you can either use the Device Twin's desired properties what will be a standing command until it is changed. A device will always receive only the latest Desired Property when it gets online.

    Finally, you can use Cloud-to-device messages what allow your cloud service to enqueue a command for a specific device with a maximum time-to-live of 2 days. If the device does not get online within this time your cloud service gets the statusCode expired if you request the delivery of per-message feedback. It is up to your cloud service to resend the command and ensure in order delivery.

    When choosing a cloud-to-device communication also consider the Quotas and throttling limits and how they match your scenario's requirements.

    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.