This is the final post on configuring 1-Wire for temperature monitoring. In Part 1 I got all of the wiring figured out. Part 2 was all about gathering the data into a log format.
This post is about getting that data stored and then rendered into a graphical format.
The first thing I did was slightly refactor my gathering. I wrote a bash script that queries the sensors in order and logs them to the same log file as before, but also puts that data into a round-robin database (RRD). That way later on I can graph the data any way I want.
The big requirement before this script is run is to have existing RRD files. I used a Perl script to create these RRD files. This script would need to be updated for each sensor:
[code]
#!/usr/bin/perl -w
use Time::Local;
use RRDp;
# Create the database
RRDp::start "/usr/bin/rrdtool";
RRDp::cmd "create Living_Room.rrd
-b 1373820664
-s 60
DS:temp:GAUGE:180:U:U
RRA:AVERAGE:0.5:1:10080
RRA:AVERAGE:0.5:5:4032
RRA:AVERAGE:0.5:30:1344
RRA:AVERAGE:0.5:120:21900
RRA:MIN:0.5:1:10080
RRA:MIN:0.5:5:4032
RRA:MIN:0.5:30:1344
RRA:MIN:0.5:120:21900
RRA:MAX:0.5:1:10080
RRA:MAX:0.5:5:4032
RRA:MAX:0.5:30:1344
RRA:MAX:0.5:120:21900
" ;
RRDp::end;
[/code]
Building that specification for the RRD is a bit of a dark-art. Here is the best way I can explain the options.
- The “-b 1373820664” is the earliest data point allowed into the RRD. It is in epoch form. I did this because I wanted to back-fill my log data. This is optional if you are starting your graph from scratch.
- The “-s 60” option is the data interval time in seconds. The “DS:temp:GAUGE:180:U:U” is the data point to be entered – “temp” is a label on that data point so it can be easily queried later on.
- The RRA blocks are repeated for AVERAGE, MIN, and MAX so I will only describe one. “RRA:AVERAGE:0.5:1:10080” tells the RRD to keep 10080 data rows with 1 step. Each step is 1 minute. This ends up being 7 full days of data. “RRA:AVERAGE:0.5:5:4032” is similar in that it keeps 4032 data rows, but this time with a step of 5. This ends up being 14 days of data on a 5 minute interval. “RRA:AVERAGE:0.5:30:1344” keeps 28 days of data on a 30 minute interval. And “RRA:AVERAGE:0.5:120:21900” keeps 1825 days of data (about 5 years) with a 2 hour interval.
All of this data will be stored in a file that is 879K. Not bad for 5 years of data.
Here is the new digitemp_gather script. This script runs every minute via a cronjob:
[code]
#!/bin/bash
DIGITEMP=/usr/bin/digitemp
DIGITEMP_OPTS="-q -a"
DIGITEMP_CONF="-c /etc/digitemprc"
LOGFILE=/var/log/temperatures
RRD_PATH=/var/lib/rrd
#
# Function to call digitemp with a given sensor ID and location.
# Generates a log message in $LOGFILE
#
function ProcessSensor {
sensor_id=$1
sensor_location=$2
cur_date=$(date +"%Y/%m/%d %H:%M:%S")
dateEpoch=$(date -d "$cur_date" +%s)
cur_temp=$(/usr/bin/digitemp -t $sensor_id -q -c /etc/digitemprc -o%.2F)
echo $cur_date $sensor_location F: $cur_temp >> $LOGFILE
rrdtool update $RRD_PATH/$sensor_location.rrd $dateEpoch:$cur_temp
}
ProcessSensor 0 Attic
ProcessSensor 1 Garage
ProcessSensor 2 Living_Room
ProcessSensor 3 Master_Bedroom
[/code]
Now your cronjob is filling your RRD and it also continues to update the log file. It is not useful to graph data until you have at least 15 or 30 minutes worth of data- so time for a break.
Assuming you now have a bunch of data stored in your RRDs, it is pretty easy to extract that data with rrdtool and graph it as a PNG. This script creates the graphs in a temporary directory and I run it every 15 minutes. In my case, the last step is to copy the graphs to my web host so I can see them wherever I am.
[code]
#!/bin/bash
RRD_PATH=/var/lib/rrd
GRAPHS_DIR=/tmp/digitemp_graphs
#
# Function that builds graphs for a specific sensor on a given interval
#
# Usage: CreateGraph Sensor_Name Interval
# Example: CreateGraph Living_Room hour
#
function CreateGraph {
sensor_name=$1
time_length=$2
png_file=$sensor_name
png_file+="_$time_length"
png_file+=".png"
rrdtool graph $GRAPHS_DIR/$png_file
-h 80 -w 600
–start -1$time_length –end now
-t "$sensor_name :: last $time_length"
–lazy
-v "degrees F"
–slope-mode
DEF:temp=$RRD_PATH/$sensor_name.rrd:temp:AVERAGE
DEF:min=$RRD_PATH/$sensor_name.rrd:temp:MIN
DEF:max=$RRD_PATH/$sensor_name.rrd:temp:MAX
LINE1:temp#0000FF
GPRINT:temp:AVERAGE:"Avg\: %6.1lf"
GPRINT:temp:MAX:"Max\: %6.1lf"
GPRINT:temp:MIN:"Min\: %6.1lf"
GPRINT:temp:LAST:"Current\: %6.1lf degrees F\n"
> /dev/null
}
# Remove all of the existing graphs and recreate the graphs directory
if [ -e $GRAPHS_DIR ] ; then
/bin/rm -rf $GRAPHS_DIR
fi
mkdir $GRAPHS_DIR
# Build all of the graphs
CreateGraph Attic hour
CreateGraph Attic day
CreateGraph Attic week
CreateGraph Garage hour
CreateGraph Garage day
CreateGraph Garage week
CreateGraph Living_Room hour
CreateGraph Living_Room day
CreateGraph Living_Room week
CreateGraph Master_Bedroom hour
CreateGraph Master_Bedroom day
CreateGraph Master_Bedroom week
# Copy the graphs to my webhost
scp -q $GRAPHS_DIR/*.png hosting.example.org:/var/www/temp-graphs
[/code]