Displays weather info on text LCD using Google API

Dependencies:   NetServices TextLCD mbed spxml HTTPClient_ToBeRemoved

Committer:
4180_1
Date:
Sat Apr 21 03:45:58 2012 +0000
Revision:
0:537d849c3d8c

        

Who changed what in which revision?

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