Smart bulbs or how to be lazier (part 4): IoT Hub

In the previous posts I showed how to use AllJoyn bridges in order to make a universal hub based on Raspberry Pi 2 which is running Windows 10. Here are the links to my previous posts:

Smart bulbs or how to be lazier (part 1)

Smart bulbs or how to be lazier (part 2)

Smart bulbs or how to be lazier (part 3)

In the previous post we created our first AllJoyn client that allows us to manage our bulbs in the local environment. Therefore, our next step is to bring the solution to the cloud. That’s why in the next couple of posts we will discuss the Azure services which help us achieve this goal.

IoT Hub vs IoT Suite

Frankly speaking, Microsoft Azure supported lots of IoT related services prior to announcing IoT Hub or IoT Suite. You can use Event Hub or similar services directly even without any services with IoT prefix. But by announcing IoT Hub and IoT Suite, Microsoft sends a clear message to all developers that Azure is ready to support even the most complex IoT scenarios. At the same time, Microsoft publishes lots of SDKs, training materials and guidelines that show how to use various Azure services for IoT.

Talking about IoT Hub and IoT Suite I would like to mention that they are two fully different services. In fact, IoT Suite is not a service at all – it’s a ready to use solution that you can use as a demo to see how to build an enterprise IoT solution. Once you create an IoT Suite instance, it will provision IoT Hub, Event Hub, Stream Analytic and other services – all things that you can build from scratch if you have strong knowledge in Azure. Since we are developers and we want to understand all things that we are doing, I am not going to use IoT Suite. Instead, we will use IoT Hub that is a special service that allows us to manage all IoT devices in our solution, supports communication protocols between devices and the cloud, and implements ways to establish secure connections to the cloud.

How to create IoT Hub

First of all, we have to create an IoT Hub instance. You can do it using Azure Portal, just clicking couple buttons:


Note that IoT Hub support a free pricing tier that is limited in number of messages and in number of Event Hub partitions but it will be enough for our bulbs. You can see that in the case of S2 plan, you can send up to 6M messages per day/unit and it is possible to create up to 200 units. So, it should be enough even for complex solutions.

Once you create the IoT Hub instance, you will be able to use the dashboard in order to see statistic and setup some parameters:


In general, you will not use the dashboard very often because value of it is very limited. Don’t try to find any dashboards with messages or list of connected devices. IoT Hub is an infrastructure service and doesn’t include any presentation logic. The most important part there is Shared access policies. Using this tab, you can get all needed keys and connection strings. All other tasks, like device registration, sending and reading messages, we will implement using IoT Hub API.

Looking at Share access policies tab you can note that there are four different permissions: Registry read, Registry write, Service connect, Device connect. The first two permissions allow you to implement logic that will register new devices or read information about existing devices. Thanks to Service connect, you can implement logic that can read messages from event hub and send messages to devices. Finally, Device connect permission should be assigned to a device. Thanks to that, the device will be able to send and receive messages to/from IoT Hub.

How to register our Hub

So, how to use IoT Hub in order to start collecting messages from our bulbs? We have to start with registration of our devices in IoT Hub.

Thinking about device registration, we can separate our devices into two groups: a hub (Raspberry Pi) and all connected to the hub devices (bulbs). Obviously, we cannot register our bulbs in advance and we have to implement a good logic that will allow us to add/remove a bulb to/from our network. At the same time, we can add and configure our hub (Raspberry Pi) even before selling it. So, on the first step I want to prepare my Raspberry board.

In general, I can not register the hub in my network at all but I want to use IoT Hub to communicate with Raspberry as well. It helps me avoid using any additional services, related to bulb registration process and so on.

During the registration process, I have to provide the device name, and IoT Hub will generate a special token that I can store on the device side. Later, the device will use the name and the token in order to send messages to the IoT Hub. Depending on a scenario, we can implement the registration process in several ways: build a portal for consumers, create a tool in order to preregister all devices and hardcoded token inside a configuration file and so on. In our case, we will generate a token in advance and add it to a configuration file on Raspberry Pi. In order to do it, we can use .NET SDK and implement the following code:

 class Program
    static RegistryManager registryManager;
    static string connectionString = ";SharedAccessKeyName=registryReadWrite;SharedAccessKey="

    static void Main(string[] args)
        registryManager = RegistryManager.CreateFromConnectionString(connectionString);

    private async static Task AddDeviceAsync()
        string deviceId = "RaspberryPiHub";
        Device device;
            device = await registryManager.AddDeviceAsync(new Device(deviceId));
        catch (DeviceAlreadyExistsException)
            device = await registryManager.GetDeviceAsync(deviceId);
        Console.WriteLine("Generated device key: {0}", device.Authentication.SymmetricKey.PrimaryKey);

This code is simple: we used the RegisterManager class in order to add the device to IoT Hub, passing name of the device. Once the device is registered, we have to get a key that we will store on Raspberry side. Of course, the connection string should contain a key with read/write registry permissions.

In order to execute this code, you have to add a reference to the Microsoft.Azure.Devices assembly using NuGet package manager:


Once you run the application, the console will print the code which you can copy and store in a config file on Raspberry side:


Ok. In this post we got enough knowledge to start working with IoT Hub. In the next post we will start working around bulbs.