Sunday, 24 July 2016

IoT Adventure: 4 - Sending sensor data to IoT Hub

I want to send sensor data from my .NET application to Azure. How do I do that?

To send sensor data from our .NET application we will use JSON messages and send them to the Azure IoT Hub. After that, other Azure parts like Stream Analytics can 'subscribe' to the IoT Hub to actually do something with the sensor data. But that will be described in a next blogpost.
The first step is to setup an IoT Hub to receive messages. The second step is to adjust our .NET application to send those messages.

1) Create Resource Group
To create an IoT Hub we first need a Resource Group to store the data from the IoT Hub. You can skip this step by creating a Resource Group within the IoT Hub form or if you already have one in your subscription.
Go to New, Management and then Resource Group. The most important option is the Resource group location. You can't combine Azure items from different resource groups. Because we live in the Netherlands, West Europe is the most obvious location to choose for us. That way the data stays in the Netherlands.
Azure Portal - Adding Resource Group

2) IoT Hub
Now the actual IoT Hub. Go to New, Internet of Things and then IoT Hub. For this example we are using the F1 scale tier which is free of charge, but has a limit of 8000 messages a day. So sending each second isn't possible, but once a minute is no problem. The size of the messages is limited as well to 0,5kb for F1 and 4kb for the other editions.
Azure Portal - Adding IoT Hub

3) Consumer groups
We need to create consumer groups. We will create one for the hot path called 'PowerBi' and one for the cold path called 'AzureDB'. Stream Analytics will use these consumer groups to run the queries. Using multiple consumer groups makes it possible for several consumer applications to read data from this IoT Hub independently. If you would use only one consumer group then one consumer application will retain the lease and the others will loose the connection.
Go to the newly created IoT Hub. Click on 'All Settings', 'Messaging' and then scroll down to the consumer groups. Add two groups: PowerBi and AzureDB and click on save.
Azure Portal - Adding Consumer Groups

4) IoT Hub Connection String
Next step is to create the Connection String which we need in our code to send JSON messages to this IoT Hub. First click on the Key icon on the IoT Hub dashboard. Then in the Shared access policies window click on 'iothubowner'. Now copy the 'Connection string - primary key' for the next step.
Azure Portal - Connection string - primary key

5) Device Explorer - Connection String
With the Device Explorer tool we are going to convert the connection string from the previous step to something we can use in our code.
  • Start up Device Explorer (see prerequisites) and paste the connection string on the Configuration tab under IoT Hub Connection String and click on Update.
  • Next go to the Management tab and click on Create. Enter the unique name (or id) of your device and click on the Create button. A new line will be added in the Device Grid.
  • Right click it and choose 'Copy connection string for selected device'. This is the string we need in our code.

6) Adjust .NET code
Go to your .NET project and use this connection string to send messages to the IoT Hub. If you are using our project then here is what you need to change:
Our Sensory project

Not a very exciting and visible step, but very necessary for your IoT project. Moneywise the chosen Tier is very important, but the Consumer Groups are also very essential if you have multiple consuming applications. Also read Azure Event Hub vs IoT Hub to check the differences between those two hubs.

No comments:

Post a comment

Related Posts Plugin for WordPress, Blogger...