利用M2M实现设备之间联动
方案设计
基于IoT物联网平台中规则引擎的M2M能力,我们可以实现设备间联动通信,技术方案如下:
1.创建产品
1.1 油烟机
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"}
5.2 设备运行日志
【往期回顾】
1.自建MQTT集群迁移阿里云IoT平台
2.IoT时代:WiFi配网技术剖析
3.微信小程序和IoT智能家居实践
4.IoT云端通用数据解析脚本实践