Getting the weather station online

Weather Underground has a very simple data upload protocol published on their site at http://wiki.wunderground.com/index.php/PWS_-_Upload_Protocol. Because running child processes and fetching web pages is easy in PHP, I chose it for coding the application to read, parse and upload the weather data. (All of the code I wrote for this can be downloaded in a zip file.)

Sure, there are packages like RRD Weather that can do this too, but where’s the fun in that.

The te923con application returns most of the data included in the wunderground protocol directly, so it’s a simple matter of parsing it from the application output. Weather Underground wants imperial units, but maybe someday I’ll want proper metric units, so I made it a parse option.

The weather station apparently does not return a calculated dew point, which is part of the upload protocol. It turns out that calculating dew point is not straighforward. But many people have implemented calculators. A guy named Wolfgang Kühn implemented one in javascript that seemed very thorough, so I did a quick port of it to PHP.

The weather station also returns only the accumulated rainfall total since the station was started, so I implemented a simple array mechanism to retain historical data from the rain counter. I can then calculate the “rain in last hour” and “rain since midnight” values required by the WUnderground protocol.

From there, it’s a simple matter to build the URL string for data upload, and a file_get_contents call to upload and get the success or failure result.

I save the rain counters and the other weather data as PHP code in a cache file that is reloaded with a PHP include the next time the script runs.

WUnderground can accept data as fast as once a second, but the weather station does not update from its sensors with anywhere near that frequency, so I felt scheduling upload once a minute via cron was more than often enough. The script takes station id, password and cache file name on the command line (all on one line):

* * * * * php te923WunderUpload.php <station ID> 
                       <wunderground password> <cache file name>

Because of the permissions issues with the USB device, for now this is scheduled in the root user’s crontab.

Now on to fixing the bugs

Tags: ,

One Response to “Getting the weather station online”

  1. Well done with all your work – very interesting.
    I arrived here from a search as I’m currently getting data from an Instromet station and uploading to Wunderground using php. You’ve given me some very useful pointers.
    Many thanks!

Leave a Reply