Step 6: Customizing It: Umbrella Alarm
Here's how to use it to grab a weather forecast off the Internet and remind you when to grab an umbrella;
1. Find the web page that has the data you want
Small pages work best. For weather forecasts, I surfed around the NOAA site and found this page. This is what I entered;
Start the networking with:
startnetworking(@servername, @uri, @IP, port)
2. Identify the data you want to use
On that page, the data element <pop> is the probability of precipitation for my area. For my Umbrella Alarm, I don't need the whole page - just the value stored in the pair of <pop> tags. The
addfieldmethod is how you identify what parts of the page you want to use in your project. Grab the data between the first pair of <pop> tags with the line;
The first argument,
string("pop"), tells the program that we're looking for data within the <pop> tag. The second argument, @rain, tells the program where the data it finds should be stored. The search always stops when it finds the first tag that matches the criteria.
3. Grab the web page!
Now we know what web page our data is stored on, what data we want to retrieve, and where to store the result. serverrefresh grabs the page and updates the values. Each time we call it, a request is sent to the remote server and the local values are updated with whatever the server returns.
Keep in mind the result is always returned as a text string. If the result should be a number (like the probability of rain), the str2dec method will convert it to a number for you.
This umbrella alarm grabs a web page that includes a weather forecast, parses it and returns the probability of rain. If the probability of rain is greater than 30%, our red LED turns on. Here's the complete program;