Reader small image

You're reading from  Building Smart Homes with Raspberry Pi Zero

Product typeBook
Published inOct 2016
PublisherPackt
ISBN-139781786466952
Edition1st Edition
Right arrow
Author (1)
Marco Schwartz
Marco Schwartz
author image
Marco Schwartz

Marco Schwartz is an electrical engineer, entrepreneur, and blogger. He has a master's degree in electrical engineering and computer science from Supélec, France, and a master's degree in micro engineering from the Ecole Polytechnique Fédérale de Lausanne (EPFL), Switzerland. He has more than five years' experience working in the domain of electrical engineering. Marco's interests center around electronics, home automation, the Arduino and Raspberry Pi platforms, open source hardware projects, and 3D printing. He has several websites about the Arduino, including the Open Home Automation website, which is dedicated to building home automation systems using open source hardware. Marco has written another book on home automation and the Arduino, called Home Automation With Arduino: Automate Your Home Using Open-source Hardware. He has also written a book on how to build Internet of Things projects with the Arduino, called Internet of Things with the Arduino Yun, by Packt Publishing.
Read more about Marco Schwartz

Right arrow

Chapter 10. Building a Home Automation System with Raspberry Pi Zero Boards

As this is the final chapter of this book, we are going to integrate everything that we learned in the book to build a complete home automation system based on the Raspberry Pi Zero. We are first going to see how to assemble and configure several modules based on the Raspberry Pi Zero boards, and then learn how to create a server that will communicate with the boards. Note that this server will be able to run on your own computer, and also on a Raspberry Pi Zero board.

We will then learn how to define behaviors inside the code, for example to send you an alert if motion is detected. After that, we are going to learn how to create advanced behavior inside the server, and finally, we'll see how to access your home automation system from anywhere in the world. Let's start!

Hardware and software requirements


As always, we are going to start with a list of required hardware and software components for the project. We are going to use four modules in total: a sensor module, an appliance control module, a motion sensor module, and a camera module.

Of course, you will need one Raspberry Pi Zero board for each module you use, along with supporting components, such as SD cards and power supplies. If you plan to run the central interface on a Raspberry Pi Zero as well, you will need an additional Pi Zero board.

For the sensors module, we'll use a simple DHT11 sensor, along with a 4.7k Ohm resistor.

For the appliance control module, we'll use the PowerSwitch Tail Kit that we already used in several chapters of this book.

For the motion sensor module, we'll use a simple PIR motion sensor.

For the camera module, we are going to use the C270 HD camera from Logitech. However, you can use any USB camera here.

You will also need the usual breadboard and jumper wires.

This is the...

Building all the modules


In the first part of this chapter, we are going to see how to build the modules that we will use in our home automation system. As we have already seen how to build all these modules in the book, I will simply point to the correct chapters to build all the modules.

The first module you need to build is the sensor module, which is with the DHT11 sensor. To learn how to build this module, please refer to Chapter 2, Measure Data Using Your Raspberry Pi Zero Board, of the book.

This is what you should get at the end:

For the module that will be used to control appliances in your home, such as lamps, please refer to Chapter 3, Building a Smart Home Thermostat, to know how to assemble the module.

This is what you should get at the end:

For the motion sensor module, which is basically composed of a PIR motion sensor connected to the Pi, you can refer to Chapter 6, Sending Notifications using Raspberry Pi Zero. You will get the following outcome at the end:

Finally, for the camera...

Configuring the modules


We are now going to configure each of the modules of our home automation system, so we can access them remotely later. The goal here is to configure the modules to respond to commands coming from our central server and to not act as independent units, as shown in earlier chapters.

Let's start with the sensors module; this is the complete code for this module:

// Modules
var express = require('express');
var sensorLib = require('node-dht-sensor');

// Express app
var app = express();

// aREST
var piREST = require('pi-arest')(app);
piREST.set_id('4g0d7f');
piREST.set_name('sensor_module');
piREST.set_mode('bcm');

// Start server
app.listen(3000, function () {
  console.log('Raspberry Pi Zero motion sensor started!');
});

// Sensor loop
var sensor = {
    initialize: function () {
        return sensorLib.initialize(11, 4);
    },
    read: function () {
        var readout = sensorLib.read();
        console.log('Temperature: ' + readout.temperature.toFixed(2) + 'C...

Integrating the modules into a single interface


Now that our modules are up and running, we are going to learn how to integrate everything into a single interface, so you will be able to run it on your computer or on another Raspberry Pi. You will then be able to control and monitor your smart home from a single interface.

We will first configure the server that will allow us to connect all the modules that we configured earlier. Then, we'll build an interface on top of that.

The code for the server starts by importing the required modules:

// Modules
var express = require('express');
var request = require('request');

// Express app
var app = express();

After that, this is where we'll define the IP addresses of the different modules in our home automation system:

// Raspberry Pi boards IP addresses
var motionSensorPi = "192.168.0.101:3000";
var sensorPi = "192.168.0.102:3000"
var lampPi = "192.168.0.103:3000"

We also need to define the pins on which the lamp and the motion sensor are connected...

Automating your home


Now that we have our central server running and you can use it to control & monitor your home from a single interface, we can actually define some behaviors inside the server in order to create some automation within your smart home.

As an example, we are going to automatically switch on the lamp when motion is detected by the sensor. You can imagine the scenario in which the appliance control module is connected to a lamp in your hallway and that you want it to automatically switch on whenever a movement is detected by the motion sensor.

For that, here is the code you need to add into the server code:

setInterval(function() {

  // Check sensor
  request("http://" + motionSensorPi + "/digital/" + motionSensorPin,
    function (error, response, body) {

      // If motion was detected
      if (body.return_value == true) {

        request("http://" + lampPi + "/digital/" + lampPin + '/1');

      }
      else if {

        request("http://" + lampPi + "/digital/" ...

Accessing your home automation system from anywhere


In the last section of this chapter, we are going to learn how you can access the interface of your home automation system from anywhere in the world. This way, you will be able to monitor and even to control your home when you are not around.

For that, we are going to use a tool called Ngrok, which will allow us to access the server running on our Raspberry Pi or computer from anywhere in the world.

If like me you deployed the server on another Raspberry Pi (as my computer is switched off when I am away from home), type the following command:

wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip

This will download Ngrok on your computer. Then, unzip the file with this command:

unzip ngrok-stable-linux-arm.zip

Finally, start Ngrok using the following command:

./ngrok 3000

This will basically create a web tunnel to the web server that is running on port 3000. Inside the window that appeared on your Raspberry Pi, you should now be...

Summary


In this final chapter, we used everything we learned in the book to build a complete home automation system based on the Raspberry Pi. We used several of those boards and connected each one of it to a different type of components and then interfaced all those boards with a common interface running on your computer or on another Pi.

We also learned how to create more complex behaviors and use this central server to make the Raspberry Pi modules communicate with each other. Finally, we also saw how to access your home automation system from anywhere in the world.

You now have all the tools to transform your home into a smart home using the Raspberry Pi Zero board. I hope that this book allowed you to understand how to use this small, cheap but incredibly powerful board to automate your home. I now invite you to experiment with all the projects we saw in the book and I can't wait to see what you are going to do with it in your own home!

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Building Smart Homes with Raspberry Pi Zero
Published in: Oct 2016Publisher: PacktISBN-13: 9781786466952
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Marco Schwartz

Marco Schwartz is an electrical engineer, entrepreneur, and blogger. He has a master's degree in electrical engineering and computer science from Supélec, France, and a master's degree in micro engineering from the Ecole Polytechnique Fédérale de Lausanne (EPFL), Switzerland. He has more than five years' experience working in the domain of electrical engineering. Marco's interests center around electronics, home automation, the Arduino and Raspberry Pi platforms, open source hardware projects, and 3D printing. He has several websites about the Arduino, including the Open Home Automation website, which is dedicated to building home automation systems using open source hardware. Marco has written another book on home automation and the Arduino, called Home Automation With Arduino: Automate Your Home Using Open-source Hardware. He has also written a book on how to build Internet of Things projects with the Arduino, called Internet of Things with the Arduino Yun, by Packt Publishing.
Read more about Marco Schwartz