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 4Create 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 6Click 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 8In 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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Azure.Devices;
- using Microsoft.Azure.Devices.Common.Exceptions;
-
- namespace DemoDeviceIdentity
- {
- class Program
- {
- static RegistryManager registryManager;
- static string connectionString = "{iot hub connection string}";
- private static async Task AddDeviceAsync()
- {
- string deviceId = "myFirstDevice";
- Device device;
- try
- {
- device = await registryManager.AddDeviceAsync(new Device(deviceId));
- }
- catch (DeviceAlreadyExistsException)
- {
- device = await registryManager.GetDeviceAsync(deviceId);
- }
- Console.WriteLine("Generated device key: {0}", device.Authentication.SymmetricKey.PrimaryKey);
- }
- static void Main(string[] args)
- {
- registryManager = RegistryManager.CreateFromConnectionString(connectionString);
- AddDeviceAsync().Wait();
- Console.ReadLine();
- }
- }
- }
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.