Recently I became the happy owner of a brand new Raspberry Pi 2. In case you don’t know about it, Raspberry is a pretty powerful computing device in a form factor of a credit card. You can install Windows 10 IoT Core on the Raspberry and code against it using the Universal Windows Platform (UWP_, aka write programs for this small device using C# and XAML. So, I embarked on a new self-education journey and, after some reading, I had to pick a pre-made hands-on sample and replicate it. I picked this one, since the only extra hardware it requires is a shield (Sparkfun Weather Shield) and some cables to connect it with the Raspberry. The sample instructions on hackster.io are pretty straight forward and the source code provided for the Weather Shield + UWP integration is pretty self-explanatory. Only modification I made was to add a timer to pull humidity and temperature from the shield at a fixed time interval.
However, I wanted to take this project one step further. Connect the device to Azure IoT Hub, send data to it, pass the data via a simple Azure Stream Analytics job and store it to Azure Table Storage, expose it via a Web API web service and consume it from an ASP.NET MVC web application. Well, it’s not as difficult as it sounds! I’ll briefly describe the steps I used
Azure IoT Hub
The Azure IoT Hub (in preview, at the time of writing) is a service in which millions of devices can connect and exchange messages. The IoT Hub enables bidirectional communication to and from each device, can manage each device separately and provides a high level of security. To create a new IoT Hub, login into the new Azure management portal and create it. There is also a free tier to support app development, which you can use. If you do not have an active Azure subscription, you can easily create a trial account. Even though it requires a credit card, you will not be charged at all, provided you stay within your spending limits.
On the messaging tab you can see the event hub-compatible name and endpoint address.
Azure table storage
You also need to create a storage account. Select it from New->Data+Storage section on the Azure portal. Check and copy the table service endpoint somewhere, as we’ll need it later.
It’s necessary to create a table for your data to be stored. There are various ways to do that, I myself used the awesome Azure Storage Explorer to create a table named “WeatherData”.
After creating the IoT Hub and the Storage account, we need to create the intermediate object; the one which will take the events/messages from the IoT Hub and will dump them in the table storage. As mentioned, we’ll use a Azure Stream Analytics to perform this operation.
Stream Analytics job
To create a new Stream Analytics job, select new –> Internet Of Things –> Stream Analytics job. Then, you need to configure Azure IoT Hub as the input. You’ll need keys etc. which you can find on the Azure IoT Hub settings page on the portal. I’ve selected to name the input stream “input”.
You then need to configure the storage account as the output and specify the necessary settings. You can find the storage account key on the storage account settings and you need to specify the properties of your IoT Hub message that will contain the values for the Partition key and Row key columns in the table storage. I’ve also selected to name the output stream “output”.
When creating input and output streams, you are more than encouraged to click the button “Test” in order to perform a basic test to check your settings. If all goes well, you may proceed to defining the query that will transform your input to your output. For the purposes of our simple demo, we are just dumping all messages from the IoT Hub to the Table Storage. For this, we’ll use a simple query that selects all data from our input stream (recall that it is called “input”, right?) and dumps it to the output stream. This query is written in an SQL-like language that has the very cool name “Stream Analytics Query Language”. Of course, you can do lots of cool stuff with this language, check here for some examples.
Do not forget to click “test” to test your script. If the result is OK, click the “Run” button to have your Azure Stream Analytics job started.
Register the device
Each and every one device that you wish to connect to IoT Hub must be registered to obtain an access key. You can use the free Device Explorer app to do that. Use the iothubowner connection string to connect to your IoTHub. Moreover, if you are a node.js fan, you can use this solution to manage your IoT Hub registered devices.
On the Management tab, you can create a new device. If you right click on it, you can copy the connection string for that device. This will be very handy when when we write the code for our solution.
Onto the final part, our code! In order to code against the IoT Hub, we need a prerelease Nuget package called “Microsoft.Azure.Devices.Client”. Do not forget to click the “Include prerelease” checkbox or else you will be unable to find the package.
First, we need to initialize the DeviceClient object using the connection string we copied from the Device Explorer tool.
Then, the code to send a weather information message to the IoT Hub is pretty straight forward.
If you’re wondering about the weird value on the RowKey property, it is used for sorting azure table storage rows chronologically.
As soon as the raspberry starts sending messages, we can go to the Data tab on the Device Explorer tool and click “Monitor” to peek at the messages arriving on the IoT Hub.
On the ASP.NET MVC web app, we are using a Web API controller that fetches the data from the table storage which is called by a jQuery method every two methods. Of course, instead of polling the web service, we could use some SignalR functionality to notify the browser when there are new data on the table storage. Also, check here for a nice explanation on how to visualize data using PowerBI.
Connecting a device to the Azure IoT and parsing the data via a Stream Analytics job is a relatively easy process. The IoT Hub is a robust service (even though it’s still in preview!) that can parse millions of messages from your little powerful devices and I’d certainly encourage you to try it. Stay tuned for more IoT posts!
PS. Check and fork the source code here on GitHub!