部署一个端到端的IoT应用

网友投稿 820 2022-05-30

实验概述:

本实验的目的是将终端设备安全的连接到 IoT Core平台上,还展示了用户如何在设备和IoT Core之间使用MQTT协议发布/订阅消息。本次实验绝大部分使用命令行,当然你也可以使用控制台界面完成。本实验还会利用规则引擎构建 IoT 应用程序,将消息收集、处理和分析并针对数据执行操作,且无需管理任何基础设施。

最终架构如下:

当Sensor发出消息,通过规则引擎(rule engine)过滤数据并触发 Simple Notification Service以邮件的方式提醒用户温度和湿度异常。同时通过另外一条规则引擎存储过滤后的数据并通过Kibana进行展现。

前提条件:

使用具有 admin 权限的用户登陆AWS控制台。

启动一台 Linux EC2 实例作为模拟的IoT设备。在 EC2 实例上使用 Configure 命令配置好默认 Region 为 cn-north-1。在 Console 上赋予这台 EC2 实例一个具有足够权限的 Role,测试中可以直接用admin权限。

实验涵盖:

创建一个Thing

创建证书和策略

部署 Device SDK

部署和执行应用程序

创建规则引擎Rule Engine

创建Simple Notification Service(SNS)

添加SNS推送通知

创建Elasticsearch Service

设计Kibana报表

实验说明:

涉及组件:

IoT Core

EC2

S3

Simple Notification Service

Elasticsearch Service

实验流程

环境准备

在 上创建 IoT Thing

运行 IoT 设备端程序

验证消息订阅发布是否成功

创建并配置 Simple Notification Service

创建并配置 Elasticsearch Service

报表Kibana设计

部署一个端到端的IoT应用

环境准备:

1.登陆 EC2 实例,下载应用程序和device-SDK包。

$ aws configure #输入你的AK,SK Default region:cn-north-1 $ wget https://lqtestota02.s3.cn-north-1.amazonaws.com.cn/sensor-emulator.zip $ unzip -o -d /home/ec2-user sensor-emulator.zip

2.安装 node.js

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash $ . ~/.nvm/nvm.sh $ nvm install node

创建 IOT thing:

1.创建 IoT thing:

$ aws iot create-thing --thing-name aws-iot-demo #记录下输出中的 thingArn,后面会用到。 {     "thingArn": "arn:aws-cn:iot:cn-north-1:408221054609:thing/aws-iot-demo ",      "thingName": " aws-iot-demo",      "thingId": "35e3e6ab-da11-489f-8375-196427cb61f4" }

2.下载  IoT 根证书,创建 IoT 设备证书和密钥,记录下生成的 certificateArn:

$ pwd /home/ec2-user/ $ cd utils $ rm root-CA.crt $ wget https://www.amazontrust.com/repository/AmazonRootCA1.pem $ mv AmazonRootCA1.pem root-CA.crt $ aws iot create-keys-and-certificate \         --certificate-pem-outfile "certificate.pem.crt" \         --public-key-outfile "public.pem.key" \         --private-key-outfile "private.pem.key" #从上一步的命令输出中记录下自己的证书Arn, 后面的命令中会用到 #example:  "certificateArn": "arn:aws-cn:iot:cn-north-1:408221054609:cert/661bdfb4f083bf58607ac1a54904162e0f91f542e9969b58ee10136ded565925"

3.创建一个 IoT Policy,挂载给证书并激活证书:

$ cd ..  $ pwd /home/ec2-user/ # 编写一个 policy 文档,复制以下JSON格式的策略并保存为 iot-policy.json 文件 $ vi iot-policy.json   {   "Version": "2012-10-17",   "Statement": [     {       "Effect": "Allow",       "Action": [         "iot:Publish",         "iot:Subscribe",         "iot:Connect",         "iot:Receive"       ],       "Resource": [         "*"       ]     }   ] } # 创建 iot policy $ aws iot create-policy --policy-name IoTdemo-policy --policy-document file://iot-policy.json # 挂载 policy 到之前创建的 IoT 设备证书上,注意这里的 --target 替换成自己的证书Arn $ aws iot attach-policy \         --policy-name IoTdemo-policy \         --target "arn:aws-cn:iot:cn-north-1:408221054609:cert/661bdfb4f083bf58607ac1a54904162e0f91f542e9969b58ee10136ded565925"    # 激活证书,注意 --certificate-id 替换成自己证书的id $ aws iot update-certificate --certificate-id 661bdfb4f083bf58607ac1a54904162e0f91f542e9969b58ee10136ded565925 --new-status ACTIVE # Attach thing 到证书,其中 --principal 是自己证书的 Arn $ aws iot attach-thing-principal --thing-name aws-iot-demo --principal arn:aws-cn:iot:cn-north-1:408221054609:cert/661bdfb4f083bf58607ac1a54904162e0f91f542e9969b58ee10136ded565925

4.配置并执行Emulator的js程序:

# 查看自己的IoT Endpoint $ aws iot describe-endpoint --endpoint-type iot:Data-ATS{     "endpointAddress": "a1hk0pcc0rk07l.ats.iot.cn-north-1.amazonaws.com.cn"} # 更新自己的iot-properties.file的证书路径和其他信息 $ cd utils nano iot-properties.file host = a25d8uxf2d5pq.ats.iot.cn-north-1.amazonaws.com.cn port = 8883clientId = aws-iot-demo thingName = aws-iot-demo caPath = ./utils/root-CA.crt certPath = ./utils/certificate.pem.crt keyPath = ./utils/private.pem.key region = cn-north-1# 保存退出 Ctrl+X Y Enter # 执行Emulator Node.js 应用程序 $ cd /home/ec2-user  $ node temp-sensor.js # 执行后的输出类似于:

5.订阅一个主题Topic

转到 IoT管理控制台,选择测试,订阅主题为:temp_readings

Code Review:

可以通过修改参数和变量来控制发送数据的维度和间隔时间:默认是 每间隔3秒(3000)发送4个维度的3条数据。

var awsIot = require('aws-iot-device-sdk');var Faker = require('Faker');var PropertiesReader = require('properties-reader'); var properties = PropertiesReader('./utils/iot-properties.file');  const device = awsIot.device({   "host":       properties.get('host'),   "port":       properties.get('port'),   "clientId":   properties.get('clientId'),   "thingName":  properties.get('thingName'),   "caPath":     properties.get('caPath'),   "certPath":   properties.get('certPath'),   "keyPath":    properties.get('keyPath'),   "region":     properties.get('region')});     device.on('connect', function() {  console.log('\n===========Emulating Sensor Data=================\n');    setInterval(function () {          for (i=2; i>=0; i--) {             //Generate Random Sensor Data                     var temperature = Math.floor((Math.random() * 110) + 1);             var deviceId = Math.floor((Math.random() * 5000) + 1);             var IP =  Faker.Internet.ip();             var humidity = Math.floor((Math.random() * 100) + 1);                   console.log('deviceId= ' + deviceId + ' temperature= ' + temperature + ' humidity=' + humidity + ' IP=' + IP );              device.publish('temp_readings', JSON.stringify (                             {  "deviceId" : deviceId,                               "temperature" : temperature,                                        "deviceIP" : IP,                              "humidity" : humidity                                    }                         ));             }      }, 3000);      });

在实际生产环境中MCU或SoC多数都是以C语言开发为主,所以您可以使用device SDK 基于embedded-C 去进行设备端应用开发,请参考:

现在我们成功模拟设备端通过MQTT安全的将消息发布到平台,下一步我们将利用 IoT的托管服务与其他数据服务集成,实现消息的展现,处理与分析。

创建并配置Simple Notification Service

1.在 控制台中搜索SNS服务,创建一个名为 IoTDemo的主题,并创建一个订阅,订阅协议为email,终结点为email地址。

邮件验证通过后,您会收到一个订阅的邮件,请点击订阅。这里就不再赘述。

2.在 IoT控制台的导航窗格中,选择行动Act。

3.创建Email的规则,如下图:

定义规则查询语句,select * from ‘temp_readings’ where temperature > 60 and humidity < 30。

这个查询语句您也可以自定义,SELECT FROM WHERE . 例如:SELECT temperature FROM ‘iot/topic’ WHERE temperature > 50。

在设置一个或多个操作中添加SNS推送通知,确保该角色有足够的权限。

添加操作并创建规则

4.返回到模拟设备的EC2,执行js 应用程序

# 执行Emulator Node.js 应用程序 $ cd /home/ec2-user $ node temp-sensor.js # 执行后的输出类似于:

5.验证邮件通知,你会收到满足规则所定义的温度大于60湿度小于30的邮件通知。select * from ‘temp_readings’ where temperature > 60 and humidity < 30

6.停止模拟程序的运行,不然会收到太多的邮件。

创建并配置Amazon Elasticsearch Service

1.在控制台搜索elasticsearch Service并创建配置集群。由于是测试,就选择开发和测试环境就好。

2.配置集群选择默认配置就好,取一个Elasticsearch 域名,比如awsiotdemo01。

3.设置访问权限,网络配置要设置为 公有访问权限。因为Elasticsearch 规则操作不支持 VPC Elasticsearch 集群。如下图:

4.通过 IoT规则引擎添加规则,具体方法与设置Email规则类似,不再赘述。

这里,将温度大于60的消息都存储到ElasticSearch中。

select * from ‘temp_readings’ where temperature > 60

5.设置或多个操作中,添加操作 ElasticSearch,如下:

6.设计和配置Kibana报表,在设计报表之前,先回到 IoT控制台 行动Act中禁用Email的规则(为避免收到大量邮件,另外一会儿要通过Kibana Get topic/_search验证数据是否成功接收到)。

7.在EC2的模拟器中执行js 应用程序。

8.找到Kibana入口,

9.验证数据是否接收成功,在Console输入以下代码并执行:

GET temp_readings/_search {   "query": {     "match_all": {}   } }

10.设置index为topic的名字

11.在visualize中设计报表,消息的维度一共是四个,分别为deviceId、temperature、deviceIP、humidity。根据个人喜好定义维度所在的X和Y轴即可。

总结:

您可以充分利用托管服务,将数据ETL和展现与托管服务 DynamoDB、Lambda、 Elastic Search、 SNS 和  S3 等轻松集成进来。而 IoT 规则由 SQL SELECT 语句、主题筛选条件和规则操作组成。设备通过将消息发布到 MQTT 主题来向  IoT 发送信息。利用 SQL SELECT 语句,您可以从传入的 MQTT 消息提取数据。后面我们会有更多Hands on的Blog。比如如何通过Job进行OTA升级,如何通过设备管理来管理设备,如何集成智能语音服务Alexa,如何利用边缘计算服务GreenGrass与Machine Learning结合等等。

软件开发 人工智能 云计算 机器学习

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java基础 第五节 第七课
下一篇:urllib:Python爬虫必学的网络库,收藏这一篇就Go了
相关文章