Click here to Skip to main content
15,882,241 members
Articles / Internet of Things

IoTHubTrigger Azure Function and Azure IoT Hub

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
28 Dec 2018CPOL3 min read 8.8K   1   2
IoTHubTrigger Azure Function and Azure IoT Hub

This article is an entry in our Microsoft Azure IoT Contest. Articles in this section are not required to be full articles so care should be taken when voting.

Table of Contents


For the past few days, I have been playing with my MXChip and had written some articles about the same, you should be able to find those here. 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.


As I said, this article is part of my IoT article series, so 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.

Image 1


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
        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: 

Here, the IoTHubTriggerConnection is the connection string we are providing in the local.settings.json file. The consumer group will come into play if you have many applications which need to be receiving the data from your IoT Hub. Below is the class definition of EventHubTriggerAttribute.

public sealed class EventHubTriggerAttribute : Attribute
        public EventHubTriggerAttribute(string eventHubName);

        public string EventHubName { get; }
        public string ConsumerGroup { get; set; }
        public string Connection { get; set; }

Send Data to the Azure IoT Hub

As I mentioned earlier, there are two ways in which you can send the data to the Azure IoT Hub.

  1. Using a device, for example, MXChip
  2. Simulated device

Once the data is sending, you can see the message received count in Azure IoT Hub in the overview section.

Run the Azure Function

So, the IoT Hub has started receiving messages from the device, and now we can use our Azure Function to pull the data from the IoT hub with the help of IoT hub Trigger. Run your Function App, Simulated Device application and see the output.

Image 2

IoT Hub Trigger Output

As you can see in the output, our Azure Function app is receiving the data from Azure IoT hub instantly. Now you can perform any actions with this data. I will leave that to you.


Wow! Now we have learned:

  • Usage of Azure IoT Hub Trigger in Azure Function
  • Creating Azure Function App
  • See the Data from Azure IoT Hub in the Azure Function App

You can always ready my IoT articles here.

Your Turn. What Do You Think?

Thanks a lot for reading. Did I miss anything that you may think is needed in this article? Could you find this post useful? Kindly remember to share your feedback.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
Germany Germany
I am Sibeesh Venu, an engineer by profession and writer by passion. I’m neither an expert nor a guru. I have been awarded Microsoft MVP 3 times, C# Corner MVP 5 times, DZone MVB. I always love to learn new technologies, and I strongly believe that the one who stops learning is old.

My Blog: Sibeesh Passion
My Website: Sibeesh Venu

Comments and Discussions

QuestionMessage Closed Pin
27-Feb-21 22:36
Member 1508594027-Feb-21 22:36 
QuestionWhat version of Microsoft.Azure.WebJobs nuget is used? Pin
Deepak Rao Kumpala17-Jan-19 4:54
Deepak Rao Kumpala17-Jan-19 4:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.