Уреди

Делите путем


Tutorial: Monitor IoT devices and send notifications with Azure Logic Apps

Use Azure Logic Apps to monitor incoming device telemetry from IoT Hub and send notifications when alerts are triggered.

End-to-end diagram

Azure Logic Apps can help you orchestrate workflows across on-premises and cloud services, multiple enterprises, and 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. This flexibility makes Logic Apps an ideal IoT solution for IoT monitoring scenarios. For example, the arrival of 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.

In this tutorial, you perform the following tasks:

Create a Service Bus queue. Create a route in your IoT hub that sends messages to the Service Bus queue if the messages contain anomalous temperature readings. Create a logic app that watches for messaging arriving in the queue and sends an email alert.

The client code running on your device sets an application property, temperatureAlert, on every telemetry message it sends to your IoT hub. When the client code detects a temperature above a given threshold, it sets this property to true; otherwise, it sets the property to false.

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 aren't shown):

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

If you don't have an Azure subscription, create a free account before you begin.

Prerequisites

Prepare the following prerequisites before beginning this tutorial.

Create Service Bus namespace and queue

Create a Service Bus namespace and queue. Later in this article, you create a routing rule in your IoT hub to direct messages that contain a temperature alert to the Service Bus queue. A logic app monitors the queue for incoming messages and sends a notification for each alert.

Create a Service Bus namespace

  1. On the Azure portal, use the search bar to search for and select Service Bus.

  2. Select Create to create a service bus namespace.

  3. On the Create namespace pane, provide the following information:

    Parameter Value
    Subscription Choose the same subscription that contains your IoT hub.
    Resource group Choose the same resource group that contains your IoT hub.
    Namespace name Provide a name for your service bus namespace. The namespace must be unique across Azure.
    Location Choose the same location that your IoT hub uses.
    Pricing tier Select Basic from the drop-down list. The Basic tier is sufficient for this tutorial.
  4. Select Review + create.

  5. Select Create.

  6. Wait for the deployment to complete, then select Go to resource.

Add a Service Bus queue to the namespace

  1. On the Overview page of your Service Bus namespace, select Queue.

    Screenshot of the namespace overview page to add a queue.

  2. In the Name field, provide a name for the queue. Accept the default values for the other fields and select Create.

  3. In your Service Bus namespace, select Shared access policies from the Settings section of the resource menu.

  4. Select the RootManageSharedAccessKey policy to open its details.

  5. Copy the value of Primary connection string and save it to use later in this tutorial when you configure the logic app.

Add a custom endpoint and routing rule to your IoT hub

Add a custom endpoint for the Service Bus queue to your IoT hub. Then, create a message routing rule to direct messages that contain a temperature alert to that endpoint, where they're picked up by your logic app. 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 and route

  1. In the Azure portal, go to your IoT hub.

  2. In the resource menu under Hub settings, select Message routing then select Add.

    Screenshot that shows location of the Add button, to add a new route in your IoT hub.

  3. On the Endpoint tab, create an endpoint for your Service Bus queue by providing the following information:

    Parameter Value
    Endpoint type Select Service Bus queue.
    Endpoint name Provide a name for the new endpoint that maps to your Service Bus queue.
    Service Bus namespace Use the drop-down menu to select the Service Bus namespace that you created in the previous section.
    Service Bus queue Use the drop-down menu to select the Service Bus queue that you created in your namespace.

    Screenshot that shows how to create a service bus queue endpoint for your route.

  4. Select Create + next.

  5. On the Route tab, enter the following information to create a route that points to your Service Bus queue endpoint:

    Parameter Value
    Name Provide a unique name for the route.
    Data source Keep the default Device Telemetry Message data source.
    Routing query Enter temperatureAlert = "true" as the query string.

    Screenshot that shows adding a route with a query.

  6. Select Create + skip enrichments.

Configure Logic Apps for notifications

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. In the Azure portal, search for and select Logic Apps.

  2. Select Add to create a new logic app.

  3. Enter the following information on the Basics tab of Create Logic App:

    Parameter Value
    Resource group Select the same resource group that contains your IoT hub.
    Name Provide a name for your logic app.
    Region Select a location close to you.
    Plan type Select Consumption, which is sufficient for this tutorial.

    Screenshot that shows creating a logic app in the Azure portal.

  4. Select Review + create.

  5. Select Create.

  6. After the deployment completes, select Go to resource to open your logic app.

Configure the logic app trigger

  1. On your logic app resource page in the Azure portal, open the Logic app designer from the Development Tools section of the resource menu.

  2. In the designer, scroll down to Templates and select Blank Logic App.

    Screenshot that shows selecting the Blank Logic App template.

  3. Select the All tab to show all connectors and triggers, then select Service Bus.

    Select Service Bus to start creating your logic app in the Azure portal.

  4. Under Triggers, select When one or more messages arrive in a queue (auto-complete).

    Select the trigger for your logic app in the Azure portal.

  5. Create a service bus connection.

    1. Provide a Connection name and then paste the Connection string that you copied from your Service Bus namespace.

    2. Select Create.

    3. On the next screen, use the drop-down menu to select the queue that you created in your Service Bus namespace. Then, enter 175 for Maximum message count.

      Screenshot that shows configuring queue settings in the Logic app designer.

  6. Select Save on the logic app designer menu to save your changes.

Configure the logic app action

  1. Select New step in the logic app designer.

  2. Create an SMTP service connection.

    1. Search for and select SMTP.

      Screenshot that shows selecting an SMTP connection in the logic app designer.

    2. Select Send Email (V3) as the action for this step.

    3. Provide a Connection name, then fill out the rest of the fields with the SMTP information for the email account you want to use to send the notification messages.

      Use the following links to find your SMTP information, depending on your mail provider:

      Note

      You may need to disable TLS/SSL to establish the connection. 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.

      Screenshot that shows configuring the SMTP connection settings in the logic app designer.

    4. Select Create.

  3. Configure the Send Email action.

    1. From the Add new parameter drop-down on the Send Email step, select the following parameters:

      • From
      • To
      • Subject
      • Body

      Click or tap anywhere on the screen to close the selection box.

      Choose SMTP connection email fields to include in email.

    2. Enter the following information for the email parameters you selected in the previous step:

      Parameter Value
      From The email address that you configured for the SMTP connection.
      To The email address or addresses where you want to send notification emails.
      Subject High temperature detected.
      Body High temperature detected.

      If the Add dynamic content dialog opens, select Hide to close it. You don't use dynamic content in this tutorial.

      Provide details for SMTP connection email fields.

  4. Select Save to save the SMTP connection.

  5. (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. On the Logic app pane, under Development Tools, select API connections.

    2. From the list of API connections, select the SMTP connection.

    3. On the smtp API Connection pane, under General, select Edit API connection.

    4. On the Edit API Connection pane, select Enable SSL?, reenter the password for your email account, and select Save.

      Edit SMTP API connection in your logic app in the Azure portal.

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

Start sending temperature messages from your IoT device to test the monitoring and notification pipeline.

  1. Start the client application on your device. The client code randomly outputs telemetry messages, and sets the temperatureAlert property to true when the temperature exceeds 30 C.

  2. You should begin receiving email notifications sent by the logic app.

    Note

    Your email service provider may need to verify the sender identity to make sure it is you who sends the email.

Clean up resources

If you don't need the resources that you created in this tutorial any longer, delete it and the resource group in the portal. To do so, select the resource group that contains your IoT hub and select Delete.

Alternatively, use the CLI:

# Delete your resource group and its contents
az group delete --name <RESOURCE_GROUP_NAME>

Next steps

In this tutorial, you created a logic app that connects your IoT hub and your mailbox for temperature monitoring and notifications.

To continue to get started with Azure IoT Hub and to explore all extended IoT scenarios, see the following: