屬性名key | 屬性值value | 描述 |
---|---|---|
tag | 西溪園區 1-2-56 | 自定義設備位置 |
imei | XIXI2018034532 | 自定義設備序列號 |
Topic
|
權限
|
描述
|
/productKey/${deviceName}/data
|
發佈
|
上報數據
payload示例 {"temperature":23,"humidity":63}
|
/productKey/${deviceName}/control
|
訂閱
|
下行指令
payload示例 {"device": "iotLed","state": "on"}
|
開通FC函數計算服務www.aliyun.com/product/fcjavascript
const https = require('https');
//釘釘羣機器人token
const accessToken = '此處是釘釘羣機器人的token';
module.exports.handler = function(event, context, callback) {
var eventJson = JSON.parse(event.toString());
const postData = JSON.stringify({
"msgtype": "markdown",
"markdown": {
"title": "溫溼度傳感器",
"text": "#### 溫溼度傳感器上報\n" +
"> 設備位置:" + eventJson.tag + "\n\n" +
"> 設備編號:" + eventJson.imei+ "\n\n" +
"> 實時溫度:" + eventJson.temperature + "℃\n\n" +
"> 相對溼度:" + eventJson.humidity + "%\n\n" +
"> ###### " + eventJson.time + " 發佈 by [物聯網套件](https://www.aliyun.com/product/iot) \n"
},
"at": {
"isAtAll": false
}
});
const options = {
hostname: 'oapi.dingtalk.com',
port: 443,
path: '/robot/send?access_token='+accessToken,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
};
const req = https.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {});
res.on('end', () => {
callback(null, 'success');
});
});
req.on('error', (e) => {
callback(e);
});
// 寫入數據請求主體
req.write(postData);
req.end();
};
複製代碼
deviceName() as deviceName ,
timestamp('yyyy-MM-dd HH:mm:ss') as time,
attribute('tag') as tag,attribute('imei') as imei,
humidity, temperature
複製代碼
產品/+/data +通配符,表明產品下全量設備都使用這個規則
複製代碼
模擬設備的nodejs腳本iot-fc-dingtalk.jsjava
/** * package.json 添加依賴:"aliyun-iot-mqtt": "0.0.4" */
const mqtt = require('aliyun-iot-mqtt');
//設備三元組
const options = {
productKey: "產品",
deviceName: "設備",
deviceSecret: "祕鑰",
regionId: "cn-shanghai"
};
//設備與雲 創建鏈接,設備上線
const client = mqtt.getAliyunIotMqttClient(options);
//主題topic
const topic = `${options.productKey}/${options.deviceName}/data`;
const data = {
temperature: Math.floor((Math.random()*20)+10),
humidity: Math.floor((Math.random()*100)+20),
};
//指定topic發佈數據到雲端
client.publish(topic, JSON.stringify(data));
const subTopic = "/" + options.productKey + "/" + options.deviceName + "/control";
//訂閱topic
client.subscribe(subTopic)
//添加topic處理函數
client.on('message', function (topic, message){
console.log(topic + "," + message.toString())
})
複製代碼
啓動虛擬設備腳本node
$node iot-fc-dingtalk.js
複製代碼
"ruff": {
"dependencies": {
"aliyun-iot-device-mqtt": "^0.0.5",
},
"version": 1
}
複製代碼
// 引入aliyun-iot-sdk
var MQTT = require('aliyun-iot-device-mqtt');
// 我的帳號
var options = {
productKey: "",//替換爲本身的
deviceName: "",//替換爲本身的
deviceSecret: "",//替換爲本身的
regionId: "cn-shanghai",//華東2
};
// 發佈/訂閱 topic
var pubTopic = "/" + options.productKey + "/" + options.deviceName + "/data";
var subTopic = "/" + options.productKey + "/" + options.deviceName + "/control";
// 創建鏈接
var client = MQTT.createAliyunIotMqttClient(options);
$.ready(function(error) {
if (error) {
console.log(error);
return;
}
//10s上報一次
setInterval(publishData, 15 * 1000);
//訂閱topic
client.subscribe(subTopic)
//添加topic處理函數
client.on('message', doHandler)
});
//上報溫溼度
function publishData() {
$('#humirature').getTemperature(function(error, temperature) {
if (error) {
console.error(error);
return;
}
$('#humirature').getRelativeHumidity(function(error, humidity) {
if (error) {
console.error(error);
return;
}
var data = {
"temperature": temperature,//溫度
"humidity": humidity //溼度
};
console.log(JSON.stringify(data))
//發佈topic,上報數據
client.publish(pubTopic, JSON.stringify(data));
});
});
}
//接收topic,處理下行指令
function doHandler(topic, message) {
console.log(topic + "," + message.toString())
if (topic === subTopic) {
var msgJson = JSON.parse(message.toString());
//state爲on,那麼打開led-r燈
if (msgJson.state === 'on') {
$('#led-r').turnOn();
} else {
$('#led-r').turnOff();
}
}
}
複製代碼
//on開燈
{"device": "iotLed","state": "on"}
//off關燈
{"device": "iotLed","state": "off"}
複製代碼
設備管理 》設備》Topic列表json