Creating An IoT Hub On The Azure Portal

This article shows how to create an IoT Hub on the Azure Portal and also how to create the device Identity for simulating a device on PC.

In this article, I am going to demonstrate how to create the IoT Hub on the Azure portal and create a .NET console application that creates a device identity in our IoT Hub.
 
Prerequisites
  • An Azure Account.
  • Visual Studio 2015 or Higher.
What is an IoT Hub?
  
Azure IoT Hub is a fully managed service that enables reliable and secure bidirectional communications between millions of IoT devices and a solution back end. 
 
Follow the below steps to create an IoT Hub on the Azure Portal.
 
Step 1
 
Sign in to the Azure Portal.
 
Step 2
 
Press "+New" then click "Internet of Things" followed by clicking "IoT Hub."

 
 
Step 3
 
In the IoT Hub blade, enter the name for your IoT Hub. For Pricing Tier, choose the desired one for your use and press "Select".



Step 4

Create a new resource group to host your IoT Hub and also choose the nearest location to you. Then, press "Create".

 

Step 5

After the successful deployment, it opens the Properties blade for our IoT Hub. Make a note of the Hostname.

 

Step 6

Click the "Shared Access policies" under Settings, click the iothubowner policy and then copy the connection strings that connects the devices to the cloud.

 

Step 7

In this, we are going to create a .NET application that creates the device identity in the identity register in our IoT Hub. A device can't connect to IoT Hub unless it has an entry in the identity register.
 
Open Visual Studio, press File-->New-->Visual C#-->Windows classic desktop-->Console App (.NET Framework). Then, enter the name of your application and press OK.

 

Step 8

In the Solution Explorer, right-click your project, and then click "Manage NuGet Packages".

 
In the Package Manager window, choose Browser and search for Microsoft.Azure.Devices package and then press "Install" to install that package to our project by accepting the "Terms & Conditions".

 

Step 9

Copy and replace the code in Program.cs file.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.Azure.Devices;  
  7. using Microsoft.Azure.Devices.Common.Exceptions;  
  8.   
  9. namespace DemoDeviceIdentity  
  10. {  
  11.     class Program  
  12.     {  
  13.         static RegistryManager registryManager;  
  14.         static string connectionString = "{iot hub connection string}";  
  15.         private static async Task AddDeviceAsync()  
  16.         {  
  17.             string deviceId = "myFirstDevice";  
  18.             Device device;  
  19.             try  
  20.             {  
  21.                 device = await registryManager.AddDeviceAsync(new Device(deviceId));  
  22.             }  
  23.             catch (DeviceAlreadyExistsException)  
  24.             {  
  25.                 device = await registryManager.GetDeviceAsync(deviceId);  
  26.             }  
  27.             Console.WriteLine("Generated device key: {0}", device.Authentication.SymmetricKey.PrimaryKey);  
  28.         }  
  29.         static void Main(string[] args)  
  30.         {  
  31.             registryManager = RegistryManager.CreateFromConnectionString(connectionString);  
  32.             AddDeviceAsync().Wait();  
  33.             Console.ReadLine();  
  34.         }  
  35.     }  
  36. }  

Replace the 14th line of your code using the connecting string that takes from our IoT hub in the portal.

static string connectionString = "Enter your connection string Here";

Step 10

Run the application, and make a note of the key of the device generated by this program.

 

Summary

I hope you understood how to create an IoT Hub on the Azure Portal and create the device key using Visual Studio. In my next article, I am going to show how to create the console applications for reading the device to cloud messages and create a device app that simulates devices that send the device cloud message to an IoT hub.