Monitoring data from a cloud dashboard

In this first section of the chapter, we are going to connect a temperature and humidity sensor to our Raspberry Pi Zero board and send those measurements to the cloud. Later in this section, we are also going to learn how to visualize those measurements on a dashboard.

We first need to connect the DHT11 sensor to our Pi. First, place the sensor on the board, and then connect the 4.7k Ohm resistor between pin 1 and 2 of the sensor. Then, connect the first pin of the sensor to a 3.3V pin of the Pi, the second pin to GPIO4 of the Raspberry Pi, and finally the last pin of the sensor to a GND pin of the Pi.

The following image is the final result:

Monitoring data from a cloud dashboard

We are now going to see how to configure our Raspberry Pi Zero so it automatically sends data to the cloud. For that, we'll use Node.js to send data to a service called Dweet.io, which will allow us to easily store data online.

Let's first see the details of the code. First, we declare the modules that we will use for this section:

var sensorLib = require('node-dht-sensor');
var request = require('request');

After that, we need to give a name to our thing, which is the name we'll use to identify the object storing the measurements on Dweet.io:

var thingName = 'mypizero';

We will also define a main measurement loop, in which we'll make measurements from the sensor and send those measurements to Dweet.io:

var sensor = {
    initialize: function () {
        return sensorLib.initialize(11, 4);
    },
    read: function () {

        // Readout
        var readout = sensorLib.read();
        console.log('Temperature: ' + readout.temperature.toFixed(2) + 'C, ' +
            'humidity: ' + readout.humidity.toFixed(2) + '%');

        // Log data
        logData(readout);

        // Repeat
        setTimeout(function () {
            sensor.read();
        }, 2000);
    }
};

After that, we need to initialize the sensor:

if (sensor.initialize()) {
    sensor.read();
} else {
    console.warn('Failed to initialize sensor');
}

Let's now see the details of the function that is used to log data on the Dweet.io server:

function logData(readout) {

  // Build URL
  var url = "https://dweet.io/dweet/for/" + thingName;
  url += "?temperature=" + readout.temperature.toFixed(2);
  url += "&humidity=" + readout.humidity.toFixed(2);

  // Make request
  request(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      console.log(body) // Show response
    }
  });

}

We basically form a request to Dweet.io, passing the measurements inside the request URL itself.

It's finally the time to test the project! Grab all the code from the GitHub repository of the book and place it inside a folder on your Pi. Then, inside this folder, type the following command with a terminal:

npm install node-dht-sensor

This will install the required sensor library. Then, install the request module with the following command:

sudo npm install request

Finally, you can start the software by typing:

sudo node sensor_cloud_log.js

You should immediately see the answer from Dweet.io as the data is recorded to the cloud:

Monitoring data from a cloud dashboard

You can actually already visualize this data right in your web browser, by typing the following URL:

Monitoring data from a cloud dashboard

This is nice, but it's not great to actually visualize data as it is recorded. That's why we are now going to use Freeboard.io, which is a service that will allow us to create cloud dashboards using the Dweet.io data.

You can already create an account at:

http://freeboard.io/

Monitoring data from a cloud dashboard

Inside Freeboard.io, first create a new dashboard:

Monitoring data from a cloud dashboard

Then, add a new datasource with the following parameters:

Monitoring data from a cloud dashboard

This will basically link your dashboard to the 'thing' that is storing your data on Dweet.io. After that, you'll see that the connection is active inside the dashboard itself:

Monitoring data from a cloud dashboard

Now, create a new pane inside your dashboard and also a new Gauge widget for the temperature, using the following parameters:

Monitoring data from a cloud dashboard

You should be able to immediately see the temperature measurements being displayed in the dashboard:

Monitoring data from a cloud dashboard

Now, do the same for humidity, using the following parameters:

Monitoring data from a cloud dashboard

You should now have both gauges inside your dashboard, giving you an immediate glance at the temperature and humidity inside your home:

Monitoring data from a cloud dashboard

You can now add more visualizations of the same data. For example, I added two additional widgets of the type sparkline for each measurement, giving me an instant view of the recent history of each variable:

Monitoring data from a cloud dashboard
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset