Raspberry Pi, Xively and DS18B20 the Unix Way

This post is building up on previous Xively Data Logging the Unix Way, so read that one first.

DS18B20 is a cheap digital thermometer with One-Wire interface. There are many tutorials how to make it work in Arduino, Raspberry Pi and other platforms. For the Raspberry Pi I was following excellent Adafruit Tutorial. As far as the HW goes, this is perfect.

Then I didn’t like the idea of python programming. There are alternatives to the Adafruit proposed script. The most robust is probably OWFS – collection of drivers to create filesystem like access to all kinds of One-Wire devices. For my purpose that seemed as too much of an overkill.

I wanted to build the scripts on top of my previously created siple xively logging script. So the only challenge was how to get the temperature reading from DS18B20 using only standard unix commandline tools.

Once you make the kernel modules run, as Adafruit suggest here, you can read the temperature from special files /sys/bus/w1/devices/*/w1_slave (the * stands for DSs’ ids). With my particular DS18B20 the file is /sys/bus/w1/devices/28-000001b451d9/w1_slave and normally contains this:

6d 01 4b 46 7f ff 03 10 70 : crc=70 YES
6d 01 4b 46 7f ff 03 10 70 t=22812

The temperature in this example is 22.812 Celsius.

So I created the following bash script, which given the DS18B20′s ID as a parameter, prints current temperature. It’s basically one line of grep&awk plus some boilerplate.

To periodically log temperature to xively, just put the second small script to your /etc/cron.hourly or /etc/corn.minutely folder (of course you need to update it with your particular DS18B20 id and your xively channel name).