The content of this page has been automatically translated by AI. If you encounter any problems while reading, you can view the corresponding content in Chinese.
If you need to connect the gateway and sub-device type devices to the IoT Explorer, the gateway device is directly connected to the cloud platform, and the sub-devices are bound, go online or offline, and communicate with the platform via the proxy of the gateway, thereby realizing real-time control of the gateway and sub-devices. This document mainly helps you quickly get familiar with the entire process of gateway and sub-device integration.
Prerequisites
To quickly understand the integration process of gateway subdevice type devices through the following steps, the following preparations are required:
Apply for IoT Explorer platform services.
Have a physical or virtual Linux environment that can compile and run sample programs.
Run the sample program in the Linux environment for testing and verification. It is mainly based on Ubuntu 16.04. gcc-5.4 is used (gcc-4.7+ is recommended at least).
Operation Steps
Step 1: Console Operations
Create Gateway Subdevice Products and Bind Sub-Devices to Gateway Devices
1. Enter the program directory of the downloaded demo.
2. Fill in the gateway's equipment information in device_info.json, including: productId (corresponding to the product ID in the console), deviceName (corresponding to the device name in the console), key_deviceinfo->deviceSecret (corresponding to the device secret in the console).
3. Set the parameter FEATURE_GATEWAY_ENABLED to ON in CMakeLists.txt, and then execute ./cmake_build.sh. Then gateway_sim_sample and subdev_sim_sample can be generated under output/release/bin.
INF|2021-07-0615:42:58|mqtt_client.c|IOT_MQTT_Construct(125): mqtt connect withid: yfSdT successfully // Gateway successfully connected to the connection platform
DBG|2021-07-0615:42:58|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(147): topicName=$gateway/operation/result/XKVP9QORAR/test001|packet_id=4444// Subscribe to the gateway topology management topic
2. Retrieve the triplet information of the sub-device, enter the sub-device management interface of the gateway device, and click the View button:
Enter Subdevice > Device Information Page:
3. Run subdev_sim_sample in another console. The running format is ./output/release/bin/subdev_sim_sample -p ProductID -d DeviceName. After running, the sub-device will establish a connection with gateway_sim_sample, notify the gateway device that the sub-device is online, and then cyclically report attributes.
Note:
ProductID: The product ID of the subdevice.
Note: DeviceName: The device name of the subdevice.
Subdevices can run multiple. Open multiple consoles. Each console runs one. It requires specifying different productIds or deviceNames.
Logs of the sub-device are as follows:
DBG|2021-07-0615:49:36|subdev_sim_sample.c|main(164): connect gateway OK, notifying gateway that sub-device is going to be online
DBG|2021-07-0615: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}}// Report sub-device properties to the gateway device
DBG|2021-07-0615:49:53|subdev_sim_sample.c|main(192): recv {"method":"report_reply","clientToken":"subdev-1836984528","code":0,"status":"success"}// Receiving successful reporting response from the platform
Console online debugging logs:
Logs of the gateway device are as follows:
DBG|2021-07-0615:46:45|gateway_sim_sample.c|_accept_new_subdev(339): Recv conn from 127.0.0.129285// Detect a new sub-device
DBG|2021-07-0615:46:47|gateway_sim_sample.c|_deal_with_subdev_msg(382): Get msg {"subdev_type":"online","product_id":"QYEWBRB1PS","device_name":"dev1"} from subdev // Sub-device online
DBG|2021-07-0615:46:47|gateway_sim_sample.c|_deal_with_subdev_msg(402): itype is 0, pid QYEWBRB1PS dname dev1
DBG|2021-07-0615:46:47|gateway_api.c|IOT_gateway_Subdev_Online(191): no session exists, create a newsession// gateway adds a new sub-device management session
DBG|2021-07-0615: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"}]}}// Notify the platform that the sub-device is online
INF|2021-07-0615:46:47|gateway_common.c|_gateway_message_handler(387):client_id(QYEWBRB1PS/dev1), online result 0
DBG|2021-07-0615:46:47|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(147): topicName=$thing/down/property/QYEWBRB1PS/dev1|packet_id=4445// Broker sub-device subscription to property downstream topic
DBG|2021-07-0615:46:48|gateway_sim_sample.c|_deal_with_subdev_msg(421): sub subdev OK
DBG|2021-07-0615: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 Received a message from the sub-device
DBG|2021-07-0615:47:02|gateway_sim_sample.c|_deal_with_subdev_msg(402): itype is 2, pid QYEWBRB1PS dname dev1
INF|2021-07-0615:47:03|gateway_sim_sample.c|_message_handler(154): Receiving message With topicName:$thing/down/property/QYEWBRB1PS/dev1,payload:{"method":"report_reply","clientToken":"subdev-1531564604","code":0,"status":"success"}// Receiving the return message of the report from the platform
2.4 Device Control
When the platform sends control messages, the logs of the gateway device are as follows:
INF|2021-07-0615: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}}// Received a downstream message from the sub-device, pass through to the sub-device
DBG|2021-07-0615: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 sub-device // Get control reply message from sub-device
DBG|2021-07-0615: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}// Proxy subdevice replies to control message
Logs of the sub-device are as follows:
DBG|2021-07-0615: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}}// Control message passed through from the gateway device
[lighting][color:RED][brightness:||||||||||||||----]// Perform execution control operation
DBG|2021-07-0615: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}}// Report attributes after updating
After running for a while, the sub-device will go offline. After going offline, the gateway device will proxy the sub-device to send an offline message to the platform. The logs of the gateway device are as follows:
DBG|2021-07-0616:01:30|gateway_sim_sample.c|_subdev_leave(371): unsubscribe $thing/down/property/QYEWBRB1PS/dev1 returned 10391// Unsubscribe from the corresponding sub-device downstream topic
The gateway and subdevices use local TCP socket communication for simulation. In practice, it can be replaced with BLE communication, ZigBee communication, etc.
Subdevices only demonstrate Property uplink and downlink operations. If you want to use Event and Action, you can refer to the code in light_data_template_sample.c and add it to the programs of the gateway device and subdevices. The gateway device needs to add the code for proxy subscription to the corresponding downlink topic. Subdevices need to add the code for handling uplink and downlink messages.
Step 3: Bind and Control Subdevices Via Mini Program
Prerequisite: A sample program for the gateway subdevice on the device side has already been successfully executed. You need to use the Tencent Lianlian Mini Program to add and control the gateway subdevice.
1. Open WeChat, search for the "Tencent Lianlian" mini program, and turn on scan.
2. Enter the device debugging page of the gateway device, click the Device QR Code, and use mobile phone scan code to add the device.
3. You can see that both the gateway and the sub-device have been added to the family of the mini program:
4. At this point, we can control the status of the sub-device by entering the device control panel. Meanwhile, we can view the device log in the cloud:
So far, we have completed the running of the gateway subdevice device-side demo program, cloud debugging, and the beginner experience of controlling the gateway subdevice via the mini program.