adfa
Dependencies: 4DGL-uLCD-SE NetServices mbed spxml
Fork of weather by
Diff: main.cpp
- Revision:
- 1:c96f140b5710
- Parent:
- 0:4a917644acc4
- Child:
- 2:b14f99568253
--- a/main.cpp Fri May 25 00:57:24 2012 +0000 +++ b/main.cpp Thu Mar 07 21:16:40 2013 +0000 @@ -5,10 +5,13 @@ #include "spxmlnode.hpp" #include "spxmlhandle.hpp" #include "NokiaLCD.h" +#include <string> // Internet of Things weather display example: LCD displays LA current weather via internet Google API // Adapted for LCD from http://mbed.org/users/hlipka/programs/spxmltest_weather/lif782 NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type +DigitalIn but1(p13); +DigitalIn but2(p14); EthernetNetIf eth; HTTPClient http; HTTPResult result; @@ -18,31 +21,65 @@ completed = true; } -void parseWeather(SP_XmlElementNode *node) { +void parseWeather(SP_XmlElementNode *node, string loc) { //extracts current weather XML data fields for LCD SP_XmlHandle handle(node); - SP_XmlElementNode * condition = handle.getChild( "condition" ).toElement(); + SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement(); lcd.cls(); + // Print the name of the city lcd.locate(0,2); - lcd.printf("LA:"); - if (condition) { - lcd.printf("%s ",condition->getAttrValue("data")); - } - SP_XmlElementNode * tempf = handle.getChild( "temp_f" ).toElement(); - if (tempf) { - lcd.printf(" %sF",tempf->getAttrValue("data")); - } - SP_XmlElementNode * humidity = handle.getChild( "humidity" ).toElement(); - if (humidity) { - lcd.locate(0,3); - lcd.printf("%s",humidity->getAttrValue("data")); - } + lcd.printf("%s:", loc); + //Print the weather conditions + lcd.locate(0,3); + lcd.printf("%s",condition->getAttrValue("text")); + //Print the termperature (in degrees Celcius) + lcd.locate(0,4); + lcd.printf("%sC",condition->getAttrValue("temp")); + //Print the date and time + lcd.locate(0,5); + lcd.printf("%s",condition->getAttrValue("date")); } int main() { // the eth and HTTP code has be taken directly from the HTTPStream documentation page // see http://mbed.org/cookbook/HTTP-Client-Data-Containers lcd.cls(); + + // string arrays associating cities with their codes for the yahoo! API + string cities[5] = { "Atlanta", "Houston", "New York", "Seattle", "Sunnyvale" }; + string codes[5] = { "2357024", "2424766", "2459115", "2490383", "2442047" }; + + string head = "http://weather.yahooapis.com/forecastrss?w="; + //Display the cities to choose from + for(int i = 0; i < 5; i++){ + lcd.locate(1, i); + lcd.printf("%i: %s", i+1, cities[i]); + } + + int curr = 0; + int accept = but1, select = but2, pushed = 0; + //This while loop handles selection of cities for display. + while(accept == 0){ + lcd.locate(0, curr); + lcd.printf(" "); + //The next two if statements check to see if the button has been pushed, and then cycles through the selection choices as appropriate. + if(select == 1){ + pushed = 1; + } + if((select == 0)&&(pushed == 1)){ + pushed = 0; + curr = (curr + 1)%5; + } + lcd.locate(0, curr); + lcd.printf(">"); + accept = but1; select = but2; + } + //Concatenate the city code for use by the Yahoo! API + head += codes[curr]; + head += "&u=c"; + + lcd.cls(); + lcd.locate(0,2); lcd.printf("net setup"); lcd.locate(0,3); @@ -58,8 +95,8 @@ char BigBuf[512 + 1] = {0}; stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read - //Google Weather API for Los Angeles - get web page with XML - HTTPResult r = http.get("http://www.google.com/ig/api?weather=Los+Angeles", &stream, request_callback); + //Yahoo! weather API for selected city - get XML document for parsing + HTTPResult r = http.get(head.c_str(), &stream, request_callback); while (!completed) { Net::poll(); //Polls the Networking stack if (stream.readable()) { @@ -78,10 +115,10 @@ } SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() ); - SP_XmlElementNode * child2 = rootHandle.getChild( "weather" ) - .getChild( "current_conditions").toElement(); + SP_XmlElementNode * child2 = rootHandle.getChild( "channel" ) + .toElement(); if ( child2 ) { - parseWeather(child2); //parses XML "current-conditions" info + parseWeather(child2, cities[curr]); //parses XML "current-conditions" info } if ( NULL != parser.getError() ) {