More and more home automation projects are integrated into an Internet of Things (IoT) perspective. For example, you want to monitor what’s going on in your home remotely, no matter if you are in your living room or at the other end of the world. And in the DIY space, the Raspberry Pi is the perfect device to build Internet of Things projects.

In this project, we are going to use a Raspberry Pi to monitor two kind of data from anywhere in the world: the readings coming from a sensor, and the snapshots taken by a camera. Using the Raspberry Pi makes things much easier as it would be with any other platform, as we are going to see in the article. For this project, we will use a combination of Dweet.io (to store the sensor readings), Cloudinary (to store pictures from the camera), and Freeboard.io (to display everything in a single online dashboard). Let’s dive in!

Hardware & Software Requirements

You will need several hardware components for this project. I used the brand new Raspberry Pi 2, but you could perfectly an older version of the Raspberry Pi, and even a Raspberry A.

You will also need the official Raspberry Pi camera module to record pictures. For the sensor, I used a common DHT11 temperature & humidity sensor.

I also added an USB WiFi dongle to the Raspberry Pi so I can connect it to the Internet without an Ethernet cable.

This is a list of all the components you will need in this project:

picamera

On the software side, you will also need to install several components. The first one is driver for the BCM chip of the Raspberry Pi. You can install it by following the instructions from this link:

http://www.airspayce.com/mikem/bcm2835/

You will also need an account at Cloudinary, that we will use to store the pictures:

http://cloudinary.com/

You need to get your account ID, your API key & API secret.

Finally, you will also need to have Node.js installed on your Pi. You can get it by typing the following commands in a terminal:

wget http://node-arm.herokuapp.com/node_latest_armhf.deb 
sudo dpkg -i node_latest_armhf.deb

Hardware Configuration

Let’s now see how to configure this project. As there are only a few hardware components, it will be really quick and easy.

Let’s start with the DHT11 sensor. The DHT sensor signal pin (pin number 2 of the sensor) has to be connected on pin 4 of the Raspberry Pi. Also connect the VCC pin (pin number 1) of the sensor to the RPi 3.3V, and GND to GND. Finally, insert a 4.7K Ohm resistor between pin number 1 and 2, just as shown on the picture:

pisensor

After that, simply plug the Raspberry Pi camera module on the Raspberry Pi.

Monitoring Sensor Readings & Camera Snapshots

We are now going to see the details of the code that will upload the sensor readings & a snapshot from the camera to the cloud. The whole code is based on Node.js, which makes it so much easier compared than Arduino for example for such tasks. We are going to use Dweet.io to store our sensor readings, and a service called Cloudinary to store the snapshots from the camera.

It starts by including all the modules:

var RaspiCam = require("raspicam");
var cloudinary = require('cloudinary');
var sensorLib = require('node-dht-sensor');
var dweetClient = require("node-dweetio");

We also create an instance of the Dweet.io module:

var dweetio = new dweetClient();

After that, we initialize the camera module:

var camera = new RaspiCam({mode: "photo",
  width: 1280,
  height: 720,
  output: "./image.jpg",
  encoding: "jpg",
  timeout: 0,
  n: true,
  rot: 180
});

We also need to configure the cloudinary access, this is where you will need to enter your cloud name, API key & API secret that you got before:

cloudinary.config({ 
  cloud_name: 'cloud_name', 
  api_key: 'api_key', 
  api_secret: 'api_secret' 
});

Then, we are going to take a picture at regular intervals (every 10 seconds), and then upload it to Cloudinary, always at the same URL. This is done by the following piece of code:

setInterval(function() {

  camera.start();

  camera.once("read", function(err, timestamp, filename){
    console.log("Picture recorded");
    camera.stop();
  });

  camera.once('stop', function() {

    // Upload picture
    cloudinary.uploader.upload("image.jpg", function(result) { 
      console.log(result) 
    }, { public_id: "raspberry_pi_camera"});
  });

}, 10000);

After that, we will also read the data (temperature & humidity) from the DHT11 sensor every 10 seconds, and then send this data to Dweet.io. This is done by the following piece of code:

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

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

      // Send to Dweet
      dweetio.dweet({temperature: temperature, humidity: humidity}, function(err, dweet){

        console.log(dweet.thing); // The generated name
        console.log(dweet.content); // The content of the dweet
        console.log(dweet.created); // The create date of the dweet
      });

      setTimeout(function () {
          sensor.read();
      }, 10000);
  }
};

Finally, we also need to initialise our DHT11 sensor:

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

The complete code for this project can be found inside the corresponding GitHub repository:

https://github.com/openhomeautomation/iot-raspberry-pi

Now, once you downloaded the code & entered the right credentials for Cloudinary, go to a terminal inside the code folder and type:

sudo npm install raspicam cloudinary node-dht-sensor node-dweetio

Wait a bit, and when everything is installed type:

sudo node app.js

Then, inside the console, you will see two important data. The first one is the name of your ‘thing’ on Dweet.io, which is attributed automatically:

Screen Shot 2015-03-02 at 09.56.19

You will also see the URL on which the picture was uploaded. You can try it out right away by pasting this URL into a web browser.

Now, we are going to build our dashboard to monitor our project remotely. Go to the Freeboard.io website at:

https://www.freeboard.io/

Create a new account there if you don’t have one already, and then create a new dashboard. After that, also create a datasource. This is where we link our dashboard to the readings of the sensors that are stored on Dweet.io. You need to enter the name of your ‘thing’ on Dweet.io:

Screen Shot 2015-03-02 at 09.55.38

After that, add a gauge for the temperature, and link it to the datasource you defined before:

Screen Shot 2015-03-02 at 09.55.46

Repeat the same operation for the humidity. Now insert a picture widget, and link it to the Cloudinary URL you saw in the console:

Screen Shot 2015-03-02 at 09.55.52

Your dashboard is now ready! This is the final result with the three widgets:

Screen Shot 2015-03-02 at 09.55.22

Note that you can view this page from anywhere in the world, you just need a simple Internet connection!

How to Go Further

Let’s summarise what we did in this project. We built an online dashboard that can be accessed from anywhere to monitor your Raspberry Pi. Of course, there are many ways to improve this project. You can for example connect more sensors to your Raspberry Pi, and also monitor them remotely inside the same dashboard.

You could even think about adding more elements to your dashboard, like the pictures taken by other camera modules that can be installed in your home. And everything going into the same dashboard to always have a quick view on what is going on in your home. And this can be coupled with motion sensors, and linked to automated alerts in case movement is detected in your home.

This is already the end of this article, I hope you enjoyed it! Did you also build Internet of Things projects with your Raspberry Pi? Any feedback on this project? Please share in the comments!