A weather station that uses GPS and WiFi to get the weather where you are. Almost better than actually going outside!
Dependencies: 4DGL-uLCD-SE ESP8266NodeMCUInterface GPS MbedJSONValue mbed-rtos mbed-src
Revision 1:fc07d1430053, committed 2016-03-14
- Comitter:
- krbng4180
- Date:
- Mon Mar 14 03:04:19 2016 +0000
- Parent:
- 0:c4da54879553
- Commit message:
- publish
Changed in this revision
diff -r c4da54879553 -r fc07d1430053 4DGL-uLCD-SE.lib --- a/4DGL-uLCD-SE.lib Sun Mar 13 21:09:49 2016 +0000 +++ b/4DGL-uLCD-SE.lib Mon Mar 14 03:04:19 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/4180_1/code/4DGL-uLCD-SE/#061279131e57 +https://developer.mbed.org/users/krbng4180/code/4DGL-uLCD-SE/#061279131e57
diff -r c4da54879553 -r fc07d1430053 ESP8266NodeMCUInterface.lib --- a/ESP8266NodeMCUInterface.lib Sun Mar 13 21:09:49 2016 +0000 +++ b/ESP8266NodeMCUInterface.lib Mon Mar 14 03:04:19 2016 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/teams/ESP8266/code/ESP8266NodeMCUInterface/#a7741fe989ca +https://developer.mbed.org/users/krbng4180/code/ESP8266NodeMCUInterface/#7c4a5bdcb624
diff -r c4da54879553 -r fc07d1430053 GPS.lib --- a/GPS.lib Sun Mar 13 21:09:49 2016 +0000 +++ b/GPS.lib Mon Mar 14 03:04:19 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/simon/code/GPS/#09efba55c197 +https://developer.mbed.org/users/krbng4180/code/GPS/#09efba55c197
diff -r c4da54879553 -r fc07d1430053 example_response.txt --- a/example_response.txt Sun Mar 13 21:09:49 2016 +0000 +++ b/example_response.txt Mon Mar 14 03:04:19 2016 +0000 @@ -1,329 +1,4 @@ +{ "data": { "current_condition": [ {"cloudcover": "75", "FeelsLikeC": "25", "FeelsLikeF": "77", "humidity": "50", "observation_time": "10:38 PM", "precipMM": "0.6", "pressure": "1011", "temp_C": "24", "temp_F": "75", "visibility": "16", "weatherCode": "116", "weatherDesc": [ {"value": "Partly Cloudy" } ], "weatherIconUrl": [ {"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" } ], "winddir16Point": "W", "winddirDegree": "260", "windspeedKmph": "20", "windspeedMiles": "13" } ], "request": [ {"query": "Lat 33.78 and Lon -84.41", "type": "LatLon" } ] }} + -{ - "response": { - "version":"0.1", - "termsofService":"http://www.wunderground.com/weather/api/d/terms.html", - "features": { - "geolookup": 1 - } - } - , "location": { - "type":"CITY", - "country":"US", - "country_iso3166":"US", - "country_name":"USA", - "state":"GA", - "city":"Atlanta", - "tz_short":"EST", - "tz_long":"America/New_York", - "lat":"33.780000", - "lon":"-84.400000", - "zip":"30332", - "magic":"1", - "wmo":"99999", - "l":"/q/zmw:30332.1.99999", - "requesturl":"US/GA/Atlanta.html", - "wuiurl":"http://www.wunderground.com/US/GA/Atlanta.html", - "nearby_weather_stations": { - "airport": { - "station": [ - { "city":"Atlanta Fulton", "state":"GA", "country":"US", "icao":"KFTY", "lat":"33.77914047", "lon":"-84.52136230" } - ,{ "city":"Atlanta Dekalb", "state":"GA", "country":"US", "icao":"KPDK", "lat":"33.87836075", "lon":"-84.29801941" } - ,{ "city":"Atlanta", "state":"GA", "country":"US", "icao":"KATL", "lat":"33.63010025", "lon":"-84.44180298" } - ,{ "city":"Marietta", "state":"GA", "country":"US", "icao":"KMGE", "lat":"33.91538239", "lon":"-84.51631927" } - ] - } - , - "pws": { - "station": [ - { - "neighborhood":"Georgia Tech Police Station WeatherSTEM", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN215", - "lat":33.778946, - "lon":-84.401291, - "distance_km":0, - "distance_mi":0 - }, - { - "neighborhood":"Georgia Tech (Midtown)", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN49", - "lat":33.779148, - "lon":-84.395859, - "distance_km":0, - "distance_mi":0 - }, - { - "neighborhood":"Georgia Tech Campus Recreation Center WeatherSTEM", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN216", - "lat":33.774937, - "lon":-84.403671, - "distance_km":0, - "distance_mi":0 - }, - { - "neighborhood":"MesoWest", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"MGA307", - "lat":33.780560, - "lon":-84.391418, - "distance_km":0, - "distance_mi":0 - }, - { - "neighborhood":"Bobby Dodd Stadium WeatherSTEM", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN214", - "lat":33.772316, - "lon":-84.392807, - "distance_km":1, - "distance_mi":0 - }, - { - "neighborhood":"Midtown", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN108", - "lat":33.779575, - "lon":-84.387108, - "distance_km":1, - "distance_mi":0 - }, - { - "neighborhood":"Midtown", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN124", - "lat":33.784660, - "lon":-84.385109, - "distance_km":1, - "distance_mi":0 - }, - { - "neighborhood":"Midtown", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN228", - "lat":33.777763, - "lon":-84.383232, - "distance_km":1, - "distance_mi":0 - }, - { - "neighborhood":"Midtown", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN222", - "lat":33.783314, - "lon":-84.383118, - "distance_km":1, - "distance_mi":0 - }, - { - "neighborhood":"Midtown", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN180", - "lat":33.782230, - "lon":-84.382416, - "distance_km":1, - "distance_mi":0 - }, - { - "neighborhood":"Midtown", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN183", - "lat":33.781288, - "lon":-84.380478, - "distance_km":1, - "distance_mi":1 - }, - { - "neighborhood":"Downtown", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN238", - "lat":33.764450, - "lon":-84.390388, - "distance_km":1, - "distance_mi":1 - }, - { - "neighborhood":"Midtown", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN133", - "lat":33.780827, - "lon":-84.374130, - "distance_km":2, - "distance_mi":1 - }, - { - "neighborhood":"Ansley Park", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN125", - "lat":33.798199, - "lon":-84.377342, - "distance_km":2, - "distance_mi":1 - }, - { - "neighborhood":"Virginia Highland", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN106", - "lat":33.784058, - "lon":-84.363556, - "distance_km":3, - "distance_mi":2 - }, - { - "neighborhood":"Morningside-Lenox Park", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN165", - "lat":33.791653, - "lon":-84.365471, - "distance_km":3, - "distance_mi":2 - }, - { - "neighborhood":"Heritage Preparatory School", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN54", - "lat":33.801754, - "lon":-84.368134, - "distance_km":3, - "distance_mi":2 - }, - { - "neighborhood":"Underwood Hills", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN143", - "lat":33.809158, - "lon":-84.426079, - "distance_km":4, - "distance_mi":2 - }, - { - "neighborhood":"Poncey-Highland", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN220", - "lat":33.773075, - "lon":-84.355103, - "distance_km":4, - "distance_mi":2 - }, - { - "neighborhood":"Underwood Hills - Marquis Midtown", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN260", - "lat":33.810131, - "lon":-84.428574, - "distance_km":4, - "distance_mi":2 - }, - { - "neighborhood":"Peachtree Hills", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN116", - "lat":33.817566, - "lon":-84.384659, - "distance_km":4, - "distance_mi":2 - }, - { - "neighborhood":"Cabbagetown", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN202", - "lat":33.749310, - "lon":-84.362762, - "distance_km":4, - "distance_mi":2 - }, - { - "neighborhood":"Buckhead: The Wakefield", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN15", - "lat":33.828011, - "lon":-84.388420, - "distance_km":5, - "distance_mi":3 - }, - { - "neighborhood":"Grant Park", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN40", - "lat":33.738831, - "lon":-84.365257, - "distance_km":5, - "distance_mi":3 - }, - { - "neighborhood":"Midtown/Buckhead", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN16", - "lat":33.810043, - "lon":-84.348610, - "distance_km":5, - "distance_mi":3 - }, - { - "neighborhood":"Oakdale Road Northeast", - "city":"Atlanta", - "state":"GA", - "country":"US", - "id":"KGAATLAN262", - "lat":33.782814, - "lon":-84.335724, - "distance_km":5, - "distance_mi":3 - } - ] - } - } - } -} +
diff -r c4da54879553 -r fc07d1430053 main.cpp --- a/main.cpp Sun Mar 13 21:09:49 2016 +0000 +++ b/main.cpp Mon Mar 14 03:04:19 2016 +0000 @@ -4,8 +4,15 @@ #include "MbedJSONValue.h" #include "uLCD_4DGL.h" -#define DEBUG(msg) pc.printf(msg) -#define DEBUGF(msgf) pc.printf msgf +#define DEBUG_MODE + +#ifdef DEBUG_MODE +# define DEBUG(msg) pc.printf(msg) +# define DEBUGF(msgf) pc.printf msgf +#else +# define DEBUG(msg) +# define DEBUGF(msgf) +#endif /* Lua code comes from here: * https://learn.adafruit.com/adafruit-huzzah-esp8266-breakout/using-nodemcu-lua @@ -15,6 +22,8 @@ #define SSID "ThisIsWiFi" #define PWD "benandjerrys101" #define API_KEY "f99ab4720b04e9a5" +#define FORECAST_API_KEY "9cfbfbf425552305838467b4b838b426" +#define WWO_API_KEY "43911d834dc04583b4e221235161303" /* * The GPS unit continuously transmits data over serial in standard ASCII text. @@ -46,40 +55,71 @@ uLCD_4DGL uLCD(p28,p27,p30); -void dev_recv() -{ - while(esp.readable()) { - pc.putc(esp.getc()); - } -} - - -#define REQ_TEMPLATE "GET /api/%s/geolookup/q/%f,%f.json HTTP/1.1\r\nHost: api.wunderground.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n" -#define REQ_TEMPLATE_NOPE "GET /testwifi/index.html HTTP/1.1\r\nHost: www.adafruit.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n" +#define REQ_TEMPLATE_WWO "GET /free/v2/weather.ashx?key=%s&format=json&cc=yes&fx=no&q=%f,%f HTTP/1.1\r\nHost: api.worldweatheronline.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n" +#define REQ_TEMPLATE_WU "GET /api/%s/geolookup/q/%f,%f.json HTTP/1.1\r\nHost: api.wunderground.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n" +#define REQ_TEMPLATE "GET /forecast/%s/%f,%f?exclude=minutely,hourly,daily,alerts,flags HTTP/1.1\r\nHost: api.forecast.io\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n" static char http_request_buf[512] = { 0 }; -#define RESP_LEN 1024 +#define RESP_LEN 2048 static char http_response_buf[RESP_LEN] = { 0 }; static int http_response_len = RESP_LEN; -const char * parse_loc_response (char * resp_buff) { +/* + * Prints to LCD, theoretically. It's not very pretty. +*/ + +void draw_values (const char * weather, const char * temp) { + uLCD.printf("\n Current Conditions:"); + uLCD.text_width(2); + uLCD.text_height(2); + uLCD.printf(temp); + uLCD.printf(" F\n"); + uLCD.text_width(1); + uLCD.text_height(1); + uLCD.printf(weather); +} + +/* + * Parse location from geolocation response. +*/ + +void parse_response (char * resp_buff) { MbedJSONValue parsed; parse(parsed, resp_buff); - MbedJSONValue &inst = parsed["location"]; + MbedJSONValue &inst = parsed["data"]["current_condition"][0]; // const string &citystate = inst["city"].get<string>() + ", " + inst["state"].get<string>(); // string[2] ar; - string test = inst["city"].get<string>().c_str(); - string test2 = " "; - string test3 = inst["city"].get<string>().c_str(); - const char * ret = (test + test2 + test3).c_str(); - //ret[0] = inst["city"].get<string>().c_str(); - //ret[1] = inst["city"].get<string>().c_str(); - return ret; - + const char* weather = inst["wetherDesc"][0]["value"].get<string>().c_str(); + const char* temp = inst["temp_f"].get<string>().c_str(); + //if we want an icon, inst["weatherIconUrl"][0]["value"].get<string>(); gets the URL + draw_values(weather, temp); } -void parse_cond_response (char * resp_buff) { +static char * get_weather_json(double latitude, double longitude) +{ + int http_request_len = sprintf(http_request_buf, REQ_TEMPLATE_WWO, WWO_API_KEY, latitude, longitude); + /* Send HTTP request */ + bool sent = false; + do { + sent = esp.send(http_request_buf, http_request_len); + DEBUGF(("Request sent: %d\n\r", sent)); + } while (!sent); + + DEBUGF(("%s", http_request_buf)); + wait(3); + + bool recv = false; + do { + http_response_len = RESP_LEN; + recv = esp.recv(http_response_buf, &http_response_len); + DEBUGF(("Request recv: %d\n\r", recv)); + DEBUGF(("Response: %s\n\r", http_response_buf)); + DEBUGF(("Response Length: %d\n\r", http_response_len)); + wait(2); + } while (!recv); + + return http_response_buf; } int main() { @@ -106,7 +146,7 @@ char ip[12] = { 0 }; bool dns_success = false; do { - dns_success = esp.getHostByName("api.wunderground.com", ip); + dns_success = esp.getHostByName("api.worldweatheronline.com", ip); DEBUGF(("IP: %s\n\r", ip)); } while (!dns_success); @@ -120,31 +160,28 @@ /* Generate HTTP request */ gps.sample(); - int http_request_len = sprintf(http_request_buf, REQ_TEMPLATE, API_KEY, gps.latitude, gps.longitude); - /* Send HTTP request */ - bool sent = false; - do { - sent = esp.send(http_request_buf, http_request_len); - DEBUGF(("Request sent: %d\n\r", sent)); - } while (!sent); - - DEBUGF(("%s", http_request_buf)); - - wait(10); - - bool recv = false; - do { - http_response_len = RESP_LEN; - recv = esp.recv(http_response_buf, &http_response_len); - DEBUGF(("Request recv: %d\n\r", recv)); - DEBUGF(("Response: %s\n\r", http_response_buf)); - DEBUGF(("Response Length: %d\n\r", http_response_len)); - wait(2); - } while (!recv); - - - - esp.close(); + get_weather_json(gps.latitude, gps.longitude + 56); + //int http_request_len = sprintf(http_request_buf, REQ_TEMPLATE_WWO, WWO_API_KEY, gps.latitude, gps.longitude + 56); +// /* Send HTTP request */ +// bool sent = false; +// do { +// sent = esp.send(http_request_buf, http_request_len); +// DEBUGF(("Request sent: %d\n\r", sent)); +// } while (!sent); +// +// DEBUGF(("%s", http_request_buf)); +// +// wait(3); +// +// bool recv = false; +// do { +// http_response_len = RESP_LEN; +// recv = esp.recv(http_response_buf, &http_response_len); +// DEBUGF(("Request recv: %d\n\r", recv)); +// DEBUGF(("Response: %s\n\r", http_response_buf)); +// DEBUGF(("Response Length: %d\n\r", http_response_len)); +// wait(2); +// } while (!recv); while (1) { }
diff -r c4da54879553 -r fc07d1430053 mbed-src.lib --- a/mbed-src.lib Sun Mar 13 21:09:49 2016 +0000 +++ b/mbed-src.lib Mon Mar 14 03:04:19 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed-src/#acd8185c8960 +https://developer.mbed.org/users/krbng4180/code/mbed-src/#acd8185c8960