Different ways to register devices to the Azure IoT Hub

The Azure IoT Hub is Microsoft’s Internet of Things solution in the cloud. Beside the common event message 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 a Windows 10 IoT core device’s TPM storage.

iotdashboardnewdevice

Hint: The connection from the IoT Dashboard to Microsoft Azure currently only works with the public datacenters. If your IoT Hub lives in restricted datacenters like 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 with restricted datacenters like the German ones.

Once you entered the connection string for your IoT Hub, that can be found inside the Azure Portal at your IoT Hub’s Shared 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 tokens.

deviceexplorernewdevice

Command Line Tool

To manage an 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-explorer cli 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 never register 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.Devices NuGet 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:

Start the console application and the device’s primary key will be printed out at the console and can also be seen inside the Azure 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:

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

One Comment on “Different ways to register devices to the Azure IoT Hub

Leave a Reply