Dependencies: 4DGL-uLCD-SE NixieTube MBed_Adafruit-GPS-Library NetServices wave_player mbed spxml
Final Project for ECE 4180
main.cpp@1:a6fbd8341289, 2016-12-11 (annotated)
- Committer:
- fischk08
- Date:
- Sun Dec 11 17:19:29 2016 +0000
- Revision:
- 1:a6fbd8341289
- Parent:
- 0:b35237c80489
- Child:
- 2:714d63ea1eef
v2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fischk08 | 0:b35237c80489 | 1 | // Notes https://developer.mbed.org/users/edodm85/notebook/HC-05-bluetooth/ |
fischk08 | 0:b35237c80489 | 2 | // https://developer.mbed.org/cookbook/Bluetooth-Android-Controlled-MBED |
fischk08 | 0:b35237c80489 | 3 | // https://developer.mbed.org/users/mlee350/notebook/adafruit-ultimate-gps-breakout-board/ |
fischk08 | 0:b35237c80489 | 4 | // https://developer.mbed.org/users/4180_1/notebook/ulcd-144-g2-128-by-128-color-lcd/ |
fischk08 | 0:b35237c80489 | 5 | // https://developer.mbed.org/handbook/LocalFileSystem |
fischk08 | 0:b35237c80489 | 6 | |
fischk08 | 0:b35237c80489 | 7 | // Includes Files |
fischk08 | 0:b35237c80489 | 8 | #include "mbed.h" |
fischk08 | 0:b35237c80489 | 9 | #include "rtos.h" |
fischk08 | 0:b35237c80489 | 10 | #include <string> |
fischk08 | 0:b35237c80489 | 11 | #include "uLCD_4DGL.h" |
fischk08 | 0:b35237c80489 | 12 | #include "wave_player.h" |
fischk08 | 0:b35237c80489 | 13 | #include "MBed_Adafruit_GPS.h" // GPS Unit |
fischk08 | 0:b35237c80489 | 14 | #include "spdomparser.hpp" /// Parseing Weather |
fischk08 | 0:b35237c80489 | 15 | #include "spxmlnode.hpp" /// Parseing Weather |
fischk08 | 0:b35237c80489 | 16 | #include "spxmlhandle.hpp" /// Parseing Weather |
fischk08 | 0:b35237c80489 | 17 | #include <TimeUtilities.h> // Used for Clock |
fischk08 | 0:b35237c80489 | 18 | |
fischk08 | 0:b35237c80489 | 19 | #include "EthernetNetIf.h" |
fischk08 | 0:b35237c80489 | 20 | #include "HTTPClient.h" |
fischk08 | 0:b35237c80489 | 21 | #include <string> |
fischk08 | 0:b35237c80489 | 22 | |
fischk08 | 0:b35237c80489 | 23 | |
fischk08 | 0:b35237c80489 | 24 | #include "NixieTube.h" |
fischk08 | 0:b35237c80489 | 25 | NixieTube test(p23,p24,p25,p26); |
fischk08 | 0:b35237c80489 | 26 | /* |
fischk08 | 0:b35237c80489 | 27 | int main() { |
fischk08 | 0:b35237c80489 | 28 | test.start_nixie(); |
fischk08 | 0:b35237c80489 | 29 | test.update_nixie_tube(1,2); |
fischk08 | 0:b35237c80489 | 30 | test.set_dim(1.0,200); |
fischk08 | 0:b35237c80489 | 31 | } |
fischk08 | 0:b35237c80489 | 32 | */ |
fischk08 | 0:b35237c80489 | 33 | |
fischk08 | 0:b35237c80489 | 34 | |
fischk08 | 0:b35237c80489 | 35 | EthernetNetIf eth; |
fischk08 | 0:b35237c80489 | 36 | HTTPClient http; |
fischk08 | 0:b35237c80489 | 37 | HTTPResult result; |
fischk08 | 0:b35237c80489 | 38 | |
fischk08 | 0:b35237c80489 | 39 | LocalFileSystem local("local"); // Create the local filesystem under the name "local" |
fischk08 | 0:b35237c80489 | 40 | AnalogOut DACout(p18); // DAC for producing sound |
fischk08 | 0:b35237c80489 | 41 | wave_player waver(&DACout); // wavplayer object |
fischk08 | 0:b35237c80489 | 42 | Serial * gps_Serial; // GPS Interface |
fischk08 | 0:b35237c80489 | 43 | uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; |
fischk08 | 0:b35237c80489 | 44 | |
fischk08 | 0:b35237c80489 | 45 | //LCD Files Needs to be local to the ulcd screen |
fischk08 | 0:b35237c80489 | 46 | |
fischk08 | 0:b35237c80489 | 47 | // Need To Make a few changes here |
fischk08 | 0:b35237c80489 | 48 | // Weather fucntion to grab local weather data |
fischk08 | 0:b35237c80489 | 49 | // Alarm // |
fischk08 | 0:b35237c80489 | 50 | // Clock need to take time and output it to the nixe tubes hr mins |
fischk08 | 0:b35237c80489 | 51 | // Initilizing? |
fischk08 | 0:b35237c80489 | 52 | // GPS Setup |
fischk08 | 0:b35237c80489 | 53 | // Confirm Link On ULCD Screen |
fischk08 | 0:b35237c80489 | 54 | // Confirm WIFI setup On ULCD Screen |
fischk08 | 0:b35237c80489 | 55 | // Confirm Bluetoth link? display bluetooth decal when connected? Or just Letter B in Blue just as good |
fischk08 | 0:b35237c80489 | 56 | |
fischk08 | 0:b35237c80489 | 57 | // Declarations |
fischk08 | 0:b35237c80489 | 58 | |
fischk08 | 0:b35237c80489 | 59 | bool pressPlay = false;//default on pause (song select mode) |
fischk08 | 0:b35237c80489 | 60 | int volume = 0; |
fischk08 | 0:b35237c80489 | 61 | |
fischk08 | 0:b35237c80489 | 62 | /// Functions will follow here |
fischk08 | 0:b35237c80489 | 63 | void display_screen_debug() { |
fischk08 | 0:b35237c80489 | 64 | } |
fischk08 | 0:b35237c80489 | 65 | |
fischk08 | 0:b35237c80489 | 66 | void system_setup () { |
fischk08 | 0:b35237c80489 | 67 | |
fischk08 | 0:b35237c80489 | 68 | } |
fischk08 | 0:b35237c80489 | 69 | bool completed = false; |
fischk08 | 0:b35237c80489 | 70 | |
fischk08 | 0:b35237c80489 | 71 | |
fischk08 | 0:b35237c80489 | 72 | void display_weather(int code){ |
fischk08 | 0:b35237c80489 | 73 | uLCD.flush_media(); |
fischk08 | 0:b35237c80489 | 74 | uLCD.cls(); |
fischk08 | 0:b35237c80489 | 75 | uLCD.media_init(); |
fischk08 | 0:b35237c80489 | 76 | //uLCD.printf("\n\nAn SD card is needed for image and video data"); |
fischk08 | 0:b35237c80489 | 77 | uLCD.set_sector_address(0x0000,(code*65)); |
fischk08 | 0:b35237c80489 | 78 | uLCD.display_image(0,0); |
fischk08 | 0:b35237c80489 | 79 | uLCD.cls(); |
fischk08 | 0:b35237c80489 | 80 | } |
fischk08 | 0:b35237c80489 | 81 | void request_callback(HTTPResult r) |
fischk08 | 0:b35237c80489 | 82 | { |
fischk08 | 0:b35237c80489 | 83 | result = r; |
fischk08 | 0:b35237c80489 | 84 | completed = true; |
fischk08 | 0:b35237c80489 | 85 | } |
fischk08 | 0:b35237c80489 | 86 | |
fischk08 | 0:b35237c80489 | 87 | void parseWeather(SP_XmlElementNode *node, string loc) |
fischk08 | 0:b35237c80489 | 88 | { |
fischk08 | 0:b35237c80489 | 89 | //extracts current weather XML data fields for LCD |
fischk08 | 0:b35237c80489 | 90 | SP_XmlHandle handle(node); |
fischk08 | 0:b35237c80489 | 91 | SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement(); |
fischk08 | 0:b35237c80489 | 92 | //display Weather conditions |
fischk08 | 0:b35237c80489 | 93 | display_weather((int)condition->getAttrValue("code")); |
fischk08 | 0:b35237c80489 | 94 | // Print the name of the city |
fischk08 | 0:b35237c80489 | 95 | uLCD.locate(0,1); |
fischk08 | 0:b35237c80489 | 96 | uLCD.printf("%s:", loc); |
fischk08 | 0:b35237c80489 | 97 | //Print the weather conditions |
fischk08 | 0:b35237c80489 | 98 | uLCD.locate(0,2); |
fischk08 | 0:b35237c80489 | 99 | uLCD.printf("%s",condition->getAttrValue("text")); |
fischk08 | 0:b35237c80489 | 100 | //Print the termperature (in degrees Celcius) |
fischk08 | 0:b35237c80489 | 101 | uLCD.locate(0,3); |
fischk08 | 0:b35237c80489 | 102 | uLCD.printf("%sF",condition->getAttrValue("temp")); |
fischk08 | 0:b35237c80489 | 103 | } |
fischk08 | 0:b35237c80489 | 104 | // Need To update so that this takes lat longitude gives it to a website to find local time and or time zone |
fischk08 | 0:b35237c80489 | 105 | void parseWhereAmI(SP_XmlElementNode *node, string loc) |
fischk08 | 0:b35237c80489 | 106 | { |
fischk08 | 0:b35237c80489 | 107 | //extracts current weather XML data fields for LCD |
fischk08 | 0:b35237c80489 | 108 | SP_XmlHandle handle(node); |
fischk08 | 0:b35237c80489 | 109 | SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement(); |
fischk08 | 0:b35237c80489 | 110 | //display Weather conditions |
fischk08 | 0:b35237c80489 | 111 | display_weather((int)condition->getAttrValue("code")); |
fischk08 | 0:b35237c80489 | 112 | // Print the name of the city |
fischk08 | 0:b35237c80489 | 113 | uLCD.locate(0,1); |
fischk08 | 0:b35237c80489 | 114 | uLCD.printf("%s:", loc); |
fischk08 | 0:b35237c80489 | 115 | //Print the weather conditions |
fischk08 | 0:b35237c80489 | 116 | uLCD.locate(0,2); |
fischk08 | 0:b35237c80489 | 117 | uLCD.printf("%s",condition->getAttrValue("text")); |
fischk08 | 0:b35237c80489 | 118 | //Print the termperature (in degrees Celcius) |
fischk08 | 0:b35237c80489 | 119 | uLCD.locate(0,3); |
fischk08 | 0:b35237c80489 | 120 | uLCD.printf("%sF",condition->getAttrValue("temp")); |
fischk08 | 0:b35237c80489 | 121 | |
fischk08 | 0:b35237c80489 | 122 | } |
fischk08 | 0:b35237c80489 | 123 | |
fischk08 | 0:b35237c80489 | 124 | // Query Term for Weather |
fischk08 | 0:b35237c80489 | 125 | // select+*+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text%3D%22(33.7490%2C-84.3880)%22) |
fischk08 | 0:b35237c80489 | 126 | // Query Website https://developer.yahoo.com/yql/console/#h=select+*+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text%3D%22(33.7490%2C-84.3880)%22) |
fischk08 | 0:b35237c80489 | 127 | //asuming that outputformat of lattitude and logitude is - for west + for east and does not use chars for directionals as that would be dumb |
fischk08 | 0:b35237c80489 | 128 | //"https://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text%3D%22(33.7490%2C-84.3880)%22)", &stream, request_callback); |
fischk08 | 0:b35237c80489 | 129 | // "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28SELECT%20woeid%20FROM%20geo.places%20WHERE%20text%3D%22(33.7490%2C-84.3880)%22)"(lat log) &stream, request_callback) |
fischk08 | 0:b35237c80489 | 130 | |
fischk08 | 0:b35237c80489 | 131 | // Variable Version |
fischk08 | 0:b35237c80489 | 132 | // "https://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text%3D%22(lat%2Clong)%22)", &stream, request_callback); |
fischk08 | 0:b35237c80489 | 133 | // "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28SELECT%20woeid%20FROM%20geo.places%20WHERE%20text%3D%22(%.6f%2C%.6f)%22)"(lat log) &stream, request_callback) |
fischk08 | 0:b35237c80489 | 134 | |
fischk08 | 0:b35237c80489 | 135 | int weather() |
fischk08 | 0:b35237c80489 | 136 | { |
fischk08 | 0:b35237c80489 | 137 | completed = false; |
fischk08 | 0:b35237c80489 | 138 | SP_XmlDomParser parser; |
fischk08 | 0:b35237c80489 | 139 | HTTPStream stream; |
fischk08 | 0:b35237c80489 | 140 | char buffer [240]; |
fischk08 | 0:b35237c80489 | 141 | int n; |
fischk08 | 0:b35237c80489 | 142 | float lat = 33.7490; |
fischk08 | 0:b35237c80489 | 143 | float log = -84.3880; |
fischk08 | 0:b35237c80489 | 144 | n=sprintf(buffer, "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28SELECT%20woeid%20FROM%20geo.places%20WHERE%20text%3D%22(%.6f%2C%.6f)%22)", lat, log); |
fischk08 | 0:b35237c80489 | 145 | char BigBuf[512 + 1] = {0}; |
fischk08 | 0:b35237c80489 | 146 | uLCD.printf(buffer); |
fischk08 | 0:b35237c80489 | 147 | stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read |
fischk08 | 0:b35237c80489 | 148 | //Yahoo! weather API for selected city - get XML document for parsing |
fischk08 | 0:b35237c80489 | 149 | HTTPResult r = http.get(buffer, &stream, request_callback); // This is wehre the http query for the local weather occurs |
fischk08 | 0:b35237c80489 | 150 | while (!completed) { |
fischk08 | 0:b35237c80489 | 151 | Net::poll(); //Polls the Networking stack |
fischk08 | 0:b35237c80489 | 152 | if (stream.readable()) { |
fischk08 | 0:b35237c80489 | 153 | BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string |
fischk08 | 0:b35237c80489 | 154 | parser.append( BigBuf, strlen(BigBuf)); // stream current buffer data to the XML parser |
fischk08 | 0:b35237c80489 | 155 | stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it |
fischk08 | 0:b35237c80489 | 156 | } |
fischk08 | 0:b35237c80489 | 157 | } |
fischk08 | 0:b35237c80489 | 158 | uLCD.locate(0,6); |
fischk08 | 0:b35237c80489 | 159 | if (result == HTTP_OK) { |
fischk08 | 0:b35237c80489 | 160 | uLCD.printf("Weather complete"); |
fischk08 | 0:b35237c80489 | 161 | } else { |
fischk08 | 0:b35237c80489 | 162 | uLCD. printf("Weather Error %d", result); |
fischk08 | 0:b35237c80489 | 163 | return -1; |
fischk08 | 0:b35237c80489 | 164 | } |
fischk08 | 0:b35237c80489 | 165 | |
fischk08 | 0:b35237c80489 | 166 | SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() ); |
fischk08 | 0:b35237c80489 | 167 | SP_XmlElementNode * child2 = rootHandle.getChild( "results" ).getChild( "channel" ).toElement(); |
fischk08 | 0:b35237c80489 | 168 | |
fischk08 | 0:b35237c80489 | 169 | //uLCD.printf(BigBuf); |
fischk08 | 0:b35237c80489 | 170 | |
fischk08 | 0:b35237c80489 | 171 | if ( child2 ) { |
fischk08 | 0:b35237c80489 | 172 | parseWeather(child2, "Atlanta"); //parses XML "current-conditions" info |
fischk08 | 0:b35237c80489 | 173 | } |
fischk08 | 0:b35237c80489 | 174 | |
fischk08 | 0:b35237c80489 | 175 | if ( NULL != parser.getError() ) { |
fischk08 | 0:b35237c80489 | 176 | uLCD.printf( "\n error: %s\n", parser.getError() ); |
fischk08 | 0:b35237c80489 | 177 | } |
fischk08 | 0:b35237c80489 | 178 | return 0; |
fischk08 | 0:b35237c80489 | 179 | } |
fischk08 | 0:b35237c80489 | 180 | |
fischk08 | 0:b35237c80489 | 181 | |
fischk08 | 0:b35237c80489 | 182 | // Need to update so that if it failes to get a GPS fix after say 10 tries it defaults lat and longitude values and time is found thought another method. |
fischk08 | 0:b35237c80489 | 183 | bool gps_setup(){ |
fischk08 | 0:b35237c80489 | 184 | Timer refresh_Timer; //sets up a timer for use in loop; how often do we print GPS info? |
fischk08 | 0:b35237c80489 | 185 | const int refresh_Time = 2000; //refresh time in ms |
fischk08 | 0:b35237c80489 | 186 | bool gps_off = true; |
fischk08 | 0:b35237c80489 | 187 | char c; |
fischk08 | 0:b35237c80489 | 188 | gps_Serial = new Serial(p9,p10); //serial object for use w/ GPS tx rx |
fischk08 | 0:b35237c80489 | 189 | Adafruit_GPS myGPS(gps_Serial); //object of Adafruit's GPS class |
fischk08 | 0:b35237c80489 | 190 | myGPS.begin(9600); //sets baud rate for GPS communication; note this may be changed via Adafruit_GPS::sendCommand(char *) |
fischk08 | 0:b35237c80489 | 191 | myGPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //these commands are defined in MBed_Adafruit_GPS.h; a link is provided there for command creation |
fischk08 | 0:b35237c80489 | 192 | myGPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); |
fischk08 | 0:b35237c80489 | 193 | myGPS.sendCommand(PGCMD_ANTENNA); |
fischk08 | 0:b35237c80489 | 194 | refresh_Timer.start(); //starts the clock on the timer |
fischk08 | 0:b35237c80489 | 195 | uLCD.printf("Starting Up\n"); |
fischk08 | 0:b35237c80489 | 196 | while(gps_off){ |
fischk08 | 0:b35237c80489 | 197 | uLCD.cls(); |
fischk08 | 0:b35237c80489 | 198 | c = myGPS.read(); //queries the GPS |
fischk08 | 0:b35237c80489 | 199 | if (c) { uLCD.printf("%c", c); } //this line will echo the GPS data if not paused |
fischk08 | 0:b35237c80489 | 200 | //check if we recieved a new message from GPS, if so, attempt to parse it, |
fischk08 | 0:b35237c80489 | 201 | if ( myGPS.newNMEAreceived() ) { |
fischk08 | 0:b35237c80489 | 202 | if ( !myGPS.parse(myGPS.lastNMEA()) ) { |
fischk08 | 0:b35237c80489 | 203 | continue; |
fischk08 | 0:b35237c80489 | 204 | } |
fischk08 | 0:b35237c80489 | 205 | } |
fischk08 | 0:b35237c80489 | 206 | //check if enough time has passed to warrant printing GPS info to screen |
fischk08 | 0:b35237c80489 | 207 | //note if refresh_Time is too low or pc.baud is too low, GPS data may be lost during printing |
fischk08 | 0:b35237c80489 | 208 | if (refresh_Timer.read_ms() >= refresh_Time) { |
fischk08 | 0:b35237c80489 | 209 | refresh_Timer.reset(); |
fischk08 | 0:b35237c80489 | 210 | uLCD.printf("Fix: %d\n", (int) myGPS.fix); |
fischk08 | 0:b35237c80489 | 211 | uLCD.printf("Quality: %d\n", (int) myGPS.fixquality); |
fischk08 | 0:b35237c80489 | 212 | if (myGPS.fix) { |
fischk08 | 0:b35237c80489 | 213 | uLCD.printf("Time: %d:%d:%d.%u\n", myGPS.hour, myGPS.minute, myGPS.seconds, myGPS.milliseconds); |
fischk08 | 0:b35237c80489 | 214 | uLCD.printf("Date: %d/%d/20%d\n", myGPS.day, myGPS.month, myGPS.year); |
fischk08 | 0:b35237c80489 | 215 | uLCD.printf("Location: %5.2f%c, %5.2f%c\n", myGPS.latitude, myGPS.lat, myGPS.longitude, myGPS.lon); |
fischk08 | 0:b35237c80489 | 216 | uLCD.printf("Speed: %5.2f knots\n", myGPS.speed); |
fischk08 | 0:b35237c80489 | 217 | uLCD.printf("Angle: %5.2f\n", myGPS.angle); |
fischk08 | 0:b35237c80489 | 218 | uLCD.printf("Altitude: %5.2f\n", myGPS.altitude); |
fischk08 | 0:b35237c80489 | 219 | uLCD.printf("Satellites: %d\n", myGPS.satellites); |
fischk08 | 0:b35237c80489 | 220 | gps_off = false; |
fischk08 | 0:b35237c80489 | 221 | } |
fischk08 | 0:b35237c80489 | 222 | } |
fischk08 | 0:b35237c80489 | 223 | } |
fischk08 | 0:b35237c80489 | 224 | return true; |
fischk08 | 0:b35237c80489 | 225 | } |
fischk08 | 0:b35237c80489 | 226 | |
fischk08 | 0:b35237c80489 | 227 | void sound_alarm(int volume) { |
fischk08 | 0:b35237c80489 | 228 | pressPlay = true; |
fischk08 | 0:b35237c80489 | 229 | FILE *wave_file = fopen("/local/alarm.wav","r"); |
fischk08 | 0:b35237c80489 | 230 | waver.play(wave_file,&pressPlay, &volume); //play(FILE *wavefile,bool *pressPlay, int *volume); |
fischk08 | 0:b35237c80489 | 231 | pressPlay = false; |
fischk08 | 0:b35237c80489 | 232 | fclose(wave_file); |
fischk08 | 0:b35237c80489 | 233 | } |
fischk08 | 0:b35237c80489 | 234 | |
fischk08 | 0:b35237c80489 | 235 | RealTimeClock rtc; |
fischk08 | 0:b35237c80489 | 236 | |
fischk08 | 0:b35237c80489 | 237 | void main_test() { |
fischk08 | 0:b35237c80489 | 238 | bool success; |
fischk08 | 0:b35237c80489 | 239 | char TimeTest[] = "05/29/2011 14:15:18 (-6:00)"; |
fischk08 | 0:b35237c80489 | 240 | char buf[50]; |
fischk08 | 0:b35237c80489 | 241 | success = rtc.SetTime(TimeTest); |
fischk08 | 0:b35237c80489 | 242 | rtc.GetTimeString(buf); |
fischk08 | 0:b35237c80489 | 243 | uLCD.printf("success: %i, time: %s\n", success, buf); |
fischk08 | 0:b35237c80489 | 244 | } |
fischk08 | 0:b35237c80489 | 245 | |
fischk08 | 0:b35237c80489 | 246 | int main() { |
fischk08 | 0:b35237c80489 | 247 | //sound_alarm(1); |
fischk08 | 0:b35237c80489 | 248 | //display_weather(0); |
fischk08 | 0:b35237c80489 | 249 | |
fischk08 | 0:b35237c80489 | 250 | |
fischk08 | 0:b35237c80489 | 251 | } |
fischk08 | 1:a6fbd8341289 | 252 | |
fischk08 | 1:a6fbd8341289 | 253 | |
fischk08 | 1:a6fbd8341289 | 254 | |
fischk08 | 1:a6fbd8341289 | 255 | //////////////////////////////////////////////// |
fischk08 | 1:a6fbd8341289 | 256 | |
fischk08 | 1:a6fbd8341289 | 257 | #include "mbed.h" |
fischk08 | 1:a6fbd8341289 | 258 | #include "EthernetNetIf.h" |
fischk08 | 1:a6fbd8341289 | 259 | #include "HTTPClient.h" |
fischk08 | 1:a6fbd8341289 | 260 | #include "spdomparser.hpp" |
fischk08 | 1:a6fbd8341289 | 261 | #include "spxmlnode.hpp" |
fischk08 | 1:a6fbd8341289 | 262 | #include "spxmlhandle.hpp" |
fischk08 | 1:a6fbd8341289 | 263 | #include <string> |
fischk08 | 1:a6fbd8341289 | 264 | EthernetNetIf eth; |
fischk08 | 1:a6fbd8341289 | 265 | Serial pc(USBTX, USBRX); |
fischk08 | 1:a6fbd8341289 | 266 | HTTPClient http; |
fischk08 | 1:a6fbd8341289 | 267 | HTTPResult result; |
fischk08 | 1:a6fbd8341289 | 268 | |
fischk08 | 1:a6fbd8341289 | 269 | bool completed = false; |
fischk08 | 1:a6fbd8341289 | 270 | void request_callback(HTTPResult r) |
fischk08 | 1:a6fbd8341289 | 271 | { |
fischk08 | 1:a6fbd8341289 | 272 | result = r; |
fischk08 | 1:a6fbd8341289 | 273 | completed = true; |
fischk08 | 1:a6fbd8341289 | 274 | } |
fischk08 | 1:a6fbd8341289 | 275 | |
fischk08 | 1:a6fbd8341289 | 276 | void parseWeather(SP_XmlElementNode *node, string loc) |
fischk08 | 1:a6fbd8341289 | 277 | { |
fischk08 | 1:a6fbd8341289 | 278 | //extracts current weather XML data fields for LCD |
fischk08 | 1:a6fbd8341289 | 279 | SP_XmlHandle handle(node); |
fischk08 | 1:a6fbd8341289 | 280 | SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement(); |
fischk08 | 1:a6fbd8341289 | 281 | //display Weather conditions |
fischk08 | 1:a6fbd8341289 | 282 | pc.printf("%d:", (int)condition->getAttrValue("code")); |
fischk08 | 1:a6fbd8341289 | 283 | // Print the name of the city |
fischk08 | 1:a6fbd8341289 | 284 | pc.printf("%s:", loc); |
fischk08 | 1:a6fbd8341289 | 285 | //Print the weather conditions |
fischk08 | 1:a6fbd8341289 | 286 | pc.printf("%s",condition->getAttrValue("text")); |
fischk08 | 1:a6fbd8341289 | 287 | //Print the termperature (in degrees Celcius) |
fischk08 | 1:a6fbd8341289 | 288 | pc.printf("%sF",condition->getAttrValue("temp")); |
fischk08 | 1:a6fbd8341289 | 289 | } |
fischk08 | 1:a6fbd8341289 | 290 | // Need To update so that this takes lat longitude gives it to a website to find local time and or time zone |
fischk08 | 1:a6fbd8341289 | 291 | void parseWhereAmI(SP_XmlElementNode *node, string loc) |
fischk08 | 1:a6fbd8341289 | 292 | { |
fischk08 | 1:a6fbd8341289 | 293 | //extracts current weather XML data fields for LCD |
fischk08 | 1:a6fbd8341289 | 294 | SP_XmlHandle handle(node); |
fischk08 | 1:a6fbd8341289 | 295 | SP_XmlElementNode * condition = handle.getChild( "item" ).getChild("yweather:condition").toElement(); |
fischk08 | 1:a6fbd8341289 | 296 | //display Weather conditions |
fischk08 | 1:a6fbd8341289 | 297 | pc.printf("%d:", (int)condition->getAttrValue("code")); |
fischk08 | 1:a6fbd8341289 | 298 | // Print the name of the city |
fischk08 | 1:a6fbd8341289 | 299 | pc.printf("%s:", loc); |
fischk08 | 1:a6fbd8341289 | 300 | //Print the weather conditions |
fischk08 | 1:a6fbd8341289 | 301 | pc.printf("%s",condition->getAttrValue("text")); |
fischk08 | 1:a6fbd8341289 | 302 | //Print the termperature (in degrees Celcius) |
fischk08 | 1:a6fbd8341289 | 303 | pc.printf("%sF",condition->getAttrValue("temp")); |
fischk08 | 1:a6fbd8341289 | 304 | |
fischk08 | 1:a6fbd8341289 | 305 | } |
fischk08 | 1:a6fbd8341289 | 306 | |
fischk08 | 1:a6fbd8341289 | 307 | // Query Term for Weather |
fischk08 | 1:a6fbd8341289 | 308 | // select+*+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text%3D%22(33.7490%2C-84.3880)%22) |
fischk08 | 1:a6fbd8341289 | 309 | // Query Website https://developer.yahoo.com/yql/console/#h=select+*+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text%3D%22(33.7490%2C-84.3880)%22) |
fischk08 | 1:a6fbd8341289 | 310 | //asuming that outputformat of lattitude and logitude is - for west + for east and does not use chars for directionals as that would be dumb |
fischk08 | 1:a6fbd8341289 | 311 | //"https://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text%3D%22(33.7490%2C-84.3880)%22)", &stream, request_callback); |
fischk08 | 1:a6fbd8341289 | 312 | // "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28SELECT%20woeid%20FROM%20geo.places%20WHERE%20text%3D%22(33.7490%2C-84.3880)%22)"(lat log) &stream, request_callback) |
fischk08 | 1:a6fbd8341289 | 313 | |
fischk08 | 1:a6fbd8341289 | 314 | // Variable Version |
fischk08 | 1:a6fbd8341289 | 315 | // "https://query.yahooapis.com/v1/public/yql?q=select+*+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text%3D%22(lat%2Clong)%22)", &stream, request_callback); |
fischk08 | 1:a6fbd8341289 | 316 | // "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28SELECT%20woeid%20FROM%20geo.places%20WHERE%20text%3D%22(%.6f%2C%.6f)%22)"(lat log) &stream, request_callback) |
fischk08 | 1:a6fbd8341289 | 317 | |
fischk08 | 1:a6fbd8341289 | 318 | int weather() |
fischk08 | 1:a6fbd8341289 | 319 | { |
fischk08 | 1:a6fbd8341289 | 320 | completed = false; |
fischk08 | 1:a6fbd8341289 | 321 | SP_XmlDomParser parser; |
fischk08 | 1:a6fbd8341289 | 322 | HTTPStream stream; |
fischk08 | 1:a6fbd8341289 | 323 | char buffer3 [200]; |
fischk08 | 1:a6fbd8341289 | 324 | float lattitude = 33.7490; |
fischk08 | 1:a6fbd8341289 | 325 | float longitude = -84.3880; |
fischk08 | 1:a6fbd8341289 | 326 | wait(1); |
fischk08 | 1:a6fbd8341289 | 327 | pc.printf("%.6f:",lattitude); |
fischk08 | 1:a6fbd8341289 | 328 | pc.printf("%.6f:",longitude); |
fischk08 | 1:a6fbd8341289 | 329 | int n2; |
fischk08 | 1:a6fbd8341289 | 330 | char post[] = "https://query.yahooapis.com/v1/public/yql?q=select+item.condition+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text='("; |
fischk08 | 1:a6fbd8341289 | 331 | char msg[10] = ")')"; |
fischk08 | 1:a6fbd8341289 | 332 | n2 = sprintf(buffer3, "%.6f,%.6f", lattitude, longitude); |
fischk08 | 1:a6fbd8341289 | 333 | char* temppostRequest = strcat(post,buffer3); |
fischk08 | 1:a6fbd8341289 | 334 | char* postRequest = strcat(temppostRequest,msg); |
fischk08 | 1:a6fbd8341289 | 335 | pc.printf(post); |
fischk08 | 1:a6fbd8341289 | 336 | pc.printf(msg); |
fischk08 | 1:a6fbd8341289 | 337 | pc.printf(temppostRequest); |
fischk08 | 1:a6fbd8341289 | 338 | pc.printf("Reading\r\n"); |
fischk08 | 1:a6fbd8341289 | 339 | pc.printf(postRequest); |
fischk08 | 1:a6fbd8341289 | 340 | wait(1); |
fischk08 | 1:a6fbd8341289 | 341 | //n=sprintf(buffer, "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28SELECT%20woeid%20FROM%20geo.places%20WHERE%20text%3D%22(%.6f%2C%.6f)%22)", lat, log); |
fischk08 | 1:a6fbd8341289 | 342 | char BigBuf[512 + 1] = {0}; |
fischk08 | 1:a6fbd8341289 | 343 | wait(1); |
fischk08 | 1:a6fbd8341289 | 344 | stream.readNext((byte*)BigBuf, 512); //Point to buffer for the first read |
fischk08 | 1:a6fbd8341289 | 345 | //Yahoo! weather API for selected city - get XML document for parsing |
fischk08 | 1:a6fbd8341289 | 346 | //HTTPResult r = http.get("https://query.yahooapis.com/v1/public/yql?q=select+item.condition+from+weather.forecast+where+woeid+in+(SELECT+woeid+FROM+geo.places+WHERE+text='(33.7490,-84.3880)')", &stream, request_callback); |
fischk08 | 1:a6fbd8341289 | 347 | HTTPResult r = http.get(postRequest, &stream, request_callback); // This is wehre the http query for the local weather occurs |
fischk08 | 1:a6fbd8341289 | 348 | while (!completed) { |
fischk08 | 1:a6fbd8341289 | 349 | Net::poll(); //Polls the Networking stack |
fischk08 | 1:a6fbd8341289 | 350 | if (stream.readable()) { |
fischk08 | 1:a6fbd8341289 | 351 | BigBuf[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string |
fischk08 | 1:a6fbd8341289 | 352 | parser.append( BigBuf, strlen(BigBuf)); // stream current buffer data to the XML parser |
fischk08 | 1:a6fbd8341289 | 353 | stream.readNext((byte*)BigBuf, 512); //Buffer has been read, now we can put more data in it |
fischk08 | 1:a6fbd8341289 | 354 | } |
fischk08 | 1:a6fbd8341289 | 355 | } |
fischk08 | 1:a6fbd8341289 | 356 | if (result == HTTP_OK) { |
fischk08 | 1:a6fbd8341289 | 357 | pc.printf("Weather complete"); |
fischk08 | 1:a6fbd8341289 | 358 | } else { |
fischk08 | 1:a6fbd8341289 | 359 | pc. printf("Weather Error %d", result); |
fischk08 | 1:a6fbd8341289 | 360 | return -1; |
fischk08 | 1:a6fbd8341289 | 361 | } |
fischk08 | 1:a6fbd8341289 | 362 | |
fischk08 | 1:a6fbd8341289 | 363 | SP_XmlHandle rootHandle( parser.getDocument()->getRootElement() ); |
fischk08 | 1:a6fbd8341289 | 364 | SP_XmlElementNode * child2 = rootHandle.getChild( "results" ).getChild( "channel" ).toElement(); |
fischk08 | 1:a6fbd8341289 | 365 | |
fischk08 | 1:a6fbd8341289 | 366 | //pc.printf(BigBuf); |
fischk08 | 1:a6fbd8341289 | 367 | |
fischk08 | 1:a6fbd8341289 | 368 | if ( child2 ) { |
fischk08 | 1:a6fbd8341289 | 369 | parseWeather(child2, "Atlanta"); //parses XML "current-conditions" info |
fischk08 | 1:a6fbd8341289 | 370 | } |
fischk08 | 1:a6fbd8341289 | 371 | |
fischk08 | 1:a6fbd8341289 | 372 | if ( NULL != parser.getError() ) { |
fischk08 | 1:a6fbd8341289 | 373 | pc.printf( "\n error: %s\n", parser.getError() ); |
fischk08 | 1:a6fbd8341289 | 374 | } |
fischk08 | 1:a6fbd8341289 | 375 | return 0; |
fischk08 | 1:a6fbd8341289 | 376 | } |
fischk08 | 0:b35237c80489 | 377 | |
fischk08 | 0:b35237c80489 | 378 | |
fischk08 | 1:a6fbd8341289 | 379 | int main() |
fischk08 | 1:a6fbd8341289 | 380 | { |
fischk08 | 1:a6fbd8341289 | 381 | pc.baud(9600); |
fischk08 | 1:a6fbd8341289 | 382 | pc.printf("\r\nintializing hardware,...v14\r\n"); |
fischk08 | 1:a6fbd8341289 | 383 | eth.init(); //Use DHCP |
fischk08 | 1:a6fbd8341289 | 384 | sendGETRequest: |
fischk08 | 1:a6fbd8341289 | 385 | EthernetErr ethErr = eth.setup(60000); |
fischk08 | 1:a6fbd8341289 | 386 | int retry = 0; |
fischk08 | 1:a6fbd8341289 | 387 | if (ethErr) { |
fischk08 | 1:a6fbd8341289 | 388 | pc.printf("Error in setup trying again %d",retry); |
fischk08 | 1:a6fbd8341289 | 389 | retry++; |
fischk08 | 1:a6fbd8341289 | 390 | goto sendGETRequest; |
fischk08 | 1:a6fbd8341289 | 391 | } |
fischk08 | 1:a6fbd8341289 | 392 | pc.printf("net ok testing weather\r\n"); |
fischk08 | 1:a6fbd8341289 | 393 | weather(); |
fischk08 | 1:a6fbd8341289 | 394 | //eth.disconnect(); |
fischk08 | 1:a6fbd8341289 | 395 | |
fischk08 | 1:a6fbd8341289 | 396 | } |
fischk08 | 1:a6fbd8341289 | 397 | |
fischk08 | 1:a6fbd8341289 | 398 |