利用M2M實作裝置之間關聯
方案設計
基于IoT物聯網平台中規則引擎的M2M能力,我們可以實作裝置間關聯通信,技術方案如下:
1.建立産品
1.1 油煙機
建立裝置
通信Topic
1.2 瓦斯竈
2.注冊裝置
2.1 油煙機裝置
2.2 瓦斯竈裝置
注冊裝置,并綁定目标油煙機
3.規則引擎配置 M2M規則
3.1 處理資料
SELECT
attribute('targetId') as rangehoodId,
status,
windPower
FROM
"/a****h/+/user/change/rangehood"
3.2 轉發資料
4.裝置開發
4.1 油煙機裝置
裝置端開發
const mqtt = require('aliyun-iot-mqtt');
var options = {
productKey: "替換",
deviceName: "替換",
deviceSecret: "替換",
regionId: "cn-shanghai"
};
//建立連接配接
const client = mqtt.getAliyunIotMqttClient(options);
client.subscribe(`/${options.productKey}/${options.deviceName}/user/control/params`)
client.on('message', function(topic, message) {
console.log("sub topic => " + topic)
console.log("message => " + message)
})
4.2 瓦斯竈裝置
const mqtt = require('aliyun-iot-mqtt');
var options = {
productKey: "替換",
deviceName: "替換",
deviceSecret: "替換",
regionId: "cn-shanghai"
};
var pubTopic = `/${options.productKey}/${options.deviceName}/user/change/rangehood`;
//建立連接配接
const client = mqtt.getAliyunIotMqttClient(options);
//上報資料
client.publish(pubTopic, getPostData(), { qos: 0 });
function getPostData() {
const payloadJson = {
status: "on", //on,off
windPower: "high" //low,middle,high
}
console.log("Pub Topic => " + pubTopic)
console.log("Payload => " + JSON.stringify(payloadJson))
return JSON.stringify(payloadJson);
}
5.聯機運作
5.1 裝置啟動
油煙機
$ node range-hood.js
sub topic => /a1zTlSPM9Ni/s001/user/control/params
message => {"windPower":"high","rangehoodId":"s001","status":"on"}
瓦斯竈
$ node gas-stove.js
Pub Topic => /a****h/z001/user/change/rangehood
Payload => {"status":"on","windPower":"high"}