The course is part of this learning path
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 I’ll show you how to send feedback to specific devices with Azure Functions.
We’ve seen three ways to process events and generate feedback. However we haven’t looked at ways to actually send the feedback to a specific device. So let’s create a new function that can help.
We are back in the Azure Function App and need a new function.
We’ll use an EventHub-triggered C# function, which will listen to the feedback endpoint. Let’s call it HandleFeedBack. I’ll select the "feedback" eventhub connection and the "feedback" event hub...and click create.
First up, we need to parse the message into an object.
The object needs a device ID so we know which device to send the feedback to, and a command, which represents the method we want to run on the device.
We need to add a reference to, and import our JSON serializer…
And then we can deserialize our message into a Feedback object.
Imagining we have commands, I’ll use a switch statement to check the command. And I’ll have the default rule raise an exception for command not supported.
Then we can handle the specific "SWITCH-ON" command string. This is where we’ll notify a device to execute a method. However first, we need to get the Devices library installed.
To do that we need to save, then go to "View files" and add a new project.json file which allows us to specify our dependencies; these will be downloaded from nuget. Here we need the Microsoft.Azure.Devices library.
Saving this, you can see in the log window that the packages are restored.
Now we can add the devices namespace... great.
In the switch statement, we can create a ServiceClient. This requires a connection string which you can get in the IoT Hub app, under the shared access policies; you’ll need a policy that has service permissions. I’ll use the Azure CLI 2.0 to fetch it...and copy it to the clipboard...great.
In a production app, you shouldn’t hard code this, instead you should use app settings.
Now we need to create a new CloudToDeviceMethod object and pass in the method we want to have run.
Then we can use the invoke device method call to actually send the request to the device.
So this is all we need on the server side; we have a function that listens for feedback, and when an event is picked up, we check the command type and then send off a request to the device to execute the method.
Now let’s check out the device code needed to run the method...
Notice this method here in the device simulator code. It waits for IoT Hub to send a request to run the SwitchOn method, and when it arrives it, writes some text to the console. So let’s try this out.
I have a command prompt here and I’ll run the device simulator. If I leave the data to 20 nothing happens, and that’s because no feedback is generated. However if I increase the value to 27...and we wait just a moment...
And now let’s decrease the data value...Notice we’re seeing some “Switch On requests mixed into the output, and the function is being kicked off”
Let’s do it one more time, I’ll increase the data value to 27…
And notice the output shows the results of the SwitchOn device method.
So we can use the feedback endpoint to send messages back to devices, and Azure Functions is an easy way to do it.
In the next lesson we’ll wrap up the course with some final thoughts.
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.