Introduction
One of the great features of IoT hub is its built-in systems endpoints. This allows us to quickly hook into event hub belonging to an IoT hub without the need to an EventHub, Azure Storage containers, Service Bus queues and Service Bus topics.
In this post, we are going to look into how to easily we can leverage the built-in endpoints in Azure IoT hub by creating a simple application using Azure Functions.
When you have an Azure IoT hub and want to hook into events generated by Azure IoT hub, you have two main choices.
1. Events provided as part of Azure Event Grid
Using Azure Event Grid we can use resources such as Logic Apps, Azure Functions, Web Hook, Storage Queues, Event Hubs, and Hybrid Connections.
2. Built-in endpoints
This is the built in endpoint that every Azure IoT hub offers as part of its infrastructure. Using Built-in endpoints we can hook into system and device messages. So when a device sends a message to Azure IoT hub, this "Built-in endpoints" to receive those messages.
Figure 1 - Built-in endpoints view |
Using Built-in endpoints
To use built-in endpoints, we will create a custom Azure functions that uses the built-in endpoint to listen to messages emitted by the device to Azure IoT hub. We will use the following steps to accomplish that.
Step 1- Get Event Hub-compatible endpoint
- Open the Azure portal and navigate to Azure IoT hub.
- Click "Built-in endpoints" in the navigation pane. This will show a view that is similar to view show in Figure 1.
- Copy "Event Hub-compatible endpoint" and save it to be used in the Azure Function app that is mentioned in Step 2.
Step 2 - Creating Azure Function App
1. Using the boiler plate project that is created when Azure Function App is selected, we can modify the code as shown below.
Figure 2 - Azure function code |
2. Update connection string:
Using the connection string saved in Step 1, update the local.settings.json as shown below:
Figure 3 - Event hub connection string using Built-in endpoint |
3. Managing dependencies:
In order to use the Event Hub triggers, we have to add the following NuGet packages:
Figure 4 - Dependencies |
Conclusion
We have seen how easy it is to use Built-in endpoints to hook into Azure IoT hub events. Although we have used Azure Functions app to demonstrate the application that is listening to Azure IoT events using Built-In endpoints, we can use any application platform that can be invoked using EventHubTrigger.