Keith Sterling

Its a mixed bag of stuff, but mainly tech, music and programming, oh and just a bit of social comment !!!

Keith Sterling - Its a mixed bag of stuff, but mainly tech, music and programming, oh and just a bit of social comment !!!

Room Temperature Sensor With Raspberry Pi

This article is taken from the following web pages, with a few modifications by me.

The intention is to create a Raspberry monitoring station for each room in the house, each broadcasting temperature and other information back to a central server in my office.

To start with I am using DS18B20 temperature sensor, which you can get from Amazon and eBay for about 50p-£1 each.

Along with the sensor you are going to need a breadboard, ribbon cable, some wires and 4.7k resistor


Once you have this wired up, you need to activate the sensor

$ sudo modprobe w1-gpio
$ sudo modprobe w1-therm
$ cd /sys/bus/w1/devices
$ ls

This will give you the id of the device starting 28- and looking something like


Now create a shell script we can query the sensor

nano ~/

And add the following lines

#! /bin/bash
sudo modprobe w1-gpio
sudo modprobe w1-therm
roomtemp=$(cat /sys/bus/w1/devices/28-xxxxxxxxxxxx/w1_slave | grep  -E -o ".{0,0}t=.{0,5}" | cut -c 3-)
echo "Temperature: $roomtemp"

To make it executable, give it +x permissions

chmod +x ~/

And it will print out the temp, device / 1000 for ‘C

$ ~./

Which is 18.69’C

Now lets create a little Python script that parses the data and loops printing out the value every 10 secs

nano ~/

And add the following lines

import time
while True:
        # Open the file that we viewed earlier so that python 
        # can see what is in it. Replace the serial number as before.
        tfile = open("/sys/bus/w1/devices/28-xxxxxxxxxx/w1_slave")
        # Read all of the text in the file.
        text =
        # Close the file now that the text has been read.
        # Split the text with new lines (\n) and select the second line.
        secondline = text.split("\n")[1]
        # Split the line into words, referring to the spaces, 
        # and select the 10th word (counting from 0).
        temperaturedata = secondline.split(" ")[9]
        # The first two characters are "t=", so get rid of those and 
        # convert the temperature from a string to a number.
        temperature = float(temperaturedata[2:])
        # Put the decimal point in the right place and display it.
        temperature = temperature / 1000
        print ("%2.2f'C" % temperature)

To run the script, use


Mine is currently showing a balmy 16-19’C in my office


Loading Drivers on Startup

The following lines are taken from

sudo nano /etc/modprobe.d/raspi-blacklist.conf and comment out the following 2 lines

blacklist spi-bcm2708
blacklist i2c-bcm2708

sudo nano /etc/modules


Next steps

Now that we have a temperature sensor schematic and working prototype, need to build 3 or 4 more, and then hook them up to the central server. So once they are built, I need a Restful service that each of the Pi’s can call with their temperature data. The easiest way in Python is using the Flask framework backed with a Mongo DB. Watch this space for….

Building a RESTful web service using Pythong & Flask


  • Simon Parkes says:

    Hi Keith, nice writeup, am using a DHT22 on mine, so can get Humidity as well as room temp..

    Am polling the data every 5 mins into an sqlite3 db, and at the moment, just pukes the data to screen, thinking of putting it into rrd and graphing that way..

    March 12, 2014 at 1:12 pm
    • admin says:

      Logging to csv local for backup but sending the values to a central server in the office, as now have one in each room of the house. Currently building a motion detector with the pi-cam, so that each pi becomes a room and security sensor

      March 17, 2014 at 10:36 pm
  • Simon Parkes says:

    Could use something like “Motion”:

    Used in my my pi and set it up wrong, I was just capturing jpg’s but was having issues with quality, I think was a cheap camera and a slow SD card.. But regardless managed to fill up the SD with images a bit on the quick side !
    Regardless you could use Motion as a trigger, capture for 5 secs, upload and alert or something like that ..

    March 18, 2014 at 8:27 am

Your email address will not be published. Required fields are marked *