How to Send Device Telemetry Data From Azure IoT Hub to Losant

Microsoft Azure provides the raw cloud infrastructure to build cloud-native applications and also contains their IoT management service, Azure IoT Hub. Azure IoT Hub integrates with Azure Event Grid so that you can send events to Losant via Webhoooks.

Because Losant is device agnostic, data can come from anywhere, including Azure, to power your IoT solution, including End-user Experiences. This tutorial will walk you through getting data from Azure IoT Hub into Losant.

Prerequisites

  • Familiarity with Microsoft Azure and IoT Hub
  • Devices registered and reporting telemetry data to Azure’s IoT Hub.

Creating a Webhook

First, let’s create a Webhook to receive messages from Azure Event Grid.

  1. Navigate to “Webhooks”.

Create Webhook

  1. Select “Add Workflow” and give your Webhook a friendly name.

  2. Select “Wait for reply from workflow”

  3. Select “Create Webhook”.

Create Webhook

Upon Creating your Webhook, Losant will generate a URL that looks like the following:

https://triggers.losant.com/webhooks/osf69cFXmBXqRBVRHms30J5CSmfUL5xpNU5rAc3G

Make note of this URL as we will be using to configure Azure Event Grid.

When selecting “Wait for reply from workflow”. This is helpful if you would like to send a response back to Azure. This provides an easy way for you to communicate back and forth with Azure IoT Hub. If not, Losant will always respond with {"success": true}.

Create a Workflow

In order to effectively monitor the messages being sent from Azure through the webhook endpoint, let’s create a Workflow that is triggered when our webhook is hit.

  1. Download Example Workflow.

  2. Import the example Application Workflow.

Once done, you will have imported a workflow that looks like the following:

  1. Update the Webhook Trigger Node to select your newly created Webhook as the “Webhook Name”.

Configure Azure IoT Hub

Now, you have a workflow to accept messages from Azure. Next, we will configure the Azure Event Hub and walkthrough the workflow we imported.

  1. Within IoT Hub, select “Events”.

Azure IoT Hub Events

  1. Create a new Event Subscription.

Event Subscription

  1. Configure the following properties for the new Event Subscription:

    1. Name the Event Subscription.
    2. Configure data as the “System Topic Name”.
    3. In the “Event Types” configuration, Select at least “Device Telemetry” and any other event you’d like to forward to Losant.
    4. Select “Web Hook” as the “Endpoint Type”
    5. Place the URL for the Losant Webhook created in the previous section as the “Endpoint”.

Configure Event

Note: Ensure that your Azure subscription has the EventGrid and EventHub resources registered. See the Microsoft Documentation on Registering Resource Providers for more information.

Testing

Now that you’ve configured your Webhook, and Azure IoT Hub as events are now forwarding!

Microsoft provides the following quickstart guide to simulate device data using a Node.js back end application: Quickstart: Send telemetry from a device to an IoT hub and read it with a back-end application (Node.js).

Now, let’s go through the Workflow to see how it’s functioning.

Webhook Verfication

When you first create your EventGrid subscription, the first message sent to your Webhook URL by Azure is a request for validation.

In order to successfully validate the Webhook, Losant must reply back to Azure with the validation code contained in this initial subscription validation event.

If the Event Subscription was created properly in Azure, the very first message to Losant contains a payload that resembles the following:

Azure Verfication Response

The string found under validationCode is what Losant needs to reply back to Azure with, along with a HTTP 200 OK response status code.

The reply happens in the Webhook: Reply Output Node.

In the “Reply Body” configuration, we provide a response that contains the validation code from the initial payload:

{
  "validationResponse": "{{data.body.[0].data.validationCode}}"
}

After the verfication is complete, any subsequent request will be validated.

Handling Event Types

When the Event Hub subscription was created in Azure, five options existed for event types: Device Created, Device Deleted, Device Connected, Device Disconnected and Device Telemetry.

In the example workflow, you can use the Switch Node to handle each event type in Losant.

At the payload path {{data.body.[0].eventType}} we will find the event type for the current request.

Decoding Data From Azure IoT Hub

The device telemetry data from Azure for our simulated device is delivered to Losant as a Base64 encoded string, this will need to be decoded for use by our Losant device.

Using the Mutate Node, you can use templating to decode base64-encoded payload value:

{{{decodeBase64 data.body.[0].data.body}}}

Mutate node

What’s Next

Upon completion of this workflow, device data from Azure is successfully being reported to Losant and is available for use across the Losant platform - use in other workflows, or the development of data visualization through a dashboard, as seen below:

Final Workflow

Here are further resoruces as you build your application: