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控制台 产品详情-> 在线调试 观察上报数据:
设备详情->运行状态