How to Use RRDtool on Debian NSLU2 to Capture Temperature Data

I have a 1-Wire temperature sensor IC connected to my small Debian NSLU2 computer and in order to analyze the temperature data generated I’m using a program called RRDtool:

RRDtool is the OpenSource industry standard, high performance data logging and graphing system for time series data.

– Tobi Oetiker

RRDtool is able to make all sorts of graphs for different kinds of data but for now I just need to use it for a single temperature sensor measuring in degrees Celsius.

I start by logging into the NSLU2 from my laptop computer (replace .xx with the address of the NSLU2 on the intranet):

$ ssh thomas@192.168.1.xx

and make a directory for rrdtool, and write a script that will create a basic rrdtool database:

(Note that $ in front of command means I’m working as a normal user, and # means I’m working as super user)

$ cd /home/thomas
$ mkdir rrdtool
$ cd /home/thomas/rrdtool
$ nano create_database.sh

This is what goes into the script:

#!/bin/bash
rrdtool create database.rrd --start N --step 300 \
DS:temp:GAUGE:600:U:U \
RRA:AVERAGE:0.5:1:12 \
RRA:AVERAGE:0.5:1:288 \
RRA:AVERAGE:0.5:12:168 \
RRA:AVERAGE:0.5:12:720 \
RRA:AVERAGE:0.5:288:365

After the script has been saved you’ll need to change the permissions setting of the file to make it executable:

$ chmod +x create_database.sh

rrdtool is already available in the Debian package system – you just need to install it with apt-get:

$ su
# apt-get install rrdtool
# exit

When you run the script it will create a file called database.rrd which contains the rrdtool database:

$ ./create_database.sh

A directory listing shows that the database file was created:

$ ls -la
total 28
drwxr-xr-x 2 thomas thomas  4096 Sep 15 18:50 .
drwxr-xr-x 6 thomas thomas  4096 Sep 15 17:49 ..
-rwxr-xr-x 1 thomas thomas   208 Sep 15 17:52 create_database.sh
-rw-r--r-- 1 thomas thomas 13764 Sep 15 18:50 database.rrd

The database has to be updated regularly and the commands for that can be collected in another script that I choose to name update_database.sh:

$ nano update_database.sh

The update script takes care of three things:

  1. Reading the temperature value from the sensor, and formatting the reading
  2. Updating the database file
  3. Creating, or updating, image files containing temperature graphs

This is the code for the script:

#!/bin/bash
cd /home/thomas/rrdtool

# Read temperature from sensor
tempread=`cat /home/thomas/owfs/10.4F7494010800/temperature` 

temp=`echo $tempread | cut -c -4`

# Update database
rrdtool update database.rrd N:$temp

# Create graphs
rrdtool graph temp_h.png --start -1h DEF:temp=database.rrd:temp:AVERAGE LINE1:temp#0000FF:"Temperature [deg C]"
rrdtool graph temp_d.png --start -1d DEF:temp=database.rrd:temp:AVERAGE LINE1:temp#0000FF:"Temperature [deg C]"
rrdtool graph temp_w.png --start -1w DEF:temp=database.rrd:temp:AVERAGE LINE1:temp#0000FF:"Temperature [deg C]"
rrdtool graph temp_m.png --start -1m DEF:temp=database.rrd:temp:AVERAGE LINE1:temp#0000FF:"Temperature [deg C]"
rrdtool graph temp_y.png --start -1y DEF:temp=database.rrd:temp:AVERAGE LINE1:temp#0000FF:"Temperature [deg C]"
#0000FF means blue trace color in the graphs.

(You can find more information about owfs in this post: How to Read 1-Wire Temperature Using a NSLU2 With Debian )

echo and cut is used to discard some of the excessive decimals, so that the stored value in the database is for example 23.8 instead of 23.875.

The 5 rrdtool graph commands will create graphs, where the timespan is h = hour, d = day, w = week, m = month and y = year.

A directory listing shows that the update script was created:

$ ls -la
total 32
drwxr-xr-x 2 thomas thomas  4096 Sep 15 19:04 .
drwxr-xr-x 6 thomas thomas  4096 Sep 15 17:49 ..
-rwxr-xr-x 1 thomas thomas   208 Sep 15 17:52 create_database.sh
-rw-r--r-- 1 thomas thomas 13764 Sep 15 18:50 database.rrd
-rw-r--r-- 1 thomas thomas   843 Sep 15 19:05 update_database.sh

You’ll have to modify the permissions of the update script to be able to run it:

$ chmod +x update_database.sh

If everything goes well when you run the script the only output is the dimensions of the 5 graphs:

$ ./update_database.sh
481x163
481x163
481x163
481x163
481x163

and a directory listing now shows that images files have been generated (.png stands for Portable Network Graphics):

$ ls -la
total 76
drwxr-xr-x 2 thomas thomas  4096 Sep 15 19:05 .
drwxr-xr-x 6 thomas thomas  4096 Sep 15 17:49 ..
-rwxr-xr-x 1 thomas thomas   208 Sep 15 17:52 create_database.sh
-rw-r--r-- 1 thomas thomas 13764 Sep 15 19:05 database.rrd
-rw-r--r-- 1 thomas thomas  7206 Sep 15 19:05 temp_d.png
-rw-r--r-- 1 thomas thomas  7966 Sep 15 19:05 temp_h.png
-rw-r--r-- 1 thomas thomas  7951 Sep 15 19:05 temp_m.png
-rw-r--r-- 1 thomas thomas  8040 Sep 15 19:05 temp_w.png
-rw-r--r-- 1 thomas thomas  8630 Sep 15 19:05 temp_y.png
-rwxr-xr-x 1 thomas thomas   843 Sep 15 19:05 update_database.sh

You could probably ask rrdtool to generate .jpg files instead if you can find a way to do so in the manual.

Now the database has been updated with a single snapshot of the temperature but we would like to continuously capture temperature data and keep the graphs updated. For this purpose we can use Linux crontab:

Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.

– Wikipedia

The -e option is for editing:

$ crontab -e
no crontab for thomas - using an empty one
crontab: installing new crontab

Every 5 minutes the update script is run, and any output messages are discarded:

# m h  dom mon dow   command
*/5 * * * * /home/thomas/rrdtool/update_database.sh &> /dev/null

If you want to check what jobs are installed you can use the -l option:

$ crontab -l

Since the NSLU2 doesn’t have any monitor connection on it I need to get the graphs copied to a computer with a screen to be able to see the results. For that I use FTP:

$ cd /home/thomas/rrdtool
$ ftp

Replace SERVER and USERNAME below with your own details:

ftp> open
(to) SERVER
Connected to SERVER.
Name (SERVER:thomas): USERNAME
ftp> mkdir temp
ftp> cd temp
ftp> put temp_d.png
ftp> put temp_h.png
ftp> put temp_m.png
ftp> put temp_w.png
ftp> put temp_y.png
ftp> exit

You could also copy the image files to a USB memory stick and from there onto your PC.

From my server on the Internet I can now download and view the generated graphs.

The first one (temp_h.png) covers only 1 hour in time and the air temperature is very stable over the hour so this particular graph is not very informative:

temp_d.png is more interesting as you can begin to see changes in the temperature:

temp_w.png needs a lot more time before it has been filled up with data, but if the system is stable the graph will be completed in about a week:

temp_m.png and temp_y.png are long term graphs but quite interesting to look at when they have been drawn, especially if you mounted the temperature sensor outdoors. There’s nothing to see yet though, because the system is new:

I’m going to add more and different kinds of sensors to the NSLU2 logging system in the coming weeks.

How to Read 1-Wire Temperature Using a NSLU2 With Debian

This is a small Debian NSLU2 1-Wire HowTo that explains the steps needed to get some data out of those small cool devices using a DS18S20 NSLU2 1-Wire setup.

The component covered by green heat shrink in the picture below is a 1-Wire component, specifically a temperature measuring device, model DS18S20:

(18S20 wiring is shown on this page: DS18S20 Wiring )

The 1-Wire bus runs well on both telephone cable and Ethernet cable, and since the DS18S20 was already soldered onto a piece of Ethernet cable and covered in heat shrink, I chose an easy to make test setup by connecting the Ethernet cable to a piece of ordinary telephone cable, because the connector already mounted on the telephone cable fits directly into a DS9490R 1-Wire USB adapter available from hobby-boards.com. It’s the blue device in the picture below:

(See the post called Debian on NSLU2 With USB Hard Disk and Homeplug Network for more details about the rest of the system.)

Paul Alfille wrote a clever piece of software that makes it easy to handle 1-Wire units. It’s called OWFS, and the project has its own website at owfs.org, if you want to know more about how OWFS works. I chose this software to handle the units on my 1-Wire NSLU2 Debian installation, but there’s a bit of work to do to get the 1-Wire software installed and running. Instructions are available at owfs.org / Setup / Install / Download, which points to the project files on SourceForge.net.

In order to get the installation files for OWFS onto the NSLU2 I logged in via my laptop PC using ssh and use wget on NSLU2: (Replace .xx with the address of the NSLU2 on the intranet)

$ ssh thomas@192.168.1.xx

$ mkdir /home/thomas/owfs_install
$ cd /home/thomas/owfs_install
$ wget sourceforge.net/projects/owfs/files/owfs/2.8p13/owfs-2.8p13.tar.gz/download
$ mv download owfs-2.8p13.tar.gz
$ tar -zxvf owfs-2.8p13.tar.gz
$ cd /home/thomas/owfs_install/owfs-2.8p13

For some reason the downloaded file is named download so I use the mv command to rename it to something more precise.

When I try to install OWFS it turns out that I’m missing something on my freshly installed Debian:

$ ./configure

error: no acceptable C compiler found in $PATH

The package build-essential contains the needed C compiler:

$ su
# apt-get install build-essential
# exit

Trying to configure one more time reveals a whole bunch of missing software required for OWFS to compile and run:

$ ./configure

...
configure: WARNING: Cannot find php binary. Install php or php5 package
configure: WARNING: OWPHP is disabled because php binary is not found
configure: WARNING: Cannot find python include-file. Install python-devel package.
configure: WARNING: OWPYTHON is disabled because python include-file is not found
checking for Tcl configuration... configure: WARNING: Can't find Tcl configuration definitions
configure: WARNING: OWTCL is disabled because tclConfig.sh is not found
configure: WARNING: LD_EXTRALIBS= OSLIBS=
configure: WARNING:
Can't find fuse.h - Add the search path with --with-fuseinclude
configure: WARNING: Install FUSE-2.2 or later to enable owfs - download it from http://fuse.sourceforge.net/
configure: WARNING: OWFS is disabled because fuse.h is not found.
configure: WARNING: Can't find libusb
configure: WARNING: libusb not found, usb will be disabled
...
Compile-time options:
USB is DISABLED
Profiling is DISABLED
Tracing memory allocation is DISABLED
1wire bus traffic reports is DISABLED
...
Module configuration:
owfs is DISABLED
swig is DISABLED
owperl is DISABLED
owphp is DISABLED
owpython is DISABLED
owtcl is DISABLED

Fortunately most of the missing stuff is contained in these 7 packages:

$ su
# apt-get install php5-cli python2.4-dev tcl-dev tk-dev libusb-dev swig libperl-dev
# exit

The warning about the missing FUSE software is cleared by downloading and installing like this:

$ mkdir /home/thomas/fuse
$ cd /home/thomas/fuse
$ wget http://sourceforge.net/projects/fuse/files/fuse-2.X/2.8.5/fuse-2.8.5.tar.gz/download
$ mv download fuse-2.8.5.tar.gz
$ tar -zxvf fuse-2.8.5.tar.gz
$ cd /home/thomas/fuse/fuse-2.8.5/
$ ./configure
$ make
$ su
# make install
# exit

Now only one warning is present and a few disabled functions, which is acceptable for the purpose of reading temperature from the DS18S20 IC:

$ cd /home/thomas/owfs_install/owfs-2.8p13

$ ./configure

configure: WARNING: LD_EXTRALIBS= OSLIBS=

Compile-time options:
Profiling is DISABLED
Tracing memory allocation is DISABLED
1wire bus traffic reports is DISABLED

Module configuration:
owlib is enabled
owshell is enabled
owfs is enabled
owhttpd is enabled
owftpd is enabled
owserver is enabled
ownet is enabled
ownetlib is enabled
owtap is enabled
owmon is enabled
owcapi is enabled
swig is enabled
owperl is enabled
owphp is enabled
owpython is enabled
owtcl is enabled

And a final install:

$ make
$ su
# make install
# exit

Note that $ means you are working on the CLI as a normal user, and # means you’re working as root, which should be minimized since you have total power as root which again means that you could easily cause irreversible damage to your system by accident. Therefore it’s best to do as much as you can as a normal user.

Howto access DS9490: FS in OWFS stands for File System so I created a directory for the 1-Wire devices and mounted the one-wire file system in this new directory:

$ mkdir /home/thomas/owfs

$ /opt/owfs/bin/owfs -u /home/thomas/owfs
DEFAULT: owlib.c:SetupSingleInboundConnection(201) Cannot open USB bus master
DEFAULT: owlib.c:LibStart(54) No valid 1-wire buses found

Again, the separation between root and normal users means that as a normal user (thomas) I don’t have direct access to the USB DS9490R adapter, so I have to change to superuser root to mount OWFS:

$ su
# /opt/owfs/bin/owfs -u /home/thomas/owfs
DEFAULT: ow_usb_msg.c:DS9490_open(276) Opened USB DS9490 bus master at 1:4.
DEFAULT: ow_usb_cycle.c:DS9490_ID_this_master(191) Set DS9490 1:4 unique id to 81 59 25 27 00 00 00 CE
# exit

But still more problems related to root versus normal user permissions – the mounted directory containing the 1-Wire data is not available to normal users:

$ ls -la /home/thomas
total 32
drwxr-xr-x 5 thomas thomas 4096 Sep  8 12:44 .
drwxr-xr-x 3 root   root   4096 Sep  2 20:20 ..
-rw------- 1 thomas thomas 1338 Sep  8 12:47 .bash_history
-rw-r--r-- 1 thomas thomas  220 Sep  2 20:20 .bash_logout
-rw-r--r-- 1 thomas thomas 3116 Sep  2 20:20 .bashrc
-rw-r--r-- 1 thomas thomas  675 Sep  2 20:20 .profile
drwxr-xr-x 3 thomas thomas 4096 Sep  8 09:15 fuse
d????????? ? ?      ?         ?            ? owfs
drwxr-xr-x 3 thomas thomas 4096 Sep  7 20:59 owfs_install

and can’t be listed when you’re logged in as a normal user:

$ ls -la /home/thomas/owfs
ls: cannot access owfs: Permission denied

Changing to superuser using su reveals the goodies:

$ su

# ls -la /home/thomas
total 32
drwxr-xr-x 5 thomas thomas 4096 Sep  8 12:44 .
drwxr-xr-x 3 root   root   4096 Sep  2 20:20 ..
-rw------- 1 thomas thomas 1338 Sep  8 12:47 .bash_history
-rw-r--r-- 1 thomas thomas  220 Sep  2 20:20 .bash_logout
-rw-r--r-- 1 thomas thomas 3116 Sep  2 20:20 .bashrc
-rw-r--r-- 1 thomas thomas  675 Sep  2 20:20 .profile
drwxr-xr-x 3 thomas thomas 4096 Sep  8 09:15 fuse
drwxr-xr-x 1 root   root      8 Sep  8 12:51 owfs
drwxr-xr-x 3 thomas thomas 4096 Sep  7 20:59 owfs_install

My OWFS NSLU2 system is up and running, and 10.4F7… is the DS18S20 IC on the 1-Wire bus:

# ls -la /home/thomas/owfs
total 4
drwxr-xr-x 1 root   root      8 Sep  8 12:51 .
drwxr-xr-x 5 thomas thomas 4096 Sep  8 12:44 ..
drwxrwxrwx 1 root   root      8 Sep  8 12:52 10.4F7494010800
drwxrwxrwx 1 root   root      8 Sep  8 12:52 81.592527000000
drwxr-xr-x 1 root   root      8 Sep  8 12:51 alarm
drwxr-xr-x 1 root   root      8 Sep  8 12:51 bus.0
drwxr-xr-x 1 root   root      8 Sep  8 12:51 settings
drwxrwxrwx 1 root   root      8 Sep  8 12:52 simultaneous
drwxr-xr-x 1 root   root      8 Sep  8 12:51 statistics
drwxr-xr-x 1 root   root     32 Sep  8 12:51 structure
drwxr-xr-x 1 root   root      8 Sep  8 12:51 system
drwxr-xr-x 1 root   root      8 Sep  8 12:51 uncached

In order to get access to OWFS as a normal user there’s trick you can do, but first I’m rebooting to make sure OWFS is completely shutdown. (There’s probably a clever way to do this, if you know what you’re doing ;-) ):

# reboot

Log in again from laptop to NSLU2:

$ ssh thomas@192.168.1.xx

The option --allow-other can be added when running OWFS, which allows normal users to access the 1-Wire file system without changing to superuser (the -u option means that you’re connecting via USB adapter instead of a serial connector):

$ su

# /opt/owfs/bin/owfs --allow_other -u /home/thomas/owfs
DEFAULT: ow_usb_msg.c:DS9490_open(276) Opened USB DS9490 bus master at 1:4.
DEFAULT: ow_usb_cycle.c:DS9490_ID_this_master(191) Set DS9490 1:4 unique id to 81 59 25 27 00 00 00 CE
# exit

Now there is access to the owfs directory even when you’re logged in as a normal user:

$ ls -la /home/thomas
total 32
drwxr-xr-x 5 thomas thomas 4096 Sep  8 12:44 .
drwxr-xr-x 3 root   root   4096 Sep  2 20:20 ..
-rw------- 1 thomas thomas 1452 Sep  8 12:57 .bash_history
-rw-r--r-- 1 thomas thomas  220 Sep  2 20:20 .bash_logout
-rw-r--r-- 1 thomas thomas 3116 Sep  2 20:20 .bashrc
-rw-r--r-- 1 thomas thomas  675 Sep  2 20:20 .profile
drwxr-xr-x 3 thomas thomas 4096 Sep  8 09:15 fuse
drwxr-xr-x 1 root   root      8 Sep  8 13:02 owfs
drwxr-xr-x 3 thomas thomas 4096 Sep  7 20:59 owfs_install
$ ls -la /home/thomas/owfs
total 4
drwxr-xr-x 1 root   root      8 Sep  8 13:02 .
drwxr-xr-x 5 thomas thomas 4096 Sep  8 12:44 ..
drwxrwxrwx 1 root   root      8 Sep  8 13:02 10.4F7494010800
drwxrwxrwx 1 root   root      8 Sep  8 13:02 81.592527000000
drwxr-xr-x 1 root   root      8 Sep  8 13:02 alarm
drwxr-xr-x 1 root   root      8 Sep  8 13:02 bus.0
drwxr-xr-x 1 root   root      8 Sep  8 13:02 settings
drwxrwxrwx 1 root   root      8 Sep  8 13:02 simultaneous
drwxr-xr-x 1 root   root      8 Sep  8 13:02 statistics
drwxr-xr-x 1 root   root     32 Sep  8 13:02 structure
drwxr-xr-x 1 root   root      8 Sep  8 13:02 system
drwxr-xr-x 1 root   root      8 Sep  8 13:02 uncached

To read out the temperature from the DS18S20 IC we have to take a look at the file called temperature:

$ ls -la /home/thomas/owfs/10.4F7494010800
total 0
drwxrwxrwx 1 root root   8 Sep  8 13:03 .
drwxr-xr-x 1 root root   8 Sep  8 13:02 ..
-r--r--r-- 1 root root  16 Sep  8 13:02 address
-rw-rw-rw- 1 root root 256 Sep  8 13:02 alias
-r--r--r-- 1 root root   2 Sep  8 13:02 crc8
drwxrwxrwx 1 root root   8 Sep  8 13:03 errata
-r--r--r-- 1 root root   2 Sep  8 13:02 family
-r--r--r-- 1 root root  12 Sep  8 13:02 id
-r--r--r-- 1 root root  16 Sep  8 13:02 locator
-r--r--r-- 1 root root   1 Sep  8 13:03 power
-r--r--r-- 1 root root  16 Sep  8 13:02 r_address
-r--r--r-- 1 root root  12 Sep  8 13:02 r_id
-r--r--r-- 1 root root  16 Sep  8 13:02 r_locator
-r--r--r-- 1 root root  12 Sep  8 13:02 temperature
-rw-rw-rw- 1 root root  12 Sep  8 13:03 temphigh
-rw-rw-rw- 1 root root  12 Sep  8 13:03 templow
-r--r--r-- 1 root root  32 Sep  8 13:02 type

All data from the 1-Wire IC are contained in different files in OWFS, and the content of files can be displayed with the cat command in Linux:

$ cat /home/thomas/owfs/10.4F7494010800/temperature
24

So this value shows that the system is running and temperature measurements are being done (24 deg. C). Information about the type of IC is contained in the type file:

$ cat /home/thomas/owfs/10.4F7494010800/type
DS18S20

Just to proved that the resolution is higher than 1 deg. C I did one more readout and that showed 4 more digits after the previous value:

$ cat /home/thomas/owfs/10.4F7494010800/temperature
24.0625

The obvious next step is to build even more software on top of NSLU2 OWFS installation to make nice graphs over time to gain comprehensive information about the temperature.

The DS18S20 can of course be placed in many different locations, even outdoors if you use heat shrink to protect the IC against the weather.

(Update: Do your OWFS sensors disappear from time to time? Please join the discussion below).