» Do it. Now.

Setting up a small weather monitoring station

Idea: Using 1wire protocol sensors to measure, log and display weather information.

Future: Using a relais card, I'll be able to toggle on of every device I feel like for any time from few ms to several hours.

Basis: * A box running Ubuntu (10.14 LTS in my case)

Step 1: Installing the needed packages and compiling owfs

All steps should be done beeing root.

comp# apt-get install apache2 mysql-server automake make autoconf autotools-dev gcc g++ libtool libusb-dev fuse-utils libfuse-dev swig python2.6-dev php5-dev phplib-jpgraph

after that download the sources from http://owfs.org/index.php?page=download and unpack them to for example /usr/src/owfs/

comp# cd owfs 
comp# ./bootstrap 
comp# ./configure 
comp# sudo make && make install

watch out and make sure USB and OWFS are enabled after the ./configure part.

Create the directory you want the sensors to be mounted into, I'm using /var/ow/.

/opt/owfs/bin/owfs –allow_other -F -u /var/ow
will get you started.

Step 1a: Some facts on 1wire and owfs in special

You will find quite some stuff in /var/ow/ once you got it mounted. The main thing to understand is that every chip on the 1wire net is listet as one directory. They are named after the Chip itself, thats a two character key, and the unique id of the chip.
If you are using a hub you'll find 1F.ID directories that represent a hub branch each, they are split into main and aux branches. Once you navigated into those branches they will behave as if you werent using a hub at all.
The sensorboards, i.e. the directories contain files named after the sensor data they messure. Just read their caption and you are done meassuring.

Step 2: logging information

The rest was done by Jan, thx a lot!

We simply use batch to throw the sensor data into a mysql db.

putdb.sh
#!/bin/bash 
echo "INSERT INTO data1. 
    (TEMP1,TEMP2,PRESSURE,HUMIDITY). 
    VALUES ( 
    `cat /var/ow/1F.DE6705000000/main/10.ID/temperature`, 
    `cat /var/ow/1F.DE6705000000/main/10.ID/temperature`, 
    `cat /var/ow/1F.DE6705000000/main/26.ID/B1-R1-A/pressure`, 
    `cat /var/ow/1F.DE6705000000/main/26.ID/humidity` 
    )"| mysql -uuser -psecret dbname 

The db table consits of a id, a timestamp and fey decimal fields for the sensordata. The script is run by a cronjob as often as I want it to be.

Step 3: Displaying the infomation in a JPGraph

graph.php
<?php 

// include JPGraph Library 
include ("/usr/share/jpgraph/jpgraph.php"); 
include ("/usr/share/jpgraph/jpgraph_line.php"); 
include ("/usr/share/jpgraph/jpgraph_date.php"); 


// The callback that converts timestamp to minutes and seconds 
function TimeCallback($aVal) { 
    return Date('Y.M.d-H:i',$aVal); 
} 

// create new graph 
$graph = new Graph(1300,600,"auto"); 


//get the db ready 
    error_reporting(E_ALL); 

    @mysql_connect("localhost", "user", "secret") OR die(mysql_error()); 
    mysql_select_db('db') OR die(mysql_error()); 

    $sql = "SELECT ZEIT,TEMP1,TEMP2,HUMIDITY,PRESSURE FROM table 
            ORDER BY ID;"; 
    $result = mysql_query($sql) OR die(mysql_error()); 

//push the data into $data array to get it plotted 

$i=0; 
while ($array=mysql_fetch_array($result)) { 
        $datum[$i]= strtotime($array[0]); 
        $temp1[$i]=$array[1]; 
        $temp2[$i]=$array[2]; 
        $humidity[$i]=$array[3]; 
        $pressure[$i]=$array[4]; 
$i++; 
}; 

// define graph 
$graph->SetScale("int"); 
$graph->SetY2Scale("lin"); 



$lineplot3=new LinePlot($humidity, $datum); 
$lineplot3->SetLegend('humidity'); 
$lineplot3->SetColor('green:1.5'); 


$lineplot=new LinePlot($temp1, $datum); 
$lineplot->SetLegend('temp1'); 
$lineplot->SetColor('red'); 

$lineplot1=new LinePlot($temp2, $datum); 
$lineplot1->SetLegend('temp2'); 
$lineplot1->SetColor('orange'); 

$graph->xaxis->SetLabelFormatCallback('TimeCallback'); 
$graph->xaxis->SetLabelAngle(90); 
$graph->SetMargin(40,40,30,130); 

$graph->AddY2($lineplot3); 
$graph->Add($lineplot); 
$graph->Add($lineplot1); 


$lineplot_g2=new LinePlot($pressure, $datum); 
$lineplot_g2->SetLegend('air pressure'); 
$graph->legend->Pos(0.00,0.5,"center","bottom"); 
$lineplot_g2->SetColor('darkblue'); 


// display graph 
$graph->Stroke(); 



?>

Will give you a PNG Image containig the graph. Its pretty fast so no problem rendering it live.

Thats it1

howto/setting-up-a-small-wheather-monitoring-station-using-1wire.txt · Last modified: 2010/09/13 20:04 by basti