Microsoft Azure IoT Hub

Connected devices sending telemetry have to be stored before it can be analysed. Idle choice would be an event hub, which is meant for handling large volume of streams. In this example we will try to simulate a device sending data to Azure IoT Hub.

For easy management we will create all the resources in one resource group, so that it can be deleted in one go after our job is done. Please visit resources.azure.com, choose right subscription, expand resource group and location required resource group to delete.

resources.azure.com

What is IoT Hub?

As Microsoft explains: “IoT Hub is a managed service hosted in the cloud that acts as a central message hub for communications in both directions between an IoT application and its attached devices.” You can understand it as a Event Hub used for bi-direction communication which supports multiple messaging patterns (Device to cloud, Cloud to device, files upload) and protocols (HTTPs, AMQP & MQTT).

Let’s start building something working before we go in more details. As we know every resource in Azure lives in a resource group, so start with creating a resource group using Azure CLI.

If you haven’t installed Azure CLI yet the please follow instructions at Install the Azure CLI for Windows | Microsoft Docs.

List of all az commands can be found at az | Microsoft Docs.

Login to your Azure subscription using

az login <# It will log you in your Azure Subscription #>

If you have multiple subscriptions then below command will help you choosing right subscription for this purpose

az account set --subscription SubscriptionIdOfYourChoice

All available Azure locations can be find using (choose one near to you, we will use this in next command)

az account list-locations --query “[].{DisplayName:displayName, Name:name}” -o table

Choose location near to you. I have chosen ‘uksouth’. Chose an unique name for resource group. I have chosen ‘rg-iothub’.

az group create --location uksouth --resource-group rg-iothub

Create IoT hub in free tier with 2 partitions (You can’t have less than 2 and more than 2 partitions in free tier). Execution of below command might take some time and expect a JSON output when this command completes.

az iot hub create --resource-group rg-iothub --name HelloWorldIoTHub --sku F1 --partition-count 2

Only an authenticated device can communicate with IoT Hub. So let’s create a device “hello-world-connected-device” with shared key access. Run following command to create one, output would contain primary and secondary key.

az iot hub device-identity create -n HelloWorldIoTHub -d hello-world-connected-device --am shared_private_key --ee

A connection string would be required to connect device with hub. Following command would reveal connection string using primary key. Make a note of it, this will be used in code to send some telemetry data to create IoT hub.

az iot hub device-identity connection-string show --device-id hello-world-connected-device --auth-type key --hub-name HelloWorldIoTHub --key-type primary

Download C# code from

AZ-220-Microsoft-Azure-IoT-Developer/Allfiles/Labs/04-Connect an IoT Device to Azure/Final at master · MicrosoftLearning/AZ-220-Microsoft-Azure-IoT-Developer · GitHub

Update connection string as shown in the screenshot below

Update Line 28, with the connection string noted previously

Run the code and observe it sending telemetry information to hub. Number of messages sent to IoT hub can be verified at Azure portal

Run created console app again and see this number increasing.

Don’t forget to delete resources after experiment is complete.

MS Azure, IoT, Artificial Intelligence, Machine Learning