网关子设备快速入门

最近更新时间:2024-08-26 14:55:51

我的收藏

操作场景

若需要将网关与子设备类型设备接入到物联网开发平台,网关设备直连云平台,子设备通过网关代理绑定、上下线、通信的方式与平台进行通信,从而实时控制网关与子设备。本文档主要帮助您快速熟悉网关与子设备的对接全流程。

前提条件

为了通过下面的步骤快速理解网关子设备类型设备的对接流程,需要做好以下准备工作:
申请物联网开发平台服务。
拥有一台物理或虚拟的 Linux 环境,可以编译、运行示例程序。
示例程序在 Linux 环境下测试和验证,主要基于 Ubuntu16.04 版本,gcc-5.4(建议至少 gcc-4.7+)。

操作步骤

步骤一:控制台操作

创建网关子设备产品,并将子设备绑定到网关设备下:

该部分操作可参考 网关子设备产品

步骤二:运行设备端程序

2.1 下载 demo 程序

GitHub下载,或执行下面的 git 命令。
git clone https://github.com/tencentyun/qcloud-iot-explorer-sdk-embedded-c.git

2.2 代码编译

1. 进入下载的 demo 程序目录下。


2. device_info.json中填入网关的设备信息,包括:productId(对应着控制台的产品ID)、deviceName(对应着控制台的设备名称)、key_deviceinfo->deviceSecret(对应着控制台的设备密钥)。


3. 在CMakeLists.txt中把FEATURE_GATEWAY_ENABLED这个参数设置为ON,然后执行./cmake_build.sh,即可在output/release/bin下面生成gateway_sim_samplesubdev_sim_sample

2.3 运行程序

1. 运行 gateway_sim_sample:
./output/release/bin/gateway_sim_sample
日志输出应该如下:
INF|2021-07-06 15:42:58|qcloud_iot_device.c|iot_device_info_set(56): SDK_Ver: 3.1.6, Product_ID: XKVP9QORAR, Device_Name: test001
DBG|2021-07-06 15:42:58|HAL_TLS_mbedtls.c|HAL_TLS_Connect(224): Setting up the SSL/TLS structure...
DBG|2021-07-06 15:42:58|HAL_TLS_mbedtls.c|HAL_TLS_Connect(266): Performing the SSL/TLS handshake...
DBG|2021-07-06 15:42:58|HAL_TLS_mbedtls.c|HAL_TLS_Connect(267): Connecting to /XKVP9QORAR.iotcloud.tencentdevices.com/8883...
DBG|2021-07-06 15:42:58|HAL_TLS_mbedtls.c|HAL_TLS_Connect(289): connected with /XKVP9QORAR.iotcloud.tencentdevices.com/8883...
INF|2021-07-06 15:42:58|mqtt_client.c|IOT_MQTT_Construct(125): mqtt connect with id: yfSdT success // 网关连接平台成功
DBG|2021-07-06 15:42:58|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(147): topicName=$gateway/operation/result/XKVP9QORAR/test001|packet_id=4444 // 订阅网关拓扑关系管理topic
DBG|2021-07-06 15:42:58|gateway_api.c|_gateway_event_handler(80): gateway sub|unsub(3) success, packet-id=4444 // 订阅成功
2. 获取子设备三元组信息,进入网关设备的子设备管理界面,单击查看按钮:

进入子设备 > 设备信息页面:


3. 在另外一个控制台内运行subdev_sim_sample,运行的格式为./output/release/bin/subdev_sim_sample -p ProductID -d DeviceName,运行之后,子设备会和gateway_sim_sample建立连接,并通知网关设备子设备上线,然后会循环上报属性。
说明:
ProductID:为子设备的产品ID。
DeviceName:为子设备的设备名称。
子设备可运行多个,打开多个控制台,每个控制台运行1个,需要指定不同的 productId 或 deviceName。
子设备的日志如下:
DBG|2021-07-06 15:49:36|subdev_sim_sample.c|main(164): connect gateway OK, subdev is going to be online // 通知网关设备子设备上线
DBG|2021-07-06 15:49:37|subdev_sim_sample.c|main(205): Report property {"subdev_type": "report","product_id": "QYEWBRB1PS","device_name": "dev1","msg": "{"method":"report","clientToken":"subdev-1871908611","timestamp":1625557777,"power_switch":0,"color":0,"brightness":0}} // 上报子设备属性到网关设备
DBG|2021-07-06 15:49:53|subdev_sim_sample.c|main(205): Report property {"subdev_type": "report","product_id": "QYEWBRB1PS","device_name": "dev1","msg": "{"method":"report","clientToken":"subdev-1836984528","timestamp":1625557793,"power_switch":0,"color":0,"brightness":0}}
DBG|2021-07-06 15:49:53|subdev_sim_sample.c|main(192): recv {"method":"report_reply","clientToken":"subdev-1836984528","code":0,"status":"success"} // 接收到平台返回的上报成功
控制台在线调试日志:

网关设备的日志如下:
DBG|2021-07-06 15:46:45|gateway_sim_sample.c|_accept_new_subdev(339): Recv conn from 127.0.0.1 29285 // 发现新的子设备
DBG|2021-07-06 15:46:47|gateway_sim_sample.c|_deal_with_subdev_msg(382): Get msg {"subdev_type":"online","product_id":"QYEWBRB1PS","device_name":"dev1"} from subdev // 子设备上线
DBG|2021-07-06 15:46:47|gateway_sim_sample.c|_deal_with_subdev_msg(402): itype is 0, pid QYEWBRB1PS dname dev1
DBG|2021-07-06 15:46:47|gateway_api.c|IOT_Gateway_Subdev_Online(191): there is no session, create a new session // 网关新增子设备管理session
DBG|2021-07-06 15:46:47|mqtt_client_publish.c|qcloud_iot_mqtt_publish(347): publish packetID=0|topicName=$gateway/operation/XKVP9QORAR/test001|payload={"type":"online","payload":{"devices":[{"product_id":"QYEWBRB1PS","device_name":"dev1"}]}} // 通知平台子设备在线
INF|2021-07-06 15:46:47|gateway_common.c|_gateway_message_handler(387): client_id(QYEWBRB1PS/dev1), online result 0
DBG|2021-07-06 15:46:47|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(147): topicName=$thing/down/property/QYEWBRB1PS/dev1|packet_id=4445 // 代理子设备订阅属性下行topic
DBG|2021-07-06 15:46:47|gateway_api.c|_gateway_event_handler(80): gateway sub|unsub(3) success, packet-id=4445
INF|2021-07-06 15:46:47|gateway_sim_sample.c|_event_handler(94): subscribe success, packet-id=4445 // 订阅成功
DBG|2021-07-06 15:46:48|gateway_sim_sample.c|_deal_with_subdev_msg(421): sub subdev OK
DBG|2021-07-06 15:47:02|gateway_sim_sample.c|_deal_with_subdev_msg(382): Get msg {"subdev_type": "report","product_id": "QYEWBRB1PS","device_name": "dev1","msg": "{"method":"report","clientToken":"subdev-1531564604","timestamp":1625557622,"power_switch":0,"color":0,"brightness":0}} from subdev // 收到子设备的消息
DBG|2021-07-06 15:47:02|gateway_sim_sample.c|_deal_with_subdev_msg(402): itype is 2, pid QYEWBRB1PS dname dev1
DBG|2021-07-06 15:47:02|gateway_sim_sample.c|_deal_with_subdev_msg(425): msg {"subdev_type": "report","product_id": "QYEWBRB1PS","device_name": "dev1","msg": "{"method":"report","clientToken":"subdev-1531564604","timestamp":1625557622,"power_switch":0,"color":0,"brightness":0}}
DBG|2021-07-06 15:47:02|gateway_sim_sample.c|_property_topic_publish(246): pid QYEWBRB1PS dname dev1
DBG|2021-07-06 15:47:02|gateway_sim_sample.c|_property_topic_publish(259): topic $thing/up/property/QYEWBRB1PS/dev1
DBG|2021-07-06 15:47:02|gateway_sim_sample.c|_property_topic_publish(260): publish msg {"method":"report","clientToken":"subdev-1531564604","timestamp":1625557622,"params":{"power_switch":0,"color":0,"brightness":0}} // 代理子设备上报属性
DBG|2021-07-06 15:47:02|mqtt_client_publish.c|qcloud_iot_mqtt_publish(347): publish packetID=0|topicName=$thing/up/property/QYEWBRB1PS/dev1|payload={"method":"report","clientToken":"subdev-1531564604","timestamp":1625557622,"params":{"power_switch":0,"color":0,"brightness":0}}
INF|2021-07-06 15:47:03|gateway_sim_sample.c|_message_handler(154): Receive Message With topicName:$thing/down/property/QYEWBRB1PS/dev1, payload:{"method":"report_reply","clientToken":"subdev-1531564604","code":0,"status":"success"} // 接收到平台返回的上报返回消息

2.4 设备控制

平台下发控制消息时,网关设备的日志如下:
INF|2021-07-06 15:51:02|gateway_sim_sample.c|_message_handler(154): Receive Message With topicName:$thing/down/property/QYEWBRB1PS/dev1, payload:{"method":"control","clientToken":"clientToken-2fd030d1-7f82-4708-ad5a-8905505ff197","params":{"power_switch":1,"color":0,"brightness":80}} // 收到子设备的下行消息,透传给子设备
DBG|2021-07-06 15:51:03|gateway_sim_sample.c|_deal_with_subdev_msg(382): Get msg {"method":"control_reply","clientToken":"clientToken-2fd030d1-7f82-4708-ad5a-8905505ff197","code":0} from subdev // 获取子设备的control回复消息
DBG|2021-07-06 15:51:03|gateway_sim_sample.c|_property_topic_publish(246): pid QYEWBRB1PS dname dev1
DBG|2021-07-06 15:51:03|gateway_sim_sample.c|_property_topic_publish(259): topic $thing/up/property/QYEWBRB1PS/dev1
DBG|2021-07-06 15:51:03|gateway_sim_sample.c|_property_topic_publish(260): publish msg {"method":"control_reply","clientToken":"clientToken-2fd030d1-7f82-4708-ad5a-8905505ff197","code":0}
DBG|2021-07-06 15:51:03|mqtt_client_publish.c|qcloud_iot_mqtt_publish(347): publish packetID=0|topicName=$thing/up/property/QYEWBRB1PS/dev1|payload={"method":"control_reply","clientToken":"clientToken-2fd030d1-7f82-4708-ad5a-8905505ff197","code":0} // 代理子设备回复control消息
子设备的日志如下:
DBG|2021-07-06 15:51:02|subdev_sim_sample.c|main(192): recv {"method":"control","clientToken":"clientToken-2fd030d1-7f82-4708-ad5a-8905505ff197","params":{"power_switch":1,"color":0,"brightness":80}} // 从网关设备透传下来的控制消息
[ lighting ]|[color: RED ]|[brightness:||||||||||||||||----] // 执行控制操作
DBG|2021-07-06 15:51:07|subdev_sim_sample.c|main(205): Report property {"subdev_type": "report","product_id": "QYEWBRB1PS","device_name": "dev1","msg": "{"method":"report","clientToken":"subdev-2058913913","timestamp":1625557867,"power_switch":1,"color":0,"brightness":80}} // 上报更新之后的属性
子设备在运行一段时间后,会离线,离线后网关设备会代理子设备向平台发送offline消息,网关设备的日志如下:
DBG|2021-07-06 16:01:30|gateway_sim_sample.c|_subdev_leave(371): unsubscribe $thing/down/property/QYEWBRB1PS/dev1 return 10391 // 解订阅对应的子设备下行topic
DBG|2021-07-06 16:01:30|mqtt_client_publish.c|qcloud_iot_mqtt_publish(347): publish packetID=0|topicName=$gateway/operation/XKVP9QORAR/test001|payload={"type":"offline","payload":{"devices":[{"product_id":"QYEWBRB1PS","device_name":"dev1"}]}} // 通知子设备离线
DBG|2021-07-06 16:01:30|gateway_api.c|_gateway_event_handler(80): gateway sub|unsub(6) success, packet-id=10391 // 解订阅成功
INF|2021-07-06 16:01:30|gateway_sim_sample.c|_event_handler(109): unsubscribe success, packet-id=10391
说明:
网关和子设备之间使用了本地 TCP socket 通信来模拟,在实际的场景下,可以换成 BLE 通信、ZigBee 通信等等。
子设备只演示了 Property 上行和下行操作,如果要使用 Event 和 Action,可以参考 light_data_template_sample.c 中的代码添加到网关设备和子设备的程序中,网关设备需要添加代理订阅对应下行 topic 的代码,子设备则需要添加上下行消息的处理代码。

步骤三:小程序绑定、控制子设备

前提条件:前面已经成功运行了一个设备端的网关子设备示例程序,需要使用腾讯连连小程序添加、控制网关子设备。
1. 打开微信,搜索“腾讯连连”小程序,打开“扫一扫”。



2. 进入网关设备的设备调试页面,单击设备二维码,用手机扫码添加该设备。


3. 可以看到网关和子设备均被添加到小程序家庭下:



4. 此时我们可以通过进入设备控制面板,控制子设备状态,同时可在云端查看设备日志:


至此,我们就完成了网关子设备设备端 demo 程序的运行、云端调试、小程序控制的网关子设备入门体验。