Asterisk Weather Station by Zip Code

Weather Reports for 42,740 U.S. Zip 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 Zip Code version lets you key in any of 42,740 U.S. Zip Codes using a touchtone phone. The Airport Code version lets you key in any of 1,000 U.S. airport 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] ZIP Code MySQL Database (857KB)
[zip] ZIP Code Database License (46KB)

Database Setup

After downloading the ZIP Code Database and License to your Desktop, unzip the database file on your Desktop. Read and heed the license agreement. 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->zipcodes to get to the mysql folder in the ZIP Code Database download. Choose the zipcodes1.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 first half of the database into MySQL. A message will display shortly saying, "Import has been successfully finished. 22003 queries executed."

Next, choose the zipcodes2.sql file on your Desktop and click Go again. When the import completes, you should see a message which says "Import has been successfully finished. 20741 queries executed." Double-check to make sure you got the whole database by exiting from phpMyAdmin. Then start it up again and select the zipcodes database. In the right panel, it should show a zipcodes table with 42,740 records. Close your browser if all is well. Otherwise, repeat the drill.

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 => 947,1,Answer
exten => 947,2,Wait(1)
exten => 947,3,DigitTimeout(7)
exten => 947,4,ResponseTimeout(10)
exten => 947,5,Flite("At the beep enter the five digit zip code for the weather report you wish to retrieve.")
exten => 947,6,Read(ZIPCODE,beep,5)
exten => 947,7,Flite("Please hold a moment while we contact the National Weather Service for your report.")
exten => 947,8,AGI(nv-weather-zip.php,${ZIPCODE})
exten => 947,9,NoOp(Wave file: ${TMPWAVE})
exten => 947,10,Playback(${TMPWAVE})
exten => 947,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 => 947,3,Set(TIMEOUT(digit)=7)
exten => 947,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 WeatherByZip 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 PHP script to the AGI directory:

cd /var/lib/asterisk/agi-bin
rm -f nv-weather-zip.*
rm -f
chown asterisk:asterisk nv-weather-zip.php
chmod 775 nv-weather-zip.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 Zip Code

To use the Asterisk Weather Station by Zip Code, pick up any phone connected to your Asterisk server and dial Z-I-P (947). When prompted, key in the five-digit Zip Code for the weather report you wish to retrieve. Then sit back and listen to the latest weather report for your Zip 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