Raspberry Pi as a Temperature Logger
Doug posted in Uncategorized on April 16th, 2014
I had a recent need in my home to create a temperature log. I wanted to be able to create a nice little graph which could show correlation between outside temp and temperature inside a room over time. I have three Raspberry Pi’s, so I thought I could re-purpose two of them to track temperature in two different rooms simultaneously, as well as pulling in outside temp.
Although I consider myself fairly technically savvy, I am quite pragmatic in my home electronics, generally just building what I need and not nearly the tinkerer type. But after digging around on Amazon and seeing how limited the options were to creating the data I wanted, I felt like going the “hard road” by building what I needed.
The project ended up taking a bit more time than I expected, but as with so many things, was worth the extra effort and gained quite a bit more knowledge than I would have had otherwise, as well as a much more flexible/powerful tool in the future.
What you need:
– Working Raspberry Pi running a Linux distro (I used Raspbian)
– Temp Sensor: Bosch BMP180 ($10)
– Stuff to wire the two together:
> For me, this was a breadboard, jumper wires, Soldering iron & solder
– Optional: Excel for great graphs of the data (I will include a template spreadsheet for you below)
Step 1. Get the hardware wired up and talking to the Pi
I used this tutorial (it’s a guide for the “BMP085” which is just the older version of this new sensor).
Notes: I felt like the tutorial glossed over the wiring (page 3: “Hooking everything up.”) It assumes you have some basic circuit wiring experience, used a “Pi cobbler,” and familiar with the pins on the Raspberry Pi. I had none: never used a breadboard or jumper wires and only done basic soldering in the past (to make wires and such).
New to wiring circuits? Read on…
When you get the BMP180, it will look like this:
I assume just to confuse you, you’ll notice that there are six metal pins and only five holes in the sensor. Pull one of those pins out from the left or right side, and you’ll be able to stick that little guy into the sensor – use the side with the shorter pins. This should have each pin just slightly sticking up from the sensor. From there, you will need to use your soldering iron to apply a little solder to each of the pins to lock them into place. You’ll notice from the “Hooking everything up” section in the tutorial linked above that you only need to wire four of the five pins (VIN, GND, SCL, and SDA). You’ll need to solder each of those pins into the chip. The end result will look something like this (I just removed the unneeded pin):
I expected this to be easier than it was. I purchased a this BP650NB battery-operated soldering iron as it looked awesome and was recommended by the guy at Radio Shack. Worthless. I wasted over an hour trying to get this thing to solder those tips. In the end, my old (wired) soldering iron I have had for over 20 years generated enough heat to get the job done. Waste of money, that BP650NB.
From here, you need to wire connections over to the right pins on the Raspberry Pi. For this, I used a breadboard. Had seen these before but did not follow how they worked. Simple thing really: Anything plugged into a hole on the board will be connected to anything else plugged into another hole that’s on the same “line.”
I wanted to do this the way all the other cool guys on the internet were doing it; however, you could just as well have simply attached the jumper directly to the wire from the chip that’s headed into the breadboard and cut out the breadboard altogether. Even though it doesn’t apply here, reading around it seems like the better habit to get into, as this lets you much more easily do stuff with other chips/sensors later on. It’s more important with some chips that depend on others to work in tandem to create the solution you need. Fortunately, this one is pretty simple and can be wired directly to the Pi.
Use that Hooking Everything Up page to see what pins on the Raspberry Pi should be hooked to the pins on the BMP180:
BMP180 – Pi
GND – GND
SDA – GPIO 0 (SDA)
SCL – GPIO 1 (SCL)
VIN – 3v3
Finally, head to the final step in the tutorial to pull down the python library and example code to communicate with the chip. Have to admit I was pretty excited when I ran the example code and got a response from the sensor.
sudo python Adafruit_BMP085_example.py
Step 2: Write a program to write a line with the data you need, delimited by commas
Using the example program as a base, I built a program to output a single CSV line with the following information:
location, current date/time, inside temperature, outside temperature (sourced from the NOAA), windspeed (also sourced from NOAA)
Location is a string tag which i used to know what room was collecting data (hardcoded at the top of the program). I used different hardcoded tags on each of the two Raspberry Pi’s that were running this program to identify which room the data came from.
Getting outside temp
While the code is fairly self-explanatory, there is one particular area that deserves attention, as you would want to modify this for your location.
To get the outside temp from NOAA, I found that I could use some functions in urllib2 to easily parse the XML from NOAA showing current conditions of the closest location (RDU airport, in my case.) Use this page to find the XML link for the location nearest you and update the code with that location for your use.
# get current temp from NOAA for RDU
url = 'http://w1.weather.gov/xml/current_obs/KRDU.xml'
response = urllib2.urlopen(url)
xml = response.read()
wind_mph = ET.fromstring(xml).find('wind_mph')
temp_c = ET.fromstring(xml).find('temp_c')
outsidetemp = Decimal(temp_c.text) * 9 / 5 + 32
Here is the full program, which simply appends the output to the file templog.csv on each execution: TempLogger.py
Place this in the same directory as the rest of the downloaded BMP085 libraries. This is likely:
To run it, you will need to run as the superuser:
sudo python /home/pi/Adafruit-Raspberry-Pi-Python-Code/Adafruit_BMP085/TempLogger.py
Check to verify your output file:
Run the program again and check your output file – it should now contain another line.
Step 3: Log the temperature data every five minutes
Now to make use of the lovely built-in scheduler for Unix called cron.
Pull up the cron job scheduler table by typing the following:
sudo crontab -e
Add the following line to the end of the file:
*/5 * * * * python /home/pi/Adafruit-Raspberry-Pi-Python-Code/Adafruit_BMP085/TempLogger.py
1. Changing the 5 to any other number will change the delay to that many minutes. For example, use */15 at the beginning of the line to log at 15-minute intervals.
2. We do not need to use “sudo” when running the program as root actually initiates cron jobs.
That’s it! You should now be logging the temps to your CSV file every five minutes.
Step 4: Graphing your data
But your work is not really complete, now is it? You still need to pull this data and make cool graphs and such. I used WinSCP to pull the CSV file onto my Windows machine. I then used Excel to pull in the CSV data and graph it. I first combined the data from two different CSV files (I was measuring two locations with two Raspberry Pis) onto one spreadsheet. From there, I created a graph. It was a pain to figure out how to get a line graph to work correctly with dates, and especially the gridlines at the bottom to be at 3HR marks. Use this spreadsheet as a template and change the chart data as you need to. It should greatly speed up your effort.
TempLog Graph Template
Let me know in the comments if you were able to get this to work or if you have any questions!