Azure / azure-sdk-for-c

This repository is for active development of the Azure SDK for Embedded C. For consumers of the SDK we recommend visiting our versioned developer docs at https://azure.github.io/azure-sdk-for-c.
MIT License
226 stars 120 forks source link

How to get Azure iot central twin properties data at startup of ESP32 #2363

Closed electro89 closed 1 year ago

electro89 commented 2 years ago

I want to get data from twin at startup of esp32 device from azure IoT central to initialize data . But could not find any example related to this. In the example esp32 chip the variable telemetryFrequencySecs does not get updated from azure IoT central twin property at startup of the device. Is there function to retrieve data from twin property when the user manually wants it ?

danewalton commented 2 years ago

Hi @electro89 This section here in our twin sample should be useful for that scenario:

https://github.com/Azure/azure-sdk-for-c/blob/b9ebeb34fb8395e4e19be21a5428eb5004c9ecff/sdk/samples/iot/paho_iot_hub_twin_sample.c#L252-L282

electro89 commented 2 years ago

I tried to get twin data but I got an error

2022/10/24 07:33:36 [INFO] MQTT client connecting.
2022/10/24 07:33:42 [INFO] MQTT client connected (session_present=0).
2022/10/24 07:33:42 [INFO] MQTT client subscribing to '$iothub/methods/POST/#'
2022/10/24 07:33:44 [INFO] MQTT topic subscribed (message id=59839).
2022/10/24 07:33:44 [INFO] MQTT client subscribing to '$iothub/twin/res/#'
2022/10/24 07:33:44 [INFO] MQTT topic subscribed (message id=915).
2022/10/24 07:33:44 [INFO] MQTT client subscribing to '$iothub/twin/PATCH/properties/desired/#'
2022/10/24 07:33:45 [INFO] MQTT topic subscribed (message id=22658).
2022/10/24 07:33:45 [INFO] MQTT client publishing to '$iothub/twin/PATCH/properties/reported/?$rid=0'
2022/10/24 07:33:45 [INFO] Client requesting device twin document from service.
2022/10/24 07:33:45 [INFO] MQTT client publishing to '$iothub/twin/GET/?$rid=1'
2022/10/24 07:33:45 [INFO] MQTT message received.
2022/10/24 07:33:45 [INFO] Properties update request completed (id=0, status=204)
2022/10/24 07:33:45 [INFO] MQTT message received.
2022/10/24 07:33:45 [ERROR] azure_iot_mqtt_client_message_received failed (topic=$iothub/twin/res/200/?$rid=1).
danewalton commented 2 years ago

Hmm that topic that's received says 200 which is a success. Is the error from IoT api's or device/mqtt code?

RLeclair commented 1 year ago

Hi @electro89, wanted to check in on the above question.

electro89 commented 1 year ago

@danewalton Even after using get_device_twin_document function it returned error, finally found out where the problem is, at https://github.com/Azure/azure-sdk-for-c-arduino/blob/main/examples/Azure_IoT_Central_ESP32_AzureIoTKit/AzureIoT.cpp#L773 It returns RESULT_ERROR

RLeclair commented 1 year ago

Hi @electro89, the sample doesn't leverage that functionality at the moment, but you can edit the portion you've mentioned. You can get some ideas from one of our other samples which has it implemented here.

PDE-Paul commented 1 year ago

Hi,

Would it be possible to add a Device Twin example to the ESP32 sdk?

Regards Paul Brink

RLeclair commented 1 year ago

Hi @PDE-Paul at the moment we are not adding a sample for Device Twin on ESP32 but you can take a look at our existing twin sample.

ericwolz commented 1 year ago

closing issue