Displays weather info on text LCD using Google API

Dependencies:   NetServices TextLCD mbed spxml HTTPClient_ToBeRemoved

Files at this revision

API Documentation at this revision

Comitter:
4180_1
Date:
Sat Apr 21 03:45:58 2012 +0000
Commit message:

Changed in this revision

HTTPClient.lib Show annotated file Show diff for this revision Revisions of this file
NetServices.lib Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
spxml.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPClient.lib	Sat Apr 21 03:45:58 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/HTTPClient/#d0be6af2d1db
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NetServices.lib	Sat Apr 21 03:45:58 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/segundo/code/NetServices/#966a0265edfc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Sat Apr 21 03:45:58 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/TextLCD/#44f34c09bd37
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Apr 21 03:45:58 2012 +0000
@@ -0,0 +1,84 @@
+#include "mbed.h"
+#include "EthernetNetIf.h"
+#include "HTTPClient.h"
+#include "spdomparser.hpp"
+#include "spxmlnode.hpp"
+#include "spxmlhandle.hpp"
+#include "TextLCD.h"
+// 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 
+TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d0-d3
+EthernetNetIf eth;
+HTTPClient http;
+HTTPResult result;
+bool completed = false;
+void request_callback(HTTPResult r) {
+    result = r;
+    completed = true;
+}
+
+void parseWeather(SP_XmlElementNode *node) {
+    //extracts current weather XML data fields for LCD 
+    SP_XmlHandle handle(node);
+    SP_XmlElementNode * condition = handle.getChild( "condition" ).toElement();
+    lcd.cls();
+    lcd.printf("LA:");
+    if (condition) {
+        lcd.printf("%s ",condition->getAttrValue("data"));
+    }
+    SP_XmlElementNode * tempf = handle.getChild( "temp_f" ).toElement();
+    if (tempf) {
+        lcd.printf(" %sF\n",tempf->getAttrValue("data"));
+    }
+    SP_XmlElementNode * humidity = handle.getChild( "humidity" ).toElement();
+    if (humidity) {
+        lcd.printf("%s",humidity->getAttrValue("data"));
+    }
+}
+
+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();
+    lcd.printf("net setup");
+    EthernetErr ethErr = eth.setup();
+    if (ethErr) {
+        lcd.printf("\n\rError in setup");
+        return -1;
+    }
+    lcd.printf("\n\r net ok");
+
+    SP_XmlDomParser parser;
+    HTTPStream stream;
+
+    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);
+    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();
+    if (result == HTTP_OK) {
+        lcd.printf(" Read complete\n\r");
+    } else {
+        lcd. printf(" Error %d\n", result);
+        return -1;
+    }
+
+    SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
+    SP_XmlElementNode * child2 = rootHandle.getChild( "weather" )
+                                 .getChild( "current_conditions").toElement();
+    if ( child2 ) {
+        parseWeather(child2); //parses XML "current-conditions" info
+    }
+
+    if ( NULL != parser.getError() ) {
+        lcd.printf( "\n error: %s\n", parser.getError() );
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Apr 21 03:45:58 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spxml.lib	Sat Apr 21 03:45:58 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/hlipka/code/spxml/#3fa97f2c0505