Frist steps with Azure IoT Hub

While ordering the new Raspberry Pi 3 Model B today, I just looked into the Azure IoT Hub, tried it out and wrote down what I accomplished till now.

Raspberry Pi 3 B 2016

If you are interested how I made my first steps with the Azure IoT, while implementing a device registration into ASP.net, just read on.

Basically I followed the getting started with the IoT Hub guide from Dominic Betts.

Create a new Azure IoT Hub

First you log on to the Azure Portal and click new. Next hit “Internet of Things” and then “Azure IoT Hub“. Provide a name for your hub, select the pricing tier, a resource group or create a new one, your subscription and at last the location your hub should be hosted in.

Create an Azure IoT hub

It takes about 5min to create your hub, time we can use to create our ASP.net MVC project.

ASP.net MVC Project

Open Visual Studio and create a new project, next select web and ASP.net Web Application.

Create an ASP site to comunicate with Azure IoT hub

For the simplicity hit “Empty” and get rid of the authentication.

Hint: If you don’t want to go over all steps by yourself, check out my GitHub repo and grab the complete solution.

Open the NuGet package manager and search for the “Microsoft.Azure.Devices” reference, select your project and hit install.

Add Azure IoT references in VS

Next add two folders to your solution. First add a folder called “Service“, then add a new class (I named it MyDevice here) to that folder and add the code below.


On the image below you can see how you can retrieve your connection string, copy it from your portal and replace the IOTHUBSTRING with your connection string.
Azure IoT hub connection string
At this point VS 2015 normally recognized that you’re trying to build a website and recommends to add a few packages to your project (e.g. Bootstrap). If not, you have to add at least a View folder, within this folder create another folder called like your controller (in my example a folder called Home), add an Index.cshtml file. Click right on the created index file and hit “Set As Start Page“.

Register a device

Next we want to add the code to register a device with the Azure IoT Hub. In our MyDevice class we add the following static method:


Next we have to make a few changes to our view (index.cshtml).

Then our HomeController,  at first we want to add two new using statements.

Then a new method to handle the form post and the call of our AddDevice method through the Azure IoT Hub.

List your devices and keys

To use our devices later on, we need to know the security keys first. So let’s add a method to retrieve the keys and show them on our view. Like before we add a new method to the MyDevice class.


We also want to pass the list of devices to our view, so let’s edit the Index method of our HomeController.

And also we’re adding a few new lines to our view.

Next the MyDevice class.

Now your site should look like in the screenshot below. You can add new devices by defining an id and hit “Add Device“. You also get a list of all your registered devices and you could delete them by hitting the little “trash” icon behind the keys.
Azure IoT hub control with ASP net
If you navigate back to the portal and open your IoT Hub pane you should also see your added devices on the overview page, like shown below.
IoT devices Azure hub

Demo Code

Advertisement

You may also like...