用户参考自定义透传的智能灯示例,定义目标产品的二进制数据协议,编写对应的上下行数据的解析脚本,设备侧即可基于二进制实现和平台交互。 ### 1.创建自定义透传产品,同时选择智能灯产品类型 ![](https://main.qcloudimg.com/raw/e385b9e53e2b510df5da93c2977919cb.png) ### 2. 使能二进制数据解析功能 ![](https://main.qcloudimg.com/raw/22f6e4797466f41e17111b61f9c51072.png) ### 3. 智能灯二进制协议格式 - 上报格式 ![](https://main.qcloudimg.com/raw/d4736c9fe19da201d65e80cb72ab17d2.png) - 下发格式 ![](https://main.qcloudimg.com/raw/67ed266e814783adef513dcbf764fbbc.png) ### 4. 编辑上下行二进制数据与Json数据转换的解析脚本并提交 - 目前解析脚本暂时只支持JavaScript语法 ![](https://main.qcloudimg.com/raw/2e6d2b20723d7567f64fab7889d8e87a.png) 对应的脚本代码如下: - 上行二进制数据到Json数据的解析脚本 ``` function RawToProtocol(fPort, bytes) { var data = { "method": "report", "clientToken": new Date(), "params":{} }; if (bytes[2] != 1) { return data ; } var sw = bytes[8]; if (sw == 0 || sw == 1) { data.params.power_switch = sw; } var color = bytes[9]; if (color >=0 && color < 4 ) { data.params.color = color; } var brightness = bytes[10]; if (brightness >=0 && brightness <=100 ) { data.params.brightness = brightness; } return data; } ``` - 下行Json数据到二进制数据的解析脚本 ``` function ProtocolToRaw(json) { var data = new Array(); var method = json['method']; if (method !="control") { return data; } data[0] = 0xaa;// header 0x55aa data[1] = 0x55; data[2] = 0x20; // control commandb data[3] = 0; // padding //requestid 这里随便填了 data[4] = 0xcc; data[5] = 0xcc; data[6] = 0xcc; data[7] = 0xcc; var params = json['params']; if (params.power_switch != undefined) { data[8] = params.power_switch ; //switch } else { data[8] = 255; } if (params.color != undefined) { data[9] = params.color ; //color } else { data[9] = 255; } if (params.brightness != undefined) { data[10] = params.brightness ; //brightness } else { data[10] = 255; } data[11] = 255 ; //name is invalid return data; } ``` - 可以在线调试脚本的解析功能 ![](https://main.qcloudimg.com/raw/6ce7da31c2442f7967cb89edd20ed09b.png) ### 5. 编译SDK并运行二进制示例 - 带参数 -l 二进制示例会循环执行,示例会定时上报二进制数据,收到二进制数据会dump出来。 ``` ./raw_data_mqtt_sample -l INF|2020-02-13 01:41:55|qcloud_iot_device.c|iot_device_info_set(65): SDK_Ver: 3.1.1, Product_ID: NQ7GQN6KSR, Device_Name: dev001 DBG|2020-02-13 01:41:55|HAL_TLS_mbedtls.c|HAL_TLS_Connect(206): Setting up the SSL/TLS structure... DBG|2020-02-13 01:41:55|HAL_TLS_mbedtls.c|HAL_TLS_Connect(248): Performing the SSL/TLS handshake... DBG|2020-02-13 01:41:55|HAL_TLS_mbedtls.c|HAL_TLS_Connect(249): Connecting to /nq7gqn6ksr.iotcloud.tencentdevices.com/8883... INF|2020-02-13 01:41:56|HAL_TLS_mbedtls.c|HAL_TLS_Connect(271): connected with /nq7gqn6ksr.iotcloud.tencentdevices.com/8883... INF|2020-02-13 01:41:56|mqtt_client.c|IOT_MQTT_Construct(127): mqtt connect with id: 9Z39s success INF|2020-02-13 01:41:56|raw_data_sample.c|main(306): Cloud Device Construct Success DBG|2020-02-13 01:41:56|mqtt_client_subscribe.c|qcloud_iot_mqtt_subscribe(141): topicName=$thing/down/raw/NQ7GQN6KSR/dev001|packet_id=15330 INF|2020-02-13 01:41:56|raw_data_sample.c|_mqtt_event_handler(95): subscribe success, packet-id=15330 DBG|2020-02-13 01:41:57|raw_data_sample.c|_publish_raw_data_msg(223): raw_data published dump: AA 55 01 00 00 00 00 00 01 01 38 00 DBG|2020-02-13 01:41:57|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$thing/up/raw/NQ7GQN6KSR/dev001|payload= DBG|2020-02-13 01:42:02|raw_data_sample.c|_publish_raw_data_msg(223): raw_data published dump: AA 55 01 00 01 00 00 00 01 00 54 00 DBG|2020-02-13 01:42:02|mqtt_client_publish.c|qcloud_iot_mqtt_publish(340): publish packetID=0|topicName=$thing/up/raw/NQ7GQN6KSR/dev001|payload= ``` - 收到下行数据并dump ``` INF|2020-02-13 01:47:23|raw_data_sample.c|on_raw_data_message_callback(235): Receive Message With topicName:$thing/down/raw/NQ7GQN6KSR/dev001, payloadlen:12 DBG|2020-02-13 01:47:23|raw_data_sample.c|on_raw_data_message_callback(237): raw_data reveived dump: AA 55 20 00 CC CC CC CC FF 01 FF FF INF|2020-02-13 01:47:23|raw_data_sample.c|on_raw_data_message_callback(235): Receive Message With topicName:$thing/down/raw/NQ7GQN6KSR/dev001, payloadlen:12 DBG|2020-02-13 01:47:23|raw_data_sample.c|on_raw_data_message_callback(237): raw_data reveived dump: AA 55 20 00 CC CC CC CC FF 01 FF FF ``` ### 6. 控制台日志 - 在线调试 ![](https://main.qcloudimg.com/raw/5e8dd1b8efdc079a72ffb8d9e3b52830.png) - 设备日志 ![](https://main.qcloudimg.com/raw/9ee577ed46f1170531d444703c5968f9.png)