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 copyrightLicense 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
InstallationThe 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 145This 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 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 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 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 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] 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. 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 dataBy 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 whereTemperature 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 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 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.977584736Probe 2 B= 0.867904564 I edit /var/raid/bin/GetInputFromTemperatureKit145d and I change # Probe 1$CalibrateProbeByA[1] = 0 $CalibrateProbeByB[1] = 1; To # Probe 1 When you are finished with calibrating stop and start the process: sudo /etc/init.d/GetInputFromTemperatureKit145d
stop Installation on the apache serverSo 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/ 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: 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 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"> 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
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";
And that is it. Your should be able to open the http page and view your graphs. Author and copyrightStephen.Kingham <at> kingtech.com.au Comments and suggests are welcome |