Project:

Get temperature data from Kit 145 (uses DS1820)

save it an RRDTool database and

display the data in grapghs via a web page

 

This project documents the PERL programmes used to process and graph measurements from an electronic circuit available from www.kitsrus.com that uses the DS1820 chip. Here is an example of the graphs:

View Temperature at Chapman ACT Canberra Australia

Author and copyright

License and distributed under the GNU General Public License (GPL)

Copyright (C) 2009 Stephen Kingham, Stephen.Kingham <at> kingtech.com.au

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Comments and suggests are welcome

Overview





Installation

The installation is spilt between the computer logging the data from the Kit 145 and the computer running apache2 web server that creates the graphs and displays them to a web page.

Installation on the server connected to the Kit 145

This part of the project is an exercise in using a PERL programme to read data from a serial port, sanatise it, and save it to an RRDTool database.

Build the kit and connect it to a serial port. Using minicom or similar communicate with the kit to make sure you are getting data from it. It will look like this with 4 temperature probes:

1 0022.81
2 0022.87
3 0023.25
4 0026.87
1 0022.31
2 0023.06
3 0023.25
4 0026.87
1 0023.68
2 0023.00
3 0023.25
4 0026.87
1 0022.75
2 0023.00
3 0023.25
4 0026.93

Put GetInputFromTemperatureKit145 into a directory where it can be found and executed and change the permissions so that it is executable at least by root. I put programmes like this in a directory caller /usr/local/bin/, but other locations where people often put their own executables are usualy /sbin/ or /usr/local/bin. Just choose one, the location needs to be set in /etc/init.d/GetInputFromTemperatureKit145d.

sudo cp GetInputFromTemperatureKit145 /var/raid/bin/GetInputFromTemperatureKit145
sudo chmod a+x /var/raid/bin/GetInputFromTemperatureKit145

You will need to edit /var/raid/GetInputFromTemperatureKit145 to set some the variable that tells the programme which serial port Kit 145 is connected to. Look for the following lines and change the /dev/ttyS0to the right port.

sudo vi /var/raid/bin/GetInputFromTemperatureKit145

Look for:

# Serial Settings
# my $SerialInterfaceTTYName = "/dev/ttyS0"; # port to watch On

Download GetInputFromTemperatureKit145d and in Ubuntu put it in /etc/init.d so that it will start the programme at boot, and make it executable.

sudo cp GetInputFromTemperatureKit145d /etc/init.d/GetInputFromTemperatureKit145d
sudo chmod a+x /etc/init.d/GetInputFromTemperatureKit145d

Edit /etc/init.d/GetInputFromTemperatureKit145d to set important parameters sent to GetInputFromTemperatureKit145 and also where to find it, but also where to create/save the RRDTool database files, and what user fliendly names to call the probes, these names are realy important becuase they form the basis of the filenames of the RRDTool database files. The usage of GetInputFromTemperatureKit145 is:

usage: GetInputFromTemperatureKit145 [switches]
All switches are optional, valid switches include:
-d <directory> = optional directory to create and save the RRDTOOL database files (default /tmp/)
-1<text with no spaces> = descriptive name of the first probe, eg outside, default is 1
-2<text with no spaces> = descriptive name of the first probe, eg serverRoom, default is 2
-3 <text with no spaces> = descriptive name of the first probe, eg Bed1, default is 3
-4 <text with no spaces> = descriptive name of the first probe, eg Bed2, default is 4
-5 <text with no spaces> = descriptive name of the first probe, eg Bed3, default is 5
-6 <text with no spaces> = descriptive name of the first probe, eg GardenShed, default is 6
-7 <text with no spaces> = descriptive name of the first probe, eg Garage, default is 7
-8 <text with no spaces> = descriptive name of the first probe, eg Laundry, default is 8
-debug = sends raw temperature read from probe and value saved to RDDTOOL database
-log <absulute filename> = name of the file to write log information, default is /var/log/GetInputFromTemperaturekit145.log

The names of the probes are used to create the filenames of the RRDTool

Example 1:

/var/raid/bin/GetInputFromTemperatureKit145 -1 OutsideFront -2 Bed4 -3 WineCellar -4 ServerRoom -d /var/raid/data/tempuratureData/ -log /var/log/GetInputFromTemperatureKit145.log -debug &

The above will:

- Probe 1 will have the name "OutsideFront". The filename for the RRDTool database will use this name, and the graph will also use it, rather than the default.
- Probe 2 will have the name "Bed4". The filename for the RRDTool database will use this name, and the graph will also use it, rather than the default.
- Probe 3 will have the name "WineCellar". The filename for the RRDTool database will use this name, and the graph will also use it, rather than the default.
- Probe 4 will have the name "ServerRoom". The filename for the RRDTool database will use this name, and the graph will also use it, rather than the default.
- The directory to save the RRDTool database files is /var/raid/data/tempuratureData/.
- Any log information will be written to /var/log/GetInputFromTemperatureKit145.log
- debug switch is enabled, so the raw measurement as well as an corrections applied will be written to the log file.

Example 2:

/var/raid/bin/GetInputFromTemperatureKit145 -1 OutsideFront -2 Bed4 -3 WineCellar -4 ServerRoom -d /var/raid/data/percyTemp/ -log /var/log/GetInputFromTemperatureKit145.log &

- Is exactly the same as the first example only there will not be exessive log information written to the log file.

The following on ubuntu will set up the /etc/init.d/ GetInputFromTemperatureKit145d so that the daemon will start at the typical multiuser run levels of 2, 3, 4, and 5, and that it will be one of the last started, and one of the first stopped.

sudo update-rc.d -f GetInputFromTemperatureKit145d start 99 2 3 4 5 . stop 99 2 3 4 5 .

Install RRDTool. This tool written by the same author as MRTG is a databased that efficiently stores data.

sudo apt-get install rrdtool

The programmes are written in Perl, so you have to have PERL installed, and they also use a PERL Module to communicate with the serial port. I use the CPAN shell which I start be issuing the command:

sudo perl -MCPAN -e 'shell'

Then at the prompt enter the following to get the necessary support to control serial ports:

install Device::SerialPort

And that it that. The programme is ready to run.

Callibration and testing logging the data

By default the programme will record the temperature as what ever the probe indicates. However I found that the probes did not always record the correct temporature when compared to a themometer or known acurate temperature sensor. So I built in a way to apply a linear correction. I need to determine for each probe an A and a B so that:

Temperature = A times ProbeValue + B

where
Temperature is the actual temperatures
A is a constant we need to provide,
B is another constant we need to provide, and
ProbeValue is the temperature the probe thinks it is.

First make sure the programme is stopped and then start it again in debug mode using these commands, error the error if you try to stop it when it is not running.

sudo /etc/init.d/GetInputFromTemperatureKit145d stop
sudo /etc/init.d/GetInputFromTemperatureKit145d debug

In debug there will be some very interesting information logged to /var/log/GetInputFromTemperatureKit145d.log tail -f /var/log/GetInputFromTemperatureKit145d.log For example:

17:55 12Feb2011 Probe1=OutsideFront Temp= 26.1 * 1.0 + 0.0= 27.1, moving average= 27.1
17:55 12Feb2011 Probe1=OutsideFront Temp= 33.2* 1.0 + 0.0= 15.3, moving average= 33.2

17:55 12Feb2011 Probe1=OutsideFront Temp= 25.9* 1.0 + 0.0= 15.3, moving average= 25.9

17:55 12Feb2011 Probe1=OutsideFront Temp= 27.3* 1.0 + 0.0= 15.3, moving average= 27.3

17:55 12Feb2011 Probe1=OutsideFront Temp= 24.6* 1.0 + 0.0= 15.3, moving average= 24.6

17:55 12Feb2011 Probe1=OutsideFront Temp= 25.4* 1.0 + 0.0= 15.3, moving average= 25.4

To find A and B I take several measurements over a range of temperatures from the probe and at the same time from a known good temperature sensor. At the same time I record the temperature using an known good temperature sensor. Useing the Least Squares method to estimate a linear line through the data to determine the A and the B. I put together a spreadsheet where I just enter the measurements and it tells me A and B. For the above

Probe 1 A= 3.977584736
Probe 2 B= 0.867904564

I edit /var/raid/bin/GetInputFromTemperatureKit145d and I change

# Probe 1
$CalibrateProbeByA[1] = 0
$CalibrateProbeByB[1] = 1;

To

# Probe 1
$CalibrateProbeByA[1] = 3.977584736;
$CalibrateProbeByB[1] = 0.867904564;

When you are finished with calibrating stop and start the process:

sudo /etc/init.d/GetInputFromTemperatureKit145d stop
sudo /etc/init.d/GetInputFromTemperatureKit145d start

Installation on the apache server

So that I can view the temperature graphs I wrote a CGI script in PERL which also alows me to select the time scale. So this programme is part of a larger apache web site using a CGI script to read an RRDTool database and display the graphs.

If the apache server is on a different computer you will need to install RRDTool on it as well so that it can read the database:

sudo apt-get install rrdtool

The programmes are written in Perl, so you have to have PERL installed, and they also use a PERL Module to communicate with the serial port. I use the CPAN shell which I start be issuing the command:

sudo perl -MCPAN -e 'shell'

If it fails try this first:

rm ~/.cpan/CPAN/MyConfig.pm

Then at the prompt enter the following to get the necessary support CGI.pm

install CGI

File::Find, install by:

install File:Find

English, install by:

install English

In apache I created a directory that I allow CGI script to create files. This directory is world wrateble and readable so I keep it seperate. I call it /var/www/tempForCGI-BIN. We need to create this directory:

sudo mkdir /var/www/tempForCGI-BIN/
sudo chmod 777 /var/www/tempForCGI-BIN/
sudo chown nobody /var/www/tempForCGI-BIN/

I also have a seperate directory where I store the CGI programmes. I lock this directory down as much as possible so that apache can still execute the programmes but can not change them. I also do not put the directory in the apache root so that web browsers can not look at the contents of the programmes.

sudo mkdir /var/cgi-bin/
sudo chmod 775 /var/cgi-bin/
sudo chown root:root /var/cgi-bin/

In apache we now need to tell it where the temp directory is and where the CGI programmes is. Edit the site file:

sudo vi /etc/apache2/sites-available\yourwebsite

and add these lines:

# Tell apache that a URL with /cgi-bin/ points to the /var/cgi-bin directory on the hard disk:
ScriptAlias /cgi-bin/ /var/cgi-bin/
#
# Tell apache that a URL with /tempForCGI-BIN/ points to the /var/www/tempForCGI-BIN/ on the disk:
Alias /tempForCGI-BIN/ /var/www/tempForCGI-BIN/
#
# Create a definition for /var/www/tempForCGI-BIN/ that allows browsers to see the files in it
<Directory /var/www/tempForCGI-BIN/>
Options MultiViews AllowOverride None
# The following Order and Allow are important
Order allow,deny Allow from all
</Directory>

I seam to recall that the following is important but I can not rememeber why, but I put it here just in case:

sudo vi /etc/apache2/apache2.conf

Add the following line somewhere

ServerName localhost

To enable CGI and perl support for apache2 server you need to install the following package

sudo aptitude install libapache2-mod-perl2

Run the following to install the perl module RRDs.pm, it is needed by the programme that reads RRDTool database and creates the graphs:

sudo apt-get install librrds-perl

Also we need to tell apache to load the cgi.load module.

sudo ln -s /etc/apache2/mods-enabled/cgi.load /etc/apache2/mods-available/cgi.load
sudo /etc/init.d/apache2 restart

Here is an example http document that will call the programme, put it somewhere on your web site:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
</HEAD>
<BODY>
<a href="http://www.kingtech.com.au/cgi-bin/TemperatureProbeMakeGraph/"> View Temperature Graphs</a>
</BODY>
</HTML>

Get TemperatureProbeMakeGraph program that is called by the CGI and put it in th CGI directory where apache expects to find it:

sudo cp TemperatureProbeMakeGraph /var/cgi-bin/TemperatureProbeMakeGraph
sudo chown root:root /var/cgi-bin/TemperatureProbeMakeGraph
sudo chmod 775 /var/cgi-bin/TemperatureProbeMakeGraph

You will need to change TemperatureProbeMakeGraph to tell it where the temporary CGI directory is where the graphs need to be saved, and where the RRDTool database files are.

my $RootDataDir="/var/raid/data/percyTemp";
# RootDataDir is the directory where the RRDTOOL database files containing the temperature data
# Apache will need to read the directory and read the files (but not write)
#
my $RootGraphDir="/var/www/tempForCGI-BIN";
# RootGraphDir is where the graphs are stored. This directory will also need to be defined in Apache.
# This is a special directory that is read by Apache, and can be written to from apache,
# so it is quite vulnerable and so is only used to write graphs to.

 

 

And that is it. Your should be able to open the http page and view your graphs.

Author and copyright

Stephen.Kingham <at> kingtech.com.au

Comments and suggests are welcome