Using a Raspberry Pi to monitor temperature remotely


The central heating in our holiday flat in Crieff failed a few years ago, in the middle of a very cold winter. When we got there, the boiler had frozen right up, and was damaged beyond repair. Unfortunately, we had regular guests due to arrive a few days later, but no way to get a replacement in time and so we had to cancel their booking. They were quite understanding about it, the weather that winter had been exceptionally bad.

Raspberry Pi set up to monitor temperature

So, since then we've kept the flat well heated over the winter, but there's always the risk that the boiler could fail (e.g. if the condensate drain freezes up) so I'd like to keep an eye on the temperature. I'm aware of a few different commercial solutions, but they only seem suitable for systems with a central thermostat, which we don't have, so I decided to put something together myself.

We have a wi-fi connection at the flat, so I decided to set up a Raspberry Pi with an temperature sensor and get it to email me with the temperature every day. This page explains the various steps I went through to get it working.

1 - Buy the kit

You will also need a computer or tablet with network access, and some way of writing an SD card. A soldering iron and some solder will be useful for connecting the resistor.

I found CPC Farnell was the best place to buy the Raspberry Pi, the rest came from Maplin and eBay. The total cost was just under £50.

I bought a Model B Pi, but the cheaper Model A, with less memory and no ethernet connector, would work just as well. The case and GPIO cable are also not strictly necessary, but were useful to have. (You could make your own box using these plans for a Raspberry Punnet)

2 - Set up the Raspberry Pi.

This stage covers getting the Raspberry Pi connected up to the network via a Wi-Fi connection, jump to step 3 if you already have that working. You set up a Pi lots of different ways, as described in many different tutorials, including these:

The choice of which way to do it will probably depend on what hardware you have available. I didn't have any HDMI cable or USB keyboard but did have a PC with SD card writer, so went for a "Headless" setup with the Raspbian distribution, which means that I prepared an SD disk image on a separate PC, then inserted it into the Pi and switched on. The Pi then boots up and obtains an IP address via DHCP, and you can then connect to it via SSH to set up the Wi-Fi connection

2.1 "Headless" setup

A Headless setup means setting up the Pi without any local connection to keyboard or monitor.

Download the Raspbian disk image, at:

Download the Win32 Disk Imager, at:

Expand the zip files that you downloaded to a location on your PC hard disk, and run Win32DiskImage.exe. Insert the SD card into your PC, and select it in the Device drop-down box on the right. (It's important to make sure you have the right device here, or you might end up overwriting something else.) Now select the the Raspbian disk image that you downloaded (it will have .img file extension) and click Write to copy it onto your SD card.

2.2 Connect to the network

Put the SD card into the Pi, and connect the Pi to your router with a network cable, and When you boot up the Pi will attempt to get an IP address via DHCP. If you're on a home network with a router this will usually happen automatically, but if not, you may need to change some settings on your router to make it happen. If it's all working correctly the LEDs on the Pi should flash a few times indicating the various stages in the boot process, and them mostly stay on with occasional flashes to indicate activity. This link has more details on what the lights mean:

You should also be able to find out the IP address that the Pi is using by looking at the admin page on the router (on a BT HomeHub it's under Advanced Settings, on the Home Network page.) The first 6 letters of the MAC address will be b8:27:eb which can help to identify the correct device.

Once you've worked out the right IP address, try ping ≤IP_ADDRESS≥ to see if you can connect to it. The next step will be to connect via SSH.

2.3 Set up SSH connection and initialise the Pi

For this step, you will need to install an SSH client on your PC, a good choice is putty which you can get here:

SSH is enabled by default with Raspbian, so you don't need to do any setup on the Pi before making an initial connection. You just need to enter the IP address of the Pi in the Host Name dialog box after you start Putty, then you will be prompted for a login name (pi) and a password (raspberry.) After logging in, a message will display asking you to configure the Pi, follow the instructions to expand the Filesystem and change the default password, and then change any other settings you want to and reboot.

This link explains how to use SSH under Windows;

2.4 Setup Public Key Authentication

This step isn't essential, but it will make access to your Pi more secure. If you're going to put it onto a public network (to allow remote access once it's in place) this is definitely recommended. There are various ways to do it so I won't go into the details, here's one description:

2.5 - Connect to the wireless network

To allow the Pi to connect to a Wi-Fi network you'll need to configure it with the SSID & WPA key to use. For a home network these are usually printed on the router somewhere.

Again, there are various different ways to do this, the simplest way I found that will allow multiple networks to be configured was to use wpa_supplicant.

I edited /etc/network/interfaces to include this:

auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
pre-up wpa_supplicant -Dwext -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B 
post-down killall -q wpa_supplicant

I then edited /etc/wpa_supplicant/wpa_supplicant.conf to include SSID & keys for the wifi networks I wanted to connect to, as follows:



This allows you to prepare the Pi for a Wi-Fi network before connecting to it, and after these steps the Pi will connect to one of the listed Wi-Fi networks if it's available when it boots. (You need to replace MySSID & MyPSK with the correct ones for your network of course.)

3 - Add temperature gauge

There is a good tutorial on how to do this here, in steps 2 and 3.

3.1 - Install drivers for the DS18b20

The DS18b20 uses the 1-Wire interface, which is not as well supported as other serial interfaces such as I2C or SPI, but you can install a driver module which will uses the GPIO-4 pin (which is the pin we connected the gauge to.) To load the driver each time the Pi boots, add them to /etc/modules, like this:


3.2 - Connect the DS18b20

The wires from the DS18B20 should connect to the GPIO as follows:

It might be possible to configure the GPIO drive GPIO 7 high instead of using a resistor with some kernel modifications, but I didn't look into it. Here's a picture of how it looked when I connected it. Not pretty, but it works. I'm sure you can do better!

DS18B20 connected to GPIO cable with some ugly soldering

I found that it was very easy to connect the gauge by plugging the wires into the end of the GPIO cable. I soldered the white & red cables to either side of the 4.7 K ohm resister and pushed the resistor ends into the GPIO cable (P1 & P7). I then pushed the remaining black wire into the cable as well at P6.

3.3 - Read the temperature

It's possible to install multiple sensors in series, so each one has unique identifier, and can be seen with:

cat /sys/bus/w1/devices/w1_bus_master1/w1_master_slaves

which will show something like:


You can then read the ouptut for that sensor with:

cat /sys/bus/w1/devices/10-00080242442b/w1_slave

which will show something like:

37 00 4b 46 ff ff 07 10 1e : crc=1e YES
37 00 4b 46 ff ff 07 10 1e t=27312 

You need to divide the number after "t=" by 1000 to get the temperature in degC. The next step is to prepare a script that will do that automatically. This is what I prepared, in a file called


rm -f /tmp/dsb
cat /sys/bus/w1/devices/28-000004ee7036/w1_slave > /tmp/dsb 
VALREAD=`tail -c10 /tmp/dsb | sed -e 's/.*[^0-9]\([0-9]\+\)[^0-9]*$/\1/'`

You'll need to change 28-000004ee7036 to match the ID of the gauge you are using, and set the script as executable, e.g. with:

chmod 755

You can test this is working by running the script, it should display the temperature.

4 - Email temperatures

Once you can read the temperature, the next step is to start a regular job which will email the temperature to you.

4.1 - setup ssmtp

Install ssmtp with:

   sudo apt-get install ssmtp

I set up a gmail account for this, but you could use any email account. You just need to configure ssmtp to use it, by editing /etc/ssmtp/ssmtp.conf. Here's what I used (you'll need to enter your own email address & password of course):

# Config file for sSMTP sendmail
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.

# The place where the mail goes. The actual machine name is required no 
# MX records are consulted. Commonly mailhosts are named

# Where will the mail seem to come from?

# The full hostname

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address

4.2 Prepare a script to send the email.

I prepared a template for the email to send, with placeholders where I wanted the temperature and date to go. I saved this in a file called

subject: Temperature is TEMP
Hi Alan,
The time and date today is DATE 
Temperature today is :  TEMP degrees


I then wrote a script to read the temperature and send an email using this template:

sed -e "s/TEMP/$TEMPERATURE/g" -e "s/DATE/`date +%c`/g" >
ssmtp ≤

You can now test this is working by setting the script as executable and running it (using your own email address for the destination), you should receive an email.

4.3 - Set up a cron job to email the temperature at regular intervals

You now want to run this script at regular intervals, say twice a day. I set it up to run at 7pm and 4am, by editing /etc/crontab to add this line, where webby is the directory containing the various scripts I created:

0 4,19  * * *     pi    cd ~/webby && ./

A slightly better way to add a cron job is to use "crontab -e", which will add the job and also start/stop the cron service. However, I needed to edit /etc/crontab anyway to prevent any emails being sent by cron, since they would go to & by default.

4.4 - Add something to check the wifi connection keeps live.

The network connection from the Pi may drop out, for various reasons. This could be because the Pi goes into power-save mode, or your router may decide that the network is inactive, and disconnect. So I added the following lines to check that the network is active before attempting to send an email:

ifconfig wlan0 | grep "inet addr" >> /dev/null
if [ $? -ne 0 ]
  sudo ifdown wlan0
  sudo ifup --force wlan0

This will restart the WiFi network if there is no connection to the router.

If you are using a gmail account to send email, you will need to login to the account yourself every now and then, or google will decide that it's not being used by a real person, and ask the Pi to fill in a captcha, which will lock it out.

5 - Extra bits and pieces

5.1 - Access the Pi remotely

If you want to connect to the Pi remotely and it's on a home network, you will need to enable Port Forwarding on your router. Your router has a public IP address, which is used by all the devices on your local network. When you enable port forwarding for a particular port, all traffic from the public internet to that port will be forwarded to the device which you specify.

5.2 - Send text messages if you don't have broadband

If you don't have a broadband connection at the location but can get mobile phone coverage, you could set it up to send a text message instead. This link gives some details of what you need to use SMS with a Raspberry Pi


If you spot any problems with the steps on this page, or have some ideas for things to add, please email me at alansraspberrypi, on