Azure IoT Hub Cloud-2-Device

SoonJoo@Genting 241 Reputation points

Hi, understand we could send cloud to device message as I require to periodically send message to my devices, wonder if I could do this using azure functions or any other method available? I do not see any schedule message sending in the portal.

Thank you!

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

1 answer

Sort by: Most helpful
  1. Sander van de Velde 29,771 Reputation points MVP

    Hello @SoonJoo@Genting ,

    welcome to this moderated Azure community forum.

    The Azure IoT Hub has a device registration for each registered device.

    With that registration, the device can both send data to the cloud and receive 'commands' from the cloud.

    The IoT Hub offers:

    1. desired properties. a json structure picked up by the device when is connects or is connected. A device can respond met reported properties.
    2. direct Methods. a function call to a device while it is connected. Does not work when the device is connected. The device can respond directly
    3. Cloud message. A queue in the cloud with commands waiting to be picked up when the device connects. Use reported properties to respond.

    All three solutions need custom logic on the device to respond in the correct way.

    All three solutions need custom code in the cloud. This can be eg. logic running in an Azure Function, Logic Apps, a website, etc.

    This can be executed be using the service SDKs.

    I recommend using the desired properties because this is a async pattern. The device does not need to be connected while setting the properties.

    So, if you want to execute logic every X minutes or hours, you need to write some logic using this pattern:

    Azure Function (timer triggered) using Service SDK -> IoT Hub Device twin desired properties of a certain device -> device.

    For a code example, follow this link.

    When using C#, use the Microsoft.Azure.Devices Nuget package.

    Here are some code examples:

    // update direct method
    using var serviceClient = ServiceClient.CreateFromConnectionString("");
    await serviceClient.InvokeDeviceMethodAsync("deviceX", new CloudToDeviceMethod("doSomething"));
    // update desired properties
    RegistryManager registryManager = RegistryManager.CreateFromConnectionString("");
    var twin = await registryManager.GetTwinAsync("deviceX");
    twin.Properties.Desired["someKey"] = "someValue";
    await registryManager.UpdateTwinAsync(twin.DeviceId, twin, twin.ETag);

    Now you change one device.

    If you want to work with multiple devices at once (eg. because you have tagged devices as a group), you can work with a IoT Hub Job to update (desired properties for) multiple devices.

    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