After succefully get your 1-wire bus working with OWFS and been able to read the temperatures, it is time to create some nice graphs. The red line is the temperature and the blue is the sliding avarage temperature the last 24 hours. I’m using RRDTools to store the data and create the graphs. If you want to use RRDTools and RRDGraph for your own purposes I recommend you to read the manual pages several times and start easy and build more and more complex solutions.
Here is the script that I’m using to read the temperatures, store the data in a round robin database and finally create the graphs.
#!/usr/bin/perl
# By Peter Andersson peter@it-slav.net
# rrd_tempsensor.pl
use RRDs;
use OW;
# define location of rrdtool databases
my $rrd = '/root/owfs/script/rrd_db';
# define location of images
my $img = '/home/peter/public_html/temps';
# process data for each interface (add/delete as required)
&ProcessSensor(0, "Temperatur i garaget", "127.0.0.1" , "3001" , "10.87507C010800");
&ProcessSensor(1, "Temperatur i PDCn", "op5.mynet",3001, "10.04E060010800");
&ProcessSensor(2, "Temperatur ute", "op5.mynet",3001,"10.DEF05F010800");
sub ProcessSensor
{
# process sensor
# inputs: $_[0]: sensor number (ie, 0/1/2/etc)
# $_[1]: sensor description
print "number:$_[0] desc:$_[1], server:$_[2] port:$_[3], id: $_[4]\n";
my $owserver = "$_[2]:$_[3]";
unless(OW::init($owserver)) {
$status = $ERRORS{CRIT};
$message = "OWServer not running at $owserver\n";
exit $status;
}
# get temperature from sensor
my $handle = OW::get("$_[4]/temperature");
# print "handle=$handle\n";
$handle =~ s/^\s*(.*?)\s*$/$1/;
## Check if input is an integer or decimal
unless (($handle =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/) || ($handle =~ /^[+-]?\d+$/))
{
print "Not an integer or a decimal\n";
return($ERRORS{CRITICAL});
}
$temp=$handle;
# remove eol chars
chomp($temp);
print "sensor $_[0]: $temp degrees C\n";
# if rrdtool database doesn't exist, create it
if (! -e "$rrd/temp$_[0].rrd")
{
print "creating rrd database for temp sensor $_[0]...\n";
RRDs::create "$rrd/temp$_[0].rrd",
"-s 300",
"DS:temp:GAUGE:600:U:U",
"RRA:AVERAGE:0.5:1:2016",
"RRA:AVERAGE:0.5:6:1344",
"RRA:AVERAGE:0.5:24:2190",
"RRA:AVERAGE:0.5:144:3650";
}
if ($ERROR = RRDs::error) { print "$0: failed to create $_[0] database file: $ERROR\n"; }
# check for error code from temp sensor
if (int $temp eq 85)
{
print "failed to read value from sensor $_[0]\n";
}
else
{
# insert values into rrd
RRDs::update "$rrd/temp$_[0].rrd",
"-t", "temp",
"N:$temp";
if ($ERROR = RRDs::error) { print "$0: failed to insert $_[0] data into rrd: $ERROR\n"; }
}
# create graphs for current sensor
&CreateGraph($_[0], "day", $_[1]);
&CreateGraph($_[0], "week", $_[1]);
&CreateGraph($_[0], "month", $_[1]);
&CreateGraph($_[0], "year", $_[1]);
}
sub CreateGraph
{
# creates graph
# inputs: $_[0]: sensor number (ie, 0/1/2/etc)
# $_[1]: interval (ie, day, week, month, year)
# $_[2]: sensor description
RRDs::graph "$img/temp$_[0]-$_[1].png",
"-s -1$_[1]",
"-t $_[2]",
"-h", "150", "-w", "475",
"-a", "PNG",
"-v WWW.It-Slav.Net",
"--slope-mode",
"DEF:temp=$rrd/temp$_[0].rrd:temp:AVERAGE",
"LINE2:temp#FF0000::",
"CDEF:tempavarage=temp,86400,TREND",
"LINE1:tempavarage#000088::",
"GPRINT:temp:MIN: Min\\: %6.1lf",
"GPRINT:temp:MAX: Max\\: %6.1lf",
"GPRINT:temp:AVERAGE: Snitt\\: %6.1lf",
"GPRINT:temp:LAST: Nuvarande\\: %6.1lf grader C\\n";
if ($ERROR = RRDs::error) { print "$0: unable to generate sensor $_[0] $_[1] graph: $ERROR\n"; }
}
Use crontab to run it every 5 minutes:
*/5 * * * * /root/owfs/script/tempgraph.pl > /dev/null
5 Responses to “Temperature Graphs”
Leave a Reply
You must be logged in to post a comment.
February 14th, 2009 at 5:08 pm
Using this script i get an error: Can’t locate OW.pm in @INC
I installed perl and owfs. What i’m i missing?
February 14th, 2009 at 5:21 pm
Have you installed the owfs perl package?
If yes, which version of owfs are you running?
In the the script I’m using 2.7, newer versions have another syntax.
/Peter
July 5th, 2009 at 1:50 am
[…] The graphing is just a modified version of http://www.it-slav.net/blogs/?p=75 […]
September 12th, 2012 at 1:20 pm
hello,
what are the syntah for 2.8.x version ?
September 27th, 2012 at 8:35 pm
Have you looked in the manual?