--- title: "2. 创建智能灯泡模拟器" date: 2019-09-05T23:06:51+08:00 draft: false chapter: false weight: 202 --- 这个章节将介绍创建一个虚拟的灯泡,并且能够与 AWS IoT Core 进行交互。 ## 在 AWS IoT Core 控制台创建事物 在本节中,我们会创建Iot事物、证书以及安全策略。 1. 登陆 AWS IoT Core 控制台,显示如下。 ![](/images/smart-home/simulator/simulator-1.png) 1. 在左侧导航栏中,选择 **Onboard** 菜单 1. 在 **Configuring a device** 中, 点击 **Getting Started** 按钮 1. 在 **How are you connecting to AWS IoT?** 页面, 选择 **Linux** 作为运行平台; 选择 **Node.js** for IoT Device SDK, 然后点击 **Next** ![](/images/smart-home/simulator/simulator-3.png) 1. 在 **Name** 字段中输入 `ratchet`, 点击 **Next step** ![](/images/smart-home/simulator/simulator-5.png) > 如果您与别人共享这个 AWS 账号,请输入一些前缀来区别您和别人创建的事物, e.g. `cw_ratchet`。 1. 在 **Download connection kit** 页面中,点击 **Linux/OSX** 按钮来下载我们所需的文件。当下载完成后,点击 **Next** 按钮。 ![](/images/smart-home/simulator/simulator-6.png) 总结一下, 我们在之前的步骤中,创建了如下内容: - 一个 安全策略 来控制事物访问 IoT Core - 一个 Linux/OSX zip 文件,包含连接 IoT Core 所需的证书 ![](/images/smart-home/simulator/simulator-8.png) {{% notice warning %}} 请不要丢失这个 ZIP 文件,它包含 **private key**(私有安全密钥), 一旦遗失,无法再次获取。 {{% /notice %}} ## 修改默认的安全策略 默认的安全策略会限制这个设备能发布的消息 topic。在这个实验中,我们将会创建一个更加开放的安全策略,以便我们能够通过APP进行连接。因此,需要先找到 之前创建的策略。 1. 在 AWS IoT Core 控制台点击 **Manage**,它会默认选中 **Things** 1. 找到之前步骤中创建的事物, 例如本实验中叫做 **ratchet** 1. 选择这个事物 1. 在左侧菜单栏,点击 **Security** 1. 点击已经附加的策略,如下 ![](/images/smart-home/simulator/simulator-15.png) 1. 点击 **Policies** ![](/images/smart-home/simulator/simulator-16.png) 1. 选择 Policy, 这个实验中,应该叫 **ratchet-Policy** 1. 点击 **Edit Policy Document** 1. Enter the following for your document ``` { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "iot:Publish", "iot:Subscribe", "iot:Connect", "iot:Receive" ], "Resource": [ "*" ] } ] } ``` 1. 点击 **Save as new version** ![](/images/smart-home/simulator/simulator-17.png) 现在,你的设备可以发布和订阅任何消息。至今,我们已经完成了三个 IoT 组件的创建: * 设备证书 * 一个安全策略 * 安全策略与设备证书与事物的关联 ## 连接 IoT Core {{% notice info %}} 本实验使用使用 Cloud9 开发环境,请确保您已经完成 创建 Cloud 9 工作环境. 这个链接会打开一个新的 tab, 请在完成 创建后回到当前页面。 {{% /notice %}} {{% notice note %}} 请确认当前网络环境是否屏蔽了 8883 端口。 如果被屏蔽,可以考虑使用一个 guest network 或者 个人热点。 {{% /notice %}} 1. Git clone 这个[代码仓库](https://github.com/aws-samples/aws-alexa-workshop-smarthome-lamp), 在 Cloud9 的 terminal 中输入 ``` git clone https://github.com/aws-samples/aws-alexa-workshop-smarthome-lamp --depth=1 cd aws-alexa-workshop-smarthome-lamp ``` 1. 解压之前下载的 ZIP 文件. 您将获得如何文件: - 一个 **private key**, `.private.key` - 一个 **device certificate**, `.cert.pem` 1. 把 private key 重命名为 **private.key** 并且拖拽上传到 **credentials** 文件夹. 1. 将 device certificate 重命名为 **cert.pem** 并且拖拽上传到 **credentials** 文件夹 1. 修改 `index.js` 中的配置并保存 - `iotEndpoint`. 在 **IoT Core 控制台**左侧 **Settings** 菜单中可以找到 ![](/images/smart-home/simulator/simulator-18.png?width=500) - `thingName`. 之前创建的事物名称 - `deviceBindingUrl`. 这个 url 是之前 [创建设备绑定程序]({{< ref "1.build-device-ui.zh.md" >}}) 步骤中创建的,请在 AWS Amplify Console 中找到它,在 **Production branch URL** 中 ![](/images/smart-home/amplify-url.png?width=500) 1. 安装依赖. 在 Cloud9 的 terminal 中运行 `npm install` 1. 运行 `node index.js` 启动应用程序。运行成功后, terminal 将会输出一个二维码。 在[下一步]({{< ref "3.bind-device.zh.md" >}})中,我们将使用这个二维码完成用户和设备之间的绑定关系。 ![](/images/smart-home/simulator/qrcode-terminal.png?width=400) {{% notice info %}} 因为我们只做设备模拟,因此这个应用程序监听 shadow 的信息,并且在收到 shadow 信息后立即上报。在现实场景中,您需要在实际改变设备状态之后(如打开灯泡),再上报状态。 {{% /notice %}} ## 测试设备影子(可选) 该步骤中,我们通过发送 MQTT 消息来更改设备的 ON/OFF 状态。 消息主题: ``` $aws/things//shadow/update ``` 消息 body 部分的内容如下: ``` { "state": { "desired": { "power": "ON" } } } ``` ![](/images/smart-home/simulator/simulator-19.png) 您可以从虚拟灯泡的 terminal 中观察到输出,例如: ``` turn ON Smart Lamp turn OFF Smart Lamp ``` 关于设备影子的更多信息,请参考[using shadows](https://docs.aws.amazon.com/zh_cn/iot/latest/developerguide/using-device-shadows.html)