通过连接 IoT 中心和邮箱的 Azure 逻辑应用进行 IoT 远程监视并发送通知IoT remote monitoring and notifications with Azure Logic Apps connecting your IoT hub and mailbox

端到端关系图

备注

开始本教程前,请设置设备Before you start this tutorial, set up your device. 在本文中,将设置 Azure IoT 设备和 IoT 中心,并部署一个在设备上运行的示例应用程序。In the article, you set up your Azure IoT device and IoT hub, and you deploy a sample application to run on your device. 该应用程序会将收集的传感器数据发送到 IoT 中心。The application sends collected sensor data to your IoT hub.

Azure 逻辑应用可帮助你协调跨一个或多个企业中的本地和云服务以及跨各种协议的工作流。Azure Logic Apps can help you orchestrate workflows across on-premises and cloud services, one or more enterprises, and across various protocols. 逻辑应用以触发器开始,后接一个或多个可以使用内置控制机制(例如条件和迭代器)定序的操作。A logic app begins with a trigger, which is then followed by one or more actions that can be sequenced using built-in controls, such as conditions and iterators. 这种灵活性使得逻辑应用成为 IoT 监视方案的理想 IoT 解决方案。This flexibility makes Logic Apps an ideal IoT solution for IoT monitoring scenarios. 例如,设备将遥测数据发送到 IoT 中心终结点后,可以启动逻辑应用工作流以便在 Azure 存储 Blob 中存储数据、发送电子邮件警报来警示出现数据异常、在设备报告故障时安排技术人员上门解决,等等。For example, the arrival of telemetry data from a device at an IoT Hub endpoint can initiate logic app workflows to warehouse the data in an Azure Storage blob, send email alerts to warn of data anomalies, schedule a technician visit if a device reports a failure, and so on.

学习内容What you learn

了解如何创建连接 IoT 中心和邮箱的逻辑应用,以监视温度并发送通知。You learn how to create a logic app that connects your IoT hub and your mailbox for temperature monitoring and notifications.

每当设备上运行的客户端代码将遥测消息发送到 IoT 中心时,都会设置应用程序属性 temperatureAlertThe client code running on your device sets an application property, temperatureAlert, on every telemetry message it sends to your IoT hub. 当客户端代码检测到温度超过 30 摄氏度时,会将此属性设置为 true;否则将此属性设置为 falseWhen the client code detects a temperature above 30 C, it sets this property to true; otherwise, it sets the property to false.

到达 IoT 中心的消息如下所示,正文中包含遥测数据,应用程序属性中包含 temperatureAlert 属性(未显示系统属性):Messages arriving at your IoT hub look similar to the following, with the telemetry data contained in the body and the temperatureAlert property contained in the application properties (system properties are not shown):

{
  "body": {
    "messageId": 18,
    "deviceId": "Raspberry Pi Web Client",
    "temperature": 27.796111770668457,
    "humidity": 66.77637926438427
  },
  "applicationProperties": {
    "temperatureAlert": "false"
  }
}

若要详细了解 IoT 中心消息格式,请参阅创建和读取 IoT 中心消息To learn more about IoT Hub message format, see Create and read IoT Hub messages.

在本主题中,你将在 IoT 中心上设置路由,以将 temperatureAlert 属性为 true 的消息发送到服务总线终结点。In this topic, you set up routing on your IoT hub to send messages in which the temperatureAlert property is true to a Service Bus endpoint. 然后设置一个逻辑应用,该应用在到达服务总线终结点的消息上触发并向你发送电子邮件通知。You then set up a logic app that triggers on the messages arriving at the Service Bus endpoint and sends you an email notification.

准备工作What you do

  • 创建服务总线命名空间,并向其添加服务总线队列。Create a Service Bus namespace and add a Service Bus queue to it.
  • 将自定义终结点和路由规则添加到 IoT 中心,以将包含温度警报的消息路由到服务总线队列。Add a custom endpoint and a routing rule to your IoT hub to route messages that contain a temperature alert to the Service Bus queue.
  • 创建、配置并测试一个逻辑应用,以使用来自服务总线队列的消息,并将通知电子邮件发送到所需的收件人。Create, configure, and test a logic app to consume messages from your Service Bus queue and send notification emails to a desired recipient.

需要什么What you need

  • 完成 Raspberry Pi 联机模拟器教程或其中一个设备教程;例如将 Raspberry Pi 与 Node.js 配合使用Complete the Raspberry Pi online simulator tutorial or one of the device tutorials; for example, Raspberry Pi with node.js. 这包括以下要求:These cover the following requirements:

    • 一个有效的 Azure 订阅。An active Azure subscription.
    • 已在订阅中创建一个 Azure IoT 中心。An Azure IoT hub under your subscription.
    • 设备上正在运行一个可将遥测消息发送到 Azure IoT 中心的客户端应用程序。A client application running on your device that sends telemetry messages to your Azure IoT hub.

创建服务总线命名空间和队列Create Service Bus namespace and queue

创建服务总线命名空间和队列。Create a Service Bus namespace and queue. 在本主题的稍后部分,你将在 IoT 中心创建一个路由规则,以将包含温度警报的消息定向到服务总线队列,而某个逻辑应用将在该队列中拾取这些消息,并触发其中的警报来发送通知电子邮件。Later in this topic, you create a routing rule in your IoT hub to direct messages that contain a temperature alert to the Service Bus queue, where they will be picked up by a logic app and trigger it to send a notification email.

创建服务总线命名空间Create a Service Bus namespace

  1. Azure 门户中,选择“创建资源” > “企业集成” > “服务总线” 。On the Azure portal, select Create a resource > Enterprise Integration > Service Bus.

  2. 在“创建命名空间”窗格中提供以下信息: On the Create namespace pane, provide the following information:

    名称:服务总线命名空间的名称。Name: The name of the service bus namespace. 该命名空间必须在整个 Azure 中唯一。The namespace must be unique across Azure.

    定价层:从下拉列表中选择“基本”。 Pricing tier: Select Basic from the drop-down list. 基本层对本教程而言就已足够。The Basic tier is sufficient for this tutorial.

    资源组:使用 IoT 中心所用的同一资源组。Resource group: Use the same resource group that your IoT hub uses.

    位置:使用 IoT 中心所用的同一位置。Location: Use the same location that your IoT hub uses.

    在 Azure 门户中创建服务总线命名空间

  3. 选择“创建” 。Select Create. 等待部署完成,然后转到下一步。Wait for the deployment to complete before moving on to the next step.

将服务总线队列添加到命名空间Add a Service Bus queue to the namespace

  1. 打开服务总线命名空间。Open the Service Bus namespace. 转到服务总线命名空间的最简单方法是从资源窗格中选择“资源组”,选择你的资源组,然后从资源列表中选择该服务总线命名空间。 The easiest way to get to the Service Bus namespace is to select Resource groups from the resource pane, select your resource group, then select the Service Bus namespace from the list of resources.

  2. 在“服务总线命名空间”窗格中,选择“+ 队列”。 On the Service Bus Namespace pane, select + Queue.

  3. 为队列输入一个名称,然后选择“创建” 。Enter a name for the queue and then select Create. 成功创建队列后,“创建队列”窗格会关闭。 When the queue has been successfully created, the Create queue pane closes.

    在 Azure 门户中添加服务总线队列

  4. 返回“服务总线命名空间”窗格,在“实体”下,选择“队列”。 Back on the Service Bus Namespace pane, under Entities, select Queues. 从列表中打开服务总线队列,然后选择“共享访问策略” > “+ 添加”。 Open the Service Bus queue from the list, and then select Shared access policies > + Add.

  5. 为策略输入一个名称,选中“管理” ,然后选择“创建” 。Enter a name for the policy, check Manage, and then select Create.

    在 Azure 门户中添加服务总线队列策略

将自定义终结点和路由规则添加到 IoT 中心Add a custom endpoint and routing rule to your IoT hub

将服务总线队列的自定义终结点添加到 IoT 中心,并创建一个消息路由规则,以将包含温度警报的消息定向到该终结点,而逻辑应用将在该终结点中拾取这些消息。Add a custom endpoint for the Service Bus queue to your IoT hub and create a message routing rule to direct messages that contain a temperature alert to that endpoint, where they will be picked up by your logic app. 路由规则使用路由查询 temperatureAlert = "true",根据设备上运行的客户端代码设置的 temperatureAlert 应用程序属性值来转发消息。The routing rule uses a routing query, temperatureAlert = "true", to forward messages based on the value of the temperatureAlert application property set by the client code running on the device. 有关详细信息,请参阅基于消息属性的消息路由查询To learn more, see Message routing query based on message properties.

添加自定义终结点Add a custom endpoint

  1. 打开 IoT 中心Open your IoT hub. 转到 IoT 中心的最简单方法是从资源窗格中选择“资源组”,选择你的资源组,然后从资源列表中选择该 IoT 中心。 The easiest way to get to the IoT hub is to select Resource groups from the resource pane, select your resource group, then select your IoT hub from the list of resources.

  2. 在“消息传送”下,选择“消息路由”。 Under Messaging, select Message routing. 在“消息路由”窗格中选择“自定义终结点”选项卡,然后选择“+ 添加”。 On the Message routing pane, select the Custom endpoints tab and then select + Add. 从下拉列表中选择“服务总线队列”。 From the drop-down list, select Service bus queue.

    在 Azure 门户中,将终结点添加到 IoT 中心

  3. 在“添加服务总线终结点”窗格中输入以下信息: On the Add a service bus endpoint pane, enter the following information:

    终结点名称:终结点的名称。Endpoint name: The name of the endpoint.

    服务总线命名空间:选择你创建的命名空间。Service Bus namespace: Select the namespace you created.

    服务总线队列:选择你创建的队列。Service Bus queue: Select the queue you created.

    在 Azure 门户中,将终结点添加到 IoT 中心

  4. 选择“创建” 。Select Create. 成功创建终结点后,转到下一步。After the endpoint is successfully created, proceed to the next step.

添加路由规则Add a routing rule

  1. 返回“消息路由”窗格,选择“路由”选项卡,然后选择“+ 添加”。 Back on the Message routing pane, select the Routes tab and then select + Add.

  2. 在“添加路由”窗格中输入以下信息: On the Add a route pane, enter the following information:

    名称:路由规则的名称。Name: The name of the routing rule.

    终结点:选择你创建的终结点。Endpoint: Select the endpoint you created.

    数据源:选择“设备遥测消息”。 Data source: Select Device Telemetry Messages.

    路由查询:输入 temperatureAlert = "true"Routing query: Enter temperatureAlert = "true".

    在 Azure 门户中添加路由规则

  3. 选择“保存” 。Select Save. 可以关闭“消息路由”窗格。 You can close the Message routing pane.

创建并配置逻辑应用Create and configure a Logic App

在上一部分,你已设置 IoT 中心,以将包含温度警报的消息路由到服务总线队列。In the preceding section, you set up your IoT hub to route messages containing a temperature alert to your Service Bus queue. 现在,请设置一个逻辑应用,以监视服务总线队列,并在每次将消息添加到队列时都发送一则电子邮件通知。Now, you set up a logic app to monitor the Service Bus queue and send an e-mail notification whenever a message is added to the queue.

创建逻辑应用Create a logic app

  1. 选择“创建资源” > “集成” > “逻辑应用”。 Select Create a resource > Integration > Logic App.

  2. 输入以下信息:Enter the following information:

    名称:逻辑应用的名称。Name: The name of the logic app.

    资源组:使用 IoT 中心所用的同一资源组。Resource group: Use the same resource group that your IoT hub uses.

    位置:使用 IoT 中心所用的同一位置。Location: Use the same location that your IoT hub uses.

    在 Azure 门户中创建逻辑应用

  3. 选择“创建” 。Select Create.

配置逻辑应用触发器Configure the logic app trigger

  1. 打开逻辑应用。Open the logic app. 转到逻辑应用的最简单方法是从资源窗格中选择“资源组”,选择你的资源组,然后从资源列表中选择该逻辑应用。 The easiest way to get to the logic app is to select Resource groups from the resource pane, select your resource group, then select your logic app from the list of resources. 选择逻辑应用时,逻辑应用设计器会打开。When you select the logic app, the Logic Apps Designer opens.

  2. 在逻辑应用设计器中,向下滚动到“模板”并选择“空白逻辑应用”。 In the Logic Apps Designer, scroll down to Templates and select Blank Logic App.

    在 Azure 门户中,从空白逻辑应用着手

  3. 选择“所有”选项卡,然后选择“服务总线”。 Select the All tab and then select Service Bus.

    选择“服务总线”,开始在 Azure 门户中创建逻辑应用

  4. 在“触发器”下,选择“当一个或多个消息出现在队列中时(自动完成)”。 Under Triggers, select When one or more messages arrive in a queue (auto-complete).

    在 Azure 门户中选择逻辑应用的触发器

  5. 创建服务总线连接。Create a service bus connection.

    1. 输入连接名称,并从列表中选择你的服务总线命名空间。Enter a connection name and select your Service Bus namespace from the list. 下一个屏幕将会打开。The next screen opens.

      在 Azure 门户中为逻辑应用创建服务总线连接

    2. 选择服务总线策略 (RootManageSharedAccessKey)。Select the service bus policy (RootManageSharedAccessKey). 然后选择“创建”。 Then select Create.

      在 Azure 门户中为逻辑应用创建服务总线连接

    3. 在最后一个屏幕上,对于“队列名称”,请从下拉列表中选择创建的队列。 On the final screen, for Queue name, select the queue that you created from the drop-down. 对于“最大消息计数”,请输入 175Enter 175 for Maximum message count.

      在逻辑应用中,为服务总线连接指定最大消息计数

    4. 在逻辑应用设计器顶部的菜单中选择“保存”以保存更改。 Select Save on the menu at the top of the Logic Apps Designer to save your changes.

配置逻辑应用操作Configure the logic app action

  1. 创建 SMTP 服务连接。Create an SMTP service connection.

    1. 选择“新建步骤”。 Select New step. 在“选择操作”中,选择“所有”选项卡。 In Choose an action, select the All tab.

    2. 在搜索框中键入 smtp,在搜索结果中选择“SMTP”服务,然后选择“发送电子邮件”。 Type smtp in the search box, select the SMTP service in the search result, and then select Send Email.

      在 Azure 门户中,在逻辑应用中创建 SMTP 连接

    3. 输入邮箱的 SMTP 信息,然后选择“创建”。 Enter the SMTP information for your mailbox, and then select Create.

      在 Azure 门户中,在逻辑应用中输入 SMTP 连接信息

      Hotmail/Outlook.comGmailYahoo Mail 获取 SMTP 信息。Get the SMTP information for Hotmail/Outlook.com, Gmail, and Yahoo Mail.

      备注

      可能需要禁用 TLS/SSL 才能建立连接。You may need to disable TLS/SSL to establish the connection. 如果存在这种情况,并且你想要在建立连接后重新启用 TLS,请参阅本部分末尾的可选步骤。If this is the case and you want to re-enable TLS after the connection has been established, see the optional step at the end of this section.

    4. 在“发送电子邮件”步骤的“添加新参数”下拉列表中,选择“发件人”、“收件人”、“主题”和“正文”。 From the Add new parameter drop-down on the Send Email step, select From, To, Subject and Body. 单击或点击屏幕上的任意位置以关闭选框。Click or tap anywhere on the screen to close the selection box.

      选择 SMTP 连接电子邮件字段

    5. 输入“发件人” 和“收件人” 的电子邮件地址,并为“主题” 和“正文” 输入 High temperature detectedEnter your email address for From and To, and High temperature detected for Subject and Body. 如果“从此流中使用的应用和连接器添加动态内容”对话框已打开,请选择“隐藏”将其关闭。 If the Add dynamic content from the apps and connectors used in this flow dialog opens, select Hide to close it. 本教程不使用动态内容。You do not use dynamic content in this tutorial.

      填写 SMTP 连接电子邮件字段

    6. 选择“保存”以保存 SMTP 连接。 Select Save to save the SMTP connection.

  2. (可选)如果必须禁用 TLS 才能与电子邮件提供程序建立连接,并且事后你想要重新启用 TLS,请执行以下步骤:(Optional) If you had to disable TLS to establish a connection with your email provider and want to re-enable it, follow these steps:

    1. 在“逻辑应用”窗格中的“开发工具”下,选择“API 连接”。 On the Logic app pane, under Development Tools, select API connections.

    2. 从 API 连接列表中选择 SMTP 连接。From the list of API connections, select the SMTP connection.

    3. 在“SMTP API 连接”窗格中的“常规”下,选择“编辑 API 连接”。 On the smtp API Connection pane, under General, select Edit API connection.

    4. 在“编辑 API 连接”窗格中选择“启用 SSL?”,重新输入电子邮件帐户的密码,然后选择“保存”。 On the Edit API Connection pane, select Enable SSL?, re-enter the password for your email account, and select Save.

      在 Azure 门户中编辑逻辑应用中的 SMTP API 连接

逻辑应用现已准备好处理来自服务总线队列的温度警报,并将通知发送到你的电子邮件帐户。Your logic app is now ready to process temperature alerts from the Service Bus queue and send notifications to your email account.

测试逻辑应用Test the logic app

  1. 在设备上启动客户端应用程序。Start the client application on your device.

  2. 如果使用物理设备,请小心地将热源摆放到热传感器的附近,直到温度超过 30 摄氏度。如果使用在线模拟器,客户端代码将随机输出遥测消息,指出温度超过 30 摄氏度。If you're using a physical device, carefully bring a heat source near the heat sensor until the temperature exceeds 30 degrees C. If you're using the online simulator, the client code will randomly output telemetry messages that exceed 30 C.

  3. 随后应会开始收到逻辑应用发送的电子邮件通知。You should begin receiving email notifications sent by the logic app.

    备注

    电子邮件服务提供商可能需要验证发件人身份,以确保是你本人发送的电子邮件。Your email service provider may need to verify the sender identity to make sure it is you who sends the email.

后续步骤Next steps

逻辑应用已创建成功,它可以连接 IoT 中心和邮箱,以监视温度并发送通知。You have successfully created a logic app that connects your IoT hub and your mailbox for temperature monitoring and notifications.

若要继续了解 Azure IoT 中心入门知识并浏览所有扩展的 IoT 方案,请参阅:To continue to get started with Azure IoT Hub and to explore all extended IoT scenarios, see the following: