Azure Functions: “EventHub Trigger” to process new events to a EventHub

Azure Function eventhub trigger

Azure Functions: “EventHub Trigger” to process new events to a EventHub

Dave Rendón Follow
1 Star2 Stars3 Stars4 Stars5 Stars

In this article I will show you how to create an Azure Function “EventHub Trigger” to process new events to a EventHub so that you can digest all the data coming from your IoT Device to an EventHub by using Azure Functions. This article is part of the IoT Virtual Bootcamp –  Predict Temperature using Azure IoT & Raspberry Pi3.

  • Execution Time: 10 minutes.
  • Required Hardware:
    • Windows 10 PC
    • IoT Hardware kit: https://www.adafruit.com/product/3605 or similar hardware.
    • Access to a WiFi network (without a captive portal aka web page login)
  • Required Operating System:
    • Windows 10
  • Other Requirements:
    • Active Azure Subscription

Steps:

  1. Go to the Azure Portal and create a new Azure Function with the proper parameters as shown below:

EventHub Azure functions -wikiazure

 

2. Once created go to the Azure Function blade and click on Functions -> webhook + API -> Select C# -> Create this function:

Create Azure Function - wikiazure

3. Now type in the search bar “event hub” and select the Event Hub Trigger Function:

azure-IoT-eventhub-trigger-wikiazure

 

4. Now you will need to select the values for the connection for your Azure Function:

azure-functions-4-wikiazure

5. Now click on Functions and select the Function you just created, then you will modify the code directly on the Azure Portal, you will see the default C# code:

Note: If you have data coming in, you will see it logged. If you do not see data, verify that your device is connected and sending data.

azure-functions-5-1-wikiazure

6. Now modify the default C# code and place the following:

#r "Newtonsoft.Json"

using System;
using System.Text;
using Newtonsoft.Json.Linq;

public static async Task Run(string myEventHubMessage, TraceWriter log)
{
//log.Info(myEventHubMessage);
try{
dynamic jsonData = JObject.Parse(myEventHubMessage);
double temperatureC = jsonData["temperature"];
double temperatureF = (temperatureC * 1.8) + 32;
log.Info("temperatureF=" + temperatureF.ToString());
log.Info("temperatureC=" + temperatureC.ToString());
} catch (Exception er) {
//log.Error(er.Message.ToString());
}
}

You will see an output like the one below:

AzureFunction-eventhub-wikiazure

Note: Because we are using the same consumer group as Device Manager and Stream Analytics, you might have a conflict. Either: Disable Azure Function or use a different consumer group.

This completes the Lab on how to create an Azure Function “EventHub Trigger” to process new events to a EventHub. Please refer to the  IoT Virtual Bootcamp –  Predict Temperature using Azure IoT & Raspberry Pi3.

2 Comments

Vasyl

August 31, 2018 at 3:24 am

This article is not full?
I can’t find Eventhub output.

 Reply

Dave Rendón

August 31, 2018 at 10:36 am

Hi Vasyl,

First of all, thanks for taking a look at this article, this is just a part of the entire guide lab that you can find here: https://wikiazure.com/azure-iot/predict-temperature-using-azure-iot-raspberry-pi3/

Please let me know any further question, thanks!

 Reply

Leave a Reply