实验概述:
本实验的目的是将终端设备安全的连接到 AWS IoT Core 平台上,还展示了用户如何在设备和 IoT Core 之间使用 MQTT 协议发布/订阅消息。本次实验绝大部分使用命令行,当然你也可以使用控制台界面完成。本实验还会利用规则引擎构建 IoT 应用程序,将消息收集、处理和分析并针对数据执行操作,且无需管理任何基础设施。
最终架构如下:
当 Sensor 发出消息,通过规则引擎(rule engine)过滤数据并触发 Amazon Simple Notification Service 以邮件的方式提醒用户温度和湿度异常。同时通过另外一条规则引擎存储过滤后的数据并通过 Kibana 进行展现。
前提条件:
使用具有 admin 权限的用户登陆 AWS 控制台。
启动一台 Amazon Linux EC2 实例作为模拟的 IoT 设备。在 EC2 实例上使用 AWS Configure 命令配置好默认 Region 为 cn-north-1。在 AWS Console 上赋予这台 EC2 实例一个具有足够权限的 Role,测试中可以直接用 admin 权限。
实验涵盖:
实验说明:
涉及AWS组件:
AWS IoT Core
AWS EC2
AWS S3
Amazon Simple Notification Service
Amazon Elasticsearch Service
实验流程
环境准备
在 AWS 上创建 IoT Thing
运行 IoT 设备端程序
验证消息订阅发布是否成功
创建并配置Amazon Simple Notification Service
创建并配置Amazon Elasticsearch Service
报表Kibana设计
环境准备:
1.登陆 EC2 实例,下载应用程序和 aws-device-SDK 包。
$ aws configure $ 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:$ . ~/.nvm/nvm.sh$ nvm install node
复制代码
在 AWS 上创建 IOT thing:
1.创建 IoT thing:
$ aws iot create-thing { "thingArn": "arn:aws-cn:iot:cn-north-1:408221054609:thing/aws-iot-demo ", "thingName": " aws-iot-demo", "thingId": "35e3e6ab-da11-489f-8375-196427cb61f4"}
复制代码
2.下载 AWS IoT 根证书,创建 IoT 设备证书和密钥,记录下生成的 certificateArn:
$ pwd/home/ec2-user/$ cd utils$ rm root-CA.crt$ wget https:$ 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 到之前创建的 IoT 设备证书上,注意这里的 $ aws iot attach-policy \ # 激活证书,注意 $ aws iot update-certificate # Attach thing 到证书,其中 $ aws iot attach-thing-principal
复制代码
4.配置并执行 Emulator 的 js 程序:
$ aws iot describe-endpoint --endpoint-type iot:Data-ATS{ "endpointAddress": "a1hk0pcc0rk07l.ats.iot.cn-north-1.amazonaws.com.cn"}$ cd utilsnano iot-properties.file
host = a25d8uxf2d5pq.ats.iot.cn-north-1.amazonaws.com.cnport = 8883clientId = aws-iot-demothingName = aws-iot-democaPath = ./utils/root-CA.crtcertPath = ./utils/certificate.pem.crtkeyPath = ./utils/private.pem.keyregion = cn-north-1Ctrl+XYEnter$ cd /home/ec2-user $ node temp-sensor.js
复制代码
5.订阅一个主题 Topic
转到 AWS 的 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--) { 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 语言开发为主,所以您可以使用 AWS device SDK 基于 embedded-C 去进行设备端应用开发,请参考:
https://docs.amazonaws.cn/iot/latest/developerguide/iot-embedded-c-sdk.html
现在我们成功模拟设备端通过 MQTT 安全的将消息发布到平台,下一步我们将利用 AWS IoT 的托管服务与其他数据服务集成,实现消息的展现,处理与分析。
创建并配置 Amazon Simple Notification Service
1.在 AWS 控制台中搜索 SNS 服务,创建一个名为 AWSIoTDemo 的主题,并创建一个订阅,订阅协议为 email,终结点为 email 地址。如下图:
邮件验证通过后,您会收到一个订阅的邮件,请点击订阅。这里就不再赘述。
2.在 AWS 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.在 AWS 控制台搜索 elasticsearch Service 并创建配置集群。由于是测试,就选择开发和测试环境就好。
2.配置集群选择默认配置就好,取一个 Elasticsearch 域名,比如 awsiotdemo01。
3.设置访问权限,网络配置要设置为 公有访问权限。因为 Elasticsearch 规则操作不支持 VPC Elasticsearch 集群。如下图:
4.通过 AWS IoT 规则引擎添加规则,具体方法与设置 Email 规则类似,不再赘述。
这里,将温度大于 60 的消息都存储到 ElasticSearch 中。
select * from ‘temp_readings’ where temperature > 60
5.设置或多个操作中,添加操作 ElasticSearch,如下:
6.设计和配置 Kibana 报表,在设计报表之前,先回到 AWS 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轴即可。

您可以充分利用AWS的托管服务,将数据ETL和展现与AWS的托管服务Amazon DynamoDB、AWS Lambda、Amazon Elastic Search、Amazon SNS 和 Amazon S3 等轻松集成进来。而AWS IoT 规则由 SQL SELECT 语句、主题筛选条件和规则操作组成。设备通过将消息发布到 MQTT 主题来向 AWS IoT 发送信息。利用 SQL SELECT 语句,您可以从传入的 MQTT 消息提取数据。后面我们会有更多Hands on的Blog。比如如何通过Job进行OTA升级,如何通过设备管理来管理设备,如何集成智能语音服务Alexa,如何利用边缘计算服务GreenGrass与Machine Learning结合等等。
**作者介绍:**
李强AWS解决方案架构师,负责基于AWS的云计算方案架构的咨询和设计,同时致力于AWS云服务在国内的应用和推广,在物联网和微软的技术栈有着广泛的设计和实践经验。在加入AWS之前,曾在东芝中国负责系统开发和运维工作,在微软中国负责中小企业的技术咨询和方案设计工作。
**本文转载自AWS技术博客。**
**原文链接:**https://amazonaws-china.com/cn/blogs/china/deploy-an-end-to-end-iot-application/
复制代码
评论