Friday, 11 January 2013

Home brew temperature logger with Raspberry Pi


Been wanting to do this one since I got my first Pi!

In my old (now old-old-) job, I worked a lot with temperature monitoring (and various other metrics), which I loved because I'm a giant nerd and I love data! I've written a few posts about various logging projects that I've half-baked with Arduinos, but really wanted to take a step up with the Raspberry Pi.

I bought a USB temperature probe on eBay for about £8 (~$12). The software with it is a bit rubbish and I wanted to integrate it into something bigger. Finally I found a script which works in Linux, which made it perfect to attach to a Raspberry Pi!

The finished product is fully self-contained, featuring its own Web interface with purdy graphs. It's currently hooked up to a home beer making kit in my cupboard :)
 


Temperatures are really high in the graph above because I hooked the probe up to the central heating pipes

What you'll need:

  1. Raspberry Pi
  2. SD card
  3. Power supply
  4. USB temperature probe - (eBay link: http://www.ebay.co.uk/itm/230707587490)
  5. Case (optional but useful)
  6. Internet connection for RPi (wired or wireless)

Installation - It's all packaged into a nice shell script so the intricate bits are all taken care of

NB: all of this can be done from the console - if you're like me, none of your Raspberry Pi projects will have screens attached so you should get comfortable with using the console instead of the graphical interface. This means you save on a lot of resources (processing power!!!) and you can remotely access the RPi over SSH. Anyhoo, on to the real stuff...

  1. Start your RPi 
  2. If you're working from a fresh install of Raspbian: (if you've already done this, then skip to #3)
    1. Start raspi-config (it may auto-start, if not type "sudo raspi-config" without quotes, then hit enter)
    2. Expand your root FS
    3. Hit finish, then reboot
  3. Download the files https://www.dropbox.com/s/ngykfondakrcgkl/pcsensor-pi-1.0.0.zip
  4. Unzip files
    1. "unzip pcsensor-pi-1.0.0.zip"
  5. Run setup script
    1. Pick a "root" mysql password and remember it - it's going to ask you for it a few times
    2. "cd pcsensor"
    3. "chmod u+x setup_pcsensor.sh"
    4. "./setup_pcsensor.sh"
  6. (re)insert your USB temperature probe
  7. Open a web browser, point to http://raspberrypi/temps.php
Note: You're going to get two identical graphs at the start - the left hand on displays the last 60 minutes, the right hand one displays the last 8 hours
Note 2: The web page is optimised for mobile phone viewing ;)


How does it work?

pcsensor.c / pcsensor

Someone (first link at the bottom) has very kindly written a small C program which simply reads the current temperature and outputs it to the console. I has to modify it slightly so that when you call it with an argument ("pcsensor -c"), it only outputs the temperature.

log_pcsensor.sh

Now that we have a way to read the data, it's simply a case of sending this data to a database table every so often (I chose every 1 minute). The log_pcsensor.sh file is a shell script which takes the output from the pcsensor script and inserts a new record into the database every time the script is called

cron job

cron is a Linux background service which executes scripts according to a schedule. I just added a job to cron to call the log_pcsensor.sh script every 1 minute

temps.php and tempgraph.php

temps.php - Data isn't awesome unless we can read and interpret it! I started off with a very simple php page which displayed the latest temperature, and a table with the previous 25 readings. It was cool, but not cool enough!
tempgraph.php - Then I thought, "Why can't I graph it?" So I researched some graphing packages for PHP, found one I liked (JpGraph), and created a page which will read the database and display a graph of the data. 

setup_pcsensor.sh

Finally, I knew I was going to have to blog about this (I feel better as a person when I see my page views go up!) so I decided to make it easy for people to install. It's probably not the most educational way to present the project (the RPi is, after all, an educational tool), but I prefer to learn by example rather than blindly follow a list of instructions. 


What the script does - Software we're installing:
  1. Raspbian (Raspberry Pi operating system)
  2. Apache2 Web server
  3. PHP5
  4. MySQL server
What the script does - Additional steps:
  1. Configures MySQL DB
  2. Installs PCSensor code (link at bottom)
  3. Installs my scripts
  4. Configures cron job
  5. Installs jpGraph (link at bottom)
  6. Installs my web app

Links that helped me complete this project:

Big thanks to all below
http://www.panticz.de/Ubuntu-Temperature-monitoring-with-TEMPer-USB-sensor - USB Sensor driver. So glad someone made a script to work with this!
http://jpgraph.net/ - Graphing package for PHP
http://ubuntuincident.wordpress.com/2011/01/28/drawing-graphs-in-php-with-jpgraph/ - How to fix the anti-aliasing problem with JpGraph in this setup