共计 2634 个字符,预计需要花费 7 分钟才能阅读完成。
1. 筹备工作
1.1 注册阿里云账号
应用集体淘宝账号或手机号,开明阿里云账号,并通过__实名认证 (能够用支付宝认证)__
1.2 收费开明 IoT 物联网套件
产品官网 https://www.aliyun.com/product/iot
1.3 软件环境
Nodejs 装置 https://nodejs.org/en/download/
编辑器 sublimeText/nodepad++/vscode
MQTT lib https://www.npmjs.com/package/mqtt
2. 开发步骤
2.1 云端开发
1) 创立高级版产品
2) 性能定义,产品物模型增加属性
增加产品属性定义
物模型对应属性上报 topic
/sys/ 替换为 productKey/ 替换为 deviceName/thing/event/property/post
物模型对应的属性上报 payload
{ | |
id: 123452452, | |
params: { | |
temperature: 26.2, | |
humidity: 60.4 | |
}, | |
method: "thing.event.property.post" | |
} |
3) 设施治理 > 注册设施,取得身份三元组
2.2 设施端开发
咱们以 nodejs 程序来模仿设施,建设连贯,上报数据。
1. 创立文件夹 aliyun-iot-demo-nodejs | |
2. 进入文件夹,创立 package.json 文件,增加内容 | |
3. 执行 npm install 命令,装置 sdk | |
4. 创立 thermometer.js 文件,增加内容 | |
5. 执行 node thermometer.js 命令 |
1) package.json 增加阿里云 IoT 套件 sdk 依赖
{ | |
"name": "aliyun-iot", | |
"dependencies": {"mqtt": "2.18.8"}, | |
"author": "wongxming", | |
"license": "MIT" | |
} |
2) 下载安装 SDK
在 aliyun-iot-demo-nodejs 文件夹下,执行命令
$ npm install
3) 应用程序目录构造
4) 模仿设施 thermometer.js 代码
/** | |
"dependencies": {"mqtt": "2.18.8"} | |
*/ | |
const crypto = require('crypto'); | |
const mqtt = require('mqtt'); | |
// 设施身份三元组 + 区域 | |
const deviceConfig = require("./iot-device-config.json"); | |
const options = { | |
productKey: deviceConfig.productKey, | |
deviceName: deviceConfig.deviceName, | |
timestamp: Date.now(), | |
clientId: Math.random().toString(36).substr(2) | |
} | |
options.password = signHmacSha1(options, deviceConfig.deviceSecret); | |
options.clientId = `${options.clientId}|securemode=3,signmethod=hmacsha1,timestamp=${options.timestamp}|`; | |
options.username = `${options.deviceName}&${options.productKey}`; | |
const url = `tcp://${deviceConfig.productKey}.iot-as-mqtt.${deviceConfig.regionId}.aliyuncs.com:1883`; | |
// 建设连贯 | |
const client = mqtt.connect(url,options); | |
// 属性上报的 Topic | |
const topic = `/sys/${deviceConfig.productKey}/${deviceConfig.deviceName}/thing/event/property/post`; | |
setInterval(function() { | |
// 公布数据到 topic | |
client.publish(topic, getPostData()); | |
}, 5 * 1000); | |
function getPostData(){ | |
const payloadJson = {id: Date.now(), | |
params: {temperature: Math.floor((Math.random() * 20) + 10), | |
humidity: Math.floor((Math.random() * 40) + 60) | |
}, | |
method: "thing.event.property.post" | |
} | |
console.log("===postData topic=" + topic) | |
console.log(payloadJson) | |
return JSON.stringify(payloadJson); | |
} | |
/* | |
生成基于 HmacSha1 的 password | |
参考文档:https://help.aliyun.com/document_detail/73742.html?#h2-url-1 | |
*/ | |
function signHmacSha1(options, deviceSecret) {let keys = Object.keys(options).sort(); | |
// 按字典序排序 | |
keys = keys.sort(); | |
const list = []; | |
keys.map((key) => {list.push(`${key}${options[key]}`); | |
}); | |
const contentStr = list.join(''); | |
return crypto.createHmac('sha1', deviceSecret).update(contentStr).digest('hex'); | |
} |
设施配置文件
{ | |
"productKey": "替换 productKey", | |
"deviceName": "替换 deviceName", | |
"deviceSecret": "替换 deviceSecret", | |
"regionId": "cn-shanghai" | |
} |
3. 启动运行
3.1 设施启动
$ node thermometer.js
3.2 云端查看设施运行状态
物联网平台产品介绍详情:https://www.aliyun.com/product/iot/iot_instc_public_cn
阿里云物联网平台客户交换群
正文完
发表至: javascript
2023-03-15