adfa
Dependencies: 4DGL-uLCD-SE NetServices mbed spxml
Fork of weather by
main.cpp@4:fd8c945b8d79, 2016-04-28 (annotated)
- Committer:
- agamemaker
- Date:
- Thu Apr 28 02:21:46 2016 +0000
- Revision:
- 4:fd8c945b8d79
- Parent:
- 3:9e55847f25c4
- Child:
- 5:6c3b0bd1f680
hello;
Who changed what in which revision?
User | Revision | Line number | New 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" |
ashea6 | 2:b14f99568253 | 7 | //#include "NokiaLCD.h" |
ashea6 | 2:b14f99568253 | 8 | #include "uLCD_4DGL.h" |
lballiet91 | 1:c96f140b5710 | 9 | #include <string> |
4180_1 | 0:4a917644acc4 | 10 | // Internet of Things weather display example: LCD displays LA current weather via internet Google API |
4180_1 | 0:4a917644acc4 | 11 | // Adapted for LCD from http://mbed.org/users/hlipka/programs/spxmltest_weather/lif782 |
4180_1 | 0:4a917644acc4 | 12 | |
ashea6 | 2:b14f99568253 | 13 | //NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type |
ashea6 | 2:b14f99568253 | 14 | uLCD_4DGL lcd(p28,p27,p29); |
4180_1 | 0:4a917644acc4 | 15 | EthernetNetIf eth; |
4180_1 | 0:4a917644acc4 | 16 | HTTPClient http; |
4180_1 | 0:4a917644acc4 | 17 | HTTPResult result; |
4180_1 | 0:4a917644acc4 | 18 | bool completed = false; |
agamemaker | 4:fd8c945b8d79 | 19 | void request_callback(HTTPResult r) |
agamemaker | 4:fd8c945b8d79 | 20 | { |
4180_1 | 0:4a917644acc4 | 21 | result = r; |
4180_1 | 0:4a917644acc4 | 22 | completed = true; |
4180_1 | 0:4a917644acc4 | 23 | } |
4180_1 | 0:4a917644acc4 | 24 | |
agamemaker | 4:fd8c945b8d79 | 25 | void parseWeather(SP_XmlElementNode *node, string loc) |
agamemaker | 4:fd8c945b8d79 | 26 | { |
4180_1 | 0:4a917644acc4 | 27 | //extracts current weather XML data fields for LCD |
4180_1 | 0:4a917644acc4 | 28 | SP_XmlHandle handle(node); |
lballiet91 | 1:c96f140b5710 | 29 | SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement(); |
agamemaker | 4:fd8c945b8d79 | 30 | //lcd.cls(); |
lballiet91 | 1:c96f140b5710 | 31 | // Print the name of the city |
4180_1 | 0:4a917644acc4 | 32 | lcd.locate(0,2); |
lballiet91 | 1:c96f140b5710 | 33 | lcd.printf("%s:", loc); |
lballiet91 | 1:c96f140b5710 | 34 | //Print the weather conditions |
lballiet91 | 1:c96f140b5710 | 35 | lcd.locate(0,3); |
lballiet91 | 1:c96f140b5710 | 36 | lcd.printf("%s",condition->getAttrValue("text")); |
lballiet91 | 1:c96f140b5710 | 37 | //Print the termperature (in degrees Celcius) |
lballiet91 | 1:c96f140b5710 | 38 | lcd.locate(0,4); |
agamemaker | 3:9e55847f25c4 | 39 | lcd.printf("%sF",condition->getAttrValue("temp")); |
4180_1 | 0:4a917644acc4 | 40 | } |
4180_1 | 0:4a917644acc4 | 41 | |
agamemaker | 4:fd8c945b8d79 | 42 | void parseTraffic(SP_XmlElementNode *node) |
agamemaker | 4:fd8c945b8d79 | 43 | { |
agamemaker | 4:fd8c945b8d79 | 44 | //extracts current weather XML data fields for LCD |
agamemaker | 4:fd8c945b8d79 | 45 | SP_XmlHandle handle(node); |
agamemaker | 4:fd8c945b8d79 | 46 | SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement(); |
agamemaker | 4:fd8c945b8d79 | 47 | //Print the weather conditions |
agamemaker | 4:fd8c945b8d79 | 48 | lcd.locate(0,5); |
agamemaker | 4:fd8c945b8d79 | 49 | lcd.printf("%s",condition->getAttrValue("realTime")); |
agamemaker | 4:fd8c945b8d79 | 50 | } |
agamemaker | 4:fd8c945b8d79 | 51 | |
agamemaker | 4:fd8c945b8d79 | 52 | int weather() |
agamemaker | 4:fd8c945b8d79 | 53 | { |
agamemaker | 4:fd8c945b8d79 | 54 | 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"; |
4180_1 | 0:4a917644acc4 | 55 | // the eth and HTTP code has be taken directly from the HTTPStream documentation page |
4180_1 | 0:4a917644acc4 | 56 | // see http://mbed.org/cookbook/HTTP-Client-Data-Containers |
agamemaker | 4:fd8c945b8d79 | 57 | //lcd.cls(); |
agamemaker | 4:fd8c945b8d79 | 58 | |
agamemaker | 4:fd8c945b8d79 | 59 | lcd.locate(0,1); |
agamemaker | 4:fd8c945b8d79 | 60 | lcd.printf("Weather"); |
4180_1 | 0:4a917644acc4 | 61 | |
4180_1 | 0:4a917644acc4 | 62 | SP_XmlDomParser parser; |
4180_1 | 0:4a917644acc4 | 63 | HTTPStream stream; |
4180_1 | 0:4a917644acc4 | 64 | |
4180_1 | 0:4a917644acc4 | 65 | char BigBuf[512 + 1] = {0}; |
4180_1 | 0:4a917644acc4 | 66 | stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read |
lballiet91 | 1:c96f140b5710 | 67 | //Yahoo! weather API for selected city - get XML document for parsing |
agamemaker | 4:fd8c945b8d79 | 68 | HTTPResult r = http.get(weather_domain_name, &stream, request_callback); |
4180_1 | 0:4a917644acc4 | 69 | while (!completed) { |
4180_1 | 0:4a917644acc4 | 70 | Net::poll(); //Polls the Networking stack |
4180_1 | 0:4a917644acc4 | 71 | if (stream.readable()) { |
4180_1 | 0:4a917644acc4 | 72 | BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string |
4180_1 | 0:4a917644acc4 | 73 | parser.append( BigBuf, strlen(BigBuf)); // stream current buffer data to the XML parser |
4180_1 | 0:4a917644acc4 | 74 | stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it |
4180_1 | 0:4a917644acc4 | 75 | } |
4180_1 | 0:4a917644acc4 | 76 | } |
4180_1 | 0:4a917644acc4 | 77 | lcd.cls(); |
4180_1 | 0:4a917644acc4 | 78 | lcd.locate(0,2); |
4180_1 | 0:4a917644acc4 | 79 | if (result == HTTP_OK) { |
4180_1 | 0:4a917644acc4 | 80 | lcd.printf(" Read complete"); |
4180_1 | 0:4a917644acc4 | 81 | } else { |
4180_1 | 0:4a917644acc4 | 82 | lcd. printf(" Error %d", result); |
4180_1 | 0:4a917644acc4 | 83 | return -1; |
4180_1 | 0:4a917644acc4 | 84 | } |
4180_1 | 0:4a917644acc4 | 85 | |
4180_1 | 0:4a917644acc4 | 86 | SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() ); |
agamemaker | 3:9e55847f25c4 | 87 | SP_XmlElementNode * child2 = rootHandle.getChild( "results" ).getChild( "channel" ) |
lballiet91 | 1:c96f140b5710 | 88 | .toElement(); |
agamemaker | 4:fd8c945b8d79 | 89 | |
agamemaker | 4:fd8c945b8d79 | 90 | lcd.printf(BigBuf); |
agamemaker | 4:fd8c945b8d79 | 91 | wait(1); |
agamemaker | 4:fd8c945b8d79 | 92 | lcd.cls(); |
agamemaker | 4:fd8c945b8d79 | 93 | |
4180_1 | 0:4a917644acc4 | 94 | if ( child2 ) { |
agamemaker | 3:9e55847f25c4 | 95 | parseWeather(child2, "Atlanta"); //parses XML "current-conditions" info |
4180_1 | 0:4a917644acc4 | 96 | } |
agamemaker | 4:fd8c945b8d79 | 97 | //lcd.printf("help"); |
4180_1 | 0:4a917644acc4 | 98 | if ( NULL != parser.getError() ) { |
4180_1 | 0:4a917644acc4 | 99 | lcd.printf( "\n error: %s\n", parser.getError() ); |
4180_1 | 0:4a917644acc4 | 100 | } |
agamemaker | 4:fd8c945b8d79 | 101 | return 0; |
agamemaker | 4:fd8c945b8d79 | 102 | } |
agamemaker | 4:fd8c945b8d79 | 103 | |
agamemaker | 4:fd8c945b8d79 | 104 | int traffic() |
agamemaker | 4:fd8c945b8d79 | 105 | { |
agamemaker | 4:fd8c945b8d79 | 106 | 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"; |
agamemaker | 4:fd8c945b8d79 | 107 | //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"; |
agamemaker | 4:fd8c945b8d79 | 108 | //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"; |
agamemaker | 4:fd8c945b8d79 | 109 | //char* traf_domain_name; |
agamemaker | 4:fd8c945b8d79 | 110 | //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]; |
agamemaker | 4:fd8c945b8d79 | 111 | // the eth and HTTP code has be taken directly from the HTTPStream documentation page |
agamemaker | 4:fd8c945b8d79 | 112 | // see http://mbed.org/cookbook/HTTP-Client-Data-Containers |
agamemaker | 4:fd8c945b8d79 | 113 | lcd.printf("hello1"); |
agamemaker | 4:fd8c945b8d79 | 114 | traf_domain_name[84] = 38; |
agamemaker | 4:fd8c945b8d79 | 115 | traf_domain_name[95] = 38; |
agamemaker | 4:fd8c945b8d79 | 116 | traf_domain_name[162] = 44; |
agamemaker | 4:fd8c945b8d79 | 117 | traf_domain_name[176] = 44; |
agamemaker | 4:fd8c945b8d79 | 118 | traf_domain_name[162] = 44; |
agamemaker | 4:fd8c945b8d79 | 119 | traf_domain_name[230] = 44; |
agamemaker | 4:fd8c945b8d79 | 120 | traf_domain_name[238] = 44; |
agamemaker | 4:fd8c945b8d79 | 121 | |
agamemaker | 4:fd8c945b8d79 | 122 | lcd.printf("hello2"); |
agamemaker | 4:fd8c945b8d79 | 123 | //lcd.cls(); |
agamemaker | 4:fd8c945b8d79 | 124 | lcd.locate(0,4); |
agamemaker | 4:fd8c945b8d79 | 125 | lcd.printf("Traffic"); |
agamemaker | 4:fd8c945b8d79 | 126 | |
agamemaker | 4:fd8c945b8d79 | 127 | SP_XmlDomParser parser; |
agamemaker | 4:fd8c945b8d79 | 128 | HTTPStream stream; |
agamemaker | 4:fd8c945b8d79 | 129 | lcd.printf("hello3"); |
agamemaker | 4:fd8c945b8d79 | 130 | lcd.printf("%s", traf_domain_name); |
agamemaker | 4:fd8c945b8d79 | 131 | char BigBuf[16384 + 1] = {0}; |
agamemaker | 4:fd8c945b8d79 | 132 | stream.readNext((byte*)BigBuf, 16384 ); //Point to buffer for the first read |
agamemaker | 4:fd8c945b8d79 | 133 | //Yahoo! weather API for selected city - get XML document for parsing |
agamemaker | 4:fd8c945b8d79 | 134 | HTTPResult r = http.get(traf_domain_name, &stream, request_callback); |
agamemaker | 4:fd8c945b8d79 | 135 | while (!completed) { |
agamemaker | 4:fd8c945b8d79 | 136 | Net::poll(); //Polls the Networking stack |
agamemaker | 4:fd8c945b8d79 | 137 | if (stream.readable()) { |
agamemaker | 4:fd8c945b8d79 | 138 | BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string |
agamemaker | 4:fd8c945b8d79 | 139 | parser.append( BigBuf, strlen(BigBuf)); // stream current buffer data to the XML parser |
agamemaker | 4:fd8c945b8d79 | 140 | stream.readNext((byte*)BigBuf, 16384 ); //Buffer has been read, now we can put more data in it |
agamemaker | 4:fd8c945b8d79 | 141 | } |
agamemaker | 4:fd8c945b8d79 | 142 | } |
agamemaker | 4:fd8c945b8d79 | 143 | lcd.locate(0,2); |
agamemaker | 4:fd8c945b8d79 | 144 | if (result == HTTP_OK) { |
agamemaker | 4:fd8c945b8d79 | 145 | lcd.printf(" Read complete"); |
agamemaker | 4:fd8c945b8d79 | 146 | } else { |
agamemaker | 4:fd8c945b8d79 | 147 | lcd. printf(" Error %d", result); |
agamemaker | 4:fd8c945b8d79 | 148 | return -1; |
agamemaker | 4:fd8c945b8d79 | 149 | } |
agamemaker | 4:fd8c945b8d79 | 150 | |
agamemaker | 4:fd8c945b8d79 | 151 | SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() ); |
agamemaker | 4:fd8c945b8d79 | 152 | SP_XmlElementNode * child2 = rootHandle.getChild( "route" ) |
agamemaker | 4:fd8c945b8d79 | 153 | .toElement(); |
agamemaker | 4:fd8c945b8d79 | 154 | |
agamemaker | 4:fd8c945b8d79 | 155 | lcd.printf(BigBuf); |
agamemaker | 4:fd8c945b8d79 | 156 | wait(2); |
agamemaker | 4:fd8c945b8d79 | 157 | lcd.cls(); |
agamemaker | 4:fd8c945b8d79 | 158 | if ( child2 ) { |
agamemaker | 4:fd8c945b8d79 | 159 | parseTraffic(child2); //parses XML "current-conditions" info |
agamemaker | 4:fd8c945b8d79 | 160 | } |
agamemaker | 4:fd8c945b8d79 | 161 | else{ |
agamemaker | 4:fd8c945b8d79 | 162 | lcd.printf("no child"); |
agamemaker | 4:fd8c945b8d79 | 163 | } |
agamemaker | 4:fd8c945b8d79 | 164 | if ( NULL != parser.getError() ) { |
agamemaker | 4:fd8c945b8d79 | 165 | lcd.printf( "\n error: %s\n", parser.getError() ); |
agamemaker | 4:fd8c945b8d79 | 166 | } |
agamemaker | 4:fd8c945b8d79 | 167 | return 0; |
agamemaker | 4:fd8c945b8d79 | 168 | } |
agamemaker | 4:fd8c945b8d79 | 169 | |
agamemaker | 4:fd8c945b8d79 | 170 | int main() |
agamemaker | 4:fd8c945b8d79 | 171 | { |
agamemaker | 4:fd8c945b8d79 | 172 | |
agamemaker | 4:fd8c945b8d79 | 173 | lcd.locate(0,2); |
agamemaker | 4:fd8c945b8d79 | 174 | lcd.printf("net setup"); |
agamemaker | 4:fd8c945b8d79 | 175 | lcd.locate(0,3); |
agamemaker | 4:fd8c945b8d79 | 176 | EthernetErr ethErr = eth.setup(60000); |
agamemaker | 4:fd8c945b8d79 | 177 | if (ethErr) { |
agamemaker | 4:fd8c945b8d79 | 178 | lcd.printf("Error in setup"); |
agamemaker | 4:fd8c945b8d79 | 179 | return -1; |
agamemaker | 4:fd8c945b8d79 | 180 | } |
agamemaker | 4:fd8c945b8d79 | 181 | lcd.printf("net ok"); |
agamemaker | 4:fd8c945b8d79 | 182 | //weather(); |
agamemaker | 4:fd8c945b8d79 | 183 | traffic(); |
agamemaker | 3:9e55847f25c4 | 184 | } |