首页 > 解决方案 > 使用 Azure IOT 直接方法时直接方法失败

问题描述

我目前在 TI CC-3235 微控制器上使用 azure-iot-sdk-c。我尝试使用 Azure Direct 方法在微控制器上调用方法 (blinkLED1)。Azure IoT 中心给我一个错误,说“未注册”,如下所示: {"message":"Device {\"Message\":\"{\\\"errorCode\\\":404103,\\\"trackingId\\\":\\\"c1763a63a5b54381aa555198de1e884c-TimeStamp:07/01/2020 12:03:39\\\",\\\"message\\\":\\\"Timed out waiting for device to connect.\\\",\\\"info\\\":{\\\"timeout\\\":\\\"00:03:45\\\"},\\\"timestampUtc\\\":\\\"2020-07-01T12:03:39.1927271Z\\\"}\",\"ExceptionMessage\":\"\"} not registered"}

下面的代码是对来自 TI/Microsoft 的示例代码的修改。微控制器C代码如下:

{
    (void)userContextCallback;
    (void)payload;
    Display_printf(display, 0, 0, "\r\nDevice Method called");
    Display_printf(display, 0, 0, "Device Method name:    %s", method_name);
    Display_printf(display, 0, 0, "Device Method payload: %.*s", (int)size, (const char*)payload);

    int status = 200;
    char* RESPONSE_STRING = "{ \"Response\": \"This is the response from the device\" }";
    Display_printf(display, 0, 0, "\r\nResponse status: %d", status);
    Display_printf(display, 0, 0, "Response payload: %s\r\n", RESPONSE_STRING);
    if(strcmp("blinkLED1",method_name)==0){
        const char deviceMethodResponse[] = "{\"Response\":\"LED Blink1 Received\"}";
        Display_printf(display, 0, 0, "\r\nDevice Method called");
        *resp_size = sizeof(deviceMethodResponse)-1;
        *response = malloc(*resp_size);
        (void)memcpy(*response, deviceMethodResponse, *resp_size);
        status = 200;
    }
    *resp_size = strlen(RESPONSE_STRING);
    if ((*response = malloc(*resp_size)) == NULL)
    {
        status = -1;
    }
    else
    {
        (void)memcpy(*response, RESPONSE_STRING, *resp_size);
    }
    g_continueRunning = false;
    return status;
}

TI 示例参考:iothub_client_sample_device_method

Azure IoT SDK C 参考:在 Windows 上运行一个简单的设备 Twin C 示例

标签: azure-iot-hubazure-iot-sdk

解决方案


推荐阅读