Weather station documentation

Jonas Sextl, jonny@bndlg.de

v0.1, 01.06.1999


Here you find a documentation about the linux solution for the Petra II weather station.

Introduction

The Petra II project developed a weather station during the years 1992 until 1994. They built everything on their own. From the chassis to the different detectors. They also built an interface to a PC using the parallel port and they wrote a very simple DOS application the collect the data and store it into simple textfiles. The team consisted of members from Denmark, Greece and Germany. To exchange the collected data they used CC-Mail from Lotus and a direct modem connection, which of couse was a little bit expensive.

So after a while the whole system was shut down and isn't running any more at the moment.

Now my intention was to reactivate the weather station which was lying around full of dust. But of couse I didn't want to build the old modem system any more. So it was a very lucky moment for me, cause I searched for a chance to write a small linux device driver while I war reading a book about writing linux drivers. And because I like linux very much the idea was born to write some tools under linux to drive the weather station with an old PC and put the data into a SQL database, so everybody can access them over the network with all the nice standard tools.

Here you find some pictures of the "live" system here in Dillingen: pictures.html.

Overview of the system

The system consists of (at least from my point of view) three main parts. This is just an overview of the next sections, so that you know the reasons for the different parts and their functions.

Here's an overview of the whole system. You can see two weather stations each with a datagrabber PC which distribute their data to the database server. The clients can then query the database server and retrieve the weather data.

The device driver for linux

The device driver is the interface between the parallel port and the application programs which collect the data from the station. It is responsible for generating the software clock ticks necessary to drive the A/D chip on the mainboard of the station. And the driver also reads all the bits from the station and puts them together so that the user program gets 2 bytes.

The interface program which collects the data

The device driver only produces simple bytes, but of course has no interface to the database nor does it convert the raw bytes into human readable values like windspeed or temperature. Therefore we need a small program which let's us specify, which data we want to see, in which intervals they should be displayed, where they should be saved to, etc.

The data plot program

Now our data is inside the database. But now everybody wants to see it. Not only the experts, who can do SQL queries on the shell level. So I wrote some small scripts which run on a webserver and allow everybody to query the data. The program constructs a gif on the fly and delivers it to the client. Of couse you can always get the raw data in a HTML table, too.

Here is an overview of how the data travels through the three main parts shown above:

The linux device driver

Overview

The concept was adapted from the existing pascal source code, which you can find on the Petra II CDROM. Unfortunately it was the only documentation I got about the interface of the weather station. So I couldn't do any optimistaions or alternations to the code and I only ported it to plain C code.

I wrapped around the structure of a kernel module.

The module is called petra and can be compiled on linux 2.0.x. 2.2.x doesn't work at the moment, cause there are some changes to the interface of kernel modules.

Usage

The module can be loaded with the following command:
modprobe -v petra [petra_base=HEX_IOPORT] [petra_major=MAJOR_DEVICE_NUMBER]
If you don't specify the petra_base parameter the default will be 0x378 which is normally the first parallel port in your PC.

After loading the module, you can »access« the weather station through several devices. Each corresponds to one data-channel of the station. So you can for example capture the temperature every 10 seconds but the windspeed only once per hour. It is also possible that different applications access the device simultanously, cause the kernel code can't be interrupted during a read. So a read of 2 bytes from one of the devices is an atomic operation.

Here is a table of the channels whith our station in Dillingen:

channel numberdatadevice
0temperature/dev/petra0
1pressure/dev/petra1
2humidity/dev/petra2
3windspeed/dev/petra3
4winddir/dev/petra4
5unused
6rain/dev/petra6
7unused

If you read data, you must know that every two bytes represent one value from the station. It's range is between 0 and 4096 cause the D/A converter in the station can handle 12 bit. The remaining bits are simply set to zero by the driver.

The driver also supports two IOCTL calls. One to reset the rain counter and one to start the rain counter.
You can use them from user programs with a code similar to the following:

		if(ioctl(fd, PETRA_IOC_RESETRAIN) != 0)
			perror("error in ioctl\n");

		if(ioctl(fd, PETRA_IOC_STARTRAIN) != 0)
			perror("error in ioctl\n");
		

Of course you have to include the petra.h header file which defines the PETRA_IOC_* constants.

The code itself

Here you find the code of the module: petra.c. If you want to try it yourself, I recommend downloading the whole package including a makefile and the user interface program. Here you can find a tarball: petra.tgz.

The user interface program

Overview

The user interface program is a command line tool. So it doesn't have any graphical interface (yet). It doesn't need any special rights to execute except for read permissions on /dev/petraX. You don't need a MySQL database nor a network connection to run the program. Just don't specify the parameters for MySQL if you don't want to use it.

Functions of the program

Requirements

To run this program you need nothing more than a libc on the host system. The whole MySQL and network stuff can be linked in statically and the binary will be only about 40k. This is necessary, so it fits on the one disk distribution shown in the next section.

To compile the program of course you need the MySQL development system, which consists of the header files and the mysqlclient library. You can get MySQL for free from http://www.tcx.se. The SDK files are included there. You also need a C compiler and GNU make of course.

For usage of the MySQL function, you need of course a MySQL server anywhere reachable with TCP/IP. The port number is 3306 if this is relevant perhaps, cause you've got a firewall. But I wouldn't recommend opening an existing MySQL server for everybody, cause this could be a big security whole.

You also need a table on the server. grabdata by default uses the following table structure:

fieldnamedatatype
tsTIMESTAMP
temperatureDOUBLE
windspeedDOUBLE
winddirDOUBLE
humidityDOUBLE
pressureDOUBLE
rainDOUBLE

Usage

The usage is very simple. Just invoke the help screen first: ./grabdata --help

You get a similiar output to the following:

grabdata v0.2 - petra II weather station data grabber

usage: grabdata [options]
        -h, --host=mysql_hostname       specify mysql hostname
        -d, --db=mysql_database         specify mysql database
        -t, --table=mysql_table         specify mysql table
        -u, --user=mysql_user           specify mysql user
        -p, --password=mysql_password   specify mysql password
        -i, --intervall=grab intervall  pause between each grab
        -l, --loop                      loop until killed
        -r, --rawdata                   also display raw sensoric data
        -c, --channel                   only display selected channel

available channels:
        0 - temperature
        1 - pressure
        2 - humidity
        3 - windspeed
        4 - winddir
        6 - rain
		

If you want, you can use long and short options together without any problems. Just don't specify options for MySQL if you don't want to use it and the program won't complain about a missing server ;-)

The code

You can view the source code here: grabdata.c or you can download the tarball: petra.tgz which includes the kernel module, grabdata and a makefile for both.

The database interface and plot tool

The plot system is not finished yet. I just need to complete some scripts to query the database. It will consist of some PHP/FI scripts, which implement the HTML forms the user sees and call the plot program which is written in C with the right parameters.

When it is finished I will include it into the tarball distribution.

To sweeten your waiting you can see a preview here: sample.gif.

It is a temperature plot of our server room here in Dillingen, where we tested the weather station.

A complete mini-distribution fitting on one floppy

Why?

Well now it's time to set up a datagrabber. I already mentioned that the system is running with linux, didn't I? Ok, but linux is a very complex system and besides it would be very much overkill to use a complete linux distribution with X and compilers and everything else to run a little small weather station.

Because the hardware should be as cheap as possible we don't want to use a harddisk and we want to use our old i486 which also waits for the grabage collection to come by ;-)

So we need a very small solution which ideally fits onto one floppy (cause then you can reboot the machine without wating to change any disks...). And because linux is so flexible we can have a complete running linux onto one floppy.

Apart from the petra II driver and datagrabber the linux disk contains nothing more than:

Because it would not be very easy to compile such a mini distribution by ourselves, I decided to use a already existing solution. The so called LRP - Linux Router Project. Its aim is to develop a small linux system fitting on one disk which can be used to build routers. I just thrown out the programs, we would never need and included the petra driver (only one file, the kernel module) and the grabdata program.

Download and installation

is to come soon...

Configuration

is to come soon...

Literature and links

[1] MySQL database server; http://www.tcx.se

[2] PHP/FI script language; http://www.php.net

[3] Apache web server; http://www.apache.org

[4] LRP - the linux router project; http://www.linuxrouter.org