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

Files at this revision

API Documentation at this revision

Comitter:
krbng4180
Date:
Sun Mar 13 21:09:49 2016 +0000
Child:
1:fc07d1430053
Commit message:
it works!!!!!!!

Changed in this revision

4DGL-uLCD-SE.lib Show annotated file Show diff for this revision Revisions of this file
ESP8266NodeMCUInterface.lib Show annotated file Show diff for this revision Revisions of this file
GPS.lib Show annotated file Show diff for this revision Revisions of this file
MbedJSONValue.lib Show annotated file Show diff for this revision Revisions of this file
connect.lua Show annotated file Show diff for this revision Revisions of this file
example_response.txt Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed-src.lib Show annotated file Show diff for this revision Revisions of this file
query.lua Show annotated file Show diff for this revision Revisions of this file
test_query.lua Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/4DGL-uLCD-SE.lib	Sun Mar 13 21:09:49 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/4180_1/code/4DGL-uLCD-SE/#061279131e57
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP8266NodeMCUInterface.lib	Sun Mar 13 21:09:49 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/ESP8266/code/ESP8266NodeMCUInterface/#a7741fe989ca
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/GPS.lib	Sun Mar 13 21:09:49 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/GPS/#09efba55c197
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MbedJSONValue.lib	Sun Mar 13 21:09:49 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/samux/code/MbedJSONValue/#10a99cdf7846
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/connect.lua	Sun Mar 13 21:09:49 2016 +0000
@@ -0,0 +1,7 @@
+LUA(
+    wifi.setmode(wifi.STATION);
+    wifi.sta.config("%s", "%s");
+    wifi.sta.connect();
+    while wifi.sta.status() ~= 5 do end;
+    print(wifi.sta.status());
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example_response.txt	Sun Mar 13 21:09:49 2016 +0000
@@ -0,0 +1,329 @@
+
+{
+  "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
+        }
+        ]
+        }
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Mar 13 21:09:49 2016 +0000
@@ -0,0 +1,152 @@
+#include "mbed.h"
+#include "GPS.h"
+#include "ESP8266Interface.h"
+#include "MbedJSONValue.h"
+#include "uLCD_4DGL.h"
+
+#define DEBUG(msg) pc.printf(msg)
+#define DEBUGF(msgf) pc.printf msgf
+
+/* Lua code comes from here:
+ *   https://learn.adafruit.com/adafruit-huzzah-esp8266-breakout/using-nodemcu-lua
+ */
+#define LUA(x) #x
+
+#define SSID "ThisIsWiFi"
+#define PWD "benandjerrys101"
+#define API_KEY "f99ab4720b04e9a5"
+
+/*
+ * The GPS unit continuously transmits data over serial in standard ASCII text.
+ * Using this API, we call `gps.sample()` to get the most recent longitude and
+ * latitude. To then read those values, we call `gps.longitude` and `gps.latitude`.
+ *
+ * Important:
+ *      It is worth noting that `gps.sample()` is synchronous and blocking; because
+ *      the GPS continously transmits data, `gps.sample()` can take a varying amount
+ *      of time. The worst case scenario is that the GPS unit *just* transmitted the
+ *      relevant information right before we call `gps.sample()`.  If we needed GPS
+ *      information frequently, it would be prudent to move this to a separate thread
+ *      and interface with that thread via mutex-protected global variables for 
+ *      latitude and longitude.  
+ */
+GPS gps(p13, p14);
+
+/*
+ * Serial communications with the computer. Used for debugging.
+ */
+Serial pc(USBTX, USBRX);
+
+/*
+ * We communicate with the wifi firmware over serial. The firmware accepts
+ * Lua commands, so we send those over serial.
+ */
+//Serial wifi(p9, p10);
+ESP8266Interface esp(p9, p10, p8, 9600, 7000);
+
+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"
+static char http_request_buf[512] = { 0 };
+
+#define RESP_LEN 1024
+static char http_response_buf[RESP_LEN] = { 0 };
+static int http_response_len = RESP_LEN;
+
+const char * parse_loc_response (char * resp_buff) {
+    MbedJSONValue parsed;
+    parse(parsed, resp_buff);
+    MbedJSONValue &inst = parsed["location"];
+//    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;
+    
+}
+
+void parse_cond_response (char * resp_buff) {
+    
+}
+
+int main() {
+    
+    //wifi.attach(&wifi_rx_handler, Serial::RxIrq);
+    //wifi.attach(&wifi_tx_handler, Serial::TxIrq);
+    
+    //wifi.baud(9600);
+    pc.baud(9600);
+    pc.printf("HELLO WORLD\n\r");
+    /*
+     * Intialize connection to AP.
+     */
+    esp.init();
+    
+    bool connected = false;
+    do {
+        connected = esp.connect(SSID, PWD);
+        DEBUGF(("Connected to AP: %d\n\r", connected));
+    } while (!connected);
+    
+    /* Perform DNS lookup */
+    
+    char ip[12] = { 0 };
+    bool dns_success = false;
+    do {
+        dns_success = esp.getHostByName("api.wunderground.com", ip);
+        DEBUGF(("IP: %s\n\r", ip));
+    } while (!dns_success);
+    
+    /* Open connection */
+    
+    bool opened = false;
+    do {
+        opened = esp.open(1, ip, 80);
+        DEBUGF(("Connection opened: %d\n\r", opened));
+    } while (!opened);
+    
+    /* 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();
+    
+    while (1) {
+    }
+        
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Sun Mar 13 21:09:49 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#dfc27975e193
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-src.lib	Sun Mar 13 21:09:49 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-src/#acd8185c8960
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/query.lua	Sun Mar 13 21:09:49 2016 +0000
@@ -0,0 +1,6 @@
+LUA(
+    sk=net.createConnection(net.TCP, 0);
+    sk:on("receive", function(sck, c) print(c) end);
+    sk:connect(80, "%s");
+    sk:send("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");
+)
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test_query.lua	Sun Mar 13 21:09:49 2016 +0000
@@ -0,0 +1,6 @@
+LUA(
+    sk=net.createConnection(net.TCP, 0);
+    sk:on("receive", function(sck, c) print(c) end);
+    sk:connect(80, "207.58.139.247");
+    sk:send("GET /testwifi/index.html HTTP/1.1\r\nHost: www.adafruit.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n");
+)
\ No newline at end of file