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:
- desired properties. a json structure picked up by the device when is connects or is connected. A device can respond met reported properties.
- 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
- 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.