by Toradex Leonardo Graboski Veiga
1). Introduction
The essence of the Internet of things concept is actually about sending data to the network, so it's called a cloud service. With the development of the Times and advances in technology, people can use smaller, less power-consuming electronic devices and easily connect to the cloud, but one problem has always plagued electronics engineers: How do you use the data you get? And this is the subject of the Internet of things.
There are practical application examples on the Microsoft home page to showcase IoT applications: An elevator company improves and provides pre-maintenance through the Internet of Things, an industrial automation company that understands the oil and gas supply chain through the internet of things, while providing pre-maintenance And a company that predicts driver behavior through IoT and optimizes car use. After reading this series of articles, we expect readers to have enough knowledge and tools to deploy applications to deep view and optimize the entire system-not just to accumulate a bunch of data, but to get useful results from it!
Azure is a Microsoft-provided cloud service platform that offers a wide range of applications such as databases, virtual machines, application services, machine learning, data flow analysis, media and CDN Services, Big Data solutions, and many other applications including IoT hub. The large number of applications it provides itself is a good reason to use Azure services, but Microsoft is further demonstrating that its scenario is better by comparing it with Amazon Web Services – a strong manifesto that is confirmed by users and time, in contrast to the previous one. Plus, high security, ease of integration and ease of access is another reason to choose Azure Services.
This series of articles develops an IoT application, from reading field sensor data to presenting data to acquiring business intelligence (BI). The platform used to connect sensors and upload data to the cloud: the Colibri VF61 computer module of Azure IoT Certified Partner Toradex + Iris Carrier Board. The application obtains sensor data and uploads it to an IoT service called Azure IoT hub from a Microsoft Azure cloud solution, which can then be processed by a variety of Microsoft Azure Services as needed. This section will focus on the second part of this series, where we focus on how to configure Azure IoT hub and upload data to it.
We use the IOT environment to monitor the model car. For demonstration purposes, the Toradex platform and sensor are placed in a RC model car, as shown in 1, while Figure 2 shows a block diagram of the target application.
Figure 1: Remote control car
Figure 2: Application Block diagram
The application programming language we use for JavaScript is mated to node. JS: A JavaScript interpreter compiled by the Chrome V8 engine on a server-side (the Toradex embedded system in this article). This option takes into account the development libraries that Azure IoT hub SDKs can provide. However, it is important to note that the IoT Hub SDKs is now in a frequent update, and every update will change (at least node-related), so you need to consider it before using it. The Azure IoT node package version used in this article is 1.0.1.
The entire environment, from the development of embedded system applications to the configuration of Azure to get the data we are divided into 3 major steps, the following will be described separately:
./Configure Azure Environment
./Add device and send information to IoT hub
./Toradex Embedded System Application Development
2). Configure the Azure environment
You first need to create a new Azure account: From the Azure website you can request a free account for 30 trial. You can then use a certain amount of credit in your account to deploy your app for free using azure services, and the IoT hub also has a free version for development that includes limited resources and is not subject to a trial period. For more information on pricing and IoT hub, see here.
Once you've set up your Azure account, you'll need to create an IoT hub. Sign in to Azure portal with your newly created Azure account and choose +new > Internet of things > Azure IoT hub. The new IoT hub configuration interface, as shown in 3, "Pricing and scale tier" option requires "free" and then a new resource group is created in the "Resource Group" option, and the "location" option needs to be consistent with the services deployed later; "Name" can be set freely, and the "IoT hub Units" and "Device-to-cloud" options cannot be modified in the free version. After clicking "Create", the service is deployed, which can take a few 10 seconds.
Figure 3: Creating an IoT hub from Azure Portal
When the above operation is complete, you can see that the IoT hub has already appeared in the console, the Azure Portal home page. When clicked, the page shown in 4 opens: "Essentials" is the basic information such as the IoT hub region, and "Usage" is the feedback provided to the system administrator about the number of devices enrolled and the number of messages sent from the device; "Monitoring" is the number of messages received.
Figure 4:iot Hub main panel
Still on the IoT hub main panel, in order for other apps to access the service, the Shared access policies option in the Settings tab needs to be selected. In the newly opened Shared Access Policies tab, click the "iothubowner" rule option, which contains all the possible permissions for this IoT hub. As shown in 5, the "Iothubowner" tab opens and then copies the contents of "Connection string–primary key" for later use: This is the next key to manage and monitor this IoT hub service.
Figure 5: Get Iothubowner connection string
3). Add device and send information to IoT hub
Now that the cloud setting is complete, we need to install the Iothub-explorer tool on the development host to add the device to the IoT hub, and if the development host is Windows, you can also choose the Device Explorer tool. Since the development host system used in this article is Ubuntu 14.04, we use Iothub-explorer. Note that the required node version is 0.12.x or above (according to the instructions, 4.x or above is required for full functionality), but currently the Apt-get tool can only install 0.10.x versions. In order to solve this problem, we need to install node version Manager (NVM) and node release 0.12.9 successively. Then use NPM (Node package Manager) in the terminal to install Iothub-explorer.
---------------------
$ NPM Install [email protected]
---------------------
You can then run the Iothub-explorer help parameter to see how the usage
---------------------
$ iothub-explorer Help
---------------------
The iothub-explorer contains the Create and monitor event parameters, based on the printed results of the above command. First, we work with the connection string obtained in Figure 5 above using the Iothub-explorer tool to create a device "Tdx_iot_car". Note that the "--connection-string" parameter is used to display the device connection string (unlike the IoT hub connection string obtained in Figure 5), which needs to be saved to connect the newly created device to the IoT hub, Yes, you can use the Colibri VF61 app to send messages to the hub.
---------------------
$ iothub-explorer "hostname=toradex.azure-devices.net; 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
Authentication:
Symmetrickey:
primarykey:somesharedaccesskeyreturned
secondarykey:somesecondaryaccesskeyreturned
-
Connectionstring:hostname=toradex.azure-devices.net;deviceid=tdx_iot_car; sharedaccesskey=somesharedaccesskeyreturned
---------------------
4). Toradex Embedded System Application Development
Now set the Colibri VF61 computer module + IRIS Carrier plate. In this article, using Toradex released Precompiled Linux Image (colibri_vf_linuxconsoleimagev2.5), how to brush the image to the module please refer here. Then refer to the steps below to install node. js, the NPM package and the git-installation process takes some time, especially the curl step.
---------------------
# opkg Update
# opkg Install Nodejs
# opkg Install tar
# curl-l Https://www.npmjs.com/install.sh | Sh
# opkg Install git
---------------------
The routines shown in this article (Send_data.js) related packages Installer and node files are stored here and can be clone to the target board with the following command and install node packages
---------------------
# git clone https://github.com/leograba/azure-iot-car.git
# [Email protected]:~# cd Azure-iot-car
# [Email protected]:~# npm Install
---------------------
Now we can run a routine on the target board to send data to the IoT hub, but there are a few things to explain: the Routines Use HTTP protocol communication, but the AMQP and MQTT protocols are also supported; variable "connecionstring" The value must be the same as the string saved when creating a new device with the Iothub-explorer tool:
---------------------
var connectionString = "Hostname=toradex.azure-devices.net;deviceid=tdx_iot_car; Sharedaccesskey=somesharedaccesskeyreturned "
---------------------
The setinterval () loop function randomly generates a value sent to the IoT hub to simulate sensor data such as temperature, sonar sensor distance data, acceleration and gyroscope sensors, some GPS coordinate data, and time and date from the target board. How to get data from a real sensor is described in the next article in this series. The JSON stringify () function is used to produce a JSON-encoded data string, which is then encapsulated in message object for sending. The following is an example of a JSON-formatted data string:
---------------------
{"ObjectName": "Toradex2",
"ObjectType": "Sensortagevent",
"Temp": 24.889683,
"Acceleration:
{"accel_x": 10.018892, "accel_y": 0.039468, "accel_z":-0.081328},
"Gyroscope":
{"gyro_x": -0.0532362, "gyro_y": -0.01597086, "Gyro_z": 0},
"Distance": 0.17017,
"Boardtime": 1458064972706}
---------------------
Normally, the callback function inside the sendevent () function should not print any serial output when the program is running. The following is the serial print output that runs the program on the Colibri VF61 and operates normally:
---------------------
# 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
---------------------
To ensure that data is received, the Azure Portal IoT hub panel displays a daily message count, along with a spike display on the monitor chart, as shown in 6. Note that this information takes about 10 seconds to appear on the portal.
Figure 6: Confirm data is received in Azure Portal
Alternatively, you can use the Iothub-explorer tool to view the data stream sent to the IoT hub with the device ID with the "monitor-event" parameter, but you need to Colibri VF61 program to run synchronously while the Azure Portal viewing is not required. Figure 7 below shows the iothub-exploer receiving target board sending data, which is the specific monitoring data command:
---------------------
$ iothub-explorer "your_iothub_connection_string" Monitor-events yourdevice
---------------------
Figure 7: Sending data using the Iothub-explorer charging target board
5). Summary
The Microsoft Azure Web site provides many documents to help users develop more complex and robust applications. Refer to these documents for more useful information, such as creating a device, or getting the data from the hub to be sent by the device can be programmed to do. Additionally, in the next article we will focus on connecting the real sensor to the Colibri VF61 + Iris carrier and transmitting the real sensor data to the IoT hub, which can also be used as other azure services to provide deep viewing or variable manipulation for device deployment applications.
We hope that this article will enable users to understand and ultimately benefit from using Toradex embedded system solutions with Auzre IoT hub services. At the same time, we would also like to thank the Grupo Viceri team from Brazil for their extensive experience in azure and business intelligence that ultimately contributed to this IoT Car project.
This article was originally published in Embarcados.com, Portuguese, see here.
Microsoft Azure IoT Hub app – Part I: Sending data to the cloud