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


1527660218083-624e3fd7-92f6-4a1e-a0a1-d2333d26a243.png

1.IoT云端开发
 


  • 开通物联网套件 https://www.aliyun.com/product/iot

  • =>产品管理>创建产品

  • =>产品管理>产品详情>设备管理>添加设备

  • =>产品管理>产品详情>设备管理>添加自定义属性 tag,imei


 


属性名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函数
 


  • 创建服务 IoT_Service

  • 创建函数 pushData2Dingtalk

  • 函数脚本如下:

 

复制代码

  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 字段

 

复制代码

  1. deviceName() as deviceName ,
  2. timestamp('yyyy-MM-dd HH:mm:ss') as time,
  3. attribute('tag') as tag,attribute('imei') as imei,
  4. humidity, temperature




3.2 Topic

 

复制代码

  1. 产品/+/data  +通配符,代表产品下全量设备都使用这个规则




3.3 完整数据操作

1527660846508-84eec5ad-c553-4bae-9b79-f7cbf1c3ddda.png

3.4 转发动作-函数计算

1527660428697-e1a1505f-b5d8-4f05-bb19-931ccd5ad65e.png

3.5 启动规则引擎



4. 设备端开发



4.1 模拟设备开发

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

复制代码

  1. /**
  2. * package.json 添加依赖:"aliyun-iot-mqtt": "0.0.4"
  3. */
  4. const mqtt = require('aliyun-iot-mqtt');
  5. //设备三元组
  6. const options = {
  7.     productKey: "产品",
  8.     deviceName: "设备",
  9.     deviceSecret: "秘钥",
  10.     regionId: "cn-shanghai"
  11. };
  12. //设备与云 建立连接,设备上线
  13. const client = mqtt.getAliyunIotMqttClient(options);
  14. //主题topic
  15. const topic = `${options.productKey}/${options.deviceName}/data`;
  16. const data = {
  17.     temperature: Math.floor((Math.random()*20)+10),
  18.     humidity: Math.floor((Math.random()*100)+20),
  19. };
  20. //指定topic发布数据到云端
  21. client.publish(topic, JSON.stringify(data));
  22. const subTopic = "/" + options.productKey + "/" + options.deviceName + "/control";
  23. //订阅topic
  24. client.subscribe(subTopic)
  25. //添加topic处理函数
  26. client.on('message', function (topic, message){
  27.     console.log(topic + "," + message.toString())
  28. })

启动虚拟设备脚本

复制代码

  1. $node iot-fc-dingtalk.js




4.2 真实开发板开发
 


  • 创建文件夹 mkdir ali-iot-client

  • 进入文件夹 cd ali-iot-client

  • 创建工程 rap init

  • 添加硬件和驱动 rap device add humirature

  • 设备型号 DHT11

  • 在package.json中增加iot的sdk包 aliyun-iot-device-mqtt

 

复制代码

  1. "ruff": {
  2.     "dependencies": {
  3.       "aliyun-iot-device-mqtt": "^0.0.5",
  4.     },
  5.     "version": 1
  6.   }

 


  • 安装依赖 rap install

  • 安装完目录结构如下:


1527661322995-264b9eb8-cfdc-4222-99d2-bd25ac1c85be.png


  • 编写业务逻辑 /src/index.js

 

复制代码

  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. 钉钉群收到推送


1527661667503-649ef743-e160-4aa2-b389-ad7d37b44693.png

6.下发指令
 


  • 通过IoT套件控制台下发指令 /{productKey}/+/control

 

复制代码

  1. //on开灯
  2. {"device": "iotLed","state": "on"}
  3. //off关灯
  4. {"device": "iotLed","state": "off"}



设备管理 》设备》Topic列表
1527662205905-e2412626-ebef-4886-a432-4b6b8a7bfa46.png

阅读后请点击

转载于:https://my.oschina.net/u/3895371/blog/1836440

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值