Different ways to register devices to the Azure IoT Hub

The Azure IoT Hub is Microsoft’s Internet of Things solution inthe cloud. Beside the commoneventmessage endpoints, it contains a device management system, which allows to register and manage devices that may send data to the IoT Hub. This increases security by generating a unique access key for every single device that ensures, that devices can only do what they are supposed to allows to disable single devices when they get corrupted.Due to different scenarios, there are several ways to register devices at the IoT Hub.

Windows IoT Core Dashboard

If you are running Windows, the easiest way to register a new device might be the Windows 10 IoT Core Dashboard. It contains a section called Connect to Azure, that allows you to log in with your Microsoft Azure credentials and select an IoT Hub in your subscription, that you want to connect your device to. Once you created a new device, you can get its Security Key inside the Azure Portal or directly write it to aWindows 10 IoT core device’s TPM storageiotdashboardnewdevice](http://pumpingco.de/content/images/2016/10/iotdashboardnewdevice.png)

Hint: The connection from the IoT Dashboard to Microsoft Azure currently only works with the public datacenters. If your IoT Hub lives in restricted datacenterslike the German ones, you currently need to choose one of the other options below.

Device Explorer for IoT Hub

The easiest and at the same time most flexible way to register a device when running on Windows might be the Device Explorer which is an open source tool that manages IoT Hub connected devices simply with the respective connection string. This also works withrestricted datacenterslike the German ones.

Once you entered the connection string for your IoT Hub, that can be foundinside the Azure Portalat your IoT Hub’sShared access policies section at you iothubowner policy, hit the Update button and switch to the tool’s Management tab. Here you can manage all of your devices and their security tokensdeviceexplorernewdevice](http://pumpingco.de/content/images/2016/10/deviceexplorernewdevice.png)

Command Line Tool

To managean IoT Hub platform independent and automate things, command line interfaces based on Node.js are always a good choice. Beside, typing commands in a terminal window looks a lot cooler than clicking things on a UI. The iothub-explorercli is exactly this. It can be installed easily as a Node module and works on every platform.

Using code (C# or JavaScript)

Beside all that, Microsoft also offers libraries for several development frameworks to register a device. Especially when you want to automate device registration or you are not working on a Windows machine, this might be an interesting way for you to go.

**Hint:**Due to security reasons, devices should neverregister themselves at the IoT Hub. For the device registration, are registry write permissions (typically the iothubowner policy) required, that should not be given to a single device. Especially in case of a stolen or corrupted device, it is important, to exclude the device without giving it the chance to re-register.

.NET

When using .NET, you simply need to add the Azure.DevicesNuGet Package to your project. Unfortunately, when writing this article, the package is not compatible with .NET core yet, so it only works on Windows at the moment. Register your device with these simple lines of code:

using System; using System.Threading.Tasks; using Microsoft.Azure.Devices; using Microsoft.Azure.Devices.Common.Exceptions; class Program { static string connectionString = "{iot hub connection string}"; static string deviceId = "{device id}"; static RegistryManager registryManager; static void Main(string[] args) { registryManager = RegistryManager.CreateFromConnectionString(connectionString); AddDeviceAsync().Wait(); Console.ReadLine(); } static async Task AddDeviceAsync() { 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); } }

Start the console application and the device’s primary key will be printed out at the console and can also be seen inside theAzure Portal, of course.

Node.js

To register devices on other platforms than Windows, Microsoft also offers a Node.js package to work with the Azure IoT Hub. For this, simply add the azure-iothub package to your project and register a new device with the following code:

var iothub = require('azure-iothub'); var connectionString = "{iot hub connection string}"; var deviceId = "{device id}"; var registry = iothub.Registry.fromConnectionString(connectionString); var device = new iothub.Device(null); device.deviceId = deviceId; registry.create(device, function(err, deviceInfo, res) { if (err) registry.get(device.deviceId, printDeviceInfo); if (deviceInfo) printDeviceInfo(err, deviceInfo, res) }); function printDeviceInfo(err, deviceInfo, res) { if (deviceInfo) console.log('Generated device key: ' + deviceInfo.authentication.SymmetricKey.primaryKey); }

Same as above, the device’s primary key will be printed out at the console and can also be seen inside theAzure Portal, when running the application.Of course, both libraries also offer methods to delete or update devices.