Our HeroThanks to Asterisk®, Flite, and the National Weather Service, you now can get a quick weather update or a seven-day forecast for any combination of 1,000 U.S. cities using your plain-old touchtone telephone... just one of the many perks you receive for being an American Taxpayer. It doesn't mean the forecast will be right, mind you. But you can still listen and make your own judgments about carrying an umbrella. Just dial 611 and then a three-digit airport code for the city you're interested in. And you can customize things to support any 1,000 cities in the United States that you happen to like. [Note: Looking for Worldwide Weather Forecasts for Asterisk? Here's the link. For U.S. Weather Reports by Zip Code, go here.]

Prerequisites. To get started, all you'll need is a free Asterisk@Home or TrixBox server (Asterisk@Home or TrixBox will even run in a window on your Windows Desktop), the free Flite speech synthesis engine for Asterisk, our free MySQL database of airport codes and cities in the United States, and, of course, version 2 of our free Asterisk text-to-speech weather application. Yes, we accept donations above to underwrite the costs of future projects, but don't let that stop you if you're busy, broke, or just plain cheap. We won't track you down, and you'll still be able to sleep at night. We don't do this for a living. In fact, we don't do much of anything for a living, but we're still glad you're visiting. Please at least add yourself to the Nerd Vittles Fan Club Map and say something nice. That won't cost you a dime! We're assuming you already have an Asterisk@Home or TrixBox server up and running with Flite installed. If not, click on the links above to install your favorite flavor of Asterisk@Home or TrixBox plus Flite. And, no, this is not Rocket Science! You can finish this project in about 30 minutes without a Ph.D.

Try It, You'll Like It. At the risk of bringing our clunker TrixBox development server (with a whopping 128MB of RAM) to its knees, we decided to make it easy for you to see how the new Nerd Vittles Weather app works. You can call our Stanaphone number and take it for a spin. Just dial the number shown in the left margin. And understand that the sound quality may not be perfect due to performance limitations of this ancient Intel 386 machine.

Downloading and Testing Flite. If you already have a TrixBox server up and running, you can skip this section. TrixBox has Flite preinstalled. For those running an Asterisk@Home 2.x server, here's how to quickly install the Asterisk-specific version of Flite on your system. Log into your server as root and issue the following commands:

cd /root
wget http://nerdvittles.com/aah2/flite-1.3-1.aah.i386.rpm
rpm -ihv flite-1.3-1.aah.i386.rpm
wget http://nerdvittles.com/aah2/app_flite-0.3-1.aah.i386.rpm
rpm -ihv app_flite-0.3-1.aah.i386.rpm
amportal stop
amportal start

Once you complete the installation process above, load the Asterisk Command Line Interface (asterisk -r) while still logged in as root and be sure that the Flite application is loaded:

asterisk -r
show application flite
quit

You should get a response from Asterisk that looks something like the following:

-= Info about application 'Flite' =-

[Synopsis]
Say text to the user, using Flite

[Description]
Flite(text[|intkeys]): This will invoke the Flite TTS engine,
send a text string, get back the resulting waveform and play it to
the user, allowing any given interrupt keys to immediately terminate
and return.

Setting Up the Airport Codes Database. We'll be using a MySQL Airports IATA database and a web-based script to get things going. To install the database and pick your own favorite 1,000 airports, you'll need a version of Asterisk@Home or TrixBox that includes phpMyAdmin as part of the bundle (TrixBox->SystemAdministration->phpMyAdmin) or the Asterisk Management Portal AAH enhancements (AMP->Maintenance->phpMyAdmin). Download the zipped airports file to your desktop and unzip it. This database is licensed pursuant to a GNU General Public License. It was adapted from the AirSort application on SourceForge if you happen to be looking for a MySQL database containing worldwide airport (iata) codes. Now crank up phpMyAdmin in the Maintenance tab of Asterisk@Home or TrixBox. Then click on the SQL Query Window icon in the top of the left frame. Now click on the Import Files tab, select the airports.sql file on your Desktop, set the Character Set to ASCII, and click the Go button. Once the file is imported, click on the Home icon in the left panel, 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, iata 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. Now 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 (iata) are. Because of the layout of the alphabet on touchtone phones, as many as a dozen airport codes may share the identical dialcode which is why we needed the main field to pick one. As mentioned above, we've already picked one for each dialcode, but you may have your own ideas about which airports to make active depending upon where you live and what cities you care about. If you change the main entry for a dialcode, remember to also blank out the previous one for that 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) once the application is finished. Now get your airports database set up in a way that meets your needs so that the dialcodes 000 through 999 select the airport locations of interest to you. Don't mess with the iata codes. Even though the database doesn't enforce it, these are unique and need to stay that way.

In designing this new application for telephone use, it was tempting to support all of the airports in the United States rather than a measily 1,000 of your favorites. To do this, however, would have meant using either zip codes or giving callers a submenu of airport code cities matching a particular dialcode. We've decided to also support zip codes, and we'll cover that in a future column. The dialcode submenu sounded straight-forward enough until we discovered there were some dialcodes with a dozen or more matching airport codes. To force callers to listen to a list of a dozen cities before ever getting to choose a weather report would have been P-A-I-N-F-U-L. So we've opted for the Fab 1,000 thinking that will meet the needs of most folks. As usual, you are free to make changes in the final software to meet your needs. And, if IVR Hell, long phone calls, and 3,500+ airports at the touch of (lots of) buttons are your thing, have at it!

Downloading the nv-weather Application. In addition to the airport codes database, there are two additional pieces to version 2 of the nv-weather application: a PHP/AGI script that does the heavy lifting and an addition to your dialplan which enables you to dial 611 from any phone on your Asterisk system to obtain a weather report. To install the PHP/AGI script, log into your Asterisk server as root and execute the following commands in order:

cd /var/lib/asterisk/agi-bin
wget http://nerdvittles.com/wp-content/nv-weather2.zip
mv nv-weather.php nv-weather.old.php
unzip nv-weather2.zip
rm -f nv-weather2.zip
chmod 775 nv-weather.php
chown asterisk:asterisk nv-weather.php

If you're using version 1 (or later) of TrixBox or if you're using Asterisk@Home and loaded the dialplan script that we covered in Part I of this series, then you've already got the dialplan script so skip the next section.

Dialplan Code. In order to activate extension 611 to answer calls for weather information using Asterisk@Home, you'll need to drop the following code into your dialplan in the [from-internal-custom] context of extensions_custom.conf. For TrixBox fans, you'll find this code already lurking in /etc/asterisk/extensions_trixbox.conf.

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

For versions of AAH before 2.8, once you've added this code using AMP->Maintenance->ConfigEdit->extensions_custom.conf, click the Update button to save your changes, and then reload your Asterisk settings: AMP->Setup->IncomingCalls->SubmitChanges and click the Red Bar. If you'd prefer a different extension (rather than 611), just modify the number in each line of the code above. If you're curious how to pass variables back and forth to a PHP/AGI script, here's a good example. In line 8, we're passing the variable ${APCODE} to the PHP/AGI script with the airport code. In lines 9 and 10, the PHP/AGI script is returning the ${TMPWAVE} variable with the file name of the .wav file containing the weather report.

Housekeeping 101. Temporary files in /tmp get cleaned up by Linux housekeeping automatically. Temporary files stored elsewhere don't. The weather scripts have to store .wav files in the /var/lib/asterisk/sounds path in order to play them from within your dialplan, but it's a good example of how not to design code on busy systems because it places all of the temporary sound files for each reading of these weather reports 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/*

Following our first article, there were a few suggestions on how to automate this with a cron job. Here's the one we like the best. 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 directory every two weeks. If you'd prefer a shorter time, change the number 14 accordingly. If you'd prefer to just delete all these temporary files each night, here's a simplified version you can use:

3 0 * * * root rm -f /var/lib/asterisk/sounds/tts/tts*

Now save your changes: Ctrl-X, Y, then Enter.

Taking nv-weather For A Spin. Once you've installed the airport codes database, the PHP/AGI script, and the dialplan script, you should be good to go. Pick up a phone on your Asterisk system and dial 611. When prompted, key in the three-character airport code for the desired city, and presto! If you'd like to be able to review which airport codes are supported, take a look at Part I of this weather series and download the companion web application. Enjoy!


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.

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!

Be Sociable, Share!

This article has 19 comments

  1. Thank you Ward… I’ve been waiting for this one since the last weather app!
    Keep it up!

  2. Thanks Ward!. Works great! I’m checking the weather in every city I can think of.

  3. Hey Ward, This one works great, any chance of adding something for us north of the border?

  4. Ward, Are all the previous custom changes such as Auto-attendant still usable in TrixBox? Or this that part of your Newbie?ǨѢs Guide to TrixBox, Part XX….? It looks as though many of the new “Features” might change how we use some of the old custom changes.

    [WM: There will have to be some changes with the new freePBX architecture, but we’ll get to them … in between vacations and unpacking boxes. So hang in there.]

  5. Ward, you are great and I have been following your escapades with asteriskathome. I have been trying out your cherms in a hope we shall get broadband one day in Uganda. I need some help with mysql and flite I actually though this post would be of use but It does not do what I exactly want. I need to get records from mysql and pass them to Flite and the caller can hear after dialing a given extension and password. Mysql could have records for example studyno=6672-8921 Age=200 Sex=Female SeroStatus=Negative Name=Wasubire and I want Flite to say something like “your study number is 6672-8921, Age is 200, Sex is female, Sero Status is negative and name is wasubire. This will be great because I need to do some magic on my way to GURUHOOD…

    [WM: Thanks, Wasubire. Look in the php code of the latest weather application. It actually does lookups from MySQL and passes airport code information back to Asterisk for Flite to speak. It’ll give you all you need to get started.]

  6. Great, accept PHP my Admin is missing from the last few versions of Asterisk@Home. It is there if you’ve upgraded to FreePBX from an older version of AMP, but I don’t think it is there at all in AAH 2.8.

  7. Disregard my last post. PHPMyAdmin is still there, but it is hiding.

  8. I live in the St. Louis Area and was having trouble getting the report back. Changed the database citta name to Saint Louis and it worked. Not sure if this affects all other abbreviations or not.

  9. Ward,

    Great heads up on the Comcast Rhapsody!! I had now idea that I that tremendous resource available to me…FOR FREE!!

    Anyway to stream it to my Asterisk box? 🙂

  10. Any way to mod this to use Zip codes instead? This would allow for much more localized weather for those not close to an airport. Also, people don’t always know their airport code, they always know their zip code.

    Great APP! Thanks!

    [WM: Zip code version is on the drawing board. Hope to have it out in a week or two.]

  11. Hi. I’m running 1.1 of Trixbox. I followed your steps exactly (I think) but when I dial 611 from a connected-phone, I get the lovely “BEEP BEEP BEEP…the person you are calling is unavailable…”. Can you suggest what I have to do to correct this?…Thanks in advance…

    [WM: Setup->General Settings->Allow Anonymous Inbound SIP Calls]

  12. Ward-
    Great Stuff! Wish I had half the time you seem to have to hack around.

    How could I use 611 in one of my IVR menus?

    [WM: Look in the IVR drop-downs and you’ll see all of your Misc Destinations including the weather at 611.]

  13. Neat script! I do have a question though, using the instructions from here to set up an IVR, how could i get it to transfer to this improved weather script instead of the other one?

    I tried doing:
    [custom-weather]
    exten => s,1,Answer
    exten => s,2,AGI(nv-weather.php)
    exten => s,3,Hangup

    but the phone just hangs up when I push the extension. Thanks in advance.

  14. Where can I get the app_flite.c file? I want to compile it on Freebsd. I have the flite library installed.

    [WM: I don’t know the answer, but you might contact Francois Aucamp, who did the fantastic job rewriting this module for everyone originally. Be sure to tell him what you’re trying to do. He perhaps can save you tons of wasted energy. Take a look at our original article on Flite first.]

  15. Does anyone know where to get an international airport list?

  16. It seems the weather source is no longer working. when i enter the aiport code after beeb it hangs up. nv-weather.php returing zero results. the *61 weather also seems to not work anymore. the noaa site now states “Anonymous FTP users – FTP is not supported on weather.noaa.gov. As of Monday, January 22, 2007 service has been migrated to the Replacement NWSTG. You should use only tgftp.nws.noaa.gov for anonymous FTP service. weather.noaa.gov will support only HTTP service from this time forward.”

    All the state data folders are empty on the new ftp site are empty. Anyone have ideas where or how to get weather forcasts back?

    [WM: The current versions of our Asterisk Weather applications (airport code and zip code) use HTTP requests to NOAA. What version are you running? Try downloading the current versions.]

  17. I found why it was not working, my password to msql database was differnt.

    i like the options of the airport codes but prefer to dial the *61 weather.agi script as it is a little quicker.

    The problem with the weather.agi script is it seems noaa has chaged the locations of the data files. i found the files here:

    ftp://tgftp.nws.noaa.gov/data/forecasts/zone/

    i redirect the script to location and it works, but the .txt file has a break in the line, creating two lines of text to read back. festive only reads the first line and moves on to next forecast.

    Can this be fixed to read two lines in festival?

    Im using trixbox v2.0

    matt

  18. Do you know if there is something for outside US?

    Thanks

    [WM: Our Worldwide Weather Forecasts should do the trick.]

  19. How can I make it work using sphinx3 instead of Flite?

    Nice project, thanks.