Stuff Babies Need

Raspberry Pi temperature logger – Raspberry Pi Baby Monitor Part 2

As our original baby (the inspiration for this site!) is now a toddler, we are making steps towards a move to the “big boys” room so that the nursery is free for his new sibling!

This is Part 2 of our series in turning the cheap and tiny Raspberry Pi computer into a baby monitor – if you’ve not seen Part 1 yet, go back and check it out!

As our house is a little older, some of the rooms can be a bit drafty. We’ve not really spent much time in the room that our toddler will be moving in to, and those that have often complain that it is a little chilly at night.

Whilst the thermometer owl is good for a quick peace of mind, I want to know how the temperature changes in this room over time. Sounds like an excuse for another Raspberry Pi project – the Raspberry Pi temperature logger!

You will need

Note – if you want to follow along before your thermometer turns up, find out how to use the Raspberry Pi CPU temperature sensor to approximate the room temperature.

Step one – prepare the Raspberry Pi temperature logger

You can skip this step if you’re following on from Part 1. Otherwise, you can follow along just with this part, use the Raspbian Jessie Lite image – we don’t need any fancy desktop environments here. Write it to a fresh SD card (follow tips on our previous post).

Once you’re up an running, ssh into your Pi

ssh pi@your-pi-ip-address

Step 2 – install some packages

Our temperature logger will be written in python, so we need to install a few packages to get the temperature reading from the USB thermometer. First, some python packages need to be installed:

sudo apt-get update

Let that get an up to date package list, and then install what we need:

sudo apt-get install python-usb python-setuptools git screen

Next, download the temper-python package from github

mkdir temperature_logger
cd temperature_logger


Now install the python module:

cd temper-python-master
sudo python install

Finally, we need to make a tweak to the boot options of the Pi so that the USB module can reliably read the temperature. Add the following line to the end of the /boot/cmdline.txt file:


I didn’t do this at first, and it resulted in frequent errors:

  • usb.core.USBError: [Errno 16] Resource busy
  • Unknown error

So, it’s worth doing. Now, reboot the Pi!

Step 3 – read some temperatures!

Once that is all done, we should be able to plug the SODIAL(R) TEMPer PC USB Thermometer in and get a temperature reading. The python module added a handy command line app to print the current temperature:

sudo temper-poll

Assuming that works, we can then write our own script that will be run periodically and write the current temperature to a CSV file for future analysis.

Step 4 – writing the Raspberry Pi temperature logger

Our simple script simply needs to get the current temperature and write it to a csv file, along with the date and time. We can then simply sleep for a while, and loop!

import os.path
import datetime
import time
from temperusb import TemperHandler

#Set up the temerusb module to read the temperature
th = TemperHandler()
devs = th.get_devices()

while True:
		#If the file is new, we'll write a header row
		header_row = None
		if os.path.isfile('./temperature_log.csv') == False:
			header_row = 'datetime,temperature_c\n'
		#The temperatures will be logged to this file	
		f = open('temperature_log.csv', 'a')

		#Write the header row if needed
		if header_row:
		#Write the datetime and temperature
		f.write(str( + ',' + str(devs[0].get_temperatures()[0]['temperature_c']) + '\n')
		#Close the file until next time...
		print str( + " - " + str(devs[0].get_temperatures()[0]['temperature_c'])
	#Badly handle any exceptions...	
	except Exception as e: 
		print "An error occured..." + str(e)
	#Sleep for 10 mins before taking the next reading

Assuming you are using ssh to access the Pi, you may want to use screen to allow you to disconnect from your session whilst leaving your temperature logger running.

[press return]
sudo python

The logging script should then run and print the date, time and temperature. It also writes the temperature to a CSV file which we can look at later to see how the temperature changes in the room overnight.

To disconnect and leave the logger running press

ctrl-a d

To rejoin the session later, simply run

screen -R

and you’ll be back watching the logger running.

Future work

The Raspberry Pi temperature logger script is a little quick and dirty! It could be improved and perhaps simplified if we used cron to schedule the execution of the script. Another possible extension would be to write the temperatures into a database – this would lend itself to serving the data through a website on the Pi. However, with a baby on the way, these plans will have to wait!

Many thanks to the creators of the python-temper package.

I will try and find time to get to Part 3 finished and show how to combine this in to the baby monitor we started in Part 1!

4 comments for “Raspberry Pi temperature logger – Raspberry Pi Baby Monitor Part 2

  1. June 23, 2016 at 8:26 pm

    Thanks for part 2. Was waiting for it.

  2. Alexandra
    May 19, 2017 at 8:22 am

    Can you run the baby monitor and the temp logger together? My son is a penguin and it’s very easy for his room to get too hot! It would be nice to be able to run both programs and use it as my course proect for school!

    • June 23, 2017 at 4:16 pm

      Yeah, that sounds perfectly possible and a great idea for a project!

Leave a Reply

%d bloggers like this: