Using Logic App for IoT remote monitoring and for sending notifications

Introduction

Logic App is an Azure service which provides a way to automate processes as a series of simple steps. It can connect across various services and protocols. It begins with some trigger such as ‘When a certain message is received’, followed by a set of actions like ‘Send mail’. This makes Logic App perfect for monitoring and notifications.

 

Here, we will create a Logic App that connects to our IoT Hub and a mailbox for monitoring temperature and sending notifications.

If you do not already have a Microsoft Azure account, you can refer the topic Microsoft Azure Free Sign-Up in the Getting Started section of Microsoft Azure.

If you have not created an IoT Hub already, you can refer the topic Creating an IoT Hub in Microsoft Azure in the Getting Started section of Microsoft Azure.

If you do not have an application that sends data to your IoT Hub, you can refer the topic Sending DHT11 sensor data to IoT Hub using NodeMCU in the Getting Started section of Microsoft Azure.

 

The application that we are using sends DHT11 sensor data in a message string to the IoT Hub. The message string that the application sends also contains a temperatureAlert = "true" field which is sent when the temperature is above 30° C.

 

The Logic App created will make use of the temperatureAlert field as a trigger to send mail to specified recipients.

 

Now that we have an application that sends the specific messages as mentioned above to an IoT Hub, we will need to do the following to get our Logic App up and running :

  • Create a service bus and add a queue to the bus.
  • Add an endpoint and a routing rule to the IoT Hub.
  • Create and configure a Logic App.

 

Create a Service Bus Namespace and add a Service Bus Queue

Create a Service Bus Namespace

1.  Sign in to your Microsoft Azure account.

2.  Select New --> Enterprise Integration --> Service Bus

Service Bus

3.  A new pane (window) named Create namespace will open. Fill in the details asked.

  • Name : Choose a name. The name must be globally unique. If the name is valid, a green check mark will appear.
  • Pricing tier : Select the Basic pricing tier and click on Select.
  • Resource group : Select the same resource group as the one selected for your IoT Hub.
  • Location : Select the same location as the one selected for your IoT Hub.

Service Bus Namespace

4.  Click Create.
It may take a few minutes to create the Service Bus.
You will get a notification on completion of creation of the Service Bus.

 

Add a Service Bus Queue

1.  Open the Service Bus Namespace.

2.  Click on + Queue.

3.  Enter a name for the queue. If the name is valid, a green check mark will appear.

4.  Do not modify any other options.

Create Queue

5.  Click Create.

6.  Open the Service Bus Namespace again. Click on Shared access policies.

7.  In that, click on + Add.

8.  Enter a name for the policy; a green check mark appears if it is a valid name. Select the Manage checkbox and click Create.

Service Bus Policy

 

Add an Endpoint and a Routing Rule to the IoT Hub

Add an Endpoint

1.  If you are using an IoT Hub of F1 Free tier, it can have only one endpoint. So, if you already have created an endpoint, delete that endpoint and create a new endpoint. You will have to delete the routes created for the endpoint before deleting the endpoint.

2.  Open your IoT Hub and click on Endpoints which is listed under Messaging. Click on + Add.

Add Endpoint

3.  An Add Endpoint window will appear. Enter the details asked.

  • Name : Choose a name for the endpoint. A green checkmark appears if the name is valid.
  • Endpoint type : Select Service Bus Queue from the options available.
  • After selecting Service Bus Queue as the endpoint type, more options will appear.
  • Service Bus namespace : Select the namespace you created.
  • Service Bus queue : Select the queue you created.

4.  Click OK.

Endpoint Details

 

Add a Routing Rule

1.  Open your IoT Hub and click on Routes which is listed under Messaging.

2.  Click on + Add.

Add Route

3.  A new window named Create a new route will open. Fill the details asked.

  • Name : Choose a name for the route. A green checkmark appears if it is a valid name.
  • Data source : Select Device Messages from the list of options.
  • Endpoint : Select the endpoint you created earlier.
  • Query String : Type temperatureAlert = “true”

4.  Click Save.

Routing Rule

 

Create and Configure a Logic App

Create a Logic App

1.  On your Azure portal, select New --> Enterprise Integration --> Logic App

Logic App

2.  A Create Logic App window will open. Fill in the details asked.

  • Name : Choose a name for the Logic App. A green checkmark appears if the name is valid.
  • Resource group : Use the same resource group as the one you used for your IoT Hub.
  • Location : Use the same location as the one you used for your IoT Hub.

3.  Click Create.

Logic App Details

 

Configure the Logic App

1.  Open the Logic App you created. It will open into the Logic App Designer.

2.  In the Logic App Designer, scroll down. You will find Templates. Select the Blank Logic App template.

Blank Template

3.  In the window that will open, click on Service Bus listed under the Connectors.

Select Service Bus

4.  Triggers related to Service Bus will appear. Out of them, select the trigger for Service Bus-When one or more messages arrive in a queue (auto-complete).

Service Bus Trigger

5.   A window will open. Enter a connection name. Click on the Service Bus namespace you had created. Then click on the Service Bus policy you had created.

Service Bus Details 1

Service Bus Details 2

6.  Click on Create.

7.  A new window will open. Select the queue you had created. Enter 175 for Maximum message count.

Queue Details

8.  Click on Save.

9.  Click on New Step --> Add an action.

Add an Action

10.  Type SMTP in the search box. Click on the SMTP listed under Connectors. In the following window, click on SMTP-Send Email.

SMTP Connector

SMTP Trigger

11.  In the window that appears next, enter details of your mailbox.
The Username and Password fields require your email account ID and password.
Enter SMTP server address and SMTP port number of your email service provider. You will find this information from the website of your email service provider. SMTP information for Gmail, Yahoo Mail, and Outlook.com.
Select Enable SSL?checkbox.
Click Create.
Note : Your email service provider may need to verify the sender identity to make sure it is you who is sending the email. You will get an email asking you to verify if it is you and allowing the app access. Once you do that, you will start receiving the notification emails.
Also, disable two-step verification on your email, it will lead to authentication problems and the notifications will not be delivered.

SMTP Configuration 1

In the next window, enter the following.
Enter your email address for To and From.
Enter the password for your email.
High temperature detected for Subject and Body.

SMTP Configuration 2 

12.  Click on Save.

 

The Logic App is now configured and working.

Now connect the device and start sending DHT11 sensor data to your IoT Hub. Increase the temperature above 30° C using a heating element like a candle or a burner.

You will receive notification mail sent by the Logic App.

 


Ad