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 3. Building a Smart Home Thermostat

In the previous chapter, we learned how to read data from a sensor and log this data on the Raspberry Pi Zero board. In this chapter, we are going to use that knowledge to build a very useful home-automation component: a smart thermostat.

We are going to see how to use the Raspberry Pi Zero and a few other components to regulate the temperature in a room of your home using an electrical heater. We'll see how to connect all the different components, and also how to create a nice interface that you will be able to use to control your thermostat. 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.

Except for the Raspberry Pi Zero, the most important component for this project will be the PowerSwitch Tail Kit. This component allows your Pi to control electrical appliances such as lamps, heaters, and other appliances that use mains electricity to function.

Then, we will use the same DHT11 sensor we used in the previous chapter to measure the temperature in the room.

Finally, you will need the usual breadboard and jumper wires.

This is the list of components you will need for this project, not including the Raspberry Pi Zero:

Of course, for the project to make sense, you will need to have an electrical heater that you can...

Testing individual components


As the first project of this chapter, we are simply going to check that each individual component (the sensor and the PowerSwitch tail) are working correctly.

I will now go through the main parts of this first piece of code. It starts by including the DHT sensor module for Node.js:

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

Then, we create an object to read data from the sensor, and also initialize it when we start the software:

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

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

You can now either copy the code inside a file...

Building the thermostat


We are now going to see how to build the code for the thermostat, which will run on your Raspberry Pi Zero board. As the code is quite long, I will only highlight the most important parts here, but you can of course find the complete code inside this book's GitHub repository.

Start by importing the required modules:

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

Then, we create an Express app, which will allow us to easily structure our application:

var app = express();

Next, we define some variables that are important for our thermostat:

var targetTemperature = 25;
var threshold = 1;
var heaterPin = 29;

The threshold is here so the thermostat doesn't constantly switch between the on and off states when it is near the target temperature. A lower threshold means that you will have a temperature closer to what you want, but also that the heater will switch more frequently.

After that, we are going to define the routes that will structure our application...

Controlling the thermostat remotely


We are now going to take the exact same project we defined earlier, but add a graphical interface on top of it. Inside the JavaScript file we saw previously, you just need to add one line, as follows:

app.use(express.static('public'));

Now we are going to code two files: one HTML file with the interface, and another file containing scripts that will make the link between the interface and the server. Let's start with the HTML file:

<head>
  <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <script src="js/script.js"></script>
  <link rel="stylesheet" href="css/style.css">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

As you can see, inside the <...

Summary


In this chapter, we saw how to build a simple thermostat using the Raspberry Pi Zero board. We were able to make a thermostat that can generate its own interface and we are able to set the target temperature of the thermostat via this interface.

You can of course already use what you learned in this project, and adapt it to a heater you have in your home. I recommend you first try it on a portable heater, and then to install it on an actual heater on your wall, if you are feeling confident with your project.

In the following chapter, we will look in more detail at how to control appliances from your Raspberry Pi, such as LEDs, lamps, and other appliances you could have in your 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