设备上报二进制数据在云端配置JS脚本解析

1. 阿里云IoT控制台配置



1.1 开通阿里云IoT物联网套件


IoT物联网套件官网地址

1.2 建立高级版产品,选择数据透传





1.3 添加产品属性定义


 node


属性名

标识符

数据类型

温度

temperature

float

湿度

humidity

float





1.4 自定义payload二进制格式


 json


1位

2-5位

6-9位

9-13位

00

5b0232e1

41200000

42b00000

command

requestId

temperature

humidity



2.设备端代码



2.1 package.json引入sdk
 dom

复制代码post

  1. {
  2.   "name": "aliyun-iot",
  3.   "dependencies": {
  4.     "aliyun-iot-mqtt": "^0.0.4"
  5.   },
  6.   "author": "wongxming",
  7.   "license": "MIT"
  8. }




2.2 应用程序代码

 ui

复制代码阿里云

  1. /**
  2. * node iot_raw_data.js
  3. */
  4. const mqtt = require('aliyun-iot-mqtt');
  5. const COMMAND_REPORT = 0x00;
  6. //设备属性
  7. const options = {
  8.     productKey: "替换本身productKey",
  9.     deviceName: "替换本身deviceName",
  10.     deviceSecret: "替换本身deviceSecret",
  11.     regionId: "cn-shanghai"
  12. };
  13. //创建链接
  14. const client = mqtt.getAliyunIotMqttClient(options);
  15. //属性上报的Topic
  16. const topic = `/sys/${options.productKey}/${options.deviceName}/thing/model/up_raw`;
  17. setInterval(function() {
  18.     //发布数据到topic
  19.     client.publish(topic, getPostData());
  20. }, 5 * 1000);
  21. function getPostData() {
  22.     var temperature = Math.floor((Math.random() * 20) + 10)
  23.     var humidity = Math.floor((Math.random() * 80) + 20)
  24.     var payloadArray = [];
  25.     /**
  26.      * 按照自定义协议格式拼接 raw data
  27.      * 00|5b0232e1|41200000|42b00000
  28.      * command(1)|requestId(4)|temperature(4)|humidity(4)
  29.      */
  30.     //command字段
  31.     payloadArray = payloadArray.concat(buffer_uint8(COMMAND_REPORT));
  32.     // id
  33.     payloadArray = payloadArray.concat(buffer_int32(parseInt(new Date().getTime() / 1000)));
  34.     // 属性'temperature'的值
  35.     payloadArray = payloadArray.concat(buffer_float32(temperature));
  36.     // 属性'humidity'的值
  37.     payloadArray = payloadArray.concat(buffer_float32(humidity));
  38.     return new Buffer(payloadArray);
  39. }
  40. //uint8
  41. function buffer_uint8(value) {
  42.     var uint8Array = new Uint8Array(1);
  43.     var dv = new DataView(uint8Array.buffer, 0);
  44.     dv.setUint8(0, value);
  45.     return [].slice.call(uint8Array);
  46. }
  47. //int32
  48. function buffer_int32(value) {
  49.     var uint8Array = new Uint8Array(4);
  50.     var dv = new DataView(uint8Array.buffer, 0);
  51.     dv.setInt32(0, value);
  52.     return [].slice.call(uint8Array);
  53. }
  54. //float32
  55. function buffer_float32(value) {
  56.     var uint8Array = new Uint8Array(4);
  57.     var dv = new DataView(uint8Array.buffer, 0);
  58.     dv.setFloat32(0, value);
  59.     return [].slice.call(uint8Array);
  60. }




3. 云端数据解析脚本配置


高级版产品详情>数据解析


完整JS脚本以下:
 spa

复制代码调试

  1. var COMMAND_REPORT = 0x00;
  2. var PROPERTY_REPORT_METHOD = 'thing.event.property.post';
  3. /*
  4. 示例数据:
  5. 传入参数 ->
  6.     005b0232e14120000042b00000
  7. |command(1)|requestId(4)|temperature(4)|humidity(4)|
  8. 输出结果 ->
  9. {
  10.   "method": "thing.event.property.post",
  11.   "id": 1526870753,
  12.   "params": {
  13.     "temperature": 10,
  14.     "humidity": 88
  15.   },
  16.   "version": "1.0"
  17. }
  18. */
  19. //上行数据,自定义二进制转物模型json
  20. function rawDataToProtocol(bytes) {
  21.     var uint8Array = new Uint8Array(bytes.length);
  22.     for (var i = 0; i < bytes.length; i++) {
  23.         uint8Array[i] = bytes[i] & 0xff;
  24.     }
  25.     var dataView = new DataView(uint8Array.buffer, 0);
  26.     var jsonMap = new Object();
  27.     var fHead = uint8Array[0]; // command
  28.     if (fHead == COMMAND_REPORT) {
  29.         //属性上报method
  30.         jsonMap['method'] = PROPERTY_REPORT_METHOD;
  31.         //协议版本号固定字段
  32.         jsonMap['version'] = '1.0';
  33.         //标示该次请求id值
  34.         jsonMap['id'] = dataView.getInt32(1);
  35.         var params = {};
  36.         //对应产品属性中 temperature
  37.         params['temperature'] = dataView.getFloat32(5);
  38.         //对应产品属性中 humidity
  39.         params['humidity'] = dataView.getFloat32(9);
  40.         jsonMap['params'] = params;
  41.     }
  42.     return jsonMap;
  43. }
  44. //下行指令,物模型json转二进制格式
  45. function protocolToRawData(json) {
  46.     
  47. }




4. 运行


启动设备模拟程序:blog

复制代码ci

  1. $node iot_raw_data.js



IoT控制台 产品详情-> 在线调试 观察上报数据:

设备详情->运行状态

阅读后请点击

相关文章
相关标签/搜索