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 jim hamblen

Committer:
lballiet91
Date:
Thu Mar 07 21:16:40 2013 +0000
Revision:
1:c96f140b5710
Parent:
0:4a917644acc4
First publish of fixed code; revision of Jim Hamblen's version to use Yahoo! weather API and removal of extraneous libraries.

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"
4180_1 0:4a917644acc4 7 #include "NokiaLCD.h"
lballiet91 1:c96f140b5710 8 #include <string>
4180_1 0:4a917644acc4 9 // Internet of Things weather display example: LCD displays LA current weather via internet Google API
4180_1 0:4a917644acc4 10 // Adapted for LCD from http://mbed.org/users/hlipka/programs/spxmltest_weather/lif782
4180_1 0:4a917644acc4 11
4180_1 0:4a917644acc4 12 NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
lballiet91 1:c96f140b5710 13 DigitalIn but1(p13);
lballiet91 1:c96f140b5710 14 DigitalIn but2(p14);
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);
lballiet91 1:c96f140b5710 37 lcd.printf("%sC",condition->getAttrValue("temp"));
lballiet91 1:c96f140b5710 38 //Print the date and time
lballiet91 1:c96f140b5710 39 lcd.locate(0,5);
lballiet91 1:c96f140b5710 40 lcd.printf("%s",condition->getAttrValue("date"));
4180_1 0:4a917644acc4 41 }
4180_1 0:4a917644acc4 42
4180_1 0:4a917644acc4 43 int main() {
4180_1 0:4a917644acc4 44 // the eth and HTTP code has be taken directly from the HTTPStream documentation page
4180_1 0:4a917644acc4 45 // see http://mbed.org/cookbook/HTTP-Client-Data-Containers
4180_1 0:4a917644acc4 46 lcd.cls();
lballiet91 1:c96f140b5710 47
lballiet91 1:c96f140b5710 48 // string arrays associating cities with their codes for the yahoo! API
lballiet91 1:c96f140b5710 49 string cities[5] = { "Atlanta", "Houston", "New York", "Seattle", "Sunnyvale" };
lballiet91 1:c96f140b5710 50 string codes[5] = { "2357024", "2424766", "2459115", "2490383", "2442047" };
lballiet91 1:c96f140b5710 51
lballiet91 1:c96f140b5710 52 string head = "http://weather.yahooapis.com/forecastrss?w=";
lballiet91 1:c96f140b5710 53 //Display the cities to choose from
lballiet91 1:c96f140b5710 54 for(int i = 0; i < 5; i++){
lballiet91 1:c96f140b5710 55 lcd.locate(1, i);
lballiet91 1:c96f140b5710 56 lcd.printf("%i: %s", i+1, cities[i]);
lballiet91 1:c96f140b5710 57 }
lballiet91 1:c96f140b5710 58
lballiet91 1:c96f140b5710 59 int curr = 0;
lballiet91 1:c96f140b5710 60 int accept = but1, select = but2, pushed = 0;
lballiet91 1:c96f140b5710 61 //This while loop handles selection of cities for display.
lballiet91 1:c96f140b5710 62 while(accept == 0){
lballiet91 1:c96f140b5710 63 lcd.locate(0, curr);
lballiet91 1:c96f140b5710 64 lcd.printf(" ");
lballiet91 1:c96f140b5710 65 //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 66 if(select == 1){
lballiet91 1:c96f140b5710 67 pushed = 1;
lballiet91 1:c96f140b5710 68 }
lballiet91 1:c96f140b5710 69 if((select == 0)&&(pushed == 1)){
lballiet91 1:c96f140b5710 70 pushed = 0;
lballiet91 1:c96f140b5710 71 curr = (curr + 1)%5;
lballiet91 1:c96f140b5710 72 }
lballiet91 1:c96f140b5710 73 lcd.locate(0, curr);
lballiet91 1:c96f140b5710 74 lcd.printf(">");
lballiet91 1:c96f140b5710 75 accept = but1; select = but2;
lballiet91 1:c96f140b5710 76 }
lballiet91 1:c96f140b5710 77 //Concatenate the city code for use by the Yahoo! API
lballiet91 1:c96f140b5710 78 head += codes[curr];
lballiet91 1:c96f140b5710 79 head += "&u=c";
lballiet91 1:c96f140b5710 80
lballiet91 1:c96f140b5710 81 lcd.cls();
lballiet91 1:c96f140b5710 82
4180_1 0:4a917644acc4 83 lcd.locate(0,2);
4180_1 0:4a917644acc4 84 lcd.printf("net setup");
4180_1 0:4a917644acc4 85 lcd.locate(0,3);
4180_1 0:4a917644acc4 86 EthernetErr ethErr = eth.setup();
4180_1 0:4a917644acc4 87 if (ethErr) {
4180_1 0:4a917644acc4 88 lcd.printf("Error in setup");
4180_1 0:4a917644acc4 89 return -1;
4180_1 0:4a917644acc4 90 }
4180_1 0:4a917644acc4 91 lcd.printf("net ok");
4180_1 0:4a917644acc4 92
4180_1 0:4a917644acc4 93 SP_XmlDomParser parser;
4180_1 0:4a917644acc4 94 HTTPStream stream;
4180_1 0:4a917644acc4 95
4180_1 0:4a917644acc4 96 char BigBuf[512 + 1] = {0};
4180_1 0:4a917644acc4 97 stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read
lballiet91 1:c96f140b5710 98 //Yahoo! weather API for selected city - get XML document for parsing
lballiet91 1:c96f140b5710 99 HTTPResult r = http.get(head.c_str(), &stream, request_callback);
4180_1 0:4a917644acc4 100 while (!completed) {
4180_1 0:4a917644acc4 101 Net::poll(); //Polls the Networking stack
4180_1 0:4a917644acc4 102 if (stream.readable()) {
4180_1 0:4a917644acc4 103 BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string
4180_1 0:4a917644acc4 104 parser.append( BigBuf, strlen(BigBuf)); // stream current buffer data to the XML parser
4180_1 0:4a917644acc4 105 stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it
4180_1 0:4a917644acc4 106 }
4180_1 0:4a917644acc4 107 }
4180_1 0:4a917644acc4 108 lcd.cls();
4180_1 0:4a917644acc4 109 lcd.locate(0,2);
4180_1 0:4a917644acc4 110 if (result == HTTP_OK) {
4180_1 0:4a917644acc4 111 lcd.printf(" Read complete");
4180_1 0:4a917644acc4 112 } else {
4180_1 0:4a917644acc4 113 lcd. printf(" Error %d", result);
4180_1 0:4a917644acc4 114 return -1;
4180_1 0:4a917644acc4 115 }
4180_1 0:4a917644acc4 116
4180_1 0:4a917644acc4 117 SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() );
lballiet91 1:c96f140b5710 118 SP_XmlElementNode * child2 = rootHandle.getChild( "channel" )
lballiet91 1:c96f140b5710 119 .toElement();
4180_1 0:4a917644acc4 120 if ( child2 ) {
lballiet91 1:c96f140b5710 121 parseWeather(child2, cities[curr]); //parses XML "current-conditions" info
4180_1 0:4a917644acc4 122 }
4180_1 0:4a917644acc4 123
4180_1 0:4a917644acc4 124 if ( NULL != parser.getError() ) {
4180_1 0:4a917644acc4 125 lcd.printf( "\n error: %s\n", parser.getError() );
4180_1 0:4a917644acc4 126 }
4180_1 0:4a917644acc4 127 }