准备 Raspberry Pi 来演示 MQTT 消息通信 - Amazon IoT Core
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

准备 Raspberry Pi 来演示 MQTT 消息通信

此过程在 Amazon IoT中创建资源并在 Raspberry Pi中 演示 使用 Amazon IoTDevice Client 的 MQTT 消息通信。

创建证书文件演示 MQTT 通信

此过程为此演示创建设备证书文件。

要为 Raspberry Pi 创建和下载设备证书文件

  1. 在本地主机的终端窗口中,输入以下命令为您的设备创建设备证书文件。

    mkdir ~/certs/pubsub aws iot create-keys-and-certificate \ --set-as-active \ --certificate-pem-outfile "~/certs/pubsub/device.pem.crt" \ --public-key-outfile "~/certs/pubsub/public.pem.key" \ --private-key-outfile "~/certs/pubsub/private.pem.key"

    此命令会返回类似以下内容的响应。保存 certificateArn值供稍后使用。

    { "certificateArn": "arn:aws:iot:us-west-2:57EXAMPLE833:cert/76e7e4edb3e52f52334be2f387a06145b2aa4c7fcd810f3aea2d92abc227d269", "certificateId": "76e7e4edb3e52f5233EXAMPLE7a06145b2aa4c7fcd810f3aea2d92abc227d269", "certificatePem": "-----BEGIN CERTIFICATE-----\nMIIDWTCCAkGgAwIBAgI_SHORTENED_FOR_EXAMPLE_Lgn4jfgtS\n-----END CERTIFICATE-----\n", "keyPair": { "PublicKey": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BA_SHORTENED_FOR_EXAMPLE_ImwIDAQAB\n-----END PUBLIC KEY-----\n", "PrivateKey": "-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQE_SHORTENED_FOR_EXAMPLE_T9RoDiukY\n-----END RSA PRIVATE KEY-----\n" } }
  2. 输入以下命令设置证书目录及其文件的权限。

    chmod 700 ~/certs/pubsub chmod 644 ~/certs/pubsub/* chmod 600 ~/certs/pubsub/private.pem.key
  3. 运行此命令可查看证书目录和文件的权限。

    ls -l ~/certs/pubsub

    命令的输出应与您在此处看到的内容相同,但文件日期和时间会有所不同。

    -rw-r--r-- 1 pi pi 1220 Oct 28 13:02 device.pem.crt -rw------- 1 pi pi 1675 Oct 28 13:02 private.pem.key -rw-r--r-- 1 pi pi 451 Oct 28 13:02 public.pem.key
  4. 输入这些命令创建日志文件的目录。

    mkdir ~/.aws-iot-device-client mkdir ~/.aws-iot-device-client/log chmod 745 ~/.aws-iot-device-client/log echo " " > ~/.aws-iot-device-client/log/aws-iot-device-client.log echo " " > ~/.aws-iot-device-client/log/pubsub_rx_msgs.log chmod 600 ~/.aws-iot-device-client/log/*

预置您的设备演示 MQTT 通信

本节将在 Amazon IoT中创建预置 Raspberry Pi 的 Amazon IoT资源。

在 Amazon IoT中预置您的设备:
  1. 在本地主机的终端窗口中,输入以下命令获取您的 Amazon Web Services 账户设备数据端点的地址。

    aws iot describe-endpoint --endpoint-type IoT:Data-ATS

    自从您为上一教程运行此命令以来,端点值一直没有更改。再次在此处运行命令是为了方便地查找数据端点值并将其粘贴到本教程中使用的配置文件中。

    上面步骤的命令会返回类似以下内容的响应。记录 endpointAddress值以供将来使用。

    { "endpointAddress": "a3qjEXAMPLEffp-ats.iot.us-west-2.amazonaws.com" }
  2. 输入此命令为 Raspberry Pi 创建新的 Amazon IoT事物资源。

    aws iot create-thing --thing-name "PubSubTestThing"

    因为一个 Amazon IoT事物资源是您的设备在云中的虚拟表示,所以我们可以在 Amazon IoT中创建多个事物资源用于不同的目的。它们都可以由同一物理 IoT 设备使用来表示设备的不同方面。

    这些教程一次只能使用一个事物资源来表示 Raspberry Pi。这样,在这些教程中,它们代表不同的演示,在为演示创建 Amazon IoT资源后,可以返回并使用专门为每个演示创建的资源重复演示。

    如果您的 Amazon IoT事物资源创建后,命令会返回类似此响应。

    { "thingName": "PubSubTestThing", "thingArn": "arn:aws:iot:us-west-2:57EXAMPLE833:thing/PubSubTestThing", "thingId": "8ea78707-32c3-4f8a-9232-14bEXAMPLEfd" }
  3. 在终端窗口中:

    1. 打开文本编辑器,例如 nano

    2. 复制此 JSON 文档并将其粘贴到打开的文本编辑器中。

      { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "iot:Connect" ], "Resource": [ "arn:aws:iot:us-west-2:57EXAMPLE833:client/PubSubTestThing" ] }, { "Effect": "Allow", "Action": [ "iot:Publish" ], "Resource": [ "arn:aws:iot:us-west-2:57EXAMPLE833:topic/test/dc/pubtopic" ] }, { "Effect": "Allow", "Action": [ "iot:Subscribe" ], "Resource": [ "arn:aws:iot:us-west-2:57EXAMPLE833:topicfilter/test/dc/subtopic" ] }, { "Effect": "Allow", "Action": [ "iot:Receive" ], "Resource": [ "arn:aws:iot:us-west-2:57EXAMPLE833:topic/test/dc/subtopic" ] } ] }
    3. 在编辑器中,在策略文档的每个 Resource部分中,将 us-west-2:57EXAMPLE833 替换为您的 Amazon Web Services 区域、冒号字符 (:) 和 12 位 Amazon Web Services 账户数字。

    4. 将文本编辑器中的文件保存为 ~/policies/pubsub_test_thing_policy.json

  4. 运行此命令使用之前步骤中的策略文档创建 Amazon IoT策略。

    aws iot create-policy \ --policy-name "PubSubTestThingPolicy" \ --policy-document "file://~/policies/pubsub_test_thing_policy.json"

    如果创建策略,该命令将返回类似此类的响应。

    { "policyName": "PubSubTestThingPolicy", "policyArn": "arn:aws:iot:us-west-2:57EXAMPLE833:policy/PubSubTestThingPolicy", "policyDocument": "{\n\"Version\": \"2012-10-17\",\n\"Statement\": [\n{\n\"Effect\": \"Allow\",\n\"Action\": [\n\"iot:Connect\"\n],\n\"Resource\": [\n\"arn:aws:iot:us-west-2:57EXAMPLE833:client/PubSubTestThing\"\n]\n},\n{\n\"Effect\": \"Allow\",\n\"Action\": [\n\"iot:Publish\"\n],\n\"Resource\": [\n\"arn:aws:iot:us-west-2:57EXAMPLE833:topic/test/dc/pubtopic\"\n]\n},\n{\n\"Effect\": \"Allow\",\n\"Action\": [\n\"iot:Subscribe\"\n],\n\"Resource\": [\n\"arn:aws:iot:us-west-2:57EXAMPLE833:topicfilter/test/dc/subtopic\"\n]\n},\n{\n\"Effect\": \"Allow\",\n\"Action\": [\n\"iot:Receive\"\n],\n\"Resource\": [\n\"arn:aws:iot:us-west-2:57EXAMPLE833:topic/test/dc/*\"\n]\n}\n]\n}\n", "policyVersionId": "1"
  5. 运行此命令将策略附加到设备证书。将 certificateArn替换为您之前在本部分保存的 certificateArn值。

    aws iot attach-policy \ --policy-name "PubSubTestThingPolicy" \ --target "certificateArn"

    如果成功,该命令不返回任何内容。

  6. 运行此命令将设备证书附加到 Amazon IoT事物资源。将 certificateArn替换为您之前在本部分保存的 certificateArn值。

    aws iot attach-thing-principal \ --thing-name "PubSubTestThing" \ --principal "certificateArn"

    如果成功,该命令不返回任何内容。

在 Amazon IoT中成功配置设备后,您准备好继续 配置 Amazon IoTDevice Client 配置文件和 MQTT 测试客户端来演示 MQTT 通信

配置 Amazon IoTDevice Client 配置文件和 MQTT 测试客户端来演示 MQTT 通信

此过程将创建一个配置文件来测试 Amazon IoTDevice Client 。

要创建配置文件测试 Amazon IoTDevice Client
  1. 在连接到 Raspberry Pi 的本地主机的终端窗口中:

    1. 打开文本编辑器,例如 nano

    2. 复制此 JSON 文档并将其粘贴到打开的文本编辑器中。

      { "endpoint": "a3qEXAMPLEaffp-ats.iot.us-west-2.amazonaws.com", "cert": "~/certs/pubsub/device.pem.crt", "key": "~/certs/pubsub/private.pem.key", "root-ca": "~/certs/AmazonRootCA1.pem", "thing-name": "PubSubTestThing", "logging": { "enable-sdk-logging": true, "level": "DEBUG", "type": "STDOUT", "file": "" }, "jobs": { "enabled": false, "handler-directory": "" }, "tunneling": { "enabled": false }, "device-defender": { "enabled": false, "interval": 300 }, "fleet-provisioning": { "enabled": false, "template-name": "", "template-parameters": "", "csr-file": "", "device-key": "" }, "samples": { "pub-sub": { "enabled": true, "publish-topic": "test/dc/pubtopic", "publish-file": "", "subscribe-topic": "test/dc/subtopic", "subscribe-file": "~/.aws-iot-device-client/log/pubsub_rx_msgs.log" } }, "config-shadow": { "enabled": false }, "sample-shadow": { "enabled": false, "shadow-name": "", "shadow-input-file": "", "shadow-output-file": "" } }
    3. 端点替换为您在 在 Amazon IoT Core中配置您的设备中找到的 Amazon Web Services 账户设备数据端点。

    4. 将文本编辑器中的文件保存为 ~/dc-configs/dc-pubsub-config.json

    5. 运行这个命令在新文件上设置权限

      chmod 644 ~/dc-configs/dc-pubsub-config.json
  2. 要准备 MQTT 测试客户端订阅所有 MQTT 消息

    1. 在本地主机上,在Amazon IoT控制台,选择MQTT 测试客户端

    2. 订阅主题选项卡中,在主题筛选条件中,输入#(一个英镑符号),然后选择订阅

    3. 订阅标签下,确认您看见 #(单独英镑符号)。

    继续此教程时,让窗口的 MQTT 测试客户端 保持打开。

保存文件并配置 MQTT 测试客户端后,您便可以继续 演示使用 Amazon IoT Device Client 发布消息