adfa
Dependencies: 4DGL-uLCD-SE NetServices mbed spxml
Fork of weather by
main.cpp
- Committer:
- agamemaker
- Date:
- 2016-04-28
- Revision:
- 4:fd8c945b8d79
- Parent:
- 3:9e55847f25c4
- Child:
- 5:6c3b0bd1f680
File content as of revision 4:fd8c945b8d79:
#include "mbed.h" #include "EthernetNetIf.h" #include "HTTPClient.h" #include "spdomparser.hpp" #include "spxmlnode.hpp" #include "spxmlhandle.hpp" //#include "NokiaLCD.h" #include "uLCD_4DGL.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 uLCD_4DGL lcd(p28,p27,p29); 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("%sF",condition->getAttrValue("temp")); } void parseTraffic(SP_XmlElementNode *node) { //extracts current weather XML data fields for LCD SP_XmlHandle handle(node); SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement(); //Print the weather conditions lcd.locate(0,5); lcd.printf("%s",condition->getAttrValue("realTime")); } int weather() { char weather_domain_name[512] = "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28select%20woeid%20from%20geo.places%281%29%20where%20text%3D%22atlanta%2C%20ga%22%29%26amp"; // 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.locate(0,1); lcd.printf("Weather"); 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(weather_domain_name, &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( "results" ).getChild( "channel" ) .toElement(); lcd.printf(BigBuf); wait(1); lcd.cls(); if ( child2 ) { parseWeather(child2, "Atlanta"); //parses XML "current-conditions" info } //lcd.printf("help"); if ( NULL != parser.getError() ) { lcd.printf( "\n error: %s\n", parser.getError() ); } return 0; } int traffic() { char traf_domain_name[512] = "https://www.mapquestapi.com/directions/v2/route?key=ZoiBJSzSoqfdNiLD0Z9kZdw4uAN5QUQWzformat=xmlzxml=%3Croute%3E%3Clocations%3E%3Clocation%3E5525%20Vicarage%20WalkzJohns%20Creekz%20GA%3C/location%3E%3Clocation%3E170%205th%20St%20NWzAtlantazGA%3C/location%3E%3C/locations%3E%3C/route%3E"; //char traf_domain_name[512] = "https://www.mapquestapi.com/directions/v2/route?key=ZoiBJSzSoqfdNiLD0Z9kZdw4uAN5QUQW%26format=xml%26xml=%3Croute%3E%3Clocations%3E%3Clocation%3E5525%20Vicarage%20Walk%2CJohns%20Creek%2C%20GA%3C/location%3E%3Clocation%3E170%205th%20St%20NW%2CAtlanta%2CGA%3C/location%3E%3C/locations%3E%3C/route%3E"; //char traf_domain_name[512] = "https://www.mapquestapi.com/directions/v2/route?key=ZoiBJSzSoqfdNiLD0Z9kZdw4uAN5QUQW\&format=xml\&xml=%3Croute%3E%3Clocations%3E%3Clocation%3E5525%20Vicarage%20Walk,Johns%20Creek,%20GA%3C/location%3E%3Clocation%3E170%205th%20St%20NW,Atlanta,GA%3C/location%3E%3C/locations%3E%3C/route%3E"; //char* traf_domain_name; //traf_domain_name = [104, 116, 116, 112, 115, 058, 047, 047, 119, 119, 119, 046, 109, 097, 112, 113, 117, 101, 115, 116, 097, 112, 105, 046, 099, 111, 109, 047, 100, 105, 114, 101, 099, 116, 105, 111, 110, 115, 047, 118, 050, 047, 114, 111, 117, 116, 101, 063, 107, 101, 121, 061, 090, 111, 105, 066, 074, 083, 122, 083, 111, 113, 102, 100, 078, 105, 076, 068, 048, 090, 057, 107, 090, 100, 119, 052, 117, 065, 078, 053, 081, 085, 081, 087, 037, 050, 054, 120, 109, 108, 061, 037, 051, 067, 114, 111, 117, 116, 101, 037, 051, 069, 037, 051, 067, 108, 111, 099, 097, 116, 105, 111, 110, 115, 037, 051, 069, 037, 051, 067, 108, 111, 099, 097, 116, 105, 111, 110, 037, 051, 069, 053, 053, 050, 053, 037, 050, 048, 086, 105, 099, 097, 114, 097, 103, 101, 037, 050, 048, 087, 097, 108, 107, 044, 074, 111, 104, 110, 115, 037, 050, 048, 067, 114, 101, 101, 107, 044, 037, 050, 048, 071, 065, 037, 051, 067, 047, 108, 111, 099, 097, 116, 105, 111, 110, 037, 051, 069, 037, 051, 067, 108, 111, 099, 097, 116, 105, 111, 110, 037, 051, 069, 049, 055, 048, 037, 050, 048, 053, 116, 104, 037, 050, 048, 083, 116, 037, 050, 048, 078, 087, 044, 065, 116, 108, 097, 110, 116, 097, 044, 071, 065, 037, 051, 067, 047, 108, 111, 099, 097, 116, 105, 111, 110, 037, 051, 069, 037, 051, 067, 047, 108, 111, 099, 097, 116, 105, 111, 110, 115, 037, 051, 069, 037, 051, 067, 047, 114, 111, 117, 116, 101, 037, 051, 069, 013 010, 013, 010]; // the eth and HTTP code has be taken directly from the HTTPStream documentation page // see http://mbed.org/cookbook/HTTP-Client-Data-Containers lcd.printf("hello1"); traf_domain_name[84] = 38; traf_domain_name[95] = 38; traf_domain_name[162] = 44; traf_domain_name[176] = 44; traf_domain_name[162] = 44; traf_domain_name[230] = 44; traf_domain_name[238] = 44; lcd.printf("hello2"); //lcd.cls(); lcd.locate(0,4); lcd.printf("Traffic"); SP_XmlDomParser parser; HTTPStream stream; lcd.printf("hello3"); lcd.printf("%s", traf_domain_name); char BigBuf[16384 + 1] = {0}; stream.readNext((byte*)BigBuf, 16384 ); //Point to buffer for the first read //Yahoo! weather API for selected city - get XML document for parsing HTTPResult r = http.get(traf_domain_name, &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, 16384 ); //Buffer has been read, now we can put more data in it } } 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( "route" ) .toElement(); lcd.printf(BigBuf); wait(2); lcd.cls(); if ( child2 ) { parseTraffic(child2); //parses XML "current-conditions" info } else{ lcd.printf("no child"); } if ( NULL != parser.getError() ) { lcd.printf( "\n error: %s\n", parser.getError() ); } return 0; } int main() { lcd.locate(0,2); lcd.printf("net setup"); lcd.locate(0,3); EthernetErr ethErr = eth.setup(60000); if (ethErr) { lcd.printf("Error in setup"); return -1; } lcd.printf("net ok"); //weather(); traffic(); }