设备上报数据规则流转FC推送钉钉群



1.IoT云端开发




属性名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"}


2. 函数计算开发



2.1 开通函数计算服务


开通FC函数计算服务https://www.aliyun.com/product/fc

2.2. 创建Nodejs函数


  1. const https = require('https');
  2. //钉钉群机器人token
  3. const accessToken = '此处是钉钉群机器人的token';
  4. module.exports.handler = function(event, context, callback) {
  5.     var eventJson = JSON.parse(event.toString());
  6.     const postData = JSON.stringify({
  7.         "msgtype": "markdown",
  8.         "markdown": {
  9.             "title": "温湿度传感器",
  10.             "text": "#### 温湿度传感器上报\n" +
  11.                 "> 设备位置:" + eventJson.tag + "\n\n" +
  12.                 "> 设备编号:" + eventJson.imei+ "\n\n" +
  13.                 "> 实时温度:" + eventJson.temperature + "℃\n\n" +
  14.                 "> 相对湿度:" + eventJson.humidity + "%\n\n" +
  15.                 "> ###### " + eventJson.time + " 发布  by [物联网套件](https://www.aliyun.com/product/iot) \n"
  16.         },
  17.         "at": {
  18.             "isAtAll": false
  19.         }
  20.     });
  21.     const options = {
  22.         hostname: 'oapi.dingtalk.com',
  23.         port: 443,
  24.         path: '/robot/send?access_token='+accessToken,
  25.         method: 'POST',
  26.         headers: {
  27.             'Content-Type': 'application/json',
  28.             'Content-Length': Buffer.byteLength(postData)
  29.         }
  30.     };
  31.     const req = https.request(options, (res) => {
  32.         res.setEncoding('utf8');
  33.         res.on('data', (chunk) => {});
  34.         res.on('end', () => {
  35.             callback(null, 'success');
  36.         });
  37.     });
  38.     req.on('error', (e) => {
  39.         callback(e);
  40.     });
  41.     // 写入数据请求主体
  42.     req.write(postData);
  43.     req.end();
  44. };



3. IoT套件-规则引擎设置



3.1 字段





3.2 Topic





3.3 完整数据操作



3.4 转发动作-函数计算



3.5 启动规则引擎



4. 设备端开发



4.1 模拟设备开发

模拟设备的nodejs脚本iot-fc-dingtalk.js
启动虚拟设备脚本



4.2 真实开发板开发





  1. // 引入aliyun-iot-sdk
  2. var MQTT = require('aliyun-iot-device-mqtt');
  3. // 个人账号
  4. var options = {
  5.     productKey: "",//替换为自己的
  6.     deviceName: "",//替换为自己的
  7.     deviceSecret: "",//替换为自己的
  8.     regionId: "cn-shanghai",//华东2
  9. };
  10. // 发布/订阅 topic
  11. var pubTopic = "/" + options.productKey + "/" + options.deviceName + "/data";
  12. var subTopic = "/" + options.productKey + "/" + options.deviceName + "/control";
  13. // 建立连接
  14. var client = MQTT.createAliyunIotMqttClient(options);
  15. $.ready(function(error) {
  16.     if (error) {
  17.         console.log(error);
  18.         return;
  19.     }
  20.     //10s上报一次
  21.     setInterval(publishData, 15 * 1000);
  22.     //订阅topic
  23.     client.subscribe(subTopic)
  24.     //添加topic处理函数
  25.     client.on('message', doHandler)
  26. });
  27. //上报温湿度
  28. function publishData() {
  29.     $('#humirature').getTemperature(function(error, temperature) {
  30.         if (error) {
  31.             console.error(error);
  32.             return;
  33.         }
  34.         $('#humirature').getRelativeHumidity(function(error, humidity) {
  35.             if (error) {
  36.                 console.error(error);
  37.                 return;
  38.             }
  39.             var data = {
  40.                 "temperature": temperature,//温度
  41.                 "humidity": humidity       //湿度
  42.             };
  43.             console.log(JSON.stringify(data))
  44.             //发布topic,上报数据
  45.             client.publish(pubTopic, JSON.stringify(data));
  46.         });
  47.     });
  48. }
  49. //接收topic,处理下行指令
  50. function doHandler(topic, message) {
  51.     console.log(topic + "," + message.toString())
  52.     if (topic === subTopic) {
  53.         var msgJson = JSON.parse(message.toString());
  54.         
  55.             //state为on,那么打开led-r灯
  56.             if (msgJson.state === 'on') {
  57.                 $('#led-r').turnOn();
  58.             } else {
  59.                 $('#led-r').turnOff();
  60.                 
  61.             }
  62.     }
  63. }



5. 钉钉群收到推送




6.下发指令




设备管理 》设备》Topic列表


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值