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...

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

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.


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'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:// 

cd wiringPi


Install Python:


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


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


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 with the following command:

sudo nano python

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

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.