首页 > IT职业 > IT职场 > 10分钟物联网设备接入阿里云IoT平台
使用个人淘宝账号或手机号,开通阿里云账号,并通过实名认证(可以用支付宝认证)
产品官网 https://www.aliyun.com/product/iot

Nodejs安装 https://nodejs.org/en/download/
编辑器 sublimeText/nodepad++/vscode



我们以nodejs程序来模拟设备,建立连接,上报数据。

{
"name": "aliyun-iot",
"dependencies": {
"aliyun-iot-mqtt": "^0.0.4"
},
"author": "wongxming",
"license": "MIT"
} 下载安装SDK
$npm install /**
"dependencies": { "aliyun-iot-mqtt": "^0.0.4" }
*/
const mqtt = require('aliyun-iot-mqtt');
//设备属性
const options = {
productKey: "产品productKey",
deviceName: "设备deviceName",
deviceSecret: "设备的secret",
regionId: "cn-shanghai"
};
//建立连接
const client = mqtt.getAliyunIotMqttClient(options);
//属性上报的Topic
const topic = `/sys/${options.productKey}/${options.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() * 20) + 60)
},
method: "thing.event.property.post"
}
console.log("===postData topic=" + topic)
console.log(payloadJson)
return JSON.stringify(payloadJson);
} 3. 启动运行 $node thermometer.js 
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1595/viewspace-2809351/ ,如需转载,请注明出处,否则将追究法律责任。