When you purchase through links on our site, we may earn an affiliate commission. Here’s how it works

Home / Features / How to build a homemade Nest thermostat

How to build a homemade Nest thermostat

Want a smart thermostat on the cheap? You can make your own for less than £50 with a Raspberry Pi

Home is where the smart is these days, and one of the easiest ways to give your house a brain boost is by upgrading the heating system.

You could do that by buying an off-the-shelf thermostat from Nest, Honeywell, Netatmo, Hive or the like – check out our comprehensive smart heating test for more on them – or you could spend less and have more fun building a smart thermostat using a Raspberry Pi. 

Fancy option two? Of course you do…

Overview

We’re going to use Nest as our main comparison thermostat, so we’re going to call our Pi-powererd verson ‘PiNest’.

PiNest will use Google Calendar events to set a schedule, and because some of Nest’s more interesting smart features harness the power of If This Then That (IFTTT.com) recipes, ours will tie in with this as well.

It’s not going to be shiny and perfect like a Nest or Hive, and it doesn’t (yet) learn your schedule, but it’s a great starting point for your smart home which you can add to as time goes on. We’ll keep adding to ours and will let you know how we get on.

We’ve tried to do this in the way that requires the least programming possible, so even a complete beginner can do it. That’s one of the reason’s we’re using Google Calendar, IFTTT and email to send the temperature commands, plus a single Python file to control the Raspberry Pi.

In a nutshell: a scheduled heating event in Google Calendar triggers IFTTT to send an email which your PiNest reads, then switches your heating on or off accordingly.

Component list

Raspberry Pi (we used Pi 2 Model B) | Diode (1N4001S) | Transistor (2N3053) | Digital Thermometer (DS18B20) | Relay module (5V – 240VAC) | Resistors (10K, 1K and 220 ohm) | LED | Wi-Fi dongle (if no Ethernet nearby)

Set the heating schedule with Google Calendar

Set up a new calendar named ‘PiNest’ (or whatever you’d like to call it). Within that calendar, add repeating events to set the temperature.

The name of each event should be the desired temperature e.g. the event ‘20′ at 08:00 every day will set the heating to 20 degrees at 8am every morning. Setting other events called ‘0′ will turn it off again.

You could skip this step and just set time-based recipes on IFTTT but, especially if you’re a regular Google Calendar user, viewing and changing your schedule using Google Calendar is much quicker than adjusting multiple IFTTT recipes.

Create a new Gmail account

This account will receive temperature event emails from IFTTT, which your Raspberry Pi will read.

Use IFTTT to email your schedule to the PiNest

Sign up for a free IFTTT account and create a recipe that emails your calendar entries to a new Gmail account – you can use this recipe and fill in your own calendar and email settings. The RPi will periodically check this account and compare the target temperature (in the email subject line) to the current temperature, so it knows when to turn on/off the heating. You’ll need to set IFTTT’s email function to use the Gmail account and Google Calendar you’ve just set up.

By using email it also means that you can email the account yourself to set the heating from anywhere without changing the overall schedule. Just write your desired temp, e.g. “21” as the subject and it will send the command to your PiNest. You can also do this with one button press using IFTTT’s Do Button mobile app: assign one button to email “0” and another to email “21”. The PiNest takes its target temperature from the latest command, so this emailed command will last until the next calendar event kicks in.

Setting up the Raspberry Pi

Now that the system for sending temperature commands is taken care of, the Raspberry Pi itself needs your attention.

The hardest part – you’ll need to set up your components on a breadboard to test them. We’d advise you to screw them down to a board if possible to keep everything manageable. The digital thermometer does the temperature sensing, and the rest goes into controlling the boiler.

For setting up and reading the DS18B20 Digital Temperature Sensor, we’ve taken our cues from the excellent tutorial by Simon Monk at Adafruit.

We’ve then combined that with Pete Taylor’s tutorial on setting up the Raspberry Pi to read emails and the fork of his project by GitHub user vwillcox, which empties the inbox once the email has been read.

To start, we’ll assume that you’ve got Raspian Linux installed on your Raspberry Pi (if you’ve not done that yet, see instructions here – beginners should start with a NOOBS installation) and that you’re working with the Raspberry Pi 2 Model B, which is the version that we used.

As we’re using WiringPi’s GPIO numbering system there shouldn’t be much difference, though. You’ll also need your RPi connected to your network via an Ethernet cable or USB Wi-Fi dongle. For the latter you’ll need to configure your settings within Raspbian’s GUI.

Wiring up the components

Wiring up the components

Wire up the components on your breadboard as shown in the diagram. All the connections are included apart from the one from the relay to the boiler.

Pay particular attention to the orientation of the transistor and digital thermometer. Your digital thermometer should be placed exactly as in the diagram with its flat side towards you, but your transistor may be the larger, round type, so make sure you double-check you’ve connected it the correct way by comparing the schematic below with the diagram on your transistor’s data sheet.

RELATEDRaspberry Pi 2 review

Schematic

Schematic

Configuring the Raspberry Pi

Before our thermostat program will run, we have to configure the Raspberry Pi. You’ll have to do this via the command line (aka terminal window). If this is your first time, read raspberrypi.org’s explanation.

The indented text below denotes that it is what you should type into the terminal window.

1. Make sure you have the latest updates

sudo apt-get update

2. Enable SPI and i2C protocols

In the terminal window, type:

sudo raspi-config

Select Advanced Options, then enable both SPI and i2C

Select finish to reboot. Once it’s restarted, test installation with:

gpio load spi

gpio load i2c

If the above two return nothing, drivers are working okay. If they produce errors, try following Adafruit’s manual i2C and SPIconfiguration instructions.

3. Install Github

This is needed for the installation of the WiringPi library.

sudo apt-get install git-core

4. Install the WiringPi library and Python

git clone git://git.drogon.net/wiringPi

cd wiringPi

./build

Install Python:

cd

sudo apt-get install python-dev python-pip

Press y to confirm, enter

Install WiringPi2:

sudo pip install wiringpi2

Check installation of WiringPi2 (should return RPi board number e.g. "2"):

sudo python import wiringpi2

wiringpi2.piBoardRev()

Press ctrl+d to exit python

Test setup by typing the following:

gpio readall

You should see a rundown of the pinouts.

5. Set up the DS18B20 digital temperature sensor

Go back into the same config.txt file:

sudo nano /boot/config.txt

Add "dtoverlay=w1-gpio" to the bottom of the file, save it and reboot:

sudo reboot

Establish contact with the digital thermometer:

sudo modprobe w1-gpio

sudo modprobe w1-therm

cd /sys/bus/w1/devices

ls

A list of serial numbers of connected sensors should be shown such as "28-xxxxxxxx". If no serial numbers are shown, check all connections and try again, otherwise type the following, replacing 28-xxxxxxx with the serial number shown in your terminal window:

cd 28-xxxxxxx

cat w1_slave

If a reading is being taken successfully, one of the response lines should say “YES” in it. The temperature reading will be at the end of the second line (divide by 1000 for the actual temperature in C).

Add the code and run the program

Now that your Raspberry Pi is set up, it’s time to load the Python file that controls your thermostat.

You can find the code for the file here on GitHub.

Open a new file called pinest.py with the following command:

sudo nano python pinest.py

Copy in the code from the GitHub link above. You’ll need to edit the email address and password within the code file to match your new Gmail address. Save and close it, then run your program:

sudo python pinest.py

It should start displaying current temp, target temp and whether the heating (relay) is on or off. It will repeat this every five seconds. You should hear the relay clicking when it changes between on and off. You can test this by emailing a new target temperature to your new gmail account.

It may have trouble running if there are no emails in the account (cannot find var subject), or if there are too many to process. Starting with one email in the inbox should solve this.

Connect to your boiler

Once you’ve checked pinest.py is running properly and your relay is turning on and off as it should – you may want to connect an LED and resistor in place of the boiler circuit to make sure – the last step is to connect it to your boiler.

This last step is the most important to get right, because to control your heating the relay must be connected to the control circuit on your boiler. If, like us, you’re not a plumber or an electrician, we’d advise finding someone who is to help you do this bit. Mains electricity can be the end of you, so don’t take any chances.

The only part of your circuit that should have any contact with the boiler circuit is your relay. Do not connect mains voltage to your breadboard – it is absolutely not designed to handle anything over about 15V.

You’ll need your boiler’s instructions for a wiring diagram of where to attach a programmable thermostat.

Let us know how you get on and give us a shout if you have any suggestions or additions to this project.

NOW ADD THESE

1. By experiementing with IFTTT recipes, you can set more complex behaviours such as using your phone’s GPS to tell the PiNest to turn off the heating when you go out. You can find ours here.

2. Once your PiNest is up and running, you don’t have to stop there. Why not add a display (from US$35) and onboard temperature controls? Throw it in a nice case and you can stick it up on the living-room wall to show off to your mates.

Profile image of StuffTV StuffTV