IoT remote monitoring and notifications with Azure Logic Apps connecting your IoT hub and mailbox
Note
Before you start this tutorial, complete the Raspberry Pi online simulator tutorial or one of the device tutorials. For example, you can go to Raspberry Pi with Node.js or to one of the Send telemetry quickstarts. In these articles, you set up your Azure IoT device and IoT hub, and you deploy a sample application to run on your device. The application sends collected sensor data to your IoT hub.
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. This flexibility makes Logic Apps an ideal IoT solution for IoT monitoring scenarios. 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.
In this article, you learn how to create a logic app that connects your IoT hub and your mailbox for temperature monitoring and notifications. 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 30 C, 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 are not shown):
{
"body": {
"messageId": 18,
"deviceId": "Raspberry Pi Web Client",
"temperature": 27.796111770668457,
"humidity": 66.77637926438427
},
"applicationProperties": {
"temperatureAlert": "false"
}
}
To learn more about IoT Hub message format, see Create and read IoT Hub messages.
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.
Prerequisites
Complete the Raspberry Pi online simulator tutorial or one of the device tutorials. For example, you can go to Raspberry Pi with Node.js or to one of the Send telemetry quickstarts. These articles cover the following requirements:
- An active Azure subscription.
- An Azure IoT hub under your subscription.
- 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. 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
On the Azure portal, select + Create a resource > Integration > Service Bus.
On the Create namespace pane, provide the following information:
Name: The name of the service bus namespace. The namespace must be unique across Azure.
Pricing tier: Select Basic from the drop-down list. The Basic tier is sufficient for this tutorial.
Resource group: Use the same resource group that your IoT hub uses.
Location: Use the same location that your IoT hub uses.
Select Create. Wait for the deployment to complete before moving on to the next step.
Add a Service Bus queue to the namespace
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.
On the Service Bus Namespace pane, select + Queue.
Enter a name for the queue and then select Create. When the queue has been successfully created, the Create queue pane closes.
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.
Enter a name for the policy, check Manage, and then select Create.
Add a custom endpoint and routing rule to your IoT hub
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. 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
Open your IoT hub. 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.
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.
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.
Select Create. After the endpoint is successfully created, proceed to the next step.
Add a routing rule
Back on the Message routing pane, select the Routes tab and then select + Add.
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.
Routing query: Enter
temperatureAlert = "true"
.Select Save. You can close the Message routing pane.
Create and configure a Logic App
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
Select Create a resource > Integration > Logic App.
Enter the following information:
Name: The name of the logic app.
Resource group: Use the same resource group that your IoT hub uses.
Location: Use the same location that your IoT hub uses.
Select Create.
Configure the logic app trigger
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.
In the Logic Apps Designer, scroll down to Templates and select Blank Logic App.
Select the All tab and then select Service Bus.
Under Triggers, select When one or more messages arrive in a queue (auto-complete).
Create a service bus connection.
Enter a connection name and select your Service Bus namespace from the list. The next screen opens.
Select the service bus policy (RootManageSharedAccessKey). Then select Create.
On the final screen, for Queue name, select the queue that you created from the drop-down. Enter
175
for Maximum message count.Select Save on the menu at the top of the Logic Apps Designer to save your changes.
Configure the logic app action
Create an SMTP service connection.
Select New step. In Choose an action, select the All tab.
Type
smtp
in the search box, select the SMTP service in the search result, and then select Send Email.Enter the SMTP information for your mailbox, and then select Create.
Get the SMTP information for Hotmail/Outlook.com, Gmail, and Yahoo Mail.
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.
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.
Enter 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.Select Save to save the SMTP connection.
(Optional) If you had to disable TLS to establish a connection with your email provider and want to re-enable it, follow these steps:
On the Logic app pane, under Development Tools, select API connections.
From the list of API connections, select the SMTP connection.
On the smtp API Connection pane, under General, select Edit API connection.
On the Edit API Connection pane, select Enable SSL?, re-enter the password for your email account, and select Save.
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 the client application on your device.
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.
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.
Next steps
You have successfully 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:
Feedback
Submit and view feedback for