Add a Raspberry Pi temperature gauge to your blog

At the moment I have a Raspberry Pi sat in the kitchen at home, running a Ghost blog – you can usually find it a ghostpi.org (BT broadband connection permitting).

As I don’t usually leave computers running 24 hours a day, 7 days a week I was curious as to how warm the Raspberry Pi gets with use, so I’ve found a handy way of keeping track of the temperature of the CPU. Admittedly the blog isn’t driving massive amounts of traffic – it was mentioned on Reddit once which generated a spike of activity, but the plucky little Pi held up very well. There are heat sink kits available for the Pi that add a block of metal – either aluminium or copper to dissipate heat. At the more extreme end of the spectrum someone’s even created a water cooled Pi.

The snowman still isn't getting in the least bit warm
The snowman still isn’t getting in the least bit warm

Realistically, although they look nice, heat sinks are a bit like go faster stripes or fluffy dice – in use the biggest impact on my Raspberry Pi was what time of day it was, and if the sun was shining on the kitchen shelf. Famous last words, perhaps this will get slashdotted and I’ll come home to find a smouldering pile of molten plastic.

If you’re interested in hosting a Ghost blog I’ve posted some instructions here – it is a beta blogging platform based on node.js which launched last year – it generally works well on the Pi, although for slightly complicated reasons logging in takes about 3 minutes (enough time for a cup of tea).

I’ve been using RRDTool to track the temperature of my Pi’s CPU – every 5 minutes this updates a nice little graph of the last 24 hours as a PNG which is then copied to the images directory on my GhostPi.org blog:

This might get cached so click on the image for the latest version

This can then be added to a page on the Ghost blog giving some stats on how hard the Pi is working.

In putting this together I used the information on this blog with a few alterations to get it to work for me.

First install the RRD  (Round Robin Database) tool:

sudo apt-get install rrdtool

 

Then run this script to set up the database. I created a folder in my home/pi directory called ‘scripts’ and then used nano to edit the script.

#!/bin/bash
#
# update .rrd database with CPU temperature
#
# $Id: update_cputemp 275 2013-05-16 05:20:56Z lenik $
cd /home/pi/scripts
# create database if not exists
[ -f cputemp.rrd ] || {
/usr/bin/rrdtool create cputemp.rrd --step 300 \
DS:cputemp:GAUGE:1200:U:U \
RRA:AVERAGE:0.5:1:3200 \
RRA:AVERAGE:0.5:6:3200 \
RRA:AVERAGE:0.5:36:3200 \
RRA:AVERAGE:0.5:144:3200 \
RRA:AVERAGE:0.5:1008:3200 \
RRA:AVERAGE:0.5:4320:3200 \
RRA:AVERAGE:0.5:52560:3200 \
RRA:AVERAGE:0.5:525600:3200
}

 

Run the script with the bash ‘name of your script’ command.

Now create a script which will update the graph: – you might want to edit the line cd /home/pi/scripts to point in the right location. The last line starting cp copies the png file to the content folder on the ghost server. Again you might want to edit that.

#!/bin/bash
#
# update .rrd database with CPU temperature
#
# $Id: update_cputemp 275 2013-05-16 05:20:56Z lenik $
cd /home/pi/scripts
# read the temperature and convert .59234. into .59.234. (degrees celsius)
TEMPERATURE=`cat /sys/class/thermal/thermal_zone0/temp`
TEMPERATURE=`echo -n ${TEMPERATURE:0:2}; echo -n .; echo -n ${TEMPERATURE:2}`
/usr/bin/rrdtool update cputemp.rrd N:$TEMPERATURE
/usr/bin/rrdtool graph cputemp.png DEF:temp=cputemp.rrd:cputemp:AVERAGE LINE2:temp#00FF00
cp /home/pi/scripts/cputemp.png /home/pi/ghost/content/images/

Test this script with bash ‘name of your update script’ – run it a few times and you should see the green line start to grow on the png graph.

Finally because you don’t want to keep having to run the script manually you need to add it to crontab:

crontab -e

 

and then add the line:

*/5 * * * *   ./scripts/CPU_temp.sh

 

the */5 bit means run every 5 minutes, and again you might need to edit the location and name of the script.

RRDtool is quite simple – there are projects out there that use external sensors to track temperatures outside the Pi. Part of me is wondering if running RRDtool a lot and reloading the PNG file will then start increasing the temperature of the CPU in a sort of observer effect feedback loop…

 

2 thoughts on “Add a Raspberry Pi temperature gauge to your blog”

  1. Oh, priceless!
    I was looking for a simple way to use RRDtool for a similar measuring part of a project.
    Thanks!

    Oh, BTW, to save the SD card a bit, I’m using one of those /dev/ramX areas for the database. On boot I format /dev/ram0 to ext2, and add it to /etc/fstab. 4 megs of storage and no SD wear! I guess I could recompile the kernel (if I’m going on holiday!) to give a bigger space…but 4M is probably enough.

    -Cheers
    -Andy

Leave a Reply