# 网关设备快速入门 本文档将讲述如何在腾讯物联网开发平台(IoT Explorer)控制台申请网关设备并绑定子设备, 并结合C-SDK 的**gateway_sample**快速体验网关设备代理子设备上下线,子设备基于数据模板协议或者自定义数据,发送和接收消息。 ## 一. 控制台创建网关设备 #### 1. 创建网关产品和设备 点击页面**创建新产品**按钮, 创建一个网关产品。 ![](https://main.qcloudimg.com/raw/5a535db5040ac350f4033fd40071a1d7.jpg) 在弹出的产品窗口中,选择节点类型和产品类型,输入产品名称,选择认证方式和数据格式,输入产品描述,然后点击确定创建 在生成的产品页面下,点击**子设备管理**进行子产品和子设备绑定。 ![](https://main.qcloudimg.com/raw/5228791ee57d182f7adb71617c6be96a.jpg) 子设备是通过网关产品代理实现子设备的鉴权和通信,网关能够代理子设备鉴权和通信的前提是在平台实现了网关和子设备的绑定操作。需要注意的是子设备是无法直连物联网平台的产品,由网关设备代理连接,所以子设备的认证方式不影响连接,由网关设备来负责认证接入。 在子产品管理页面绑定子产品 ![](https://main.qcloudimg.com/raw/244dcf505fc026cb465ff26fce682d0d.jpg) 在网关产品下创建网关设备,进入子设备管理,选择绑定的子产品产品,然后选择对应子产品下需要绑定的子设备 ![](https://main.qcloudimg.com/raw/8bad6a65baaab95d0de6eab402aeb269.jpg) ![](https://main.qcloudimg.com/raw/1d65fe910deb0521595302ba5d54efda.jpg) #### 2. 定义子产品数据模板或者创建自定义产品 网关示例代理子设备上线后,默认会订阅topic $productID/$devName/data, 并向这个topic发送数据,这个topic只有自定义透传的产品才会有data topic。 ![](https://main.qcloudimg.com/raw/26b60d86e694d23b32d7a460dd8c5794.jpg) ![](https://main.qcloudimg.com/raw/cdb697cae207a87dda99fb89f51bd617.jpg) 网关示例中,开启宏定义 `SUB_DEV_USE_DATA_TEMPLATE_LIGHT`,会代理子产品数据模板为智能灯的子设进行通信,如果开启该宏定义,请绑定一个智能灯的产品并将子产品和子设备信息分别配置到`LIGHT_SUB_DEV_PRODUCT_ID`和`LIGHT_SUB_DEV_NAME`。 ``` # define SUB_DEV_USE_DATA_TEMPLATE_LIGHT # ifdef SUB_DEV_USE_DATA_TEMPLATE_LIGHT # define LIGHT_SUB_DEV_PRODUCT_ID "WPDA0S6S08" # define LIGHT_SUB_DEV_NAME "dev001" extern void* sub_dev1_thread(void *ptr, char *product_id, char *device_name); # endif ``` ## 二. 编译运行示例程序 #### 1. 配置网关和子设备信息 修改设备信息配置device_info.json,配置网关产品、设备、密钥或者证书,配置绑定的子设备数量,对应的产品和设备名 ``` { "auth_mode":"KEY", "productId":"PRODUCT_ID", //网关产品ID "productSecret":"YOUR_PRODUCT_SECRET", "deviceName":"YOUR_DEV_NAME", //网关设备名 "key_deviceinfo":{ "deviceSecret":"xxxxx" //密钥认证,网关设备的密钥 }, "cert_deviceinfo":{ "devCertFile":"YOUR_DEVICE_CERT_FILE_NAME", //证书认证,网关设备的公钥证书文件名 "devPrivateKeyFile":"YOUR_DEVICE_PRIVATE_KEY_FILE_NAME" //证书认证,网关设备的私钥证书文件名 }, "subDev":{ "subdev_num":6, //子设备数量 "subdev_list": //对应的子产品和子设备 [ {"sub_productId": "WPDA0S6S08", "sub_devName": "dev001"}, {"sub_productId": "WPDA0S6S08", "sub_devName": "dev002"}, {"sub_productId": "WPDA0S6S08", "sub_devName": "dev003"}, {"sub_productId": "Y8T6NB8DM0", "sub_devName": "test001"}, {"sub_productId": "Y8T6NB8DM0", "sub_devName": "test002"}, {"sub_productId": "Y8T6NB8DM0", "sub_devName": "test003"} ] } } ``` #### 2. 修改编译选项 - 使用cmake编译,修改CMakeLists.txt(以**密钥认证设备**为例),使能网关功能,网关会关联打开多线程功能 ``` set(BUILD_TYPE "release") set(COMPILE_TOOLS "gcc") set(PLATFORM "linux") set(FEATURE_MQTT_COMM_ENABLED ON) set(FEATURE_GATEWAY_ENABLED ON) set(FEATURE_MULTITHREAD_ENABLED ON) set(FEATURE_AUTH_MODE "KEY") ``` 执行脚本编译 ``` ./cmake_build.sh ``` 示例输出位于`output/release/bin`文件夹中 - 使用makefile编译,修改CMakeLists.txt(以**密钥认证设备**为例),使能网关功能,网关会关联打开多线程功能 ``` PLATFORM_CC = gcc PLATFORM_AR = ar PLATFORM_OS = linux FEATURE_GATEWAY_ENABLED = y FEATURE_MULTITHREAD_ENABLED = y ``` 执行make编译 ``` make ``` 示例输出位于`output/release/bin`文件夹中 #### 3. 执行网关示例程序 如下日志信息显示示例程序通过MQTT网关代理子设备上下线状态变化,发布和订阅消息。 ``` ./gateway_sample DBG|2020-02-27 05:53:59|HAL_Device_linux.c|HAL_GetGwDevInfo(632): sub device num:6 DBG|2020-02-27 05:53:59|HAL_Device_linux.c|HAL_GetGwDevInfo(634): 0th subDevPid:BK7EEF4UIB subDevName:dev001 DBG|2020-02-27 05:53:59|HAL_Device_linux.c|HAL_GetGwDevInfo(634): 1th subDevPid:BK7EEF4UIB subDevName:dev002 DBG|2020-02-27 05:53:59|HAL_Device_linux.c|HAL_GetGwDevInfo(634): 2th subDevPid:BK7EEF4UIB subDevName:dev003 DBG|2020-02-27 05:53:59|HAL_Device_linux.c|HAL_GetGwDevInfo(634): 3th subDevPid:7P3KIFQ1JD subDevName:test001 DBG|2020-02-27 05:53:59|HAL_Device_linux.c|HAL_GetGwDevInfo(634): 4th subDevPid:7P3KIFQ1JD subDevName:test002 DBG|2020-02-27 05:53:59|HAL_Device_linux.c|HAL_GetGwDevInfo(634): 5th subDevPid:7P3KIFQ1JD subDevName:test003 INF|2020-02-27 05:53:59|qcloud_iot_device.c|iot_device_info_set(65): SDK_Ver: 3.1.1, Product_ID: 0WUKPUCOTC, Device_Name: GW001 DBG|2020-02-27 05:53:59|HAL_TLS_mbedtls.c|HAL_TLS_Connect(228): Setting up the SSL/TLS structure... DBG|2020-02-27 05:53:59|HAL_TLS_mbedtls.c|HAL_TLS_Connect(270): Performing the SSL/TLS handshake... DBG|2020-02-27 05:53:59|HAL_TLS_mbedtls.c|HAL_TLS_Connect(271): Connecting to /0wukpucotc.iotcloud.tencentdevices.com/8883... INF|2020-02-27 05:53:59|HAL_TLS_mbedtls.c|HAL_TLS_Connect(293): connected with /0wukpucotc.iotcloud.tencentdevices.com/8883... INF|2020-02-27 05:53:59|mqtt_client.c|IOT_MQTT_Construct(127): mqtt connect with id: piVMy success DBG|2020-02-27 05:53:59|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(141): topicName=$gateway/operation/result/0WUKPUCOTC/GW001|packet_id=7213 DBG|2020-02-27 05:53:59|gateway_api.c|_gateway_event_handler(23): gateway sub|unsub(3) success, packet-id=7213 DBG|2020-02-27 05:54:00|gateway_sample.c|gateway_yield_thread(202): gateway yield thread start ... DBG|2020-02-27 05:54:01|gateway_api.c|IOT_Gateway_Subdev_Online(125): there is no session, create a new session DBG|2020-02-27 05:54:01|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/0WUKPUCOTC/GW001|payload={"type":"online","payload":{"devices":[{"product_id":"BK7EEF4UIB","device_name":"dev001"}]}} INF|2020-02-27 05:54:01|gateway_common.c|_gateway_message_handler(138): client_id(BK7EEF4UIB/dev001), online result 0 DBG|2020-02-27 05:54:01|gateway_sample.c|main(289): subDev Pid:BK7EEF4UIB devName:dev001 online success. DBG|2020-02-27 05:54:01|gateway_api.c|IOT_Gateway_Subdev_Online(125): there is no session, create a new session DBG|2020-02-27 05:54:01|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/0WUKPUCOTC/GW001|payload={"type":"online","payload":{"devices":[{"product_id":"BK7EEF4UIB","device_name":"dev002"}]}} INF|2020-02-27 05:54:01|gateway_common.c|_gateway_message_handler(138): client_id(BK7EEF4UIB/dev002), online result 0 DBG|2020-02-27 05:54:01|gateway_sample.c|main(289): subDev Pid:BK7EEF4UIB devName:dev002 online success. DBG|2020-02-27 05:54:01|gateway_api.c|IOT_Gateway_Subdev_Online(125): there is no session, create a new session DBG|2020-02-27 05:54:01|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/0WUKPUCOTC/GW001|payload={"type":"online","payload":{"devices":[{"product_id":"BK7EEF4UIB","device_name":"dev003"}]}} INF|2020-02-27 05:54:01|gateway_common.c|_gateway_message_handler(138): client_id(BK7EEF4UIB/dev003), online result 0 DBG|2020-02-27 05:54:01|gateway_sample.c|main(289): subDev Pid:BK7EEF4UIB devName:dev003 online success. DBG|2020-02-27 05:54:01|gateway_api.c|IOT_Gateway_Subdev_Online(125): there is no session, create a new session DBG|2020-02-27 05:54:01|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/0WUKPUCOTC/GW001|payload={"type":"online","payload":{"devices":[{"product_id":"7P3KIFQ1JD","device_name":"test001"}]}} INF|2020-02-27 05:54:01|gateway_common.c|_gateway_message_handler(138): client_id(7P3KIFQ1JD/test001), online result 0 DBG|2020-02-27 05:54:01|gateway_sample.c|main(289): subDev Pid:7P3KIFQ1JD devName:test001 online success. DBG|2020-02-27 05:54:01|gateway_api.c|IOT_Gateway_Subdev_Online(125): there is no session, create a new session DBG|2020-02-27 05:54:01|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/0WUKPUCOTC/GW001|payload={"type":"online","payload":{"devices":[{"product_id":"7P3KIFQ1JD","device_name":"test002"}]}} INF|2020-02-27 05:54:01|gateway_common.c|_gateway_message_handler(138): client_id(7P3KIFQ1JD/test002), online result 0 DBG|2020-02-27 05:54:01|gateway_sample.c|main(289): subDev Pid:7P3KIFQ1JD devName:test002 online success. DBG|2020-02-27 05:54:01|gateway_api.c|IOT_Gateway_Subdev_Online(125): there is no session, create a new session DBG|2020-02-27 05:54:01|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/0WUKPUCOTC/GW001|payload={"type":"online","payload":{"devices":[{"product_id":"7P3KIFQ1JD","device_name":"test003"}]}} INF|2020-02-27 05:54:01|gateway_common.c|_gateway_message_handler(138): client_id(7P3KIFQ1JD/test003), online result 0 DBG|2020-02-27 05:54:01|gateway_sample.c|main(289): subDev Pid:7P3KIFQ1JD devName:test003 online success. ERR|2020-02-27 05:54:01|gateway_sample.c|main(311): create sub_dev light thread success DBG|2020-02-27 05:54:01|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(141): topicName=$thing/down/property/BK7EEF4UIB/dev002|packet_id=7214 DBG|2020-02-27 05:54:01|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(141): topicName=$thing/down/property/BK7EEF4UIB/dev003|packet_id=7215 DBG|2020-02-27 05:54:01|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(141): topicName=$thing/down/property/7P3KIFQ1JD/test001|packet_id=7216 DBG|2020-02-27 05:54:01|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(141): topicName=$thing/down/property/7P3KIFQ1JD/test002|packet_id=7217 DBG|2020-02-27 05:54:01|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(141): topicName=$thing/down/property/7P3KIFQ1JD/test003|packet_id=7218 DBG|2020-02-27 05:54:01|sub_dev001.c|sub_dev1_thread(563): sub_dev1_thread ... INF|2020-02-27 05:54:01|qcloud_iot_device.c|iot_device_info_set(65): SDK_Ver: 3.1.1, Product_ID: BK7EEF4UIB, Device_Name: dev001 DBG|2020-02-27 05:54:01|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(141): topicName=$thing/down/property/BK7EEF4UIB/dev001|packet_id=7219 DBG|2020-02-27 05:54:01|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(141): topicName=$thing/down/event/BK7EEF4UIB/dev001|packet_id=7220 DBG|2020-02-27 05:54:01|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(141): topicName=$thing/down/action/BK7EEF4UIB/dev001|packet_id=7221 INF|2020-02-27 05:54:01|sub_dev001.c|sub_dev1_thread(574): Cloud Device Construct Success DBG|2020-02-27 05:54:01|sub_dev001.c|_usr_init(337): add your init code here INF|2020-02-27 05:54:01|sub_dev001.c|_register_data_template_property(376): data template property=power_switch registered. INF|2020-02-27 05:54:01|sub_dev001.c|_register_data_template_property(376): data template property=color registered. INF|2020-02-27 05:54:01|sub_dev001.c|_register_data_template_property(376): data template property=brightness registered. INF|2020-02-27 05:54:01|sub_dev001.c|_register_data_template_property(376): data template property=name registered. INF|2020-02-27 05:54:01|sub_dev001.c|sub_dev1_thread(589): Register data template propertys Success INF|2020-02-27 05:54:01|sub_dev001.c|_register_data_template_action(277): data template action=blink registered. INF|2020-02-27 05:54:01|sub_dev001.c|sub_dev1_thread(599): Register data template actions Success DBG|2020-02-27 05:54:01|gateway_api.c|_gateway_event_handler(23): gateway sub|unsub(3) success, packet-id=7214 INF|2020-02-27 05:54:01|gateway_sample.c|_event_handler(74): subscribe success, packet-id=7214 DBG|2020-02-27 05:54:01|gateway_api.c|_gateway_event_handler(23): gateway sub|unsub(3) success, packet-id=7216 INF|2020-02-27 05:54:01|gateway_sample.c|_event_handler(74): subscribe success, packet-id=7216 DBG|2020-02-27 05:54:01|gateway_api.c|_gateway_event_handler(23): gateway sub|unsub(3) success, packet-id=7218 INF|2020-02-27 05:54:01|gateway_sample.c|_event_handler(74): subscribe success, packet-id=7218 DBG|2020-02-27 05:54:01|gateway_api.c|_gateway_event_handler(23): gateway sub|unsub(3) success, packet-id=7217 INF|2020-02-27 05:54:01|gateway_sample.c|_event_handler(74): subscribe success, packet-id=7217 DBG|2020-02-27 05:54:01|gateway_api.c|_gateway_event_handler(23): gateway sub|unsub(3) success, packet-id=7215 INF|2020-02-27 05:54:01|gateway_sample.c|_event_handler(74): subscribe success, packet-id=7215 DBG|2020-02-27 05:54:01|gateway_api.c|_gateway_event_handler(23): gateway sub|unsub(3) success, packet-id=7219 INF|2020-02-27 05:54:01|gateway_sample.c|_event_handler(74): subscribe success, packet-id=7219 DBG|2020-02-27 05:54:01|gateway_api.c|_gateway_event_handler(23): gateway sub|unsub(3) success, packet-id=7220 INF|2020-02-27 05:54:01|gateway_sample.c|_event_handler(74): subscribe success, packet-id=7220 DBG|2020-02-27 05:54:01|gateway_api.c|_gateway_event_handler(23): gateway sub|unsub(3) success, packet-id=7221 INF|2020-02-27 05:54:01|gateway_sample.c|_event_handler(74): subscribe success, packet-id=7221 DBG|2020-02-27 05:54:02|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$thing/up/property/BK7EEF4UIB/dev001|payload={"method":"report_info", "clientToken":"BK7EEF4UIB-0", "params":{"module_hardinfo":"ESP8266","module_softinfo":"V1.0","fw_ver":"3.1.1","imei":"11-22-33-44","lat":"22.546015","lon":"113.941125", "device_label":{"append_info":"your self define info"}}} DBG|2020-02-27 05:54:02|data_template_client.c|_reply_ack_cb(169): replyAck=0 DBG|2020-02-27 05:54:02|data_template_client.c|_reply_ack_cb(172): Received Json Document={"method":"report_info_reply","clientToken":"BK7EEF4UIB-0","code":0,"status":"success"} DBG|2020-02-27 05:54:02|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$thing/up/property/BK7EEF4UIB/dev001|payload={"method":"get_status", "clientToken":"BK7EEF4UIB-1"} DBG|2020-02-27 05:54:02|data_template_client.c|_get_status_reply_ack_cb(185): replyAck=0 DBG|2020-02-27 05:54:02|data_template_client.c|_get_status_reply_ack_cb(189): Received Json Document={"method":"get_status_reply","clientToken":"BK7EEF4UIB-1","code":0,"status":"success","data":{"reported":{"brightness":0,"name":"dev001","power_switch":1,"color":1}}} DBG|2020-02-27 05:54:02|sub_dev001.c|sub_dev1_thread(625): Get data status success DBG|2020-02-27 05:54:02|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$thing/up/property/BK7EEF4UIB/dev001|payload={"method":"report", "clientToken":"BK7EEF4UIB-2", "params":{"power_switch":0,"color":0,"brightness":0,"name":""}} INF|2020-02-27 05:54:02|sub_dev001.c|sub_dev1_thread(666): data template reporte success INF|2020-02-27 05:54:02|sub_dev001.c|OnReportReplyCallback(360): recv report_reply(ack=0): {"method":"report_reply","clientToken":"BK7EEF4UIB-2","code":0,"status":"success"} DBG|2020-02-27 05:54:03|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$thing/up/property/BK7EEF4UIB/dev002|payload={"method":"report","clientToken":"123","params":{}} DBG|2020-02-27 05:54:03|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$thing/up/property/BK7EEF4UIB/dev003|payload={"method":"report","clientToken":"123","params":{}} DBG|2020-02-27 05:54:03|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$thing/up/property/7P3KIFQ1JD/test001|payload={"method":"report","clientToken":"123","params":{}} DBG|2020-02-27 05:54:03|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$thing/up/property/7P3KIFQ1JD/test002|payload={"method":"report","clientToken":"123","params":{}} DBG|2020-02-27 05:54:03|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$thing/up/property/7P3KIFQ1JD/test003|payload={"method":"report","clientToken":"123","params":{}} INF|2020-02-27 05:54:03|gateway_sample.c|_message_handler(124): Receive Message With topicName:$thing/down/property/BK7EEF4UIB/dev002, payload:{"method":"report_reply","clientToken":"123","code":0,"status":"success"} INF|2020-02-27 05:54:03|gateway_sample.c|_message_handler(124): Receive Message With topicName:$thing/down/property/7P3KIFQ1JD/test001, payload:{"method":"report_reply","clientToken":"123","code":0,"status":"success"} INF|2020-02-27 05:54:03|gateway_sample.c|_message_handler(124): Receive Message With topicName:$thing/down/property/7P3KIFQ1JD/test002, payload:{"method":"report_reply","clientToken":"123","code":0,"status":"success"} INF|2020-02-27 05:54:03|gateway_sample.c|_message_handler(124): Receive Message With topicName:$thing/down/property/7P3KIFQ1JD/test003, payload:{"method":"report_reply","clientToken":"123","code":0,"status":"success"} INF|2020-02-27 05:54:03|gateway_sample.c|_message_handler(124): Receive Message With topicName:$thing/down/property/BK7EEF4UIB/dev003, payload:{"method":"report_reply","clientToken":"123","code":0,"status":"success"} DBG|2020-02-27 05:54:07|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$thing/up/property/BK7EEF4UIB/dev001|payload={"method":"report", "clientToken":"BK7EEF4UIB-3", "params":{"power_switch":0,"color":0,"brightness":0,"name":""}} INF|2020-02-27 05:54:07|sub_dev001.c|sub_dev1_thread(666): data template reporte success INF|2020-02-27 05:54:07|sub_dev001.c|OnReportReplyCallback(360): recv report_reply(ack=0): {"method":"report_reply","clientToken":"BK7EEF4UIB-3","code":0,"status":"success"} ``` #### 4. 观察子设备上线 如下日志信息显示示例程序的网关设备Gateway-demo通过MQTT代理6个子设备上线成功: ``` DBG|2020-01-13 16:11:24|gateway_api.c|IOT_Gateway_Subdev_Online(125): there is no session, create a new session DBG|2020-01-13 16:11:24|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/MMC8QP0VAQ/GW001|payload={"type":"online","payload":{"devices":[{"product_id":"WPDA0S6S08","device_name":"dev001"}]}} INF|2020-01-13 16:11:24|gateway_common.c|_gateway_message_handler(138): client_id(WPDA0S6S08/dev001), online result 0 DBG|2020-01-13 16:11:25|gateway_sample.c|main(282): subDev Pid:WPDA0S6S08 devName:dev001 online success. DBG|2020-01-13 16:11:25|gateway_api.c|IOT_Gateway_Subdev_Online(125): there is no session, create a new session DBG|2020-01-13 16:11:25|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/MMC8QP0VAQ/GW001|payload={"type":"online","payload":{"devices":[{"product_id":"WPDA0S6S08","device_name":"dev002"}]}} INF|2020-01-13 16:11:25|gateway_common.c|_gateway_message_handler(138): client_id(WPDA0S6S08/dev002), online result 0 DBG|2020-01-13 16:11:25|gateway_sample.c|main(282): subDev Pid:WPDA0S6S08 devName:dev002 online success. DBG|2020-01-13 16:11:25|gateway_api.c|IOT_Gateway_Subdev_Online(125): there is no session, create a new session DBG|2020-01-13 16:11:25|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/MMC8QP0VAQ/GW001|payload={"type":"online","payload":{"devices":[{"product_id":"WPDA0S6S08","device_name":"dev003"}]}} INF|2020-01-13 16:11:25|gateway_common.c|_gateway_message_handler(138): client_id(WPDA0S6S08/dev003), online result 0 DBG|2020-01-13 16:11:25|gateway_sample.c|main(282): subDev Pid:WPDA0S6S08 devName:dev003 online success. DBG|2020-01-13 16:11:25|gateway_api.c|IOT_Gateway_Subdev_Online(125): there is no session, create a new session DBG|2020-01-13 16:11:25|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/MMC8QP0VAQ/GW001|payload={"type":"online","payload":{"devices":[{"product_id":"Y8T6NB8DM0","device_name":"test001"}]}} INF|2020-01-13 16:11:25|gateway_common.c|_gateway_message_handler(138): client_id(Y8T6NB8DM0/test001), online result 0 DBG|2020-01-13 16:11:25|gateway_sample.c|main(282): subDev Pid:Y8T6NB8DM0 devName:test001 online success. DBG|2020-01-13 16:11:25|gateway_api.c|IOT_Gateway_Subdev_Online(125): there is no session, create a new session DBG|2020-01-13 16:11:25|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/MMC8QP0VAQ/GW001|payload={"type":"online","payload":{"devices":[{"product_id":"Y8T6NB8DM0","device_name":"test002"}]}} INF|2020-01-13 16:11:25|gateway_common.c|_gateway_message_handler(138): client_id(Y8T6NB8DM0/test002), online result 0 DBG|2020-01-13 16:11:25|gateway_sample.c|main(282): subDev Pid:Y8T6NB8DM0 devName:test002 online success. DBG|2020-01-13 16:11:25|gateway_api.c|IOT_Gateway_Subdev_Online(125): there is no session, create a new session DBG|2020-01-13 16:11:25|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/MMC8QP0VAQ/GW001|payload={"type":"online","payload":{"devices":[{"product_id":"Y8T6NB8DM0","device_name":"test003"}]}} INF|2020-01-13 16:11:25|gateway_common.c|_gateway_message_handler(138): client_id(Y8T6NB8DM0/test003), online result 0 DBG|2020-01-13 16:11:26|gateway_sample.c|main(282): subDev Pid:Y8T6NB8DM0 devName:test003 online success. ``` #### 5. 观察子设备下线 如下日志信息显示示例程序的网关设备Gateway-demo通过MQTT代理子设备下线成功: ```` DBG|2020-02-27 05:58:49|gateway_sample.c|main(388): subDev Pid:BK7EEF4UIB devName:dev002 offline success. DBG|2020-02-27 05:58:49|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/0WUKPUCOTC/GW001|payload={"type":"offline","payload":{"devices":[{"product_id":"BK7EEF4UIB","device_name":"dev003"}]}} INF|2020-02-27 05:58:49|gateway_common.c|_gateway_message_handler(143): client_id(BK7EEF4UIB/dev003), offline result 0 DBG|2020-02-27 05:58:49|gateway_sample.c|main(388): subDev Pid:BK7EEF4UIB devName:dev003 offline success. DBG|2020-02-27 05:58:49|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/0WUKPUCOTC/GW001|payload={"type":"offline","payload":{"devices":[{"product_id":"7P3KIFQ1JD","device_name":"test001"}]}} INF|2020-02-27 05:58:49|gateway_common.c|_gateway_message_handler(143): client_id(7P3KIFQ1JD/test001), offline result 0 DBG|2020-02-27 05:58:49|gateway_sample.c|main(388): subDev Pid:7P3KIFQ1JD devName:test001 offline success. DBG|2020-02-27 05:58:49|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/0WUKPUCOTC/GW001|payload={"type":"offline","payload":{"devices":[{"product_id":"7P3KIFQ1JD","device_name":"test002"}]}} INF|2020-02-27 05:58:49|gateway_common.c|_gateway_message_handler(143): client_id(7P3KIFQ1JD/test002), offline result 0 DBG|2020-02-27 05:58:49|gateway_sample.c|main(388): subDev Pid:7P3KIFQ1JD devName:test002 offline success. DBG|2020-02-27 05:58:49|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$gateway/operation/0WUKPUCOTC/GW001|payload={"type":"offline","payload":{"devices":[{"product_id":"7P3KIFQ1JD","device_name":"test003"}]}} INF|2020-02-27 05:58:49|gateway_common.c|_gateway_message_handler(143): client_id(7P3KIFQ1JD/test003), offline result 0 DBG|2020-02-27 05:58:49|gateway_sample.c|main(388): subDev Pid:7P3KIFQ1JD devName:test003 offline success. ``` #### 6. 观察网关发送接收子设备的消息 通过控制台控制智能灯子设备开关状态、灯光颜色和亮度,如下日志信息 ``` DBG|2020-02-27 06:02:19|data_template_client_manager.c|_on_template_downstream_topic_handler(527): control_str:{"power_switch":1,"brightness":50} INF|2020-02-27 06:02:19|sub_dev001.c|OnControlMsgCallback(349): Property=brightness changed INF|2020-02-27 06:02:19|sub_dev001.c|OnControlMsgCallback(349): Property=power_switch changed [ lighting ]|[color: RED ]|[brightness:||||||||||----------]|[] DBG|2020-02-27 06:02:19|data_template_client.c|IOT_Template_ControlReply(698): Report Document: {"code":0, "clientToken":"clientToken-db6220ff-ab9f-4655-945d-dd14b6e3498d"} DBG|2020-02-27 06:02:19|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$thing/up/property/BK7EEF4UIB/dev001|payload={"method":"control_reply", "code":0, "clientToken":"clientToken-db6220ff-ab9f-4655-945d-dd14b6e3498d"} DBG|2020-02-27 06:02:19|sub_dev001.c|sub_dev1_thread(649): Contol msg reply success ``` #### 6. 观察网关代理子设备发送消息 智能灯子设备会周期上报,开关状态发生变化会上报事件 ``` DBG|2020-02-27 06:02:19|mqtt_client_publish.c|qcloud_iot_mqtt_publish(334): publish topic seq=58053|topicName=$thing/up/event/BK7EEF4UIB/dev001|payload={"method":"event_post", "clientToken":"BK7EEF4UIB-8", "eventId":"status_report", "type":"info", "timestamp":0, "params":{"status":1,"message":"light on"}} INF|2020-02-27 06:02:19|gateway_sample.c|_event_handler(101): publish success, packet-id=58053 INF|2020-02-27 06:02:19|sub_dev001.c|OnReportReplyCallback(360): recv report_reply(ack=0): {"method":"report_reply","clientToken":"BK7EEF4UIB-7","code":0,"status":"success"} DBG|2020-02-27 06:02:19|data_template_event.c|_on_event_reply_callback(105): recv:{"method":"event_reply","clientToken":"BK7EEF4UIB-8","code":0,"status":"","data":{}} DBG|2020-02-27 06:02:19|data_template_event.c|_on_event_reply_callback(123): eventToken:BK7EEF4UIB-8 code:0 status:(null) DBG|2020-02-27 06:02:19|sub_dev001.c|event_post_cb(169): recv event reply, clear event DBG|2020-02-27 06:02:19|data_template_event.c|_traverse_event_list(79): eventToken[BK7EEF4UIB-8] released DBG|2020-02-27 06:02:21|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$thing/up/property/BK7EEF4UIB/dev001|payload={"method":"report", "clientToken":"BK7EEF4UIB-9", "params":{"power_switch":1,"color":0,"brightness":50,"name":""}} INF|2020-02-27 06:02:21|sub_dev001.c|sub_dev1_thread(666): data template reporte success INF|2020-02-27 06:02:21|sub_dev001.c|OnReportReplyCallback(360): recv report_reply(ack=0): {"method":"report_reply","clientToken":"BK7EEF4UIB-9","code":0,"status":"success"} ```