I am using the azure iot sdk for C in Nucleus RTOS, but the question i have is related to the sdk directly
Can you please tell how can i retrieve the device twin or reported properties using the azure iot sdk for C ?
I see an API IoTHubDeviceTwin_GetTwin() but this is for the back end app not the device app.
Also in the .Net sample there is code which retrieves the device twin . The code is as follows
Console.WriteLine("Connecting to hub");
Client = DeviceClient.CreateFromConnectionString(DeviceConnectionString, TransportType.Mqtt);
Console.WriteLine("Retrieving twin");
await Client.GetTwinAsync(); }
I am probably looking for similar code in the azure iot sdk for c.
Thanks,
Arsalan
Hi,
You should lokk at this sample there is a call back able to retrieve tin upon modification, this is triggered when the device connect to the hub.
IoTHubClient_SetDeviceTwinCallback is a not exactly a get twin but it might be what you are searching for.
Thanks for the reply @lmussier .
If i use the API 'IoTHubClient_SetDeviceTwinCallback()' then the desired properties callback given in the 'WITH_DESIRED_PROPERTY()' macro of the serializer never get called.
The only callback that gets called is the one given in the 'IoTHubClient_SetDeviceTwinCallback' API. The callbacks given in the serializer macros never get called.
Hi @ArsalanSaleemGhuman,
The C SDK does not have GetTwin API implemented, because it is not necessary. The device should know it's state and properties at any moment. The callback what we set with IoTHubClient_SetDeviceTwinCallback is to confirm if the service successfully saved the reported properties for the device after the device sent them.
Best Regards,
Zoltan
Hi @zolvarga,
I was considering the device twin to be a sort of persistent registry of device state. For example, if a device has an LED which can be turned on/off using a device twin property. If a user has turned the LED on, this information will be conveyed to the device and will also be persistently stored in the device twin in the cloud. Now if the device reboots, it should be able to get the last state of the LED as set by the user in the cloud. So in this case a device should be able to read its reported properties on reboot. Does this use-case make sense?
A less elegant solution would be to use desired properties for this purpose instead of reported properties. All desired properties are reported to the device again, on reboot. So another scheme which may work is to never clear the desired properties, so that all customizations to properties made by the user from the cloud get reflected in the desired properties.
Thanks for the reply @zolvarga .
Regarding the GetTwin API. The documentation related to the device twin mentions that the device can read the device twin. The device twin documentation I am referring is "https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-device-twins". The document says:
Device operations
The device app operates on the device twin using the following atomic operations:
Retrieve device twin. This operation returns the device twin document (including tags and desired, reported and system properties) for the currently connected device.
Hi @zolvarga,
We have the exact same intent here. We have no persistent state across reboot, the reported state is the current state of the device (or the last known state of the device from a service point of view). And we hope to see devices get a consistent state after a reboot.
As @fakhir says, the perfect way would be to be able to get the reported properties from the hub.
At least it is what we was expecting after reading https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-device-twins as @ArsalanSaleemGhuman point out
Hello @lmussier , @ArsalanSaleemGhuman ,
just sharing an update with you, we are working to have a "GetTwin" function added to the Azure IoT C Client SDK.
We will let you know as soon as it is available.
That will be great. Thanks :)
Just updating, we are still working on the feature.
It got slightly delayed due to some high-priority fixes but we should be back on track soon.
We understand the request and leaving this open for tracking purposes, but this unfortunately isn't in our short/mid range planning. Commenting as we want to make sure we set expectations here.
Hi, any updates regarding this issue? I saw that @zolvarga was assigned to this few days ago, does that mean that you are currently working on it?
We do still need (with @lmussier) a "GetTwin" function to solve our problem regarding non persistent state across device reboot.
When the C SDK first connects to the service it will receive a twin callback. This first callback contains the complete desired and reported state. After that only updates will be sent to the callback. If your device rebooted with the reported state of an LED set to on then it will receive that at restart and can set the LED to match the reported state.
_Mark Radbourne MSFT_
Hi Mark, Thanks for the details. We are currently using the serializer to leverage the use of device twin. As I understood, we can not use both serializer and the twin callback at the same time (the serializer register it's own callback to receive twin). The idea was to have a solution to get those reported state received at the connection, without having to move from the serializer to a complete manual management of the twin json document.
This feature has now been implemented and merged (https://github.com/Azure/azure-iot-sdk-c/pull/771).
Please refer to this sample for functionality:
https://github.com/Azure/azure-iot-sdk-c/blob/master/iothub_client/samples/iothub_client_device_twin_and_methods_sample/iothub_client_device_twin_and_methods_sample.c#L348
(void)IoTHubDeviceClient_GetTwinAsync(iotHubClientHandle, getCompleteDeviceTwinOnDemandCallback, NULL);
Please pass the appropriate context as needed, and check for the return of the function (omitted in samples for simplifying).
@lmussier, @ArsalanSaleemGhuman, @fakhir, @ewertons, @jspaith, @alexmrtn, @massand, thank you for your contribution to our open-sourced project! Please help us improve by filling out this 2-minute customer satisfaction survey
Most helpful comment
This feature has now been implemented and merged (https://github.com/Azure/azure-iot-sdk-c/pull/771).
Please refer to this sample for functionality:
https://github.com/Azure/azure-iot-sdk-c/blob/master/iothub_client/samples/iothub_client_device_twin_and_methods_sample/iothub_client_device_twin_and_methods_sample.c#L348
Please pass the appropriate context as needed, and check for the return of the function (omitted in samples for simplifying).