Updated version of Jim Hamblen's Nokia LCD weather App. Updated to suport Yahoo!'s weather API instead of the Google API. Also removed extraneous Libraries.

Dependencies:   NetServices mbed spxml

Fork of weather_Nokia_LCD_display by jim hamblen

Committer:
4180_1
Date:
Fri May 25 00:57:24 2012 +0000
Revision:
0:4a917644acc4
Child:
1:c96f140b5710

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:4a917644acc4 1 #include "mbed.h"
4180_1 0:4a917644acc4 2 #include "EthernetNetIf.h"
4180_1 0:4a917644acc4 3 #include "HTTPClient.h"
4180_1 0:4a917644acc4 4 #include "spdomparser.hpp"
4180_1 0:4a917644acc4 5 #include "spxmlnode.hpp"
4180_1 0:4a917644acc4 6 #include "spxmlhandle.hpp"
4180_1 0:4a917644acc4 7 #include "NokiaLCD.h"
4180_1 0:4a917644acc4 8 // Internet of Things weather display example: LCD displays LA current weather via internet Google API
4180_1 0:4a917644acc4 9 // Adapted for LCD from http://mbed.org/users/hlipka/programs/spxmltest_weather/lif782
4180_1 0:4a917644acc4 10
4180_1 0:4a917644acc4 11 NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
4180_1 0:4a917644acc4 12 EthernetNetIf eth;
4180_1 0:4a917644acc4 13 HTTPClient http;
4180_1 0:4a917644acc4 14 HTTPResult result;
4180_1 0:4a917644acc4 15 bool completed = false;
4180_1 0:4a917644acc4 16 void request_callback(HTTPResult r) {
4180_1 0:4a917644acc4 17 result = r;
4180_1 0:4a917644acc4 18 completed = true;
4180_1 0:4a917644acc4 19 }
4180_1 0:4a917644acc4 20
4180_1 0:4a917644acc4 21 void parseWeather(SP_XmlElementNode *node) {
4180_1 0:4a917644acc4 22 //extracts current weather XML data fields for LCD
4180_1 0:4a917644acc4 23 SP_XmlHandle handle(node);
4180_1 0:4a917644acc4 24 SP_XmlElementNode * condition = handle.getChild( "condition" ).toElement();
4180_1 0:4a917644acc4 25 lcd.cls();
4180_1 0:4a917644acc4 26 lcd.locate(0,2);
4180_1 0:4a917644acc4 27 lcd.printf("LA:");
4180_1 0:4a917644acc4 28 if (condition) {
4180_1 0:4a917644acc4 29 lcd.printf("%s ",condition->getAttrValue("data"));
4180_1 0:4a917644acc4 30 }
4180_1 0:4a917644acc4 31 SP_XmlElementNode * tempf = handle.getChild( "temp_f" ).toElement();
4180_1 0:4a917644acc4 32 if (tempf) {
4180_1 0:4a917644acc4 33 lcd.printf(" %sF",tempf->getAttrValue("data"));
4180_1 0:4a917644acc4 34 }
4180_1 0:4a917644acc4 35 SP_XmlElementNode * humidity = handle.getChild( "humidity" ).toElement();
4180_1 0:4a917644acc4 36 if (humidity) {
4180_1 0:4a917644acc4 37 lcd.locate(0,3);
4180_1 0:4a917644acc4 38 lcd.printf("%s",humidity->getAttrValue("data"));
4180_1 0:4a917644acc4 39 }
4180_1 0:4a917644acc4 40 }
4180_1 0:4a917644acc4 41
4180_1 0:4a917644acc4 42 int main() {
4180_1 0:4a917644acc4 43 // the eth and HTTP code has be taken directly from the HTTPStream documentation page
4180_1 0:4a917644acc4 44 // see http://mbed.org/cookbook/HTTP-Client-Data-Containers
4180_1 0:4a917644acc4 45 lcd.cls();
4180_1 0:4a917644acc4 46 lcd.locate(0,2);
4180_1 0:4a917644acc4 47 lcd.printf("net setup");
4180_1 0:4a917644acc4 48 lcd.locate(0,3);
4180_1 0:4a917644acc4 49 EthernetErr ethErr = eth.setup();
4180_1 0:4a917644acc4 50 if (ethErr) {
4180_1 0:4a917644acc4 51 lcd.printf("Error in setup");
4180_1 0:4a917644acc4 52 return -1;
4180_1 0:4a917644acc4 53 }
4180_1 0:4a917644acc4 54 lcd.printf("net ok");
4180_1 0:4a917644acc4 55
4180_1 0:4a917644acc4 56 SP_XmlDomParser parser;
4180_1 0:4a917644acc4 57 HTTPStream stream;
4180_1 0:4a917644acc4 58
4180_1 0:4a917644acc4 59 char BigBuf[512 + 1] = {0};
4180_1 0:4a917644acc4 60 stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read
4180_1 0:4a917644acc4 61 //Google Weather API for Los Angeles - get web page with XML
4180_1 0:4a917644acc4 62 HTTPResult r = http.get("http://www.google.com/ig/api?weather=Los+Angeles", &stream, request_callback);
4180_1 0:4a917644acc4 63 while (!completed) {
4180_1 0:4a917644acc4 64 Net::poll(); //Polls the Networking stack
4180_1 0:4a917644acc4 65 if (stream.readable()) {
4180_1 0:4a917644acc4 66 BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
4180_1 0:4a917644acc4 67 parser.append( BigBuf, strlen(BigBuf)); // stream current buffer data to the XML parser
4180_1 0:4a917644acc4 68 stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
4180_1 0:4a917644acc4 69 }
4180_1 0:4a917644acc4 70 }
4180_1 0:4a917644acc4 71 lcd.cls();
4180_1 0:4a917644acc4 72 lcd.locate(0,2);
4180_1 0:4a917644acc4 73 if (result == HTTP_OK) {
4180_1 0:4a917644acc4 74 lcd.printf(" Read complete");
4180_1 0:4a917644acc4 75 } else {
4180_1 0:4a917644acc4 76 lcd. printf(" Error %d", result);
4180_1 0:4a917644acc4 77 return -1;
4180_1 0:4a917644acc4 78 }
4180_1 0:4a917644acc4 79
4180_1 0:4a917644acc4 80 SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
4180_1 0:4a917644acc4 81 SP_XmlElementNode * child2 = rootHandle.getChild( "weather" )
4180_1 0:4a917644acc4 82 .getChild( "current_conditions").toElement();
4180_1 0:4a917644acc4 83 if ( child2 ) {
4180_1 0:4a917644acc4 84 parseWeather(child2); //parses XML "current-conditions" info
4180_1 0:4a917644acc4 85 }
4180_1 0:4a917644acc4 86
4180_1 0:4a917644acc4 87 if ( NULL != parser.getError() ) {
4180_1 0:4a917644acc4 88 lcd.printf( "\n error: %s\n", parser.getError() );
4180_1 0:4a917644acc4 89 }
4180_1 0:4a917644acc4 90 }