title | description | services | author | ms.service | ms.topic | ms.date | ms.author | ms.custom |
---|---|---|---|---|---|---|---|---|
Tutorial - Use IoT Hub events to trigger Azure Logic Apps |
This tutorial shows how to use the event routing service of Azure Event Grid, create automated processes to perform Azure Logic Apps actions based on IoT Hub events. |
iot-hub, event-grid |
philmea |
iot-hub |
tutorial |
09/14/2020 |
philmea |
devx-track-azurecli |
Azure Event Grid enables you to react to events in IoT Hub by triggering actions in your downstream business applications.
This article walks through a sample configuration that uses IoT Hub and Event Grid. At the end, you have an Azure logic app set up to send a notification email every time a device connects or disconnects to your IoT hub. Event Grid can be used to get timely notification about critical devices disconnecting. Metrics and Diagnostics can take several (i.e. 20 or more -- though we don't want to put a number on it) minutes to show up in logs/alerts. That might be unacceptable for critical infrastructure.
[!INCLUDE quickstarts-free-trial-note]
- An email account from any email provider that is supported by Azure Logic Apps, such as Office 365 Outlook or Outlook.com. This email account is used to send the event notifications.
[!INCLUDE azure-cli-prepare-your-environment.md]
You can quickly create a new IoT hub using the Azure Cloud Shell terminal in the portal.
-
Sign in to the Azure portal.
-
On the upper right of the page, select the Cloud Shell button.
-
Run the following command to create a new resource group:
az group create --name {your resource group name} --location westus
-
Run the following command to create an IoT hub:
az iot hub create --name {your iot hub name} --resource-group {your resource group name} --sku S1
-
Minimize the Cloud Shell terminal. You will return to the shell later in the tutorial.
Next, create a logic app and add an HTTP event grid trigger that processes requests from IoT hub.
-
In the Azure portal, select Create a resource, then type "logic app" in the search box and select return. Select Logic App from the results.
-
On the next screen, select Create.
-
Give your logic app a name that's unique in your subscription, then select the same subscription, resource group, and location as your IoT hub.
-
Select Review + create.
-
Verify your settings and then select Create.
-
Once the resource is created, select Go to resource.
-
In the Logic Apps Designer, page down to see Templates. Choose Blank Logic App so that you can build your logic app from scratch.
A trigger is a specific event that starts your logic app. For this tutorial, the trigger that sets off the workflow is receiving a request over HTTP.
-
In the connectors and triggers search bar, type HTTP.
-
Scroll through the results and select Request - When an HTTP request is received as the trigger.
-
Select Use sample payload to generate schema.
-
Paste the Device connected event schema JSON into the text box, then select Done:
[{ "id": "f6bbf8f4-d365-520d-a878-17bf7238abd8", "topic": "/SUBSCRIPTIONS/<subscription ID>/RESOURCEGROUPS/<resource group name>/PROVIDERS/MICROSOFT.DEVICES/IOTHUBS/<hub name>", "subject": "devices/LogicAppTestDevice", "eventType": "Microsoft.Devices.DeviceConnected", "eventTime": "2018-06-02T19:17:44.4383997Z", "data": { "deviceConnectionStateEventInfo": { "sequenceNumber": "000000000000000001D4132452F67CE200000002000000000000000000000001" }, "hubName": "egtesthub1", "deviceId": "LogicAppTestDevice", "moduleId" : "DeviceModuleID" }, "dataVersion": "1", "metadataVersion": "1" }]
This event publishes when a device is connected to an IoT hub.
Note
You may receive a pop-up notification that says, Remember to include a Content-Type header set to application/json in your request. You can safely ignore this suggestion, and move on to the next section.
Actions are any steps that occur after the trigger starts the logic app workflow. For this tutorial, the action is to send an email notification from your email provider.
-
Select New step. This opens a window to Choose an action.
-
Search for Outlook.
-
Based on your email provider, find and select the matching connector. This tutorial uses Outlook.com. The steps for other email providers are similar.
-
Select the Send an email (V2) action.
-
Select Sign in and sign in to your email account. Select Yes to let the app access your info.
-
Build your email template.
-
To: Enter the email address to receive the notification emails. For this tutorial, use an email account that you can access for testing.
-
Subject: Fill in the text for the subject. When you click on the Subject text box, you can select dynamic content to include. For example, this tutorial uses
IoT Hub alert: {eventType}
. If you can't see Dynamic content, select the Add dynamic content hyperlink -- this toggles it on and off. -
Body: Write the text for your email. Select JSON properties from the selector tool to include dynamic content based on event data. If you can't see the Dynamic content, select the Add dynamic content hyperlink under the Body text box. If it doesn't show you the fields you want, click more in the Dynamic content screen to include the fields from the previous action.
Your email template may look like this example:
-
-
Select Save in the Logic Apps Designer.
Before you leave the Logic Apps Designer, copy the URL that your logic apps is listening to for a trigger. You use this URL to configure Event Grid.
-
Expand the When a HTTP request is received trigger configuration box by clicking on it.
-
Copy the value of HTTP POST URL by selecting the copy button next to it.
-
Save this URL so that you can refer to it in the next section.
In this section, you configure your IoT Hub to publish events as they occur.
-
In the Azure portal, navigate to your IoT hub. You can do this by selecting Resource groups, then select the resource group for this tutorial, and then select your IoT hub from the list of resources.
-
Select Events.
-
Select Event subscription.
-
Create the event subscription with the following values:
-
In the EVENT SUBSCRIPTION DETAILS section:
- Provide a name for the event subscription.
- Select Event Grid Schema for Event Schema.
-
In the TOPIC DETAILS section:
- Confirm that the Topic type is set to IoT Hub.
- Confirm that the name of the IoT hub is set as the value for the Source Resource field.
- Enter a name for the system topic that will be created for you. To learn about system topics, see Overview of system topics.
-
In the EVENT TYPES section:
-
In the ENDPOINT DETAILS section:
- Select Endpoint Type as Web Hook.
- Click select an endpoint, paste the URL that you copied from your logic app, and confirm selection.
When you're done, the pane should look like the following example:
-
-
Select Create.
Test your logic app by quickly simulating a device connection using the Azure CLI.
-
Select the Cloud Shell button to re-open your terminal.
-
Run the following command to create a simulated device identity:
az iot hub device-identity create --device-id simDevice --hub-name {YourIoTHubName}
-
Run the following command to simulate connecting your device to IoT Hub and sending telemetry:
az iot device simulate -d simDevice -n {YourIoTHubName}
-
When the simulated device connects to IoT Hub, you will receive an email notifying you of a "DeviceConnected" event.
-
When the simulation completes, you will receive an email notifying you of a "DeviceDisconnected" event.
This tutorial used resources that incur charges on your Azure subscription. When you're finished trying out the tutorial and testing your results, disable or delete resources that you don't want to keep.
To delete all of the resources created in this tutorial, delete the resource group.
-
Select Resource groups, then select the resource group you created for this tutorial.
-
On the Resource group pane, select Delete resource group. You are prompted to enter the resource group name, and then you can delete it. All of the resources contained therein are also removed.
- Learn more about Reacting to IoT Hub events by using Event Grid to trigger actions.
- Learn how to order device connected and disconnected events
- Learn about what else you can do with Event Grid.
For a complete list of supported Logic App connectors, see the
[!div class="nextstepaction"] Connectors overview.