Azure IoT makes it relatively easy to build an IoT application from scratch. In this tutorial, you'll find out how to do it.
This article is an excerpt from the book, Enterprise Internet of Things Handbook, written by Arvind Ravulavaru. This book will help you work with various trending enterprise IoT platforms.
To get started with Azure IoT, we need to have an Azure account. If you do not have an Azure account, you can create one by navigating to this URL: https://azure.microsoft.com/en-us/free/.
Once you have created your account, you can log in and navigate to the Azure portal or you can visit https://portal.azure.com to reach the required page.
The following are the steps required for the setup. Once we are on the portal dashboard, we will be presented with the dashboard home page as illustrated here:
As you can see, there are no devices. Using the +Add button at the top, create a new device.
Do note the Connection string-primary key field. We will get back to this in our next section.
Now that we have our device set up in Azure IoT, we are going to complete the remaining operations on the Raspberry Pi 3 to send data.
The things required to set up the Raspberry Pi DHT11 node are as follows:
The steps for setting up the smart device are as follows:
If Node.js is not installed, please refer to the following steps:
$ sudo apt update $ sudo apt full-upgrade
$ curl -sL https://deb.nodesource.com/setup_7.x | sudo -Ebash- $ sudo apt install nodejs
$ node -v $ npm -v
Now we will set up the app and write the required code:
$ npm init -y
Along with this, we are going to install the azure-iot-device (https://www.npmjs.com/package/azure-iot-device) and async modules (https://www.npmjs.com/package/async). Execute the following command:
$ npm install azure-iot-device-mqtt azure-iot-device async --save
$ npm install rpi-dht-sensor --save
{ "name":"Azure-IoT-Device", "version":"1.0.0", "description":"", "main":"index.js", "scripts":{ "test":"echo"Error:notestspecified"&&exit1" }, "keywords":[], "author":"", "license":"ISC", "dependencies":{ "async":"^2.6.0", "azure-iot-device-mqtt":"^1.3.1", "rpi-dht-sensor":"^0.1.1" } }
The device has successfully connected, and we are sending the data to both the device twin and the MQTT event.
Now, if we head back to the Azure IoT portal, navigate to IoT Hub | IoT Device | Pi3-DHT11-Node | Device Details and click on the device twin, we should see the last data record that was sent by the Raspberry Pi 3, as shown in the following image:
Now that we are able to send the data from the device, let's read this data from another MQTT client.
To read the data from the device, you can either use the same Raspberry Pi 3 or another computer. I am going to use my MacBook as a client that is interested in the data sent by the device:
$ npm init --yes
$ npm install azure-event-hubs --save
var EventHubClient = require('azure-event-hubs').Client; var connectionString = 'HostName=Pi3-DHT11-Nodes.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=J0MTJVy+RFkSaaenfegGMJY3XWKIpZp2HO4eTwmUNoU='; constTAG = '[TESTDEVICE]>>>>>>>>>'; var printError = function(err) { console.log(TAG, err); }; var printMessage = function(message) { console.log(TAG, 'Messagereceived:', JSON.stringify(message.body)); }; var client = EventHubClient.fromConnectionString(connectionString); client.open() .then(client.getPartitionIds.bind(client)) .then(function(partitionIds) { returnpartitionIds.map(function(partitionId) { returnclient.createReceiver('$Default', partitionId, { 'startAfterTime': Date.now() }) .then(function(receiver) { //console.log(TAG,'Createdpartitionreceiver:'+partitionId) console.log(TAG, 'Listening...'); receiver.on('errorReceived', printError); receiver.on('message', printMessage); }); }); }) .catch(printError);
$ node index.js
The following console screenshot shows the command's output:
This way, any client that is interested in the data from this device can use this approach to get the latest data.
You can also use an MQTT library on the client side to do the same, but do keep in mind that this is not advisable as the connection string is exposed. Instead, you can have a backend micro service that can achieve the same for you and then expose the data via HTTPS.
With this, we conclude the section on posting data to Azure IoT and fetching that data. In the next section, we are going to work with building a dashboard for our data.
Now that we have seen how a client can read data from our device on demand, we will move to building a dashboard on which we will show data in real time.
For this, we are going to use an Azure stream analytics job and Power BI.
Azure stream analytics is a managed event-processing engine set up with real-time analytic computations on streaming data. It can gather data coming from various sources, collate it, and stream it into a different source. Using stream analytics, we can examine high volumes of data streamed from devices, extract information from that data stream, and identify patterns, trends, and relationships.
Read more about Azure stream analytics.
Power BI is a suite of business-analytics tools used to analyze data and share insights. A Power BI dashboard updates in real time and provides a single interface for exploring all the important metrics. With one click, users can explore the data behind their dashboard using intuitive tools that make finding answers easy.
Creating dashboards and accessing them across various sources is also quite easy.
Read more about Power BI.
As we have seen in the architecture section, we are going to follow the steps given in the next section to create a dashboard in Power BI.
These are the steps that need to be followed:
So let's get started.
Navigate to the Power BI sign-in page, and use the Sign up free option and get started today form on this page to create an account. Once an account has been created, validate the account.
Log in to Power BI with your credentials and you will land on your default workspace.
Now that we have created a new Power BI, let's set up the remaining pieces:
Next, we will create a new stream analytics job.
Let's see how to create a stream analytics job by following these steps:
Field | Value |
Output alias | powerbi |
Group workspace | My workspace (after completing the authorization step) |
Dataset name | pi3dht11 |
Table name | dht11 |
Once the job starts successfully, you should see the Status of Running instead of Starting.
Now that the entire setup is done, we will start pumping data into the Power BI.
Head back to the Raspberry Pi 3 that was sending the DHT11 temperature and humidity data, and run our application.
We should see the data being published to the IoT hub as the Data Sent log gets printed:
Now that the data is being pumped to Power BI via the Azure IoT hub and stream analytics, we will start building the dashboard:
This wraps up the section of building a visualization using Azure IoT hub, a stream analytics job, and Power BI.
With this, we have seen the basic features and implementation of an end to end IoT application with Azure IoT platform.
If you found this post useful, do check out the book, Enterprise Internet of Things Handbook, to build end-to-end IoT solutions using popular IoT platforms.
Introduction to IOT
Introducing IoT with Particle's Photon and Electron
Five developer centric sessions at IoT World 2018