working weather

Dependencies:   4DGL-uLCD-SE NetServices mbed spxml

Fork of weather_Nokia_LCD_display_yahoo by Layton Balliet

Committer:
ashea6
Date:
Mon Apr 25 16:22:39 2016 +0000
Revision:
2:b14f99568253
Parent:
1:c96f140b5710
random letters

Who changed what in which revision?

UserRevisionLine numberNew 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);
ashea6 2:b14f99568253 15 DigitalIn but1(p19);
ashea6 2:b14f99568253 16 DigitalIn but2(p20);
4180_1 0:4a917644acc4 17 EthernetNetIf eth;
4180_1 0:4a917644acc4 18 HTTPClient http;
4180_1 0:4a917644acc4 19 HTTPResult result;
4180_1 0:4a917644acc4 20 bool completed = false;
4180_1 0:4a917644acc4 21 void request_callback(HTTPResult r) {
4180_1 0:4a917644acc4 22 result = r;
4180_1 0:4a917644acc4 23 completed = true;
4180_1 0:4a917644acc4 24 }
4180_1 0:4a917644acc4 25
lballiet91 1:c96f140b5710 26 void parseWeather(SP_XmlElementNode *node, string loc) {
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();
4180_1 0:4a917644acc4 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);
lballiet91 1:c96f140b5710 39 lcd.printf("%sC",condition->getAttrValue("temp"));
lballiet91 1:c96f140b5710 40 //Print the date and time
lballiet91 1:c96f140b5710 41 lcd.locate(0,5);
lballiet91 1:c96f140b5710 42 lcd.printf("%s",condition->getAttrValue("date"));
4180_1 0:4a917644acc4 43 }
4180_1 0:4a917644acc4 44
4180_1 0:4a917644acc4 45 int main() {
4180_1 0:4a917644acc4 46 // the eth and HTTP code has be taken directly from the HTTPStream documentation page
4180_1 0:4a917644acc4 47 // see http://mbed.org/cookbook/HTTP-Client-Data-Containers
4180_1 0:4a917644acc4 48 lcd.cls();
lballiet91 1:c96f140b5710 49
lballiet91 1:c96f140b5710 50 // string arrays associating cities with their codes for the yahoo! API
lballiet91 1:c96f140b5710 51 string cities[5] = { "Atlanta", "Houston", "New York", "Seattle", "Sunnyvale" };
lballiet91 1:c96f140b5710 52 string codes[5] = { "2357024", "2424766", "2459115", "2490383", "2442047" };
lballiet91 1:c96f140b5710 53
lballiet91 1:c96f140b5710 54 string head = "http://weather.yahooapis.com/forecastrss?w=";
lballiet91 1:c96f140b5710 55 //Display the cities to choose from
lballiet91 1:c96f140b5710 56 for(int i = 0; i < 5; i++){
lballiet91 1:c96f140b5710 57 lcd.locate(1, i);
lballiet91 1:c96f140b5710 58 lcd.printf("%i: %s", i+1, cities[i]);
lballiet91 1:c96f140b5710 59 }
lballiet91 1:c96f140b5710 60
lballiet91 1:c96f140b5710 61 int curr = 0;
lballiet91 1:c96f140b5710 62 int accept = but1, select = but2, pushed = 0;
lballiet91 1:c96f140b5710 63 //This while loop handles selection of cities for display.
lballiet91 1:c96f140b5710 64 while(accept == 0){
lballiet91 1:c96f140b5710 65 lcd.locate(0, curr);
lballiet91 1:c96f140b5710 66 lcd.printf(" ");
lballiet91 1:c96f140b5710 67 //The next two if statements check to see if the button has been pushed, and then cycles through the selection choices as appropriate.
lballiet91 1:c96f140b5710 68 if(select == 1){
lballiet91 1:c96f140b5710 69 pushed = 1;
lballiet91 1:c96f140b5710 70 }
lballiet91 1:c96f140b5710 71 if((select == 0)&&(pushed == 1)){
lballiet91 1:c96f140b5710 72 pushed = 0;
lballiet91 1:c96f140b5710 73 curr = (curr + 1)%5;
lballiet91 1:c96f140b5710 74 }
lballiet91 1:c96f140b5710 75 lcd.locate(0, curr);
lballiet91 1:c96f140b5710 76 lcd.printf(">");
lballiet91 1:c96f140b5710 77 accept = but1; select = but2;
lballiet91 1:c96f140b5710 78 }
lballiet91 1:c96f140b5710 79 //Concatenate the city code for use by the Yahoo! API
lballiet91 1:c96f140b5710 80 head += codes[curr];
lballiet91 1:c96f140b5710 81 head += "&u=c";
lballiet91 1:c96f140b5710 82
lballiet91 1:c96f140b5710 83 lcd.cls();
lballiet91 1:c96f140b5710 84
4180_1 0:4a917644acc4 85 lcd.locate(0,2);
4180_1 0:4a917644acc4 86 lcd.printf("net setup");
4180_1 0:4a917644acc4 87 lcd.locate(0,3);
ashea6 2:b14f99568253 88 EthernetErr ethErr = eth.setup(60000);
4180_1 0:4a917644acc4 89 if (ethErr) {
4180_1 0:4a917644acc4 90 lcd.printf("Error in setup");
4180_1 0:4a917644acc4 91 return -1;
4180_1 0:4a917644acc4 92 }
4180_1 0:4a917644acc4 93 lcd.printf("net ok");
4180_1 0:4a917644acc4 94
4180_1 0:4a917644acc4 95 SP_XmlDomParser parser;
4180_1 0:4a917644acc4 96 HTTPStream stream;
4180_1 0:4a917644acc4 97
4180_1 0:4a917644acc4 98 char BigBuf[512 + 1] = {0};
4180_1 0:4a917644acc4 99 stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read
lballiet91 1:c96f140b5710 100 //Yahoo! weather API for selected city - get XML document for parsing
ashea6 2:b14f99568253 101 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&amp", &stream, request_callback);
4180_1 0:4a917644acc4 102 while (!completed) {
4180_1 0:4a917644acc4 103 Net::poll(); //Polls the Networking stack
4180_1 0:4a917644acc4 104 if (stream.readable()) {
4180_1 0:4a917644acc4 105 BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
4180_1 0:4a917644acc4 106 parser.append( BigBuf, strlen(BigBuf)); // stream current buffer data to the XML parser
4180_1 0:4a917644acc4 107 stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
4180_1 0:4a917644acc4 108 }
4180_1 0:4a917644acc4 109 }
4180_1 0:4a917644acc4 110 lcd.cls();
4180_1 0:4a917644acc4 111 lcd.locate(0,2);
4180_1 0:4a917644acc4 112 if (result == HTTP_OK) {
4180_1 0:4a917644acc4 113 lcd.printf(" Read complete");
4180_1 0:4a917644acc4 114 } else {
4180_1 0:4a917644acc4 115 lcd. printf(" Error %d", result);
4180_1 0:4a917644acc4 116 return -1;
4180_1 0:4a917644acc4 117 }
4180_1 0:4a917644acc4 118
4180_1 0:4a917644acc4 119 SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
lballiet91 1:c96f140b5710 120 SP_XmlElementNode * child2 = rootHandle.getChild( "channel" )
lballiet91 1:c96f140b5710 121 .toElement();
ashea6 2:b14f99568253 122
ashea6 2:b14f99568253 123 lcd.printf(BigBuf);
ashea6 2:b14f99568253 124
4180_1 0:4a917644acc4 125 if ( child2 ) {
lballiet91 1:c96f140b5710 126 parseWeather(child2, cities[curr]); //parses XML "current-conditions" info
4180_1 0:4a917644acc4 127 }
4180_1 0:4a917644acc4 128
4180_1 0:4a917644acc4 129 if ( NULL != parser.getError() ) {
4180_1 0:4a917644acc4 130 lcd.printf( "\n error: %s\n", parser.getError() );
4180_1 0:4a917644acc4 131 }
4180_1 0:4a917644acc4 132 }