Introduction
For the past few days, I have been playing with my MXChip and wrote some articles about it. Here in this article, we will send some data to our Azure IoT hub and we will connect an Azure Function with our IoT Hub using IoTHubTrigger Event Hub Trigger Attribute. If you are ready, let’s do this. You can always read this article on my blog here.
Background
As I said, this article is part of my IoT article series. If you have read my previous articles on this topic, it may be easier for you to understand the concept. Before you start this article, please make sure that you had already created an Azure IoT hub and it is running. You can always send the messages to this IoT Hub either by connecting the actual device, let’s say an MXChip or using a simulating device.
IoTHubTrigger Demo
Creating an Azure Function App
I am going to create an Azure Function App in Visual Studio, if you are not sure about how we can create and publish the Azure Function, please read this section of my previous article. Let’s create a new solution now.

IoTHubTrigger
Once you click OK, a new Azure Function will be generated for you with some initial codes in it. Let’s edit the Function as below.
- using Microsoft.Azure.EventHubs;
- using Microsoft.Azure.WebJobs;
- using Microsoft.Extensions.Logging;
- using System.Text;
- using IoTHubTrigger = Microsoft.Azure.WebJobs.EventHubTriggerAttribute;
- namespace IoTHubTrigger_Azure_Function_and_Azure_IoT_Hub {
- public static class IoTHubFunc {
- [FunctionName("IoTHubData")]
- public static void Run([IoTHubTrigger("messages/events", Connection = "IoTHubTriggerConnection", ConsumerGroup = "FuncGroup")] EventData message, ILogger log)
- {
- log.LogInformation($ "C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.Body.Array)}");
- }
- }
- }


R KumarPosted Mar 7, 2021, 11:47 AM
Hi Sibeesh.. Thanks for sharing your knowledge. I have a problem, please let me know if you have any solution. How to trigger an Azure function whenever a new IoT Device is added in the IoT Hub?