Now that we've solved Asterisk weather forecasting for the rest of the world, wouldn't you know our U.S. weather reports would come unglued. One of the problems with depending upon the web data of other organizations (such as the National Weather Service) is that, when they shift gears, you have to make adjustments to your applications as well. So this week is cleanup time at Nerd Vittles. In its infinite wisdom, the National Weather Service decided last week to add latitude, longitude, and elevation information to all of their weather reports. This caused some wrinkles in both the Asterisk® Weather Station by Airport Code and Zip Code applications which didn't know how to interpret this new data. Personally, we were always big supporters of more federal holidays because it meant the bureaucrats and politicians had less time to screw things up. These new "improvements" are a good example of what happens when federal workers have too much time on their hands.

All of the Nerd Vittles weather applications for Asterisk (except last week's Worldwide Weather Forecasts) depend upon NOAA web site information at a web link that looks like the following. You can enter either a zip code or a city and state.

http://www.srh.noaa.gov/port/port_zc.php?inputstring=Charleston,SC

Using MySQL databases, we convert telephone input of airport codes and zip codes into City and State information which then can be passed to NOAA's web link to collect the information that we use to convert the text to speech with the Flite application. If you look at one of the reports, you'll see that the main page display now looks like this:


Charleston, SC
Current Local Conditions at:
Charleston Air Force Base
Lat: 32.91 Lon: -80.03 Elev: 59.055
Last Update: 02/20/07, 08:56 AM EST
Fair: 48 °F

Forecast at a Glance
Detailed 7-day Forecast
Your Local Radar
Current Conditions
Satellite Image
Hazardous Weather
Area Forecast Discussion

Changes in Latitudes. So, Problem #1 is that there's a new Line 4 on the web page with the Latitude, Longitude, and Elevation information. Problem #2 is what to do with it. The two options, of course, are to discard it and leave things the way they were in the Asterisk weather reports or to convert it into intelligible text and play it with the weather report. The addition is a little puzzling quite frankly because it's static information and it really doesn't pertain to the weather other than telling you how close to the equator you are or how close to the sun you live plus or minus 93 million miles. They could just as easily have added the State Bird or State Flower but, no, we have Latitude, Longitude, and Elevation to deal with instead. The good news is that, if you ever want to build an application that computes the distance between two cities, this new information combined with searches for two cities using the NOAA link above will give you everything you need to make the calculation without having to rely upon a ZIP code database with latitude and longitude information (which we've previously given you). So we'll leave that as your homework project. With a little Googling, you can find the formula. And we'll throw in one of our PBX-in-a-Flash drives to the first person that posts a comment with working PHP code that correctly calculates the distance between two U.S. cities using NOAA website data only.

Manually Updating the Weather Applications. We've made the management decision to throw the new data in the proverbial bit bucket, but we also want to show you how to convert the data into a usable format for the one or two folks that just can't live without it. Why are we going to such pains? Because we want to encourage others to build PHP applications for Asterisk that do something useful. And, if you walk through the Nerd Vittles code for these applications, it will give you a good idea how to get started. Looking through the code, you'll notice that we have to parse the web data to actually collect the information we need to "play" a weather report on the telephone. So, to strip out this line of text, there are a couple ways to approach it. One way would be to look for Lat: and then search for the next line break character: <br> and strip out the intervening text. We chose to actually count line breaks after the Current Local Conditions line (above) until we got to the desired string of text:

// new code to delete Latitude, Longitude, and Elevation info from report
$start = strpos($newvalue,"<br>") ;
$start2 = strpos($newvalue,"<br>",$start+1) ;
$start3 = strpos($newvalue,"<br>",$start2+1) ;
$newvalue = substr($newvalue,0,$start2) . " " . substr($newvalue,$start3) ;
// new code ends here

So the new code above needs to be inserted immediately after the line that looks like this:

$newvalue=substr($newvalue, 0, $finish);

This change should be made in both /var/lib/asterisk/agi-bin/nv-weather.php and /var/lib/asterisk/agi-bin/nv-weather-zip.php if you happen to be using both the Weather Station by Airport Code and by Zip Code. And, not to worry, we've fixed the original downloadable code so that newbie's will never know anything changed.

If you'd prefer to actually have the Latitude, Longitude, and Elevation information spoken to callers, then you'd obviously not include the new code above. Instead, we need to add three new parsing instructions after the following line:

$newvalue = str_replace( "NA:", " ", $newvalue );

Here are the new lines you'd insert to translate the NOAA abbreviations into Plain English:

$newvalue = str_replace( "Lat:", "Latitude: ", $newvalue );
$newvalue = str_replace( "Lon:", ". Longitude: ", $newvalue );
$newvalue = str_replace( "Elev:", ". Elevation: ", $newvalue );

Now that we've covered all the theory, it occurred to us that some of you might just like to download the new code. If you prefer that approach, here are the step-by-step instructions for each application. These instructions assume that you've already installed the Weather Applications previously. If not, just choose your desired installation from the Best of Nerd Vittles site.

Installing Weather by Airport Code Update. To install the Weather by Airport Code update, log into your Asterisk server as root and issue the following commands:

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

Installing Weather by Zip Code Update. To install the Weather by Zip Code update, log into your Asterisk server as root and issue the following commands:

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

Test Driving the Latest Asterisk Weather Applications. 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.

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.


Nerd Vittles Demo Hot Line (courtesy of les.net). 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.

Be Sociable, Share!

This article has 8 comments

  1. Kudos on yet another fantastic Asterisk addition, NV crew!

    In a related topic, I had noticed a set of recordings included in the Trixbox sounds that pertain to weather. However, there doesn’t seem to be any application out there that takes advantage of these. Is there a known parser or converter of some sort that can string these together to speak weather conditions using Allison’s voice? Going a step further, is there a parser that can take advantage of the included recorded phoneme groups? Although Flite and especially Cepstral are good and very useful speech synths, it would be neat to put the human-recorded voices to use. Any thoughts?

    Very respectfully,
    Kris

  2. You asked for a distance between 2 cities, here is one… it assumes that there are 2 cities and their web pages have been loaded into the 2 variables $city1value and $city2value. The output is a simple message "Distance x miles". One can obviously elaborate from here.

    The program uses a ‘simple’ great circle algorithm with the ‘assumption’ that the earth has a radius of 3693 miles. If you want kilometers, just change this to the radius in kilometers – 6378 (and the output of ‘miles’ to ‘kilometers’).

    $latpos = strpos($city1value, "Lat: ") + 4;
    $lonpos = strpos($city1value, "Lon: ", $latpos) + 4;
    $elepos = strpos($city1value, "Elev: ", $lonpos) + 5;
    $endpos = strpos($city1value, "", $elepos) + 3;

    $city1lat = substr($city1value, $latpos, $lonpos – $latpos – 4);
    $city1lon = substr($city1value, $lonpos, $elepos – $lonpos – 5);
    $city1ele = substr($city1value, $elepos, $endpos – $elepos – 3);

    $latpos = strpos($city2value, "Lat: ") + 4;
    $lonpos = strpos($city2value, "Lon: ", $latpos) + 4;
    $elepos = strpos($city2value, "Elev: ", $lonpos) + 5;
    $endpos = strpos($city2value, "", $elepos) + 3;

    $city2lat = substr($city2value, $latpos, $lonpos – $latpos – 4);
    $city2lon = substr($city2value, $lonpos, $elepos – $lonpos – 5);
    $city2ele = substr($city2value, $elepos, $endpos – $elepos – 3);

    $radius = 3963; # Earth’s Radius in Miles

    $distance = $radius * acos((sin(deg2rad($city1lat)) * sin(deg2rad($city2lat))) + (cos(deg2rad($city1lat)) * cos(deg2rad($city2lat)) * cos(deg2rad($city2lon) – deg2rad($city1lon))));

    print ‘Distance: ‘. $distance .’miles’;

    [WM: Looks like we already have a winner unless I’ve missed something. Thanks, Glen.]

  3. After a frustrating couple of days of getting long delays followed by ‘fast busy’ after entering the zip code, I finally read through your code and found out why it wasn’t working for me.

    If you’ve changed your root mysql password to increase security, you’ll need to change the password in the php script to match your new one (a search for ‘passw0rd’ will find the entry in the script). Once changed, the script work fine.

    [WM: Unless your Asterisk server is physically vulnerable, there is NO REASON to change the root MySQL password since it only permits access from localhost. As with all software, once you customize your setup, you’ve got to find the wrinkles your setup causes. That’s not a bad thing. But it’s something to consider before making customizations.]

  4. Thanks to you and your web site Nerd Vittles for the fine application(s) and updates. Thanks to Lee Winsor above solving the password problem.
    I also noticed that the weather.agi ‘*61’ in asterisk was not working. So I brought up the web page http://weather.noaa.gov and found that the ftp site for has been moved to "tgftp.nws.noaa.gov". When I change the /var/lib/asterisk/agi-bin/weather.agi file accordingly, all is now working.
    Thanks again for the great applications.

  5. Weather by Zip is broken again. They added more XML tags and data. Should be a simple fix.

    [WM: Yeah. They broke both the Weather by ZIP and Weather by Airport Code apps. Just download and reinstall the nv-weather-zip.zip and nv-weather2.zip files from Best of Nerd Vittles and you’ll be back in business. Thanks for the heads up.]

  6. Is it broken again? I can hear silence for few secs and then it disconnects.

    [WM: Works fine. Check the Asterisk CLI for errors.]

  7. In the CLI I see the following:
    ——————————————
    — Executing AGI("SIP/220-09c04be0″, "nv-weather-zip.php|10118″) in new stack
    — Launched AGI Script /var/lib/asterisk/agi-bin/nv-weather-zip.php
    — AGI Script nv-weather-zip.php completed, returning 0
    — Executing NoOp("SIP/220-09c04be0″, "Wave file: ") in new stack
    — Executing Playback("SIP/220-09c04be0″, "") in new stack
    == Spawn extension (from-internal, 947, 10) exited non-zero on ‘SIP/220-09c04be0’
    — Executing Macro("SIP/220-09c04be0″, "hangupcall") in new stack
    ——————————

    looks like WAV file is missing – Any idea why?

    [WM: Are you sure you installed the original Weather Station by ZIP Code application? You can’t update something that was never installed. Just reinstall the application from the Best of Nerd Vittles site.]

  8. Could someone who has modified the "nv-weather-zip.php" to support the Allison-8khz Cepstral voice post the steps needed?

    Thanks