六步开启 Amazon IoT | 亚马逊云科技技术支持浅谈 IoT

物联网
Amazon DynamoDB
0
0
{"value":"Amazon Web Services 支持服务是一对一快速反应支持通道, 配备富有经验的技术支持工程师, 提供(24x7x365)全年无休的技术支持服务。该服务有助于各种规模和技术能力的客户成功使用 Amazon Web Services 提供的产品和功能。Amazon Web Services 支持服务专注于细节,可以使您的业务更加迅捷交付。技术支持工程师均来自于行业一线各个领域的顶尖企业,无论您来自传统行业坯是来自互联网行业, 无论您的业务基于传统的企业业务模型还是互联网类型,都可以从 Amazon Web Services 技术支持团队得到无微不至的帮助。通过引入 Amazon Web Services 技术支持服务,可以让 Amazon Web Services 的技术专家成为您的技术伙伴,让您的团队更加专注于业务逻辑本身而没有后顾之忧。\n\n中国区技术支持更多信息及购买方式详见:[www.amazonaws.cn/support/support-plans/?nc2=h」2_su](https://www.amazonaws.cn/support/)。\n\n支持团队在处理日常工作的同时每天都在与时俱进, 以便与客户分享最新的技术信息。在2015年10月的 **Amazon Web Services re:lnvent** 大会上,Amazon Web Services 正式发布了 Amazon Web Services loT服务,用来帮助物联网行业用户更好在云端部署他们的业务环境。在上期杂志中我们介绍了架构图和基本模块,下面通过一篇来自技术支持团队的 Amazon Web Services loT 六步曲,帮您快速进入云端物联网技术世界。\n\n### **第一步 安装**\n\n\n**通过awscli工具操作 Amazon Web Services loT服务。**\nawscli 是一个 python 实现的发送 Amazon Web Services API 的命令行工具。可以通过 pip 安装:\n\n```\\npip install awscli\\n```\n\npip 是-个开源的跨平台 python 包管理器,在常见操作系统中均可安装,例如,在 CentOS 中:\n\n```\\nyum install python-pip\\n```\n\n安装好 awscli 之后, 可以通过如下命令查看 Amazon Web Services loT 相关的操作:\n\n\n```\\naws iot help\\n```\n\n对于具体的某个 Amazon Web Services loT 命令, 可以通过如下命令查看用法:\n\n\n```\\naws iot command help\\n```\n\n### **第二步 创建设备**\n\n\n```\\naws iot creaare-thing --thing-name \\"lightbulb\\"\\n```\n\n上述命令会在创建-个名为 lightbulb 的设备, 并返回设备的 arn(在 Amazon Web Services 上的资源的唯一编号}:\n\n\n```\\n{\\n \\"thingARN\\": \\"arn: aws: iot: us-east-1: <aws-account-id>: thing/lightbulb\\",\\n \\"thingName\\": \\"lightbulb\\"\\n}\\n```\n\n通过 list-things 命令可以查看所有已经创建的设备:\n\n\n```\\naws iot list-things\\n```\n\n输出如下:\n\n```\\n{\\n \\"things\\": [ {\\n \\"attributes\\" : { },\\n ”thingsName\\": \\"lightbulb\\"\\n} ]\\n}\\n```\n\n### **第三步 加密通讯**\n\n\n**在设备和 Amazon Web Services loT服务之间进行安全的加密通讯**\n可以通过 Amazon Web Services iot命令创建-个x.509证书,用于设备和 Amazon Web Services loT服务之间的通信 . 并且赋予这个证书调用指定的 Amazon Web Services api的权限,让使用这个证书的设备有权限调用特定的 Amazon Web Services api。\n\n通过如下命令创建一个证书:\n\n```\\naws iot create-keys-and-certificate --set-as-active --output text\\n```\n\n命令的输出包括证书,证书的 arn,公钥和私钥。将私钥保存到名叫 privateKey.pem 的文件中,后面会用到。\n\n可以用 describe-certificate 命令保存证书:\n\n```\\naws iot describe-certificate --certificate-id \\"certificate-id\\" --output text --query certificateDescription.certificatePem > cert.pem\\n```\n\n创建一个 Amazon Web Services loT Policy,并将其和前面创建的证书关联起来。\n\n通过如下命令创建证书:\n\n```\\naws iot create-policy --policy-name \\"AllowAllioIotOperationsPolicy\\" --policy-documentfile://AllowAllIotOperations.json\\n```\n\nAllowAllIotOperations.json文件中包含了 Policy 的内容,示例如下:\n\n```\\n{\\n \\"Version\\": \\"2012-10-17\\",\\n \\"statement\\": [{\\n \\"Effect\\": \\"Allow\\",\\n \\"Action\\": [\\"iot:*\\"],\\n \\"Resource\\": [\\"*\\"]\\n }]\\n}\\n```\n\n通过 attach-principal-policy 命令将 Policy 和证书关联起来:\n\n```\\naws iot attach-principal-policy --principal \\"certificate-arn\\" --policy-name \\"AllowAllIotOperationsPolicy\\"\\n```\n\n将证书和设备关联起来:\n\n```\\naws iot attach-thing-principal --thing-name \\"lightbulb\\" --principal \\"certificate-arn\\"\\n```\n\n这样,lightbulb 这个设备就拥有调用所有 iot:* API 的权限了。\n\n### **第四步 收发消息**\n\n**通过MQTT订阅和推选消息**\nMQTT 是一个用于设备间通信的消息订阅/推送服务,Amazon Web Services loT 集成了 MQTT 服务器,设备可以通过 MQTT 客户端与 Amazon Web Services loT 的 MQTT 服务器通信。在通信过程中,需要用到根证书,可以在这里下载:\nwww.symantec.com/ content /en/us/enterprise/verisign/ roots/Ve『iSign-C lass%203-Public-P 『imary-Certification -Authority-G5.pem\n\n通过 describe-endpoint 命令获取 Amazon Web Services loT 的 MQTT 服务器的 endpoint:\n\n```\\naws iot describe-endpoint\\n```\n\n通过 endpoint,根证书,已经前面步骤中创建的 cert.pem 和privateKey.pem,可以让设备使用任意的 MQTT 客户端与 Amazon Web Services loT 的 MQTT 服务器通信,可以实现设备之间的互相通信,后面我们会看到,Amazon Web Services loT 服务预设了一些内置的 MQTT topic,可以让设备将自身的信息推送到这些 topic,便于服务器端进行控制。\n\n### **第五步 规则配置**\n\n\n**配置 Amazon Web Services loTRule**\n通过 Amazon Web Services loT Rule,可以设置当 Amazon Web Services loT 的 MQTT 服务器收到消息后该采取什么样的行动,例如:将消息存入 DynamoDB,触发 lambda function,或者推送到 SNS 。\n\n为 Amazon Web Services IoT 创建一个 IAM Rule:\n\n```\\naws iam create-role --role-name \\"iot-actions-role\\" --assume-role-policy-document file://path-to-file/trust-policy-file\\n```\n\n其中,trust-poIicy-file 文件的内容如下:\n\n```\\n{\\n \\"Version\\": \\"2012-10-17\\",\\n \\"Statement\\":[{\\n \\"Sid\\": \\"\\",\\n \\"Effect\\": \\"Allow\\",\\n \\"Principal\\": {\\n \\"Service\\": \\"iot.amazonaws.com\\"\\n },\\n \\"Action\\": \\"sts:AssumeRole\\"\\n }]\\n}\\n```\n为这个 Rule 创建一个 Policy:\n\n```\\naws iam create-policy --policy-name “iot-actions-policy” --policy-documentfile://IAM-policy-document-file-path\\n```\n\n假设我们要允许向 DynamoDB 中写入数据以及调用 lambda function, IAM-policy-document-file-path 的内容如下:\n\n```\\n{\\n \\"Version\\": \\"2012-10-17\\",\\n \\"Statement\\": [{\\n \\"Effect\\": \\"Allow\\",\\n \\"Action\\": [ \\"dynamodb:*\\", \\"lambda:InvokeFunction\\"],\\n \\"Resource\\": [\\"*\\"]\\n}]\\n}\\n```\n\n将 Policy 和 Rule 关联起来:\n\n```\\naws iam attach-role-policy --role-name \\"iot-actions-role\\" --policy-arn \\"policy-ARN\\"\\n```\n\n到这里,IAM 的 Rule 和 Policy 就创建好了,现在 Amazon Web Services IoT 服务有权限写入数据到 DynamoDB 或者调用 lambda function。接下来,我们定义一条让 Amazon Web Services IoT Rule,让 Amazon Web Services IoT 服务接受到一条 MQTT 消息之后,将其存入到 DynamoDB 中。命令如下:\n\n```\\naws iot create-topic-rule --rule-name \\"saveToDynamoDB\\" --topic-rule-payloadfile://path-to-file/DynamoDbRule\\n```\n\n其中,path-to-file/DynameDbRule 是一个 json 格式的文件,使用 SQL 风格的语言对消息进行选择和操作:\n\n```\\n{\\n \\"sql\\": 'SELECT * FROM 'topic/test'\\",\\n \\"ruleDisabled\\": false,\\n \\"actions\\": [{\\n \\"dynamoDB\\": {\\n \\"tableName\\": \\"sampleTable\\",\\n \\"hashKeyField\\": \\"key\\",\\n \\"hashKeyValue\\": \\"\${topic(2)}\\",\\n \\"rangeKeyField\\": \\"timestamp',\\n \\"rangeKeyValue\\": \\"\${timestamp( )}\\",\\n \\"roleArn\\": \\"arn:aws:iam::123456789012:role/iot-actions-role\\"\\n }\\n }]\\n}\\n```\n\n配置好 Amazon Web Services IoT Rule 之后,用 MQTT 客户端推送一条消息到 test topic,消息就会被记录到 DynamoDB。\n\n### **第六步 管理物理设备**\n\n**注册设备并使用Device Shadows**\nDevice Shadows相当于物理设备在 Amazon Web Services loT上的-个 “影子” ,物理设备可以把自身的状态同步到Device Shadows上面,应用程序可以从Device Shadows上获取设备的状态,并发送命令给Device Shadows,物理设备通过订阅Device Shadows的推送消息,可以了解到应用程序发送了什么命令,并按照命令执行操作。这样,就完成了应用程序与物理设备之间的交互。物理设备和Device Shadows之间的通过MQTT交互,应用程序和Device Shadows之间通过 Amazon Web Services loT的Restful API交互。操作示例如下:\n\n首先,注册-个设备:\n\n```\\naws iot create-thing --thing-name “lightbulb1”\\n```\n\n\n\n\n让设备订阅 \$aws/things/thingName/shadow/update/rejected这个topic ,设备推送消息到 Amazon Web Services loT 的 MQTT 服务器如果发生了任何错误,都会发送到这个 topic,例如,用 Eclipse Mosquitto 客户端模拟设备的操作:\\n\\n\\n```\\nmosquito_sub –cafile \\"rootCA.pem\\" --cert \\"thing-cert\\" --key \\"thing-private-key\\" –h \\"account-specific-endpoint\\" –p 8883 –q 1 –d –t '\$aws/things/lightbulb1/shadow/update/rejected'\n```\\n\\n再启动另-个 Eclipse Mosquitto 客户端,模拟设备的消息推送动作,例如.将 lightbulb1 的状态标识为红色:\\n\\n```\nmosquitto_pub -cafile \"rootCA.pemt\" --cert \"thing-cert\" --key \"thing-private-key\" -h \"account-specific-endpoint\" -p 8883 -q 1 -d -t '\$aws/things/lightbulb1/shadow/update' -m \\"{ \\\\\\"state\\\\\\": {\\\\\\"reported\\\\\\": {\\\\\\"color\\\\\\": \\\\\\"RED\\\\\\" } } }\\"\\n```\\n\\n应用程序通过 get-thing-shadow API 从 Device Shadows 获得设备的状态:\\n\\n```\\naws iot-data get-thing-shadow --thing-name \\"lightbulb1\\" \\"output.txt\\" && cat output.txt\\n```\\n\\n应用程序将 lightbulb1 的状态标识别为绿色:\\n\\n```\\naws iot-data update-thing-shadow --thing-name \\"lightbulb1\\" --payload \\"{ \\\\\\"state\\\\\\": {\\\\\\"desired\\\\\\": {\\\\\\"color\\\\\\": \\\\\\"GREEN\\\\\\" } } }\\" output.txt && cat output.txt\\n```\\n\\n如果设备不再使用,可以用 delete-thing API 将设备删除,在此之前,可以先通过 MQTT 将设备状态标识为空:\\n\\n```\\nmosquito_pub --cafile \\"rootCA.pem\\" --cert \\"thing-cert\\" --key \\"thing-private-key\\" -h\\"account-specific-endpoint\\"-p 8883 –q 1 –d –t '\$aws/things/lightbulb1/shadow/update' –m \"{ \\\"state\\\": null }\"\n```\\n\\n删除设备:\\n\\n```\naws iot delete-thing --thing-name lightbulb1\n```\n\n最终,通过 Amazon Web Services loT 服务,可以将各种设备轻松的连接起来,开始畅想物联网带来的备种便利。\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","render":"<p>Amazon Web Services 支持服务是一对一快速反应支持通道, 配备富有经验的技术支持工程师, 提供(24x7x365)全年无休的技术支持服务。该服务有助于各种规模和技术能力的客户成功使用 Amazon Web Services 提供的产品和功能。Amazon Web Services 支持服务专注于细节,可以使您的业务更加迅捷交付。技术支持工程师均来自于行业一线各个领域的顶尖企业,无论您来自传统行业坯是来自互联网行业, 无论您的业务基于传统的企业业务模型还是互联网类型,都可以从 Amazon Web Services 技术支持团队得到无微不至的帮助。通过引入 Amazon Web Services 技术支持服务,可以让 Amazon Web Services 的技术专家成为您的技术伙伴,让您的团队更加专注于业务逻辑本身而没有后顾之忧。</p>\n<p>中国区技术支持更多信息及购买方式详见:<a href=\\"https://www.amazonaws.cn/support/\\" target=\\"_blank\\">www.amazonaws.cn/support/support-plans/?nc2=h」2_su</a>。</p>\\n<p>支持团队在处理日常工作的同时每天都在与时俱进, 以便与客户分享最新的技术信息。在2015年10月的 <strong>Amazon Web Services re:lnvent</strong> 大会上,Amazon Web Services 正式发布了 Amazon Web Services loT服务,用来帮助物联网行业用户更好在云端部署他们的业务环境。在上期杂志中我们介绍了架构图和基本模块,下面通过一篇来自技术支持团队的 Amazon Web Services loT 六步曲,帮您快速进入云端物联网技术世界。</p>\\n<h3><a id=\\"__6\\"></a><strong>第一步 安装</strong></h3>\\n<p><strong>通过awscli工具操作 Amazon Web Services loT服务。</strong><br />\\nawscli 是一个 python 实现的发送 Amazon Web Services API 的命令行工具。可以通过 pip 安装:</p>\n<pre><code class=\\"lang-\\">pip install awscli\\n</code></pre>\\n<p>pip 是-个开源的跨平台 python 包管理器,在常见操作系统中均可安装,例如,在 CentOS 中:</p>\n<pre><code class=\\"lang-\\">yum install python-pip\\n</code></pre>\\n<p>安装好 awscli 之后, 可以通过如下命令查看 Amazon Web Services loT 相关的操作:</p>\n<pre><code class=\\"lang-\\">aws iot help\\n</code></pre>\\n<p>对于具体的某个 Amazon Web Services loT 命令, 可以通过如下命令查看用法:</p>\n<pre><code class=\\"lang-\\">aws iot command help\\n</code></pre>\\n<h3><a id=\\"__36\\"></a><strong>第二步 创建设备</strong></h3>\\n<pre><code class=\\"lang-\\">aws iot creaare-thing --thing-name &quot;lightbulb&quot;\\n</code></pre>\\n<p>上述命令会在创建-个名为 lightbulb 的设备, 并返回设备的 arn(在 Amazon Web Services 上的资源的唯一编号}:</p>\n<pre><code class=\\"lang-\\">{\\n &quot;thingARN&quot;: &quot;arn: aws: iot: us-east-1: &lt;aws-account-id&gt;: thing/lightbulb&quot;,\\n &quot;thingName&quot;: &quot;lightbulb&quot;\\n}\\n</code></pre>\\n<p>通过 list-things 命令可以查看所有已经创建的设备:</p>\n<pre><code class=\\"lang-\\">aws iot list-things\\n</code></pre>\\n<p>输出如下:</p>\n<pre><code class=\\"lang-\\">{\\n &quot;things&quot;: [ {\\n &quot;attributes&quot; : { },\\n ”thingsName&quot;: &quot;lightbulb&quot;\\n} ]\\n}\\n</code></pre>\\n<h3><a id=\\"__71\\"></a><strong>第三步 加密通讯</strong></h3>\\n<p><strong>在设备和 Amazon Web Services loT服务之间进行安全的加密通讯</strong><br />\\n可以通过 Amazon Web Services iot命令创建-个x.509证书,用于设备和 Amazon Web Services loT服务之间的通信 . 并且赋予这个证书调用指定的 Amazon Web Services api的权限,让使用这个证书的设备有权限调用特定的 Amazon Web Services api。</p>\n<p>通过如下命令创建一个证书:</p>\n<pre><code class=\\"lang-\\">aws iot create-keys-and-certificate --set-as-active --output text\\n</code></pre>\\n<p>命令的输出包括证书,证书的 arn,公钥和私钥。将私钥保存到名叫 privateKey.pem 的文件中,后面会用到。</p>\n<p>可以用 describe-certificate 命令保存证书:</p>\n<pre><code class=\\"lang-\\">aws iot describe-certificate --certificate-id &quot;certificate-id&quot; --output text --query certificateDescription.certificatePem &gt; cert.pem\\n</code></pre>\\n<p>创建一个 Amazon Web Services loT Policy,并将其和前面创建的证书关联起来。</p>\n<p>通过如下命令创建证书:</p>\n<pre><code class=\\"lang-\\">aws iot create-policy --policy-name &quot;AllowAllioIotOperationsPolicy&quot; --policy-documentfile://AllowAllIotOperations.json\\n</code></pre>\\n<p>AllowAllIotOperations.json文件中包含了 Policy 的内容,示例如下:</p>\n<pre><code class=\\"lang-\\">{\\n &quot;Version&quot;: &quot;2012-10-17&quot;,\\n &quot;statement&quot;: [{\\n &quot;Effect&quot;: &quot;Allow&quot;,\\n &quot;Action&quot;: [&quot;iot:*&quot;],\\n &quot;Resource&quot;: [&quot;*&quot;]\\n }]\\n}\\n</code></pre>\\n<p>通过 attach-principal-policy 命令将 Policy 和证书关联起来:</p>\n<pre><code class=\\"lang-\\">aws iot attach-principal-policy --principal &quot;certificate-arn&quot; --policy-name &quot;AllowAllIotOperationsPolicy&quot;\\n</code></pre>\\n<p>将证书和设备关联起来:</p>\n<pre><code class=\\"lang-\\">aws iot attach-thing-principal --thing-name &quot;lightbulb&quot; --principal &quot;certificate-arn&quot;\\n</code></pre>\\n<p>这样,lightbulb 这个设备就拥有调用所有 iot:* API 的权限了。</p>\n<h3><a id=\\"__126\\"></a><strong>第四步 收发消息</strong></h3>\\n<p><strong>通过MQTT订阅和推选消息</strong><br />\\nMQTT 是一个用于设备间通信的消息订阅/推送服务,Amazon Web Services loT 集成了 MQTT 服务器,设备可以通过 MQTT 客户端与 Amazon Web Services loT 的 MQTT 服务器通信。在通信过程中,需要用到根证书,可以在这里下载:<br />\\nwww.symantec.com/ content /en/us/enterprise/verisign/ roots/Ve『iSign-C lass%203-Public-P 『imary-Certification -Authority-G5.pem</p>\n<p>通过 describe-endpoint 命令获取 Amazon Web Services loT 的 MQTT 服务器的 endpoint:</p>\n<pre><code class=\\"lang-\\">aws iot describe-endpoint\\n</code></pre>\\n<p>通过 endpoint,根证书,已经前面步骤中创建的 cert.pem 和privateKey.pem,可以让设备使用任意的 MQTT 客户端与 Amazon Web Services loT 的 MQTT 服务器通信,可以实现设备之间的互相通信,后面我们会看到,Amazon Web Services loT 服务预设了一些内置的 MQTT topic,可以让设备将自身的信息推送到这些 topic,便于服务器端进行控制。</p>\n<h3><a id=\\"__140\\"></a><strong>第五步 规则配置</strong></h3>\\n<p><strong>配置 Amazon Web Services loTRule</strong><br />\\n通过 Amazon Web Services loT Rule,可以设置当 Amazon Web Services loT 的 MQTT 服务器收到消息后该采取什么样的行动,例如:将消息存入 DynamoDB,触发 lambda function,或者推送到 SNS 。</p>\n<p>为 Amazon Web Services IoT 创建一个 IAM Rule:</p>\n<pre><code class=\\"lang-\\">aws iam create-role --role-name &quot;iot-actions-role&quot; --assume-role-policy-document file://path-to-file/trust-policy-file\\n</code></pre>\\n<p>其中,trust-poIicy-file 文件的内容如下:</p>\n<pre><code class=\\"lang-\\">{\\n &quot;Version&quot;: &quot;2012-10-17&quot;,\\n &quot;Statement&quot;:[{\\n &quot;Sid&quot;: &quot;&quot;,\\n &quot;Effect&quot;: &quot;Allow&quot;,\\n &quot;Principal&quot;: {\\n &quot;Service&quot;: &quot;iot.amazonaws.com&quot;\\n },\\n &quot;Action&quot;: &quot;sts:AssumeRole&quot;\\n }]\\n}\\n</code></pre>\\n<p>为这个 Rule 创建一个 Policy:</p>\n<pre><code class=\\"lang-\\">aws iam create-policy --policy-name “iot-actions-policy” --policy-documentfile://IAM-policy-document-file-path\\n</code></pre>\\n<p>假设我们要允许向 DynamoDB 中写入数据以及调用 lambda function, IAM-policy-document-file-path 的内容如下:</p>\n<pre><code class=\\"lang-\\">{\\n &quot;Version&quot;: &quot;2012-10-17&quot;,\\n &quot;Statement&quot;: [{\\n &quot;Effect&quot;: &quot;Allow&quot;,\\n &quot;Action&quot;: [ &quot;dynamodb:*&quot;, &quot;lambda:InvokeFunction&quot;],\\n &quot;Resource&quot;: [&quot;*&quot;]\\n}]\\n}\\n</code></pre>\\n<p>将 Policy 和 Rule 关联起来:</p>\n<pre><code class=\\"lang-\\">aws iam attach-role-policy --role-name &quot;iot-actions-role&quot; --policy-arn &quot;policy-ARN&quot;\\n</code></pre>\\n<p>到这里,IAM 的 Rule 和 Policy 就创建好了,现在 Amazon Web Services IoT 服务有权限写入数据到 DynamoDB 或者调用 lambda function。接下来,我们定义一条让 Amazon Web Services IoT Rule,让 Amazon Web Services IoT 服务接受到一条 MQTT 消息之后,将其存入到 DynamoDB 中。命令如下:</p>\n<pre><code class=\\"lang-\\">aws iot create-topic-rule --rule-name &quot;saveToDynamoDB&quot; --topic-rule-payloadfile://path-to-file/DynamoDbRule\\n</code></pre>\\n<p>其中,path-to-file/DynameDbRule 是一个 json 格式的文件,使用 SQL 风格的语言对消息进行选择和操作:</p>\n<pre><code class=\\"lang-\\">{\\n &quot;sql&quot;: 'SELECT * FROM 'topic/test'&quot;,\\n &quot;ruleDisabled&quot;: false,\\n &quot;actions&quot;: [{\\n &quot;dynamoDB&quot;: {\\n &quot;tableName&quot;: &quot;sampleTable&quot;,\\n &quot;hashKeyField&quot;: &quot;key&quot;,\\n &quot;hashKeyValue&quot;: &quot;\${topic(2)}&quot;,\\n &quot;rangeKeyField&quot;: &quot;timestamp',\\n &quot;rangeKeyValue&quot;: &quot;\${timestamp( )}&quot;,\\n &quot;roleArn&quot;: &quot;arn:aws:iam::123456789012:role/iot-actions-role&quot;\\n }\\n }]\\n}\\n</code></pre>\\n<p>配置好 Amazon Web Services IoT Rule 之后,用 MQTT 客户端推送一条消息到 test topic,消息就会被记录到 DynamoDB。</p>\n<h3><a id=\\"__219\\"></a><strong>第六步 管理物理设备</strong></h3>\\n<p><strong>注册设备并使用Device Shadows</strong><br />\\nDevice Shadows相当于物理设备在 Amazon Web Services loT上的-个 “影子” ,物理设备可以把自身的状态同步到Device Shadows上面,应用程序可以从Device Shadows上获取设备的状态,并发送命令给Device Shadows,物理设备通过订阅Device Shadows的推送消息,可以了解到应用程序发送了什么命令,并按照命令执行操作。这样,就完成了应用程序与物理设备之间的交互。物理设备和Device Shadows之间的通过MQTT交互,应用程序和Device Shadows之间通过 Amazon Web Services loT的Restful API交互。操作示例如下:</p>\n<p>首先,注册-个设备:</p>\n<pre><code class=\\"lang-\\">aws iot create-thing --thing-name “lightbulb1”\\n</code></pre>\\n<p>让设备订阅 \$aws/things/thingName/shadow/update/rejected这个topic ,设备推送消息到 Amazon Web Services loT 的 MQTT 服务器如果发生了任何错误,都会发送到这个 topic,例如,用 Eclipse Mosquitto 客户端模拟设备的操作:</p>\n<pre><code class=\\"lang-\\">mosquito_sub –cafile &quot;rootCA.pem&quot; --cert &quot;thing-cert&quot; --key &quot;thing-private-key&quot; –h &quot;account-specific-endpoint&quot; –p 8883 –q 1 –d –t '\$aws/things/lightbulb1/shadow/update/rejected'\\n</code></pre>\\n<p>再启动另-个 Eclipse Mosquitto 客户端,模拟设备的消息推送动作,例如.将 lightbulb1 的状态标识为红色:</p>\n<pre><code class=\\"lang-\\">mosquitto_pub -cafile &quot;rootCA.pemt&quot; --cert &quot;thing-cert&quot; --key &quot;thing-private-key&quot; -h &quot;account-specific-endpoint&quot; -p 8883 -q 1 -d -t '\$aws/things/lightbulb1/shadow/update' -m &quot;{ \\\\&quot;state\\\\&quot;: {\\\\&quot;reported\\\\&quot;: {\\\\&quot;color\\\\&quot;: \\\\&quot;RED\\\\&quot; } } }&quot;\\n</code></pre>\\n<p>应用程序通过 get-thing-shadow API 从 Device Shadows 获得设备的状态:</p>\n<pre><code class=\\"lang-\\">aws iot-data get-thing-shadow --thing-name &quot;lightbulb1&quot; &quot;output.txt&quot; &amp;&amp; cat output.txt\\n</code></pre>\\n<p>应用程序将 lightbulb1 的状态标识别为绿色:</p>\n<pre><code class=\\"lang-\\">aws iot-data update-thing-shadow --thing-name &quot;lightbulb1&quot; --payload &quot;{ \\\\&quot;state\\\\&quot;: {\\\\&quot;desired\\\\&quot;: {\\\\&quot;color\\\\&quot;: \\\\&quot;GREEN\\\\&quot; } } }&quot; output.txt &amp;&amp; cat output.txt\\n</code></pre>\\n<p>如果设备不再使用,可以用 delete-thing API 将设备删除,在此之前,可以先通过 MQTT 将设备状态标识为空:</p>\n<pre><code class=\\"lang-\\">mosquito_pub --cafile &quot;rootCA.pem&quot; --cert &quot;thing-cert&quot; --key &quot;thing-private-key&quot; -h&quot;account-specific-endpoint&quot;-p 8883 –q 1 –d –t '\$aws/things/lightbulb1/shadow/update' –m &quot;{ \\\\&quot;state\\\\&quot;: null }&quot;\\n</code></pre>\\n<p>删除设备:</p>\n<pre><code class=\\"lang-\\">aws iot delete-thing --thing-name lightbulb1\\n</code></pre>\\n<p>最终,通过 Amazon Web Services loT 服务,可以将各种设备轻松的连接起来,开始畅想物联网带来的备种便利。</p>\n"}
0
目录
关闭