首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >esp32-c3连接物联网平台,新建设备在动态注册的时候没有返回值怎么办?

esp32-c3连接物联网平台,新建设备在动态注册的时候没有返回值怎么办?

提问于 2024-06-18 16:46:30
回答 0关注 1查看 7

下面是根据官方改的代码,这个是官方api网站https://console.cloud.tencent.com/api/explorer?Product=iotexplorer&Version=2019-04-23&Action=CreateDevice

代码语言:txt
复制
// 动态注册函数
esp_err_t dynamic_register_device(mqtt_config_t *config) {
    generate_random_device_name(config->device_name, 7); // 生成6个字符的设备名称
    snprintf(config->product_id, sizeof(config->product_id), "%s", PRODUCT_ID);

    // ************* 步骤 1:生成 CanonicalRequest *************
    char payload[256];
    snprintf(payload, sizeof(payload), "{\"ProductId\":\"%s\",\"DeviceName\":\"%s\",\"ProductSecret\":\"%s\"}", config->product_id, config->device_name, PRODUCT_SECRET);
    printf("Payload: %s\n", payload);

    // ************* 步骤 2:生成 StringToSign *************
    char timestamp[20];
    time_t now = time(NULL);
    snprintf(timestamp, sizeof(timestamp), "%lld", (long long)now); // 使用Unix时间戳

    char date[11];
    strftime(date, sizeof(date), "%Y-%m-%d", gmtime(&now));

    char credential_scope[50];
    snprintf(credential_scope, sizeof(credential_scope), "%s/%s/tc3_request", date, SERVICE);
    printf("Credential Scope: %s\n", credential_scope);

    char canonical_request_hash[65];
    sha256_hex((const unsigned char *)payload, strlen(payload), canonical_request_hash);
    printf("Canonical Request Hash: %s\n", canonical_request_hash);

    char string_to_sign[256];
    snprintf(string_to_sign, sizeof(string_to_sign), "TC3-HMAC-SHA256\n%s\n%s\n%s", timestamp, credential_scope, canonical_request_hash);
    printf("String to Sign: %s\n", string_to_sign);

    // ************* 步骤 3:计算签名 *************
    unsigned char signing_key[32];
    my_hmac_sha256((const unsigned char *)("TC3" SECRET_KEY), strlen("TC3" SECRET_KEY), (const unsigned char *)date, strlen(date), signing_key);
    my_hmac_sha256(signing_key, 32, (const unsigned char *)SERVICE, strlen(SERVICE), signing_key);
    my_hmac_sha256(signing_key, 32, (const unsigned char *)"tc3_request", strlen("tc3_request"), signing_key);

    unsigned char signature[32];
    my_hmac_sha256(signing_key, 32, (const unsigned char *)string_to_sign, strlen(string_to_sign), signature);

    char signature_hex[65];
    for (int i = 0; i < 32; i++) {
        sprintf(signature_hex + (i * 2), "%02x", signature[i]);
    }
    printf("Signature: %s\n", signature_hex);

    // ************* 步骤 4:拼接 Authorization *************
    char authorization[512];
    snprintf(authorization, sizeof(authorization), "TC3-HMAC-SHA256 Credential=%s/%s, SignedHeaders=content-type;host;x-tc-action, Signature=%s", SECRET_ID, credential_scope, signature_hex);
    printf("Authorization: %s\n", authorization);

    // ************* 步骤 5:构造并发起请求 *************
    esp_http_client_config_t client_config = {
        .url = DYNREG_URL,
        .method = HTTP_METHOD_POST,
        .timeout_ms = 10000,
    };
    esp_http_client_handle_t client = esp_http_client_init(&client_config);
    esp_http_client_set_post_field(client, payload, strlen(payload));
    esp_http_client_set_header(client, "Content-Type", "application/json; charset=utf-8");
    esp_http_client_set_header(client, "Authorization", authorization);
    esp_http_client_set_header(client, "Host", "iotcloud.tencentcloudapi.com");
    esp_http_client_set_header(client, "X-TC-Action", ACTION);
    esp_http_client_set_header(client, "X-TC-Timestamp", timestamp);
    esp_http_client_set_header(client, "X-TC-Version", VERSION);
    esp_http_client_set_header(client, "X-TC-Region", REGION);

    esp_err_t err = esp_http_client_perform(client);
    if (err == ESP_OK) {
        int status_code = esp_http_client_get_status_code(client);
        printf("HTTP Status Code: %d\n", status_code); // 输出HTTP状态码
        if (status_code == 200) {
            char response_buffer[512];
            memset(response_buffer, 0, sizeof(response_buffer)); // 确保缓冲区已清零

            int content_length = esp_http_client_get_content_length(client);
            int total_read_len = 0;
            while (total_read_len < content_length) {
                int read_len = esp_http_client_read(client, response_buffer + total_read_len, sizeof(response_buffer) - total_read_len - 1);
                if (read_len <= 0) {
                    break;
                }
                total_read_len += read_len;
            }
            response_buffer[total_read_len] = 0; // 确保字符串以NULL结尾

            printf("Received response: %s\n", response_buffer); // 输出响应数据

            cJSON *response_json = cJSON_Parse(response_buffer);
            if (response_json) {
                cJSON *device_secret = cJSON_GetObjectItem(response_json, "DeviceSecret");
                if (device_secret) {
                    printf("Parsed DeviceSecret: %s\n", device_secret->valuestring);
                    strncpy(config->device_secret, device_secret->valuestring, sizeof(config->device_secret) - 1);
                    config->device_secret[sizeof(config->device_secret) - 1] = '\0'; // 确保字符串以 NULL 结尾
                    cJSON_Delete(response_json);
                    esp_http_client_cleanup(client);
                    return save_mqtt_config(config);
                } else {
                    printf("DeviceSecret not found in JSON response.\n");
                }
                cJSON_Delete(response_json);
            } else {
                printf("Failed to parse JSON response.\n");
            }
        } else {
            printf("HTTP request failed with status code %d\n", status_code);
        }
    } else {
        printf("HTTP request failed with error %d\n", err);
    }
    esp_http_client_cleanup(client);
    return err;
}

下面是我返回的部分内容,就很气怪,秘钥也是乱码,错误都不好定位

代码语言:txt
复制
I (9193) wifi:<ba-add>idx:1 (ifx:0, c8:0c:c8:e1:62:e0), tid:3, ssn:0, winSize:64
HTTP Status Code: 200
Received response: 
Failed to parse JSON response.
Dynamic registration successful!
Device Name: nqWwIc
Device Secret: ��������������:�8@

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档