How it works:

The weather system consists of a Hewlett-Packard 486/66N PC with a Keithley-Metrabyte DAS16G analog to digital (A/D) converter board. The software drivers provided with the Keithley-Metrabyte A/D board left much to be desired, so I opted to write my own low-level interface to this board. I would have preferred to write this driver in C, however, I don't have a C compiler for DOS. BASIC was chosen for this interfacing because I had a BASIC compiler (which allows me to make .EXE files) and I believe that if necessary, almost anyone could modify the program to suit their needs at some point down the road.

This BASIC program does no more than necessary. When called, it reads the 12-bit A/D values on the DAS16G and returns 12-bit decimal equivalent values for each channel. Unfortunately, this program writes these values to a file in a temporary directory, as my BASIC compiler does not seem to correctly output values that Tcl/Tk can read. If this issue can be resolved, disk I/O could be decreased and data sampling delays could be decreased to seconds instead of tens of seconds.

The other 90% of this program is written in Tcl/Tk, a freely distributable language from Sun Microsystems. This language was chosen because of its platform-independant nature (the same code on Windows, UNIX and Macintosh platforms), and the ease at which it allows graphics to be placed on a computer's screen--important if one wishes to display more than numbers. The Tcl/Tk program runs the show, rotating logs, displaying graphics, calling the BASIC program which reads the A/D board, and formatting data for web availability. I have made liberal use of comments within the code itself, hoping that the code will remain maintainable.

The Tcl/Tk program starts in function 'main' by initializing canvas widgets which control what is placed on the screen and where. A widget is some object on the screen with functionality, such as updating when a varaible changes, or placing a menu on the screen when clicked upon. All widgets are set up in function 'main'. Function 'main' also calls the primary, hourly and daily event loops.

Event loops are a feature of Tcl. A call to an event loop returns immediately, however, the end of the event loop calls itself after a fixed delay, thus restarting the event loop. As an example, function 'main' calls the function 'evloop', the primary event loop for the program. (The functions of which will be described later.) At the very end of the 'evloop' function, it calls itself after a delay of one minute, or 60000 microseconds, thus, the loop will start, complete, and wait one minute to restart. There can be multiple event loops within a program. Currently, this program has three event loops. 'evloop' which does almost everything, 'hourly_evloop', to process hourly events, such as reading METARs, and 'daily_evloop' to handle rotating logs, etc. More event loops could be added as needed, and more functionality could be added to existing event loops as necessary.

The primary event loop, 'evloop' has several functions. The most important of which is to make a call procedures which read the A/D board and convert the voltages to appropriate meteorological values. Initially, only one sample of the variables was taken. At present, an average of four readings taken at two second intervals is used for the one-minute value. The number of samples and delay between samples can be modified as deemed necessary. The variables are stored in a Tcl string as seven consecutive numbers separated by spaces. The Tcl command lindex is used to access a single variable in the string of converted values. The position of the variable in the string is the same as the A/D converter channel numbers. As an example, the relative humidity sensor is connected to channel 2 on the A/D converter and would be accessed in the code by the command lindex $converted 2 where $converted is the string holding the converted values.

'evloop' also determines immediate high and low values for the day. Each variable is compared to high and low values for each variable, and these highs and lows are updated as necessary.

Another function of the main event loop is to update canvas widgets with the latest values for display on the screen. Creating a canvas widget creates a new Tcl command which consists of independantly executed code. By using this command, the functionality and variables of the widget can be updated on the fly. As an example, the text for the current values on the screen is controlled by the widget (command) .body.stat2. This widget has code which displays temperature, relative humidity, etc. Each item on the screen is assigned a variable name. To modify the value for relative humidity, the following command is used:
.body.stat2 itemconfigure rh -text "Relative humidity [format "%3.1f" $relh ] %"
This causes the value for the relative humidity to be updated.

The procedure evloop also causes HTML code to be written for the web, and for data to be logged. HTML code is written by the procedure writeHTML which simply re-prints the variables with HTML code around them. Although Tcl/Tk can act as a web server, I have opted to leave this functionality to another program running on this computer. writeHTML writes the code to the appropriate directory and file for the web server to output. In order to get graphics on the web page, an external program, fly.exe is used. Details on the use of fly can be found from the appropriate link in the software sources page. A problem exists where fly writes the same sized graphic each time it is called. In this case, the web browsers do not display the latest graphic, as it does not appear as if the graphic has changed. To avoid this problem each graphic is named uniquely, and is deleted when a newer graphic is in place. A potential problem is that users may find corrupt images on the page during the instant the file is created, however, an immediate reload of the page will display a good graphic. Finally, a call to logdata logs the one-minute values in a file on the computer's hard disk.

The logged data is in the following format:
nnnnnnnnn {0 1 2 3 4 5 6 7 8}
where nnnnnnnnn is the Tcl/Tk clock seconds value -- the number of seconds elapsed since the epoch (1970). Braces enclose the values for the eight channels, logged in order of connection on the A/D converter. The current data file for the day is named current.log and the daily_evloop event loop serves to rotate these logs daily.

Further functionality in the code exists to display the current Madison airport METAR and for rapid sampling of data. Both code segments are fully-functional, but are not fully debugged. The consequence of including the code for either at this time would be decreased stability of the system.

Last modified: Wed May 6 11:52:14 CDT 1998 by timc!