用户参考自定义透传的智能灯示例,定义目标产品的二进制数据协议,编写对应的上下行数据的解析脚本,设备侧即可基于二进制实现和平台交互。
对应的脚本代码如下:
上行二进制数据到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;
}
带参数 -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