adfa

Dependencies:   4DGL-uLCD-SE NetServices mbed spxml

Fork of weather_Nokia_LCD_display_yahoo by 4180

main.cpp

Committer:
lballiet91
Date:
2013-03-07
Revision:
1:c96f140b5710
Parent:
0:4a917644acc4
Child:
2:b14f99568253

File content as of revision 1:c96f140b5710:

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"
#include "spdomparser.hpp"
#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;
bool completed = false;
void request_callback(HTTPResult r) {
    result = r;
    completed = true;
}

void parseWeather(SP_XmlElementNode *node, string loc) {
    //extracts current weather XML data fields for LCD
    SP_XmlHandle handle(node);
    SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement();
    lcd.cls();
    // Print the name of the city
    lcd.locate(0,2);
    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);
    EthernetErr ethErr = eth.setup();
    if (ethErr) {
        lcd.printf("Error in setup");
        return -1;
    }
    lcd.printf("net ok");

    SP_XmlDomParser parser;
    HTTPStream stream;

    char BigBuf[512 + 1] = {0};
    stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read
    //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()) {
            BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
            parser.append( BigBuf, strlen(BigBuf)); // stream current buffer data to the XML parser
            stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
        }
    }
    lcd.cls();
    lcd.locate(0,2);
    if (result == HTTP_OK) {
        lcd.printf(" Read complete");
    } else {
        lcd. printf(" Error %d", result);
        return -1;
    }

    SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
    SP_XmlElementNode * child2 = rootHandle.getChild( "channel" )
                                 .toElement();
    if ( child2 ) {
        parseWeather(child2, cities[curr]); //parses XML "current-conditions" info
    }

    if ( NULL != parser.getError() ) {
        lcd.printf( "\n error: %s\n", parser.getError() );
    }
}