adfa
Dependencies: 4DGL-uLCD-SE NetServices mbed spxml
Fork of weather_Nokia_LCD_display_yahoo by
main.cpp@4:22120d759a19, 2016-04-28 (annotated)
- Committer:
- agamemaker
- Date:
- Thu Apr 28 00:16:04 2016 +0000
- Revision:
- 4:22120d759a19
- Parent:
- 3:9e55847f25c4
fa
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; |
4180_1 | 0:4a917644acc4 | 19 | void request_callback(HTTPResult r) { |
4180_1 | 0:4a917644acc4 | 20 | result = r; |
4180_1 | 0:4a917644acc4 | 21 | completed = true; |
4180_1 | 0:4a917644acc4 | 22 | } |
4180_1 | 0:4a917644acc4 | 23 | |
lballiet91 | 1:c96f140b5710 | 24 | void parseWeather(SP_XmlElementNode *node, string loc) { |
4180_1 | 0:4a917644acc4 | 25 | //extracts current weather XML data fields for LCD |
4180_1 | 0:4a917644acc4 | 26 | SP_XmlHandle handle(node); |
lballiet91 | 1:c96f140b5710 | 27 | SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement(); |
4180_1 | 0:4a917644acc4 | 28 | lcd.cls(); |
lballiet91 | 1:c96f140b5710 | 29 | // Print the name of the city |
4180_1 | 0:4a917644acc4 | 30 | lcd.locate(0,2); |
lballiet91 | 1:c96f140b5710 | 31 | lcd.printf("%s:", loc); |
lballiet91 | 1:c96f140b5710 | 32 | //Print the weather conditions |
lballiet91 | 1:c96f140b5710 | 33 | lcd.locate(0,3); |
lballiet91 | 1:c96f140b5710 | 34 | lcd.printf("%s",condition->getAttrValue("text")); |
lballiet91 | 1:c96f140b5710 | 35 | //Print the termperature (in degrees Celcius) |
lballiet91 | 1:c96f140b5710 | 36 | lcd.locate(0,4); |
agamemaker | 3:9e55847f25c4 | 37 | lcd.printf("%sF",condition->getAttrValue("temp")); |
agamemaker | 3:9e55847f25c4 | 38 | /*//Print the date and time |
lballiet91 | 1:c96f140b5710 | 39 | lcd.locate(0,5); |
lballiet91 | 1:c96f140b5710 | 40 | lcd.printf("%s",condition->getAttrValue("date")); |
agamemaker | 3:9e55847f25c4 | 41 | */ |
4180_1 | 0:4a917644acc4 | 42 | } |
4180_1 | 0:4a917644acc4 | 43 | |
agamemaker | 4:22120d759a19 | 44 | int main() { |
4180_1 | 0:4a917644acc4 | 45 | // the eth and HTTP code has be taken directly from the HTTPStream documentation page |
4180_1 | 0:4a917644acc4 | 46 | // see http://mbed.org/cookbook/HTTP-Client-Data-Containers |
4180_1 | 0:4a917644acc4 | 47 | lcd.cls(); |
lballiet91 | 1:c96f140b5710 | 48 | |
4180_1 | 0:4a917644acc4 | 49 | lcd.locate(0,2); |
4180_1 | 0:4a917644acc4 | 50 | lcd.printf("net setup"); |
4180_1 | 0:4a917644acc4 | 51 | lcd.locate(0,3); |
ashea6 | 2:b14f99568253 | 52 | EthernetErr ethErr = eth.setup(60000); |
4180_1 | 0:4a917644acc4 | 53 | if (ethErr) { |
4180_1 | 0:4a917644acc4 | 54 | lcd.printf("Error in setup"); |
4180_1 | 0:4a917644acc4 | 55 | return -1; |
4180_1 | 0:4a917644acc4 | 56 | } |
4180_1 | 0:4a917644acc4 | 57 | lcd.printf("net ok"); |
4180_1 | 0:4a917644acc4 | 58 | |
4180_1 | 0:4a917644acc4 | 59 | SP_XmlDomParser parser; |
4180_1 | 0:4a917644acc4 | 60 | HTTPStream stream; |
4180_1 | 0:4a917644acc4 | 61 | |
4180_1 | 0:4a917644acc4 | 62 | char BigBuf[512 + 1] = {0}; |
4180_1 | 0:4a917644acc4 | 63 | stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read |
lballiet91 | 1:c96f140b5710 | 64 | //Yahoo! weather API for selected city - get XML document for parsing |
ashea6 | 2:b14f99568253 | 65 | HTTPResult r = http.get("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&", &stream, request_callback); |
4180_1 | 0:4a917644acc4 | 66 | while (!completed) { |
4180_1 | 0:4a917644acc4 | 67 | Net::poll(); //Polls the Networking stack |
4180_1 | 0:4a917644acc4 | 68 | if (stream.readable()) { |
4180_1 | 0:4a917644acc4 | 69 | BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string |
4180_1 | 0:4a917644acc4 | 70 | parser.append( BigBuf, strlen(BigBuf)); // stream current buffer data to the XML parser |
4180_1 | 0:4a917644acc4 | 71 | stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it |
4180_1 | 0:4a917644acc4 | 72 | } |
4180_1 | 0:4a917644acc4 | 73 | } |
4180_1 | 0:4a917644acc4 | 74 | lcd.cls(); |
4180_1 | 0:4a917644acc4 | 75 | lcd.locate(0,2); |
4180_1 | 0:4a917644acc4 | 76 | if (result == HTTP_OK) { |
4180_1 | 0:4a917644acc4 | 77 | lcd.printf(" Read complete"); |
4180_1 | 0:4a917644acc4 | 78 | } else { |
4180_1 | 0:4a917644acc4 | 79 | lcd. printf(" Error %d", result); |
4180_1 | 0:4a917644acc4 | 80 | return -1; |
4180_1 | 0:4a917644acc4 | 81 | } |
4180_1 | 0:4a917644acc4 | 82 | |
4180_1 | 0:4a917644acc4 | 83 | SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() ); |
agamemaker | 3:9e55847f25c4 | 84 | SP_XmlElementNode * child2 = rootHandle.getChild( "results" ).getChild( "channel" ) |
lballiet91 | 1:c96f140b5710 | 85 | .toElement(); |
ashea6 | 2:b14f99568253 | 86 | |
agamemaker | 3:9e55847f25c4 | 87 | //lcd.printf(BigBuf); |
ashea6 | 2:b14f99568253 | 88 | |
4180_1 | 0:4a917644acc4 | 89 | if ( child2 ) { |
agamemaker | 3:9e55847f25c4 | 90 | parseWeather(child2, "Atlanta"); //parses XML "current-conditions" info |
4180_1 | 0:4a917644acc4 | 91 | } |
agamemaker | 3:9e55847f25c4 | 92 | |
4180_1 | 0:4a917644acc4 | 93 | if ( NULL != parser.getError() ) { |
4180_1 | 0:4a917644acc4 | 94 | lcd.printf( "\n error: %s\n", parser.getError() ); |
4180_1 | 0:4a917644acc4 | 95 | } |
agamemaker | 3:9e55847f25c4 | 96 | } |