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
- {
- "name": "aliyun-iot",
- "dependencies": {
- "aliyun-iot-mqtt": "^0.0.4"
- },
- "author": "wongxming",
- "license": "MIT"
- }
2.2 應用程序代碼
ui
複製代碼阿里雲
- /**
- * node iot_raw_data.js
- */
- const mqtt = require('aliyun-iot-mqtt');
- const COMMAND_REPORT = 0x00;
- //設備屬性
- const options = {
- productKey: "替換本身productKey",
- deviceName: "替換本身deviceName",
- deviceSecret: "替換本身deviceSecret",
- regionId: "cn-shanghai"
- };
- //創建鏈接
- const client = mqtt.getAliyunIotMqttClient(options);
- //屬性上報的Topic
- const topic = `/sys/${options.productKey}/${options.deviceName}/thing/model/up_raw`;
- setInterval(function() {
- //發佈數據到topic
- client.publish(topic, getPostData());
- }, 5 * 1000);
- function getPostData() {
- var temperature = Math.floor((Math.random() * 20) + 10)
- var humidity = Math.floor((Math.random() * 80) + 20)
- var payloadArray = [];
- /**
- * 按照自定義協議格式拼接 raw data
- * 00|5b0232e1|41200000|42b00000
- * command(1)|requestId(4)|temperature(4)|humidity(4)
- */
- //command字段
- payloadArray = payloadArray.concat(buffer_uint8(COMMAND_REPORT));
- // id
- payloadArray = payloadArray.concat(buffer_int32(parseInt(new Date().getTime() / 1000)));
- // 屬性'temperature'的值
- payloadArray = payloadArray.concat(buffer_float32(temperature));
- // 屬性'humidity'的值
- payloadArray = payloadArray.concat(buffer_float32(humidity));
- return new Buffer(payloadArray);
- }
- //uint8
- function buffer_uint8(value) {
- var uint8Array = new Uint8Array(1);
- var dv = new DataView(uint8Array.buffer, 0);
- dv.setUint8(0, value);
- return [].slice.call(uint8Array);
- }
- //int32
- function buffer_int32(value) {
- var uint8Array = new Uint8Array(4);
- var dv = new DataView(uint8Array.buffer, 0);
- dv.setInt32(0, value);
- return [].slice.call(uint8Array);
- }
- //float32
- function buffer_float32(value) {
- var uint8Array = new Uint8Array(4);
- var dv = new DataView(uint8Array.buffer, 0);
- dv.setFloat32(0, value);
- return [].slice.call(uint8Array);
- }
3. 雲端數據解析腳本配置
高級版產品詳情>數據解析
完整JS腳本以下:
spa
複製代碼調試
- var COMMAND_REPORT = 0x00;
- var PROPERTY_REPORT_METHOD = 'thing.event.property.post';
- /*
- 示例數據:
- 傳入參數 ->
- 005b0232e14120000042b00000
- |command(1)|requestId(4)|temperature(4)|humidity(4)|
- 輸出結果 ->
- {
- "method": "thing.event.property.post",
- "id": 1526870753,
- "params": {
- "temperature": 10,
- "humidity": 88
- },
- "version": "1.0"
- }
- */
- //上行數據,自定義二進制轉物模型json
- function rawDataToProtocol(bytes) {
- var uint8Array = new Uint8Array(bytes.length);
- for (var i = 0; i < bytes.length; i++) {
- uint8Array[i] = bytes[i] & 0xff;
- }
- var dataView = new DataView(uint8Array.buffer, 0);
- var jsonMap = new Object();
- var fHead = uint8Array[0]; // command
- if (fHead == COMMAND_REPORT) {
- //屬性上報method
- jsonMap['method'] = PROPERTY_REPORT_METHOD;
- //協議版本號固定字段
- jsonMap['version'] = '1.0';
- //標示該次請求id值
- jsonMap['id'] = dataView.getInt32(1);
- var params = {};
- //對應產品屬性中 temperature
- params['temperature'] = dataView.getFloat32(5);
- //對應產品屬性中 humidity
- params['humidity'] = dataView.getFloat32(9);
- jsonMap['params'] = params;
- }
- return jsonMap;
- }
- //下行指令,物模型json轉二進制格式
- function protocolToRawData(json) {
- }
4. 運行
啓動設備模擬程序:blog
複製代碼ci
- $node iot_raw_data.js
IoT控制檯 產品詳情-> 在線調試 觀察上報數據:
設備詳情->運行狀態