文档

设备分发

更新时间:

物联网平台通过设备分发实现设备跨地域、跨实例或跨账号的分发。设备本地固化收到信息后,直接连接物联网平台分发的新连接地址,免去二次烧录设备信息。本文介绍如何使用设备分发功能。

前提条件

使用流程

如下功能时序图,以设备的应用程序demos/device_bootstrap_demo.c为例,介绍设备分发的使用流程。

image

步骤1:设备初始化

  1. 添加头文件。

    #include "aiot_device_api.h"
  2. 创建设备句柄。

    void *device_client = aiot_device_create(product_key, device_name);
    if (device_client == NULL) {
        printf("device_client failed\n");
        return -1;
    }

步骤2:发送设备分发请求

调用aiot_device_bootstrap_request接口请求设备分发。

说明
  • 该接口为同步请求接口,请设置合适的超时时间,避免阻塞。

  • connect_info包含返回服务器的hostport

res = aiot_device_bootstrap_request(device_client, MQTT_PROTOCOL, timeout_ms, &connect_info);
if(res == STATE_SUCCESS){
 printf("update host %s,port %d\r\n", connect_info.host, connect_info.port);
}

(可选)步骤3: 设备建连

步骤3中获取host、port后,即可与物联网平台建立MQTT连接。

/* 连接配置参数初始化 */
aiot_linkconfig_t* config = aiot_linkconfig_init(protocol);
/* 设置服务器的host、port */
aiot_linkconfig_host(config, connect_info.host, connect_info.port);

/* 设置设备连接参数 */
aiot_device_set_linkconfig(device_client, config);
/* 设备建连 */
res = aiot_device_connect(device_client);

步骤4:删除设备句柄

    /* 销毁设备实例, 一般不会运行到这里 */
    aiot_device_delete(&device_client);
    aiot_linkconfig_deinit(&config);

  • 本页导读 (1)
文档反馈