-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master-mirror' into 'master' (merge request !16)
merge TME&gateway_scenario
- Loading branch information
Showing
30 changed files
with
1,949 additions
and
519 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,5 @@ | |
sdk-tests/certs/unittest_private.key | ||
sdk-tests/certs/unittest_private.key | ||
.vscode/settings.json | ||
.vs/ | ||
/mplayer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# 网关子设备场景示例说明 | ||
本文档将讲述如何在腾讯物联网开发平台(IoT Explorer)控制台申请网关设备并绑定子设备, 并通过C-SDK中的**gateway_sim_sample**,展示网关设备如何管理子设备**subdev_sim_sample**,并代理子设备的上下行消息。 | ||
|
||
## 一. 创建网关设备和子设备 | ||
这部分操作可参考[网关设备快速入门](./网关设备快速入门.md) | ||
|
||
## 二. 运行示例 | ||
### 1. 编译 | ||
在CMakeLists.txt中把FEATURE_GATEWAY_ENABLED这个参数设置为ON,然后执行./cmake_build.sh,即可在output/release/bin下面生成gateway_sim_sample和subdev_sim_sample。 | ||
### 2. 运行 | ||
(1) 在device_info.json中填入网关的设备信息,包括:productId(对应着控制台的产品ID)、deviceName(对应着控制台的设备名称)、key_deviceinfo->deviceSecret(对应着控制台的设备密钥); | ||
(2) 在控制台运行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 // 订阅成功 | ||
``` | ||
(3) 在另外一个控制台运行subdev_sim_sample,运行的格式为./output/release/bin/subdev_sim_sample -p QYEWBRB1PS -d dev1,其中-p后接的是子设备的产品ID,-d后是的子设备的设备名称,运行之后,子设备会和gateway_sim_sample建立连接,并通知网关设备子设备上线,然后会循环上报属性。子设备可运行多个,打开多个控制台,每个控制台运行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"} // 接收到平台返回的上报返回消息 | ||
``` | ||
下发控制消息时,网关设备的日志如下: | ||
``` | ||
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 | ||
``` | ||
## 三. 代码说明 | ||
1. 网关和子设备之间使用了本地TCP socket通信来模拟,在实际的场景下,可以换成BLE通信、ZigBee通信等等; | ||
2. 子设备只演示了Property上行和下行操作,如果要使用Event和Action,可以参考light_data_template_sample.c中的代码添加到网关设备和子设备的程序中,网关设备需要添加代理订阅对应下行topic的代码,子设备则需要添加上下行消息的处理代码。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.