usage of IoTHubDeviceClient_LL_DeviceMethodResponse In C client device IoT Hub SDK

Anonymous
2020-12-17T22:20:46.283+00:00

I am looking for support since there is not much information online, I am trying to program an ESP32 microcontroller using the C SDK port for the Arduino framework, by connecting it to a central IoT App.

I managed to make the device respond to the call of a method through a command sent from central IoT but I have not been able to send a response from the device to the cloud, I would infinitely appreciate if someone could explain to me how "IoTHubDeviceClient_LL_DeviceMethodResponse" works

Thanks for your time and patience!

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,130 questions
Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
208 questions
{count} votes

Accepted answer
  1. António Sérgio Azevedo 7,666 Reputation points Microsoft Employee
    2020-12-18T15:50:57.72+00:00

    Thank you for asking your question here @Anonymous !

    If you research on the azure iot sdk for C you will find some samples and there is one explaining how Direct Methods work:

    https://github.com/Azure/azure-iot-sdk-c/tree/master/iothub_client/samples/iothub_client_device_twin_and_methods_sample

    Here is the piece of code that you should pay attention for your described scenario:

    static int deviceMethodCallback(const char* method_name, const unsigned char* payload, size_t size, unsigned char** response, size_t* response_size, void* userContextCallback)  
    {  
        (void)userContextCallback;  
        (void)payload;  
        (void)size;  
      
        int result;  
      
        if (strcmp("getCarVIN", method_name) == 0)  
        {  
            const char deviceMethodResponse[] = "{ \"Response\": \"1HGCM82633A004352\" }";  
            *response_size = sizeof(deviceMethodResponse)-1;  
            *response = malloc(*response_size);  
            (void)memcpy(*response, deviceMethodResponse, *response_size);  
            result = 200;  
        }  
        else  
        {  
            // All other entries are ignored.  
            const char deviceMethodResponse[] = "{ }";  
            *response_size = sizeof(deviceMethodResponse)-1;  
            *response = malloc(*response_size);  
            (void)memcpy(*response, deviceMethodResponse, *response_size);  
            result = -1;  
        }  
      
        return result;  
    }  
    

    Would strongly advise you to take a look at the following doc as well to better Understand how to invoke direct methods from IoT Hub: https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-direct-methods

    Despites mentioning IoT Hub, would work the same for IoT Central since the technology behind IoT Central is using IoT Hub ;).

    Hope I could help you continue with your project.

    Remember:

    • Please accept an answer if correct. Original posters help the community find answers faster by identifying the correct answer. Here is how.
    • Want a reminder to come back and check responses? Here is how to subscribe to a notification.
    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful