![](/media/cache/group/ep_srxDLMd.webp.50x50_q85.jpg)
10.1 Kombiniert die Übung 6.4 Wenn sich jemand nähert, Lauflicht einschalten mit dem Yahoo Weather Dienst, dass bei Unterwarnungen das Lauflicht eingeschaltet wird.
Dependencies: EthernetInterface mbed-rtos mbed spxml
Fork of YahooWeather by
Diff: main.cpp
- Revision:
- 5:f81113cbe930
- Parent:
- 3:aad64a4b6ff6
- Child:
- 6:735d5412de0d
--- a/main.cpp Wed May 27 08:11:32 2015 +0000 +++ b/main.cpp Wed May 27 15:17:54 2015 +0000 @@ -1,9 +1,12 @@ -/** Beispiel HTTP GET +/** Beispiel Abfrage Yahoo Weather fuer Zuerich */ #include "mbed.h" #include "HTTPClient.h" #include "HTTPText.h" #include "EthernetInterface.h" +#include "spdomparser.hpp" +#include "spxmlnode.hpp" +#include "spxmlhandle.hpp" EthernetInterface eth; // HTTPClient Hilfsklasse @@ -15,25 +18,39 @@ int main() { - printf("HTTP Client - GET\n"); + printf("\tYahoo Weather\n"); eth.init(); eth.connect(); while(1) { myled = 1; - int ret = http.get("http://developer.mbed.org/media/uploads/mbed_official/hello.txt", message, sizeof(message)); + // Yahoo Weather fuer Zuerich + int ret = http.get("http://weather.yahooapis.com/forecastrss?w=784794&u=c", message, sizeof(message)); if ( !ret ) { - printf("Success - read %d characters.\r\n", strlen(message)); - printf("%s\r\n", message); - } + // XML Parser + SP_XmlDomParser parser; + // char[] bzw. XML parsen + parser.append( message, strlen( message ) ); + SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() ); + SP_XmlElementNode * node = rootHandle.getChild( "channel" ).toElement(); + + SP_XmlHandle handle(node); + SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement(); + // Allgemeine Lage, Sonnig, Bewoelkt etc. + printf( "%s, ", condition->getAttrValue("text") ); + // Temperatur in Celsius + printf( "%sC, ", condition->getAttrValue("temp") ); + // Datum und Zeit letzte Aktualisierung + printf( "%s\n", condition->getAttrValue("date") ); + } else printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); - + myled = 0; wait(10); } - eth.disconnect(); + //eth.disconnect(); }