The course is part of this learning path
Processing Azure IoT Hub Events and Data
Once your data is in IoT Hub, you need to be able to access it; ideally with different processors. Using different processors allows you to break out the responsibilities of each.
Azure provides a few options for processing events and sending feedback to devices. In this course we'll introduce you to Stream Analytics, Azure Functions and raw processing with C#.
If you want to follow along, or just try this out for yourself, you can find the code here.
Processing Azure IoT Hub Events and Data: What You'll Learn
|Lecture||What you'll learn|
|Course Intro||What to expect from this course|
|Understanding Streaming Data Processing for IoT Hub|
|Writing an Event Processor for IoT Hub with C#|
|Handling Device-To-Cloud data with Azure Functions|
|Processing IoT Hub data streams with Azure Stream Analytics|
|Sending feedback to devices with Azure Functions||Reviewing the code for the device|
|Final Thoughts||Wrapping up the course|
In this lesson, we’ll talk about how to process IoT Hub Device-To-Cloud messages with Azure Functions.
Using Azure Functions to process IoT Hub events allows you to perform single event processing. A function is invoked for each message present in the IoT Hub endpoint.
We have already seen in previous courses how to handle IoT Hub events with Azure Function.
However now we’re going to look at using functions for a bit more processing. We’re going to use a function for saving events to DocumentDB, and then create one for sending feedback.
So we are here in Azure Portal, we already have created an Azure Function App, as well as a function named “MonitoringEvents” with an EventHub Trigger using C#. All it does is save the events to DocumentDB.
Let’s head over to DocumentDB just to show there are no documents. And...there aren’t. Great.
Alright, let’s launch the device simulator from the command line...
Back in the Function app, notice that there are some events in the log.
I’m going to increase the value to make sure it’s working. So I’ll lower the Data value...Okay, let’s stop this, and head into DocumentDB.
Let’s go to Query Explorer...and run this query, and here’s all the messages.
So having a function that’s triggered per event is great because we don’t need to manage any infrastructure for ourselves. In this example we’re just writing the event to DocumentDB, but as you can imagine, we could also perform some data manipulation here as well.
Let’s imagine you want to add a property to the persisted data that saves a boolean indicating if an alarm should be triggered. For that you could deserialize the event into an object and then perform some check. In this case, I’ll check if the data property is greater than 24.
Then we just serialize our new object as JSON and write it to documentDB.
Let’s see this in action...if I run the device again, you can see that we have events.
I’ll increase the Data property...so we have 24, 25, 26, 27.
And let’s go back to 21...okay.
I’m going to stop this, and head back to DocumentDB.
Let’s edit this query to add a where condition. I want to see where the alarm property is set to true. And let’s run this..Awesome, there’s our documents that triggered an alarm.
If I search for "Alarm=false", run the query again, you can see the documents that didn’t trigger an alarm.
So saving events and even manipulating the data is pretty simple with an Azure Function. But you’ve seen all of this in our other courses. What about controlling the devices with a function? Let’s check that out.
Let’s create a new function, and it will also be an EventHub trigger, with C#. Let’s name it ControllingDevices.
The IoTHub connection is already configured, so I can click Create.
We need to be able to send messages to the device, and for that we can use the feedback endpoint. For that we need to go to the integrate tab and select New Output. And we’ll use an Event Hub.
Let’s create a new connection to the event hub by clicking “new” and adding a connection name...and I’ll paste in my connection string. Great.
The Event Hub name is "feedback", okay, let’s save this, and head back to the code.
I need to add the a output parameter that will contain the message to the device.
Let’s set this up to perform some processing.
I’ll Add reference to "Newtonsoft.Json" and add the namespace.
I’ll add a class for the ClientEvent type, so I can deserialize in the code.
And let’s see if the data property is greater than 24, and if so I’ll create a new feedback message...
Then I need to serialize to JSON and assign it to the eventHub out parameter
Great. I’ll save this...and check the logs...and it looks like I’m missing setting the message when the data property is less than or equal to 24. So I’ll fix this...okay, great.
Let’s start dev1 script to generate some events...
Notice we don’t have any messages. That’s because there the MonitoringEvents function is receiving the message because we using the same Event Hub and on the same default consumer group. So you can’t have competing consumers.
To get our data we need to use a different consumer group. We need to go to the Integrate tab and set a different consumer group. Let’s set this to "processor", which is a consumer group I’ve already created. And I’ll save this.
Now we’ll be able to handle our messages.
Okay, if we go here, and stop the event generation. And instead we run the feedback script, it’ll look for feedback from the IoT Hub feedback endpoint.
And you can see that we have a lot of empty commands in this demo...so let’s increase the data value...
And now now we have the feedback messages showing the "SWITCH-ON" command property set.
So Azure Functions doesn’t just make it easy to persist data, it also makes sending commands back to devices easy.
Okay, let’s wrap up this lesson here.
Processing IoT Hub events with Azure Functions is a great option, because you get to focus on business logic rather than infrastructure.
Azure functions are a great option you don’t get bulk processing out of the box, and depending on your solution that could be a deal breaker.
In the next lesson, I'll show you how to process IoT Hub data streams with Azure Stream Analytics.
About the Author
Marco Parenzan is a Research Lead for Microsoft Azure in Cloud Academy. He has been awarded three times as a Microsoft MVP on Microsoft Azure. He is a speaker in major community events in Italy about Azure and .NET development and he is a community lead for 1nn0va, an official Microsoft community in Pordenone, Italy. He has written a book on Azure in 2016. He loves IoT and retrogaming.