Home » Technology » The Asterisk Weather Station: Forecasts from Any Phone for 42,740 U.S. Zip Codes

The Most Versatile VoIP Provider: FREE PORTING

The Asterisk Weather Station: Forecasts from Any Phone for 42,740 U.S. Zip Codes

Weather MapAfter we published our article providing Asterisk® weather forecasts for 1,000 U.S. cities using airport codes, we heard from a number of folks asking if we could do something similar using zip codes to retrieve forecasts from the National Weather Service. Well, Santa is two weeks early, and today you get your wish: an enhanced weather application for Asterisk that supports every last zip code in the United States, all 42,740 of them. As with our original weather application based upon airport codes, this one requires the Flite voice synthesizer which now is an integral part of all TrixBox builds. You'll note that there are no thermometers and barometers to install with this application. So, to be perfectly candid, this is really a virtual weather station. The National Weather Service does the forecasting, the U.S. Postal Service does the locating, and the Asterisk Weather Station simply provides the glue to put the two together and retrieve and play the results using your touchtone telephone. The good news is that you actually control and manage this application rather than worrying about dialing into someone else's system only to find that they've gone out of business or quit providing the service. And, if you only want to hear current conditions for a particular zip code rather than the full 7-day forecast, we've retained the high tech solution we introduced with our original weather application: just hang up the phone. And the total cost. Well, that's the same as always here at Nerd Vittles: zero, zip, nada. Merry Christmas! [Note: Looking for Worldwide Weather Forecasts for Asterisk? Here's the link.]

How It Works. The design is pretty straightforward. You install a MySQL zip code database. Thanks, Novak! Then you add a simple dialplan to your extensions_custom or extensions_trixbox config file and tell freePBX the extension on your Asterisk system to dial to retrieve the forecasts. Finally, you download the nv-weather-zip.php script into your default Asterisk agi-bin directory. When you dial 9-4-7 (that's Z-I-P), the Asterisk Weather Station will prompt you to enter the 5-digit zip code of the city for which you want to hear the weather forecast. In a few seconds you'll be listening to the latest information from the National Weather Service. And apologies to all of our foreign friends. Yes, we know you have weather, too. We just don't know of an equivalent organization like the National Weather Service in every country. If you do, then it'll be fairly simple to adjust the code to meet your local needs. Here's a Canadian solution. If you have solutions for other countries, do everyone a favor and post a comment.

Prerequisites. For the Asterisk Weather Station to work, you'll need an Internet connection to access the National Weather Service reports. Then you'll need 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 can 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.

Downloading the Zip Code Database. We searched far and wide to find a free MySQL zip code database that could be used with TrixBox. But there wasn't one. So the Nerd Vittles elves started with Novak Banda's free zip code data and converted it into something MySQL could understand. You're welcome. Before downloading the database, kindly read and heed Novak Banda's license agreement. Then download zipcodes.zip to your desktop and unzip the file. Once you decompress the archive, you should have two files: zipcodes1.sql and zipcodes2.sql. We'll use these in a minute to build your very own, free MySQL zip code database on the fly. Buy someone in need a little Christmas present with the money you saved. You'll both be happier!

Installing the Zip Code Database. Using your web browser, access your TrixBox server by entering the IP address of the server. Click on System Administration, log in as maint with your password, and then click phpMyAdmin to enter the world of MySQL. Now click on the Import link at the bottom of the right pane. Choose the zipcodes1.sql file on your Desktop, uncheck the Partial import box and leave the other settings as they are. Then click Go. Be patient with the imports. This is a BIG database, and numerous indexes have to be created as well. When the import finishes, you should see a message which says "Import has been successfully finished. 22003 queries executed." Now 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 phpMyAdmin if all is well. Otherwise, repeat the drill.

Dialplan Code. In order to activate extension 947 to answer calls for your Asterisk Weather Station, you'll need to drop the following code into your dialplan in the [from-internal-trixbox] context of /etc/asterisk/extensions_trixbox.conf. The easiest way is to access TrixBox from your browser and choose System Administration, Config Edit. Then click on the extensions_trixbox.conf file in the left column, scroll down into the [from-internal-trixbox] context and cut-and-paste the following. 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

Adding a Miscellaneous Destination. While you're still in TrixBox with your browser, choose freePBX and then choose Setup, Misc Destination. Add a new entry for WeatherByZipCode with 947 as the Dial entry. Save your entry and then click the Red Bar to reload Asterisk.

Downloading and Installing the Asterisk Weather Station. To get the Asterisk Weather Station script installed, you'll need to log into your Asterisk server as root. Then issue the following commands in order:

cd /var/lib/asterisk/agi-bin
rm -f nv-weather-zip.*
wget http://nerdvittles.com/trixbox123/nv-weather-zip.zip
unzip nv-weather-zip.zip
rm -f nv-weather-zip.zip
chown asterisk:asterisk nv-weather-zip.php
chmod 775 nv-weather-zip.php

Taking the Asterisk Weather Station for a Spin. Now we should be all set. Just pick up an extension on your system and dial 947. You'll be prompted to enter a five-digit zip code. Punch in 90210 and check out the Valley Girls weather forecast. Enjoy!

Housekeeping 101. 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, make a mental note to remove all of these files with a command like this:

rm -f /var/lib/asterisk/sounds/tts/*

Or just log into your Asterisk server as root and edit the following file: 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.

Installation Script. Some folks may find an install script easier to deal with. Here's a Script-Trix link if you want one.

Nerd Vittles Demo Hot Line. You now can take a number of Nerd Vittles projects for a test drive... by phone! The current demos include (1) MailCall for Asterisk with password 1111 (retrieve your email by phone), (2) NewsClips for Asterisk (latest news headlines in dozens of categories), (3) Weather Forecasts by U.S. Airport Code, and (4) Weather Forecasts by U.S. ZIP Code. You're not prompted for #4 yet, but it does work! Just call our number (shown in the left margin) and take any or all of them for a spin. The sound quality may not be perfect due to performance limitations of our ancient Intel 386 demo machine. But the price is right.

Nerd Vittles Fan Club Map. Thanks for visiting! We hope you'll take a second and add yourself to our Frappr World Map compliments of Google. In making your entry, you can choose an icon: guy, gal, nerd, or geek. For those that don't know the difference in the last two, here's the best definition we've found: "a nerd is very similar to a geek, but with more RAM and a faster modem." We're always looking for the best BBQ joints on the planet. So, if you know of one, add it to the map while you're visiting as well.

Hosting Provider Special. Just an FYI that the Nerd Vittles hosting provider, BlueHost, has raised the bar again on hosting services. For $6.95 a month, you can host up to 6 domains with 50GB of disk storage and 999GB of monthly bandwidth. Free domain registration is included for as long as you have an account. That almost doubles last month's deal, and it really doesn't get any better than that. Their hosting services are flawless! We oughta know. We've tried the best of them. If you haven't tried a web hosting provider, there's never been a better time. Just use our link. You get a terrific hosting service, and we get a little lunch money.

Want More Projects? For a complete catalog of all our previous Asterisk projects, click here. For the most recent articles, click here and just scroll down the page.

Headline News for the Busy Executive and the Lazy Loafer. Get your Headline News the easy way: Planet Asterisk, Planet Gadget, Planet Mac, and Planet Daily. Quick read, no fluff.

Got a PDA or Web-Enabled Smartphone? Check out our new PDAweather.org site and get the latest weather updates and forecasts from the National Weather Service perfectly formatted for quick download and display on your favorite web-enabled PDA, cellphone, or Internet Tablet. And, of course, it's all FREE!


  1. I tried this with a bunch of AZ zipcodes, all of them say no weather information is currently available for phoenix arizona, have a nice day, goodbye. I tried Mesa, Chandler, Tempe, no dice on any of them. It works with 90210 though.

    [WM: Glad the Valley Girls are working for you. But, believe it or not, even the National Weather Service has problems from time to time and usually on weekends. Chances are that all of the cities you tried are hosted on the same (dead) server. Once your friendly government workers make it to the office this morning, it probably will get fixed. Unfortunately, there are times when cities are down for days. And it should be noted that we look for Current Conditions and a Forecast. If either is missing, we give up. As the old saying goes, "You get what you pay for." Finally, here’s a web link to check whether weather info for a particular site is available. Just enter your zip code in place of the one for Mesa, AZ that is shown: http://www.srh.noaa.gov/port/port_zc.php?inputstring=85201 ]

  2. Great job! It worked exactly as described. Now what can I do to convince you to modify the script to use a Cepstal voice? (A donation is definately in order!)

  3. nice work. but when are you going to start phasing out deprecated commands like ResponseTimeout and DigitTimeout ?

    [WM: You don’t want to get me started on deprecated commands. The basic problem is that using "new" commands breaks stuff for hundreds of thousands of folks using older versions of Asterisk. Supporting deprecated commands takes about one extra line of code per new invention so it’s not a lot of overhead. And most folks don’t swap out their phone system every time something "newer and better" gets released. Aren’t you glad you asked?]

  4. For those of you not using Trixbox but regular asterisk or with freepbx added on, you can download the zipped sql files and at a prompt type unzip zipcodes.zip then type mysql -u root -ppassword <zipcodes1.sql and then the same except zipcodes2.sql. Edit the php file with nano to replace the mysql password and username with your credentials. Create the /var/lib/asterisk/sounds/tts directory if its not there and chown it to asterisk:asterisk

    That should make the script work for non trixbox users.

  5. There is also a bug. If you enter a West Virginia zip code such as 24740 when it reads back the file it replaces West Virginia with this "Summary: This National Weather Service update provided for PRINCETON, WEastern Standard Time. VIRGINIA."

    [WM: There was a math error (mine) in the original version of nv-weather-zip.php. Just download and install the script again to fix it.]

  6. So my first comment was truncated. Here are the commands.

    unzip zipcodes.zip
    mysql -u root -ppassword < zipcodes1.sql
    mysql -u root -ppassword < zipcodes2.sql
    mkdir /var/lib/asterisk/sounds/tts
    chown asterisk:asterisk /var/lib/asterisk/sounds/tts

    Then edit the php file to the correct password for your mysql database in /var/lib/asterisk/agi-bin/.

    PS. The WEastern Standard Time VIRGINIA bug still exists from the file I downloaded today.

    [WM: Thanks. We found the other half of the bug. Just download it one more time and all should be well in West Virginia. 50 lashes to the programming department. Ouch, ouch, ouch…]

  7. I listened to your weather report by zip code and there is silence while it gets the report from the national weather service. Is there some way to play music while this is happening rather than silence?

    [WM: Not that we’re aware of. Sorry.]

  8. A bug in the string manipulation functions caused the beginning of some weather reports to be garbled. Version 1.2 fixes it. After deleting the original nv-weather-zip.php file from your /var/lib/asterisk/agi-bin directory, just download the nv-weather-zip.zip again and follow the original Asterisk Weather Station script installation instructions above. Our apologies.

  9. Ward, it looks like the weather app may be broken after upgrading to trixbox 2.0. I tried reinstalling as well, but when requesting the report it says successfully downloaded, then there is about a 5 second delay and then it hangs up. Can you verify this?

    [WM: I’m staying away from 2.0 for a while for a whole host of reasons. It breaks lots of things so I’m not surprised.]

  10. Hey from frigid Wisconsin! Sweet concept. I’m using it for a local baseball league hotline (when it warms up this spring). Instead of the clunkiness of inputting a zip code, I’m just polling for my zip…
    exten => 947,8,AGI(nv-weather-zip.php|${53185})
    Here’s cookie you can help me help me chew on… update the weather a few times in the afternoon to prerecord the conditions and forecast. Coaches and parents want to know what’s up for their 6 and 8 pm games, they can dial in and simply listen to what the national weather service said last hour. Any takers?

  11. I don’t remember where I stole this from ( I think it was
    michigan telephone) but this is another
    way to do the weather using another website:

    exten => 612,1,Answer
    exten => 612,2,Wait(1)
    exten => 612,3,DigitTimeout(7)
    exten => 612,4,ResponseTimeout(10)
    exten => 612,5,Flite("At the beep enter the five character zip code for the weather report you wish to retrieve.")
    exten => 612,6,Read(APCODE,beep,5)
    exten => 612,7,System(/usr/bin/curl -o /tmp/zip.mp3 -s http://audio.pirateweather.com/${APCODE}.mp3)
    exten => 612,8,Flite("Please hold a moment while we contact the National Weather Service for your report.")
    exten => 612,9,Wait(3)
    exten => 612,10,Playback(/tmp/zip)
    exten => 612,11,System(rm /tmp/zip.mp3 -f)
    exten => 611,12,Hangup

  12. Thank you for this program, but I cannot yet make it work on a Linux !686 RedHat Enterprise (2.4 kernel) with Asterisk 1.2.11. Used install instructions from here for the two flite apps. Asterisk tells me it’s registered. DB installed, nv-weather-zip.php has correct mysql connection data.

    Flite speaks the words from the dial plan, but after a 30-second or so wait, no weather report is returned and a busy signal.

    When trying to chown nv-weather-zip.php and tts as per instructions, command line gives "invalid user" error. /var/log/asterisk/messages records "app_flite.c: Flite: No such configuration file flite.conf," and "app_playback.c: Playback requires an argument (filename)."

    I even tried removing the rpms to reinstall, but server says they are NOT installed. Yet trying to reinstall them, server says "already installed."

    Any clues for a confused newbie will be greatly appreciated. Thanks — HHB

  13. Hello again. Since I posted the above in February about not being able to make nv-weather-zip.php work on the Asterisk 1.2.11, have checked out agi scripting in general, ran into phpagi stuff, and even though there were no includes for it on the nv-weather-zip.php script page, had read that elsewhere that it used phpagi. Had previously installed the zipcodes db, so decided to try it all again.

    Added an include for phpagi.php, configured the phpagi.conf to use the 8kHz cepstral voice here, and YES, all works A+. Can even adjust the voice lexicon, etc. Thank you for this scripting, which appears quite complicated ….


  14. I receive a beep and there is silence. I noticed the script is running while watching the asterisk console, so I went ahead and input a zip code and after a few seconds the report began playing. Any idea of what would cause the prompts to not to be played?

    [WM: Be sure your have the correct TTS engine set in both the dialplan and the AGI script.]

  15. I already have eSpeak and asterisk-espeak installed and I want to use espeak instead of swift (don’t know about free voices for swift) or flite.

    I got espeak to work by creating a simple custom extension:

    exten => s,1,Answer
    exten => s,2,eSpeak("This is a test.")
    exten => s,3,Hangup

    How do I modify the php file to get espeak to work?

    I will try to install flite (I dislike the sound of speech) in the meantime.

  16. Why doesn’t it work on Asterisk currently running on trixbox1?

    [WM: Try replacing all | characters in /etc/asterisk/extensions_custom.conf with commas. Then restart Asterisk.]

Comments are closed.