Azure IoT Hub on a Toradex Colibri VF61 – Part 1: Sending data to the cloud

terça-feira, 7 de junho de 2016



The concept of the Internet of Things is intrinsically related to the sending of data to the internet and its so called cloud services. People from the electronics field are everyday more easily connecting devices to the cloud as the evolution of technology is allowing the use of smaller and less power-consuming electronics as time goes by. Still there is an unanswered question for many of these electronics developers: how to make all of the gathered data useful? Because that is what the Internet of Things is about.

There are some examples of real applications taken from the Microsoft website in order to share a preview of what the IoT can represent: there is an elevator company using the internet of things to improve and give predictive maintenance; an automation company using IoT to have insights from the oil and gas industry's supply chain, along with predictive maintenance; and a company that uses IoT to predict drivers behaviors and optimize car utilization. By the end of this series of articles, it is expected that the reader might have enough information and tools ready to deploy applications that retrieve insights and/or optimize its overall system – not only an amount of data stacked, but some useful output!

Azure is the Microsoft cloud services platform and it provides an amount of applications such as databases, virtual machines, app services, machine learning, data stream analysis, media and CDN services, big data solutions, among many others, including the IoT Hub. By itself, the vastness of services offered is already a good reason to use the Azure services, but Microsoft goes one step ahead and compare its services with the Amazon Web Services to reinforce that their solution is the better – a strong statement that only users and time will confirm, or the other way around. Among the reasons to use their services is the security offered, the easiness of integration and the gentle learning curve.

The main goal of this article is to develop an IoT application, from the reading of field sensors to the presentation of results and the retrieving of business intelligence. The hardware used to collect the sensors data and send it to the cloud is: a Toradex Colibri VF61 SoM + the Iris Carrier Board, since Toradex is an Azure IoT certified partner. This is, therefore, an advantage, once Toradex is an Azure IoT Certified Partner. The application should get some sensors data and send it to an IoT service from the Microsoft Azure cloud solution, called Azure IoT Hub. Once the data is being received on the cloud, it can be processed in many ways by other Microsoft Azure services. That is what will be done in the second part of this article series, but for now the focus will be on how to configure the Azure IoT Hub and send messages to it.

The chosen IoT environment was the monitoring of a car. For demonstration purposes, sensors were attached to a remote controlled car, to which a Toradex customized SBC was also embedded. In the image 1 it is possible to see the picture of the demo and the image 2 holds a block representation of the aimed application.

Remote Controlled Car
Image 1: The remote controlled car
Block diagram of the application
Image 2: Block diagram of the application

The programming language chosen to develop the target application is the Javascript along with Node.js: a server-side (which in this case is the Toradex embedded system) Javascript interpreter built on the Chrome's V8 engine. Its choice was based on the availability of libraries provided by the Azure Iot Hub SDKs. It should be noticed that the IoT Hub SDKs are under heavy development, with changes being made every new release (at least for Node), and it should be taken under consideration. The release version of the Azure IoT Node packages being used in this article is the version 1.0.1.

The setup of the whole environment, from the programming of the embedded system to the configuration of Azure in order to receive data was divided in three steps, described in this article:

Configuration of the Azure environment

The first step needed to start developing the whole system is to create an Azure account: a free account can be created from the Azure website as a 30 day trial. Than a fixed amount of azure credits can be used to deploy applications that use the Azure services without charge; also, the IoT Hub has a free version dedicated to development, with limited resources, also free of charge, even beyond the trial period. For more details about the pricing and the IoT Hub per se, the IoT Hub page can be visited.

After setting an Azure account, it is first needed to create an IoT Hub. For that, the Azure portal should be accessed using the newly created account and the options +New > Internet of Things > Azure IoT Hub selected. The configuration screen for the new IoT Hub is shown in image 3. The option "Free" should be selected in the Pricing and scale tier; a new resource group should be created in the Resource Group field and the Location selected must match the location of the other services to be deployed later. Any Name can be chosen and the IoT Hub Units and Device-to-cloud partitions fields cannot be edited in the free version. After clicking create, the service will be deployed – and it might take a few seconds.

Creating an IoT Hub from the Azure Portal
Image 3: Creating an IoT Hub from the Azure Portal

That being done, the IoT Hub should appear in the Dashboard, that is, the main Azure Portal page. After clicking it, a page like in the image 4 should open: there will be some Essentials information such as the region of the IoT Hub; an Usage section that gives a feedback to the system administrator regarding how many messages were sent from devices to this service and the number of registered devices; and a Monitoring section where the number of messages received over time are displayed.

IoT Hub main panel
Image 4: IoT Hub main panel

Still on the panel from image 4, in order to gain access to the service from other applications, the Shared access policies option over the Settings tab should be selected. In the new tab Shared access policies that will open, there will be a policy called "iothubowner", which has all possible permissions to this IoT Hub and it should be clicked. The iothubowner tab will open and its Connection string – primary key should be copied for later usage: it is the key that will allow management and monitoring of this IoT Hub devices in the next steps. The tabs described in this paragraph to get the connection string are illustrated in the image 5.

Getting the iothubowner connection string
Image 5: Getting the iothubowner connection string
Adding devices and sending messages to the IoT Hub

Now that everything is configured in the cloud, the iothub-exporer tool needs to be installed on the development machine so that devices can be added to the IoT Hub. There is another tool named Device Explorer, only available for Windows systems, so if Windows is being used in the development machine, this option might be checked out. Since in this article the Ubuntu 14.04 is being used, I will stick to the iothub-explorer. It should be noted that the Node version running needs to be at least 0.12.x (it says a version 4.x or higher is needed for all the features to work), but at the time this article was written, the apt-get tool was currently installing some 0.10.x version. To solve this issue, the Node Version Manager (NVM) and, subsequently, the Node version 0.12.9 were installed. From the terminal, the iothub-explorer can be installed using the NPM (Node Package Manager):

leonardo@leonardo:~$ npm install iothub-explorer@latest

Then the iothub-explorer can be run with the help option to see its usage possibilities:

leonardo@leonardo:~$ iothub-explorer help

As it can be seen in the terminal from the previous command, among the iothub-explorer options there are create and monitor-events. In order to use this tool, the connection string acquired in the image 5 must be used. First of all, a device named "tdx_iot_car" will be created, as in the command below. Note the option –connection-string that displays the device connection string (not to be confused with the IoT Hub connection string). It should be copied since it is the key used to connect this newly created device to the IoT Hub, by enabling the Colibri VF61 application to send messages to the Hub.

leonardo@leonardo:~$ iothub-explorer ";SharedAccessKeyName=iothubowner;SharedAccessKey=putyoursharedaccesskeyfromtheconnectionstringhere" create tdx_iot_car --connection-string
Created device tdx_iot_car
  deviceId:                   tdx_iot_car
  generationId:               635931262207620183
  etag:                       MA==
  connectionState:            Disconnected
  status:                     enabled
  statusReason:               null
  connectionStateUpdatedTime: 0001-01-01T00:00:00
  statusUpdatedTime:          0001-01-01T00:00:00
  lastActivityTime:           0001-01-01T00:00:00
  cloudToDeviceMessageCount:  0
      primaryKey:   somesharedaccesskeyreturned
      secondaryKey: somesecondaryaccesskeyreturned
Programming the Toradex embedded system application

Now a Colibri VF61 SoM + Iris Carrier Board should be set. In this article a pre-build image (Colibri_VF_LinuxConsoleImageV2.5) that can be downloaded here was used. Instructions on how to flash the image to the Colibri module can be found here. In order to install the Node.js, the NPM package and git, the following instructions were issued – be aware that it might take a few minutes to complete, especially the curl instruction:

root@colibri-vf:~# opkg update
root@colibri-vf:~# opkg install nodejs
root@colibri-vf:~# opkg install tar
root@colibri-vf:~# curl -L | sh
root@colibri-vf:~# opkg install git

The repository where are the packages installer and the node file regarding this article example – send_data.js – can be cloned into the the board. To clone and install the node packages, the following commands must be run:

root@colibri-vf:~# git clone
root@colibri-vf:~# cd azure-iot-car
root@colibri-vf:~# npm install

Now the code that sends data to the IoT Hub can be run, but first let's explain some points about it: it is used the HTTP protocol to provide communication, but the AMQP and MQTT protocols are also supported; The variable named connectionString value must be the same string got from a few steps above, while creating the device with the iothub-explorer:

var connectionString = ";DeviceId=tdx_iot_car;SharedAccessKey=somesharedaccesskeyreturned"

Inside the setInterval() loop many values are randomly created to be sent to the IoT Hub as if it were data from some sensors, such as temperature, distance from an ultrasonic sensor, acceleration and gyro, some gps coordinates and the date/time from the board. How to get this data from real sensors will be addressed in the next article from this series. The JSON.Stringify() function generates a JSON encoded string from the data and it is encapsulated in a Message object to be sent. Below an example of a a JSON formatted string is presented:


In case everything goes well, the callback function from the sendEvent() method should not print anything to the console while the code is running. The following command runs the Colibri VF61 code and displays the feedback message continuously printed to the console when all goes right:

root@colibri-vf:~/azure-iot-car# node send_data.js 
sending message to the IoT Hub
sending message to the IoT Hub
sending message to the IoT Hub
sending message to the IoT Hub
sending message to the IoT Hub
sending message to the IoT Hub
sending message to the IoT Hub
sending message to the IoT Hub

As a feedback to guarantee that the data is being received, the IoT Hub section of the Azure Portal should update the daily message count and the monitoring graph should present with a spike, as shown in image 6. Note that it might take from a few seconds to more than a minute until this information gets updated in the portal.

Checking in the Azure Portal that data is being received
Image 6: Checking in the Azure Portal that data is being received

To see the data stream that is coming into the IoT Hub, the iothub-explorer tool may be used. To achieve this, the monitor-events option must be used with the device id. Note that, for this to work, the Colibri VF61 application must be running at the same time that the iothub-explorer, whereas to see the statistics from the Azure Portal there is no need for it. The image 7 displays the iothub-explorer receiving the data while the board was sending it simultaneously. The command to monitor the events is displayed above, before the image:

leonardo@leonardo:~$ iothub-explorer "your_iothub_connection_string" monitor-events yourdevice

Receiving data from the board into the iothub-explorer
Image 7: Receiving data from the board into the iothub-explorer

The Microsoft Azure website has plenty of documentation about the Iot Hub as more information is needed to develop more complex and/or robust applications. There are things such as creating a device, or getting messages sent from devices to the Hub, that can be accomplished programatically – refering to the documentation is a good way to obtain more information on the matter. Also, in the next article the focus will be in interfacing some sensors to the Colibri VF61 + Iris Carrier Board and than, as real data is sent to the IoT Hub, it can be used as an input to other Azure services that can generate insights and/or add some control variables to the deployed application.

I hope it was a helpful introductory article on how to join a Toradex SBC solution with the Azure IoT Hub service and that you may find it useful! Also, I would like to thank the Grupo Viceri team from Brazil for their expertise regarding Azure and Business Intelligence, that led to the partnership that which resulted in the IoT Car project. See you soon in the next article.


This blog post was originally featured on in Portuguese. See here.

Autor: Leonardo Graboski Veiga, Toradex Brasil

Deixe um comentário

Please login to leave a comment!
Have a Question?