Updated version of Jim Hamblen's Nokia LCD weather App. Updated to suport Yahoo!'s weather API instead of the Google API. Also removed extraneous Libraries.
Dependencies: NetServices mbed spxml
Fork of weather_Nokia_LCD_display by
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetNetIf.h" 00003 #include "HTTPClient.h" 00004 #include "spdomparser.hpp" 00005 #include "spxmlnode.hpp" 00006 #include "spxmlhandle.hpp" 00007 #include "NokiaLCD.h" 00008 #include <string> 00009 // Internet of Things weather display example: LCD displays LA current weather via internet Google API 00010 // Adapted for LCD from http://mbed.org/users/hlipka/programs/spxmltest_weather/lif782 00011 00012 NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type 00013 DigitalIn but1(p13); 00014 DigitalIn but2(p14); 00015 EthernetNetIf eth; 00016 HTTPClient http; 00017 HTTPResult result; 00018 bool completed = false; 00019 void request_callback(HTTPResult r) { 00020 result = r; 00021 completed = true; 00022 } 00023 00024 void parseWeather(SP_XmlElementNode *node, string loc) { 00025 //extracts current weather XML data fields for LCD 00026 SP_XmlHandle handle(node); 00027 SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement(); 00028 lcd.cls(); 00029 // Print the name of the city 00030 lcd.locate(0,2); 00031 lcd.printf("%s:", loc); 00032 //Print the weather conditions 00033 lcd.locate(0,3); 00034 lcd.printf("%s",condition->getAttrValue("text")); 00035 //Print the termperature (in degrees Celcius) 00036 lcd.locate(0,4); 00037 lcd.printf("%sC",condition->getAttrValue("temp")); 00038 //Print the date and time 00039 lcd.locate(0,5); 00040 lcd.printf("%s",condition->getAttrValue("date")); 00041 } 00042 00043 int main() { 00044 // the eth and HTTP code has be taken directly from the HTTPStream documentation page 00045 // see http://mbed.org/cookbook/HTTP-Client-Data-Containers 00046 lcd.cls(); 00047 00048 // string arrays associating cities with their codes for the yahoo! API 00049 string cities[5] = { "Atlanta", "Houston", "New York", "Seattle", "Sunnyvale" }; 00050 string codes[5] = { "2357024", "2424766", "2459115", "2490383", "2442047" }; 00051 00052 string head = "http://weather.yahooapis.com/forecastrss?w="; 00053 //Display the cities to choose from 00054 for(int i = 0; i < 5; i++){ 00055 lcd.locate(1, i); 00056 lcd.printf("%i: %s", i+1, cities[i]); 00057 } 00058 00059 int curr = 0; 00060 int accept = but1, select = but2, pushed = 0; 00061 //This while loop handles selection of cities for display. 00062 while(accept == 0){ 00063 lcd.locate(0, curr); 00064 lcd.printf(" "); 00065 //The next two if statements check to see if the button has been pushed, and then cycles through the selection choices as appropriate. 00066 if(select == 1){ 00067 pushed = 1; 00068 } 00069 if((select == 0)&&(pushed == 1)){ 00070 pushed = 0; 00071 curr = (curr + 1)%5; 00072 } 00073 lcd.locate(0, curr); 00074 lcd.printf(">"); 00075 accept = but1; select = but2; 00076 } 00077 //Concatenate the city code for use by the Yahoo! API 00078 head += codes[curr]; 00079 head += "&u=c"; 00080 00081 lcd.cls(); 00082 00083 lcd.locate(0,2); 00084 lcd.printf("net setup"); 00085 lcd.locate(0,3); 00086 EthernetErr ethErr = eth.setup(); 00087 if (ethErr) { 00088 lcd.printf("Error in setup"); 00089 return -1; 00090 } 00091 lcd.printf("net ok"); 00092 00093 SP_XmlDomParser parser; 00094 HTTPStream stream; 00095 00096 char BigBuf[512 + 1] = {0}; 00097 stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read 00098 //Yahoo! weather API for selected city - get XML document for parsing 00099 HTTPResult r = http.get(head.c_str(), &stream, request_callback); 00100 while (!completed) { 00101 Net::poll(); //Polls the Networking stack 00102 if (stream.readable()) { 00103 BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string 00104 parser.append( BigBuf, strlen(BigBuf)); // stream current buffer data to the XML parser 00105 stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it 00106 } 00107 } 00108 lcd.cls(); 00109 lcd.locate(0,2); 00110 if (result == HTTP_OK) { 00111 lcd.printf(" Read complete"); 00112 } else { 00113 lcd. printf(" Error %d", result); 00114 return -1; 00115 } 00116 00117 SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() ); 00118 SP_XmlElementNode * child2 = rootHandle.getChild( "channel" ) 00119 .toElement(); 00120 if ( child2 ) { 00121 parseWeather(child2, cities[curr]); //parses XML "current-conditions" info 00122 } 00123 00124 if ( NULL != parser.getError() ) { 00125 lcd.printf( "\n error: %s\n", parser.getError() ); 00126 } 00127 }
Generated on Thu Jul 28 2022 03:24:07 by 1.7.2