Asterisk Weather Station by Airport Code

Weather Reports for 1,000 U.S. Airport Codes

The Asterisk Weather Station is designed to allow you to retrieve current weather information from any touchtone phone using nothing more than your Asterisk PBX connected to the Internet. There are two flavors of the Asterisk Weather Station. The Airport Code version lets you key in any of 1,000 U.S. airport codes using a touchtone phone. The Zip Code version lets you key in any of 42,740 U.S. Zip Codes using a touchtone phone. Current conditions and a seven-day forecast for the chosen city then will be retrieved from the National Weather Service and played back to your telephone using the Flite text-to-speech engine.


For the Asterisk Weather Station to work, you'll need an Internet connection connected to your Asterisk server so that your server can access the National Weather Service reports. We recommend either a TrixBox Asterisk server or an Asterisk@Home 2.x server and the new Flite voice synthesizer for Asterisk. Instead of a dedicated Asterisk server, you also could use our VMware version of TrixBox if you just want to experiment a bit. It runs in a window on your Windows XP/2000 desktop. If it's not already part of your Asterisk system, our Flite tutorial and the Flite application software are available here. This tutorial assumes you are using a TrixBox server which already includes Flite.

Desktop Download

[zip] Airport Codes MySQL Database (78KB)
[zip] Airport Codes Database License

Database Setup

After downloading the Airport Codes Database to your Desktop and reviewing the GNU General Public License, unzip the database file on your Desktop. Then log into phpMyAdmin on your Asterisk system. If you're using a TrixBox system, point your web browser to the IP address of your Asterisk server. Then click System Administration, then phpMyAdmin. Enter maint as your username and whatever password you assigned when you set up your system.

When phpMyAdmin loads, look in the left pane immediately below the phpMyAdmin logo and click on the SQL icon. When the SQL window opens, click on the Import Files tab and then the Choose File button. Click on Desktop to get to your Desktop directory. Choose the airports.sql file. The file name now should be displayed in the SQL window pane. Using the default utf-8 character set, uncheck the Partial import box and leave the other settings as they are. Then click the Go button to load the database into MySQL. A message will display shortly saying, "Import has been successfully finished." Close your browser if all is well. Otherwise, repeat the drill.

Changing Preferred Airport Codes

Airport codes are unique; however, translating them into 3-digit telephone numbers results in duplicates. If you key in a desired airport code and get an unexpected weather report, chances are that you've stumbled upon one of the duplicates. To fix it, adjust the preferred airport code for the 3-digit number you are dialing.

Start up phpMyAdmin as outlined above. Click on the Databases pull-down, and choose Airports. Then click on the USairports table below it. When the table opens, click on the Browse tab in the right frame to display the file's contents. Nome is the airport name, iatta is the airport code, dialcode is the airport code converted to numbers on a touch-tone phone, citta is the city and state of the airport, and main is the field used to designate whether a particular airport can be accessed using a phone. An asterisk in the main field means Yes. Anything else means No.







7402 Scott Fld 0M1 Parsons, Tennesee, United States 061 *
5647 Moton Fld Muni 06A Tuskegee, Alabama, United States 062  
7377 Schaumburg Rgnl 06C Schaumburg, Illinois, United States 062 *
1147 Calaveras Co Maury Rasmussen Fld 0O3 San Andreas, California, United States 063 *

Click on the dialcode column heading to sort the table by dialcode. As you scroll through the database, you'll see that every group of matching dialcodes has one entry with an asterisk in the main column. Remember that dialcodes are not unique while airport codes (iatta) are. Because of the layout of the alphabet on touchtone phones, as many as a dozen airport codes may share the identical dialcode. To change the main entry for a particular dialcode, click the Edit icon (displays as a pencil) in the left column of the record to be changed. Change the DialCode to an asterisk, and save your change. Remember to also edit the other records with the same DialCode number. Blank out the asterisk in the DialCode field for every record with that same DialCode. Otherwise, the last DialCode entry with an asterisk in the main column will win (i.e. that's the city whose weather report will be played) when the application is run.

Dialplan Setup

Your Asterisk Dialplan needs to be modified to support the Weather Station application. If you're running TrixBox, point your web browser at your Asterisk server again. Then choose System Administration->Config Edit and choose extensions_trixbox.conf from the file listing. When the file opens, scroll down to the [from-internal-trixbox] context. Cut-and-paste the following code into the file. Then click the Update button to save your changes.

exten => 611,1,Answer
exten => 611,2,Wait(1)
exten => 611,3,DigitTimeout(7)
exten => 611,4,ResponseTimeout(10)
exten => 611,5,Flite("At the beep enter the three character airport code for the weather report you wish to retrieve.")
exten => 611,6,Read(APCODE,beep,3)
exten => 611,7,Flite("Please hold a moment while we contact the National Weather Service for your report.")
exten => 611,8,AGI(nv-weather.php,${APCODE})
exten => 611,9,NoOp(Wave file: ${TMPWAVE})
exten => 611,10,Playback(${TMPWAVE})
exten => 611,11,Hangup

NOTE: If you're running this application on an Asterisk 1.4.x server, replace lines 3 and 4 above with the following:

exten => 611,3,Set(TIMEOUT(digit)=7)
exten => 611,4,Set(TIMEOUT(response)=10)

freePBX Setup

While still connected to your TrixBox server with your browser, choose freePBX and then choose Setup, Misc Destination. Add a new entry for WeatherByAirport with 947 as the Dial entry.

 Add Misc Destination

Save your entry and then click the Red Bar to reload your Asterisk dialplan settings.

AGI Script Setup

Log into your Asterisk system as root. We need to copy the Weather by Airport Code PHP script to the AGI directory:

cd /var/lib/asterisk/agi-bin
mv nv-weather.php nv-weather.old.php
rm -f
chmod 775 nv-weather.php
chown asterisk:asterisk nv-weather.php

Missing Directory Setup

On some other distributions (not PBX in a Flash), the sounds directory in which the weather reports are stored has disappeared. The symptom for this would be a weather report that introduces the report but never plays it. Here's how to recreate the directory:

cd /var/lib/asterisk/sounds
mkdir tts
chmod 775 tts
chown asterisk:asterisk tts

System Setup

Temporary files in /tmp get cleaned up by Linux housekeeping automatically. Temporary files stored elsewhere don't unless you're using a Nerd Vittles build of TrixBox with either the VMware edition of nv-trixbox for Windows or PBX-in-a-Flash for Linux. The weather scripts store .wav files with your requested weather forecasts in /var/lib/asterisk/sounds/tts. So, from time to time, these temporary files need to be cleaned out. The easiest way is to add a crontab entry which automatically deletes the file. Log into your Asterisk server as root and issue the following command: nano -w /etc/crontab. Move to the bottom of the file and insert the following code on a blank line:

3 0 * * * /usr/bin/find /var/lib/asterisk/sounds/tts -type f -mtime +14 | /usr/bin/xargs /bin/rm -f >/dev/null 2>&1

This code will delete all of the files in the tts folder every two weeks. If you'd prefer a shorter time, change the number 14 accordingly. Now save your changes: Ctrl-X, Y, then Enter.

Using the Asterisk Weather Station by Airport Code

To use the Asterisk Weather Station by Airport Code, pick up any phone connected to your Asterisk server and dial 611. When prompted, key in the three-character Airport Code for the weather report you wish to retrieve. Then sit back and listen to the latest weather report for your Airport Code from the National Weather Service. UPDATE: The National Weather Service "improved" things over the July 4, 2012 holidays. Just download the PHP/AGI script to get things working again.

Nerd Vittles Updated Article (2/21/2007): Programmer's Corner: Asterisk Weather Station Updates