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
- Raspberry Pi – any model will do, but bear in mind the Pi Zero has fewer USB ports
- Ethernet Powerline Adapters unless you Pi has wifi
- SODIAL(R) TEMPer PC USB Thermometer
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
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 wget https://github.com/padelt/temper-python/archive/master.zip unzip master.zip
Now install the python module:
cd temper-python-master sudo python setup.py 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:
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: try: #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: f.write(header_row) #Write the datetime and temperature f.write(str(datetime.datetime.now()) + ',' + str(devs.get_temperatures()['temperature_c']) + '\n') #Close the file until next time... f.close() print str(datetime.datetime.now()) + " - " + str(devs.get_temperatures()['temperature_c']) #Badly handle any exceptions... except Exception as e: print "An error occured..." + str(e) pass #Sleep for 10 mins before taking the next reading time.sleep(600)
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.
screen [press return] sudo python temperature_logger.py
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
To rejoin the session later, simply run
and you’ll be back watching the logger running.
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!