IoTHubTrigger Azure Function And Azure IoT Hub

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
 
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.
  1. using Microsoft.Azure.EventHubs;    
  2. using Microsoft.Azure.WebJobs;    
  3. using Microsoft.Extensions.Logging;    
  4. using System.Text;    
  5. using IoTHubTrigger = Microsoft.Azure.WebJobs.EventHubTriggerAttribute;    
  6. namespace IoTHubTrigger_Azure_Function_and_Azure_IoT_Hub {    
  7.     public static class IoTHubFunc {    
  8.         [FunctionName("IoTHubData")]     
  9.         public static void Run([IoTHubTrigger("messages/events", Connection = "IoTHubTriggerConnection", ConsumerGroup = "FuncGroup")] EventData message, ILogger log)     
  10.         {    
  11.             log.LogInformation($ "C# IoT Hub trigger function processed a message: {Encoding.UTF8.GetString(message.Body.Array)}");    
  12.         }    
  13.     }    
  14. }  
Here, the IoTHubTriggerConnection is the connection string we are providing in the local.settings.json file. The consumer group will come into the play if you have many applications which need to be receiving the data from your IoT Hub. Below is the class definition of EventHubTriggerAttribute.
  1. public sealed class EventHubTriggerAttribute: Attribute {    
  2.     public EventHubTriggerAttribute(string eventHubName);    
  3.     public string EventHubName {    
  4.         get;    
  5.     }    
  6.     public string ConsumerGroup {    
  7.         get;    
  8.         set;    
  9.     }    
  10.     public string Connection {    
  11.         get;    
  12.         set;    
  13.     }    
  14. }   
Send data to the Azure IoT Hub
 
As I mentioned earlier, there are two ways 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 the 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, simulate the device application, and see the output.
 
IoT Hub Trigger Output
 
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.
 

Conclusion

 
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 read 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? Did you find this post useful? Kindly do not forget to share your feedback.