Home > database >  Getting device twin in C Sdk - Azure IoT Hub
Getting device twin in C Sdk - Azure IoT Hub

Time:05-23

Is there a way to get the device twin of a device from Azure IoT-Hub, using Azure SDK for C? As far as I know, I am able to get the device twin using the Azure SDK for NodeJS.

In nodejs we do it like.

const Client = require('azure-iot-device').Client;
cosnt Protocol = require('azure-iot-device-mqtt').Mqtt;
var client = Client.fromConnectionString(connectionString, Protocol);
function main() {
    client.open(function (err) {
    //If connection is success
    client.getTwin();
    }
});

Is there any way to get the twin data for a device and get twinchange notification in Azure SDK for C i.e A callback function when there is a change in twin data?

CodePudding user response:

Is there any way to get the twin data for a device and get twinchange notification in Azure SDK for C i.e A callback function when there is a change in twin data?

Yes, you can refer to example of callback function as per Get updates on the device side:

static void deviceTwinCallback(DEVICE_TWIN_UPDATE_STATE update_state, const unsigned char* payLoad, size_t size, void* userContextCallback)
{
    (void)userContextCallback;

    printf("Device Twin update received (state=%s, size=%zu): %s\r\n", 
        MU_ENUM_TO_STRING(DEVICE_TWIN_UPDATE_STATE, update_state), size, payLoad);
}

You can also refer to iothub_devicetwin_sample.c and iothub_device_client.c

  • Related