Home » Technology » Weather, Weather Everywhere: Finally, Worldwide Weather Forecasts for Asterisk

The Most Versatile VoIP Provider: FREE PORTING

Weather, Weather Everywhere: Finally, Worldwide Weather Forecasts for Asterisk

Wouldn't it be nice if you could just describe your product and folks would know what you meant? Well, thanks to the lawyers, you can't. So, for all sorts of legal reasons, today's new product will not be called the Asterisk® Weather Station: Whole Earth Edition. We're back from the bunny honeymoon, and we've finally found a source to generate Asterisk weather reports worldwide. We've now written so many weather applications for Asterisk that we're thinking of changing our name to Willard Scott. Ooops! Can't do that either. Whatever you want to call this new application, it was the #1 Request on the Nerd Vittles Wish List, and today it's soup. With all the news organizations in the world, you wouldn't think it would be so difficult to find a source for worldwide weather information, but it's been a twelve-month ordeal. For those of you that thought last week's column on the Nabaztag WiFi Rabbit was a big waste of time, think again. It was the proud owner of a Nabaztag bunny that provided the tip which led to today's worldwide weather application for Asterisk and TrixBox systems. Thanks to Channel 4 in London, the weather forecasts are freely available on the web for thousands of cities around the world. Assuming you visit their web site from time to time and take advantage of some of their "advertising opportunities," we're pretty sure they won't object to your retrieving their forecasts using a telephone from time to time. Just be sure it's for non-commercial use, and we think you're in pretty good shape with their terms of service. This is not legal advice... but you knew that.

Update: Due to changes at Channel4.com, this application no longer works. For a current, working Worldwide Weather application for Asterisk 1.8 using the Google Talk Guru, see this article.

Prerequisites. If you've previously installed one of the Nerd Vittles Weather Applications, then today's installation drill shouldn't take you more than 5 or 10 minutes. If you're new to all of this, then here's a quick list of what you'll need. A TrixBox 1.2.3 server for Windows, an Intel Mac, or Linux is the ideal platform because it includes all of the tools you'll need to be up and running in minutes. Almost any other Asterisk server should work so long as you've installed freePBX, PHP and the Flite voice synthesizer. But you're on your own. If you're a nuts-and-bolts Asterisk guy, then you should be able to decipher what needs to be done by reading through this tutorial. And, yes, the code could be reworked in less than six lines to support Festival or Cepstral if you'd prefer different voices for your weather reports. In fact, we use Cepstral with our Nabaztag/tag bunny, Pat the Nerd, and it works like a champ using an existing Asterisk@Home server. Our bunny tutorial will walk you through the Cepstral installation.

Installation Overview. Assuming you have all the prerequisites in place, today's installation is about a five minute chore. You'll need to download the weather application into the agi-bin directory on your Asterisk system. Then you need to configure the 10 cities you want to support with dial-up weather forecasts. Finally, you'll drop in a little snippet of dialplan code and reload Asterisk. And, you're off to the races. If you want to retrieve weather forecasts for more than 10 cities, the easiest solution is to rework the dialplan code and the program to accept multiple digits for the city code. The number of digits is up to you. Another option is to copy the program to another filename, add another chunk of dialplan code for another extension, and expand your city count to 20, or 30, or whatever. You could even have 10 cities for each continent if that's your thing. The instructions below assume you have a TrixBox 1.2.3 system in place. It works equally well on an Asterisk@Home 1.5 system and most systems in between. If you're using an older Asterisk@Home system, just remember to put the dialplan code in extensions_custom.conf instead of extensions_trixbox.conf, and you'll be just fine.

Installing nv-weather-world.php. Sounds like something out of a Sci-Fi movie, doesn't it. Well, it's not as hard as it sounds. Log into your TrixBox server as root and issue the following commands in order:

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

Configuring Cities for Phone Access to Weather Forecasts. By default, the Worldwide Weather for Asterisk application comes preconfigured to support 10 cities around the world. Here's the list:

  • $query[0]= "http://www.channel4.com/news/weather/world_city.jsp?city=Tokyo&country=Japan&continent=Asia" ;
  • $query[1]= "http://www.channel4.com/news/weather/world_city.jsp?city=Washington&country=United+States+of+America&continent=North+America" ;
  • $query[2]= "http://www.channel4.com/news/weather/world_city.jsp?city=Berlin&country=Germany&continent=Europe" ;
  • $query[3]= "http://www.channel4.com/news/weather/world_city.jsp?city=Florence&country=Italy&continent=Europe" ;
  • $query[4]= "http://www.channel4.com/news/weather/world_city.jsp?city=Gough+Island&country=Tristan+Da+Cunha&continent=Africa" ;
  • $query[5]= "http://www.channel4.com/news/weather/uk_city.jsp?city=London" ;
  • $query[6]= "http://www.channel4.com/news/weather/world_city.jsp?city=Moscow&country=Russia&continent=Europe";
  • $query[7]= "http://www.channel4.com/news/weather/world_city.jsp?city=Sydney&country=Australia&continent=Australasia" ;
  • $query[8]= "http://www.channel4.com/news/weather/world_city.jsp?city=Toronto&country=Canada&continent=North+America" ;
  • $query[9]= "http://www.channel4.com/news/weather/world_city.jsp?city=Zurich&country=Switzerland&continent=Europe" ;

  • You'll notice that the syntax is identical for every entry except London, #5. Cities in the U.K. have a different syntax... because that's Channel 4's home base. That's why. For cities outside the U.K., the syntax is pretty straight-forward. You need a city, a country, and a continent. No spaces in any of the entries. If there's a real space in either a city or country name, then you replace the spaces with plus signs (+). How do you figure out the entries? Well, for most of them, a guess is probably good enough. Cities and countries don't switch continents that often. If you flunked geography, then you can use Channel 4's web site and call up the weather report for the city you're interested in. The answer will be in the browser's address bar.

    While you're displaying a sample weather report from Channel 4, you'll probably notice the little programming challenge that we were up against. Telephones don't (yet) display images of a weather forecast or wind direction too well. Not to worry. We solved it. The only trick was trying to figure out what all 32 weather icons meant. Your guess may be better than ours so feel free to adjust the code to your heart's content. You can make it rain on a sunny day if that's your thing.

    Now where were we? You need to make yourself a list of 10 cities with the link code to the weather reports. You'll notice that our cities happen to match the letters on the telephone except for zero and one. Once you have your city links in hand, edit nv-weather-world.php: nano -w /var/lib/asterisk/agi-bin/nv-weather-world.php. Then cursor down to line 19 and edit the ten entries with the locations you've chosen. Be very careful in editing the code! The http links need to be surrounded with quotes, and each line ends with a semi-colon. Leave everything to the left of the equals sign and the equals sign alone. If you decide to add more than 10, just follow the examples and keep numbering. Before you add any additional lines, scroll down to line 148 and change strlen($code)>1 to the maximum number of digits you plan to use. Save your changes when you're finished: Ctrl-X, Y, then Enter.

    Adding the Dialplan Code Snippet. In order to activate extension 612 to answer calls for your Worldwide Weather Forecasts, 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. If you've decided to support more than 10 cities at a time, then you'll need to change the one in line 6 to a bigger number. Once you get it the way you like it, click the Update button to save your changes.

    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 code for the weather report you wish to retrieve.")
    exten => 612,6,Read(APCODE,beep,1)
    exten => 612,7,Flite("Please hold a moment while we retrieve your report.")
    exten => 612,8,AGI(nv-weather-world.php|${APCODE})
    exten => 612,9,NoOp(Wave file: ${TMPWAVE})
    exten => 612,10,Playback(${TMPWAVE})
    exten => 612,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 WorldWideWeather with 612 as the Dial entry. Save your entry and then click the Red Bar to reload Asterisk.

    Taking the WorldWide Weather Forecaster for a Spin. Now we should be all set. Just pick up an extension on your system and dial 612. You'll be prompted to enter a one-digit code. Punch in 5 and check out the weather forecast for good old London. 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, an Intel Mac, 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.

    Best of Nerd Vittles Link. This application also is now available on our Best of Nerd Vittles site.


    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 200 gigs of disk storage and 2000 gigs (that's 2 terabytes!) 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 still 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.


    Some Recent Nerd Vittles Articles of Interest...


    6 Comments

    1. Accuweather would probably me much more useful (at least here in North America – Channel 4 has a pretty limited list when the closest city I can find that is valid is 100+ miles away)…

      Also, I haven’t installed or looked at the scripts, but is the text readout given in degrees C or F? (given the source data is in C)

      [WM: We’ve already done two separate weather applications for the United States that cover airport codes and zip codes. Accuweather gets their data from the same source as our two applications: the National Weather Service. As for centigrade or fahrenheit, you get both. It’s the same little formula you learned back in 9th grade. Still works, too.]

    2. Hi, first of all i am a newcomer in the world of asterisk and this is a great thing i hope. But i am stuck. I use trixbox-2.6.1.13 and i cannot get any result. Whenever i dial 612, it just hangs up. What i did is: almost everything same as mentioned here except:
      1. i added the dial plan codes in extensions_trixbox.conf : [from-internal-trixbox] …and so on…
      2.for adding the custom destination i went to PBX-> PBX Settings-> Tools-> Custom Destination and then did this:
      Custom Destination: from-internal-trixbox,612,1
      Description: WorldWideWeather

      But nothing happens when i dial 612 from other extensions…. Please can you help me to find the problem…..??????
      Thanks.

    Comments are closed.