Getting weather data into XTension
There are several commercial home weather stations available, each with the same basic features, some with more, some are pretty pricey ($200-$1000US) and some are available as 'kits'.
Although there are many ways to justify the purchase, the most compelling reason to buy any one of them rather than to do-it-yourself, is that it is truly time-consuming. Sensors to build and calibrate, proper wiring, software and of course some analog processing hardware ...
This is not to discourage DIY's but just to point out that your time is always the most costly part of your home system, and you can easily eat up a lot of $$ and 'crow'.
( update : be sure to see the tutorial for the Radio Shack WX200)
This tutorial describes how to attach the popular Davis Weather station to your Mac and gather data from it into your XTension database. The example here should be sufficient to illustrate how you might make a different applet for use with just about any serial device.
Although there is software for the Mac and the Davis, it is no longer supported(?) and it is not AppleScriptable. By creating an AppleScript 'applet' that talks to the Davis unit via a Mac serial port, you can sample this weather data and use it as input to other XTension scripts.
This is a picture of the Davis Weather Wizard II.
There are several different models, each with the same set of basic sensors for temperature, humidity, wind speed, direction, and rainfall.
Each has a very nice front panel which displays all current readings.
Note that this tutorial specifically covers the weather data from models :
"Wizard", "Perception" and "Monitor"
How the Mac talks to the station
In order to connect any of the Davis devices to your Mac, there is an option they offer called the Davis WeatherLink. This acts as a serial interface to the Davis weather station. It is responsible for 'sampling' the station sensors periodically, and storing the data in "Link Memory".
The WeatherLink is a small circuit board that fits inside the weather station console, and comes with Mac software that is very nice but not scriptable and thus it is difficult to integrate it into your whole house system.
The Davis Weather station comes in different versions, but this is a tutorial and so we will limit the example here to a one command that is common to all of them. This command will cause the WeatherLink to return the current readings of the sensors attached to the weather station.
The one common command is "LOOPx". Receiving this, the Davis will respond with a 'packet' of weather data. The format of this data is binary, and so is not printable and must be processed by a script before we can use it as meaningful values in XTension.
Following is a simple picture of the physical setup for the tutorial :
Here we use a single Macintosh with two serial ports. Note that without some form of 'port-sharing' on the AppleTalk port, this single Mac is isolated from any other Macs you may have in the home.
The WeatherLink is not shown here because it fits inside the weather station.
Of course if you have more Macs in the home, you might want to set them up
Making it simple:
This tutorial includes a 'plug-in'. This file actually includes several files which are necessary for this to all work. All scripts are already written, but you may have to make some changes. You will of course have to 'un-stuff' them first.
Here's where you download the plug-in. <<
Here's what is in the 'plug-in' :
This is an AppleScript applet which periodically samples the Davis weather data. It runs separate from XTension, and determines whether any data have changed significantly. If so, it sends a message to XTension to put the new value into the XTension database.
This file is an XTension 'plug-in', which you must 'import' into your XTension system. It includes new units with names corresponding to the weather values that come from the Davis station. It also includes a Group and a Global Script which will be used to start up the Davis Read applet.
CTB.paul serial port OSAX
This is a AppleScript Addition, or OSAX and is used by the DavisRead applet to read and write to a serial port. It is not assumed that this osax is totally debugged, but it worked well in the tests of this applet. It should be put into the Scripting Additions folder in your System Folder:Extensions.
How it's all supposed to work
You download the 'plug-in', and import it into your XTension system.
You can put the DavisRead applet in the same folder as XTension.
Next you must connect the Davis Weather station to a serial port.
Make sure that you have the osax "CTB.paul" in the Scripting Additions folder.
Start up XTension, and select "Startup Davis" from the "Scripts" menu.
This will start up the applet, which should report any weather changes every two minutes.
You should see these as changes (if any) in the window (Weather).
You will still want to make use of this data in some of your other scripts.
You might see something like this :
In an operational system, on startup of XTension, the Startup Script causes the applet "DavisRead" to run and begin sampling weather data. Thereafter, the applet sends any changes back to XTension every two minutes, quitting only when told (like in the Shutdown Script).
The values sent by the applet to XTension are in a form which can easily be used by your scripts. All values are returned in integer form, and thus any decimal point is assumed.
In the example above, Inside Temp = 737 really means 73.7 degF,
and the Barometer = 28781 should be 28.781 inches of Mercury.
Whenever you want to display these values, you should remember to 'scale' them accordingly, such as for the Barometer :
display dialog " The current outside pressure is : " & (value of "Barometer") / 1000)
result = "The current outside pressure is 28.781"
So where do I go from here ?
If you're not interested in scripting or piddling, then this is almost as much as you need to know about either the applet or the Davis. You need only use the weather data in scripts which inform or warn you of exceptional weather events.
We'll leave it to the Weather Watchers and others to praise the virtues of having your own weather station, and perhaps there will be more added to this tutorial regarding cool things that you can do.
If however you are interested in the AppleScript of the DavisRead applet, then read on...
About the Applet : DavisRead
This applet simply prompts the Davis WeatherLink to respond with a packet containing the most recent weather data. To make sure that it's the latest data, it forces the WeatherLink to clear out any 'old' data, take a new sample of all measurements, and then send these to the Mac.
On receipt of the packet, the applet processes the values using a table (CMST), and determines whether there has been a significant change of any of them.
If there has been a change in a value, the new value is sent to XTension with the name of the corresponding database unit (ie: Inside Temp).
There in XTension, more may be done with the arriving values, according to what scripts we have attached to those units, such as "Speak Inside Temp is (70) degrees" etc.
This applet is also an example of using some very handy
AppleScript techniques :
Creating local 'tables' of data with 'names' of fields
Serial Communications using a serial OSAX
Using the 'ignoring application responses' statement with the 'tell application...'.
An appology :
I have left out some of the little things in an attempt to show this in proper 'compiled' form, so if you want to see it properly, you should start up Apple's ScriptEditor after downloading the 'plug-in'.
The Command and Measurement Status Table ( CMST )
The first part of the script defines the table that will be used to help us make this whole thing 'generic'. This will make it a lot easier to change the script to work with other versions of the Davis weather devices. As well as devices from other manufacturers.
This illustrates the use of a table with multiple 'elements', and each with multiple 'fields'. Here we have a table of 8 'measurement' elements, corresponding to the values that are returned from the "LOOPx" command to the Davis WeatherLink.
The first two fields of each element (disp and lent), describe the offset within the response where we will find the first character of the Value, and its length in characters.
Some values may be two-bytes long, and others only one. In the two-byte cases, the Davis WeatherLink returns them in 'Intel' format. For Macs, this means we have to swap the upper and lower bytes before trying to use them.
The 'name' field of each element is the same name as is found in the XTension database, corresponding to the measurement.
The 'value' field is always the last 'changed' value for the measurement, after it has been processed. This value is not modified unless the measurement has changed by an amount at least equal to the 'significant change' or SC for this element.
In this example, we use a default SC = 2 , so that at least we can filter any 'chatter' from the measurement bouncing around by only 1 degree etc.
The scaling function "SF" and "bias" are not used here because the data returned from the Davis is already 'calibrated' and 'scaled'. It is already in a 'useful' form. Those who have read the TN.analog tutorial will recognize these and those who don't should at some time take a look at it.
The data packet from the WeatherLink
Here are the elements of the weather data packet in a more readable form :
Inside Temperature - Two bytes
Outside Temperature - Two bytes
Wind Speed - One byte
Wind Direction - Two bytes
Barometer - Two bytes
Inside humidity - One byte
Outside humidity - One byte
Total Rainfall - Two bytes
Two bytes unused
Two bytes of checksum (not used in this example)
Serial Port Configuration property dwConfig:
The last line in the above fragment determines the serial port configuration 'string' which the applet will attempt to use. If you have other requirements, it should be fairly easy to modify the string.
At Startup :
The next part of the script does the 'housekeeping', and makes sure that the serial port is available and if so, it clears out any old receive data and from then on, the 'on Idle' routine will run every 2 minutes to actually sample the data.
Where the real sampling is done :
In order to better share a serial port with another device, this applet opens and closes the serial port every time it runs. This may not be necessary, but it is a little more polite.
Here you see that we clear out the receive buffer again, and then send two commands to the Davis WeatherLink which should tell it to take a new sample of the sensor data, and put it into the 'archive' memory. Then we tell the Davis to send us that weather packet.
Just to make sure we're not getting just nonsense, the first two bytes of the response are checked and must be "ACK" saying that the command was accepted, and a "SOH" which says here comes the data.
Now we process and check the data for changes :
If any values have changed then we simply "tell XTension" that a named unit has a new value. Some of this may look very complex at first, but this is an example of how to make a generic function which is 'driven' by a table ( the CMST ).
If you watch the parentheses, it will make it easier to see that each of the lines does a simple thing, it's only the references which make it look bad. (and maybe my code).
Enough for now ?
That's a big enough mouth full for right now, but no doubt there will be changes and additions to this tutorial as other versions of the Davis are very useful and even the system of choice for many commercial farmers and greenhouses.
As usual, please let us know if there are any errors with either the tutorial or the 'plug-in', and whether you have problems or special needs.
Copyright 2007, Sand Hill Engineering Inc. All rights reserved.
Last modified: May 4, 2002
Paul Ferguson, firstname.lastname@example.org