設備上報二進制數據在雲端配置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控制檯 產品詳情-> 在線調試 觀察上報數據:

設備詳情->運行狀態

閱讀後請點擊

相關文章
相關標籤/搜索