Fork from Alex

Dependencies:   mbed MbedJSONValue mbed-rtos 4DGL-uLCD-SE ESP8266NodeMCUInterface

Revision:
3:7bf41989ff8f
Parent:
2:f7d19812bdc5
Child:
4:55f0c303f56a
--- a/main.cpp	Sun Mar 31 00:53:59 2019 +0000
+++ b/main.cpp	Sun Mar 31 03:08:51 2019 +0000
@@ -3,6 +3,10 @@
 #include "ESP8266Interface.h"
 #include "TCPSocketConnection.h"
 #include "rtos.h"
+#include "MbedJSONValue.h"
+#include <string>
+
+#define BUF_SIZE 4096
 
 // We need this for being able to reset the MBED (similar to watch dogs)
 extern "C" void mbed_reset();
@@ -30,7 +34,12 @@
 
 // Weather
 Thread weather_thread;
-char* forecast_link;
+char weather_api_key[256];
+
+// Location
+char ip_api_key[256];
+double latitude = 0;
+double longitude = 0;
 
 void time_updater() {
     // We're not an interrupt, so take as much time as we need. Infinite loop
@@ -70,8 +79,34 @@
 
 void weather_updater() {
     // We can take as long as we want
+    return;
     while (true) {
         // get the weather
+        // first get the current weather
+        // Weather data is _long_
+        
+        char forecast_buf[2];
+        TCPSocketConnection sck;
+        // http://api.openweathermap.org/data/2.5/forecast?lat=33.7485&lon=-84.3871&appid=6971e1ebfcc60f29c8dcc617c532b1b6
+        sck.connect("api.openweathermap.org", 80);
+        char cmd[1024];
+        sprintf(cmd,
+            "GET /data/2.5/weather?lat=%0.4f&lon=%0.4f&APPID=%s\r\nHost: api.openweathermap.org\r\n\r\n",
+            latitude, longitude, weather_api_key);
+        sck.send_all(cmd, strlen(cmd));
+        wait(10);
+        int buf_len = wifi.recv(forecast_buf, 16384 - 1, 0);
+        forecast_buf[buf_len] = '\0';
+        
+        // Get current weather
+        char current_buf[8192];
+        sprintf(cmd,
+            "GET /data/2.5/forecast?lat=%0.4f&lon=%0.4f&APPID=%s\r\nHost: api.openweathermap.org\r\n\r\n",
+            latitude, longitude, weather_api_key);
+        sck.send_all(cmd, strlen(cmd));
+        wait(10);
+        buf_len = wifi.recv(current_buf, 8192 - 1, 0);
+        current_buf[buf_len] = '\0';
         // we'll always want to update the LCD - don't worry about the previous
         // weather
         int curr_temp = 0;
@@ -87,7 +122,6 @@
         uLCD.text_string(buf, 0, 5, FONT_7X8, WHITE);
         // done! unlock
         lcd_lock.unlock();
-        Thread::wait(3600);
     }
 }
 
@@ -134,6 +168,7 @@
     char ssid[256];
     char pass[256];
     char api_key[256];
+    
     if (true) {
         
     } else if (fid != NULL) {
@@ -235,7 +270,10 @@
         //fprintf(fid, "%s\n%s\n%s\n", ssid, pass, api_key);
         //fclose(fid);
     }
-    
+    char* tmp = "af9319bf6435ddd9bb640f763ff64d34";
+    for (int i = 0; i < strlen(tmp); i++) {
+        api_key[i] = tmp[i];
+    }
     bool res = wifi.init();
     //dev.printf("Reset: %d\n", res);
     // Set up the WiFi Access Point
@@ -249,8 +287,57 @@
     dev.printf("Connected with IP Address: %s\n", wifi.getIPAddress());
     
     // Get the time & location
-    dev.printf("Getting the current time & location...\n");
+    dev.printf("Getting the current location...\n");
+    TCPSocketConnection tcp;
+    tcp.connect("api.ipstack.com", 80);
+    
+    char tcp_cmd[256];
+    sprintf(tcp_cmd, 
+        "GET /check?access_key=af9319bf6435ddd9bb640f763ff64d34 HTTP/1.1\r\nHost: api.ipstack.com\r\n\r\n");
+    tcp.send_all(tcp_cmd, strlen(tcp_cmd));
+    
+    wait(10);
+    
+    char buffer[BUF_SIZE];
+    int read_len = wifi.recv(buffer, BUF_SIZE - 1, 0);
+    buffer[read_len] = '\0';
+    
+    MbedJSONValue parser;
+    parse(parser, buffer);
+    // for now, just print...
     
+    latitude = parser["latitude"].get<double>();
+    longitude = parser["longitude"].get<double>();
+
+    /*
+    TCPSocketConnection weather_sck;
+    //http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=6971e1ebfcc60f29c8dcc617c532b1b6
+    //http://api.openweathermap.org/data/2.5/forecast?lat=33.7485&lon=-84.3871&appid=6971e1ebfcc60f29c8dcc617c532b1b6
+    weather_sck.connect("api.openweathermap.org", 80);
+    sprintf(tcp_cmd,
+        "GET /data/2.5/weather?lat=%0.4f&lon=%0.4f&APPID=%s\r\nHost: api.openweathermap.org\r\n\r\n",
+        lat, lng, "6971e1ebfcc60f29c8dcc617c532b1b6");
+    sprintf(weather_api_key,
+        "%s", "6971e1ebfcc60f29c8dcc617c532b1b6");
+    weather_sck.send_all(tcp_cmd, strlen(tcp_cmd));
+    wait(15);
+    int read_len = wifi.recv(buffer, BUF_SIZE - 1, 0);
+    buffer[read_len] = '\0';
+    dev.printf(buffer);
+    */
+    // dev.printf("Connection: %d\n", con_res);
+    //sprintf
+    //int con_res = weather_sck.connect("api.weather.gov", 443);
+    //    dev.printf("Connection: %d\n", con_res);
+    //sprintf(tcp_cmd,
+    //    "GET /points/%0.4f,%0.4f HTTP/1.1\r\nHost: api.weather.gov\r\n\r\n",
+    //    lat, lng);
+    //weather_sck.send_all(tcp_cmd, strlen(tcp_cmd));
+    //wait(5);
+    //wait(10);
+    //int read_len = wifi.recv(buffer, BUF_SIZE - 1, 0);
+    //buffer[read_len] = '\0';
+    //dev.printf(buffer);
     set_time(1256729737);
     
     // Clear the LCD Screen first
@@ -262,53 +349,25 @@
     time_thread.start(time_updater);
     
     // Make the request to get the forecast link
-    TCPSocketConnection tcp;
-    int tcp_res = tcp.connect("alexhrao.com", 80);
-    dev.printf("Connection: %d\n", tcp_res);
-    //int sent = tcp.send("GET / HTTP/1.1\r\nHost: alexhrao.com\r\nUser-Agent: curl/7.54.0\r\nAccept: */*", 73);
-    tcp.set_blocking(true, 10000);
-    char* cmd = "GET / HTTP/1.1\r\nHost: alexhrao.com\r\n\r\n";
-    int sent = tcp.send_all(cmd, strlen(cmd));
-    dev.printf("Sent %d\n", sent);
-    char buf[128];
-    for (int i = 0; i < 32; i++) {
-        buf[i] = 0;
-    }
-//    wifi.send(cmd, strlen(cmd));
-//    wifi.recv(buf, 2048);
-//wait(15);
-    //tcp.receive(buf, 16);
-    int to_rec = 7;
-    //wifi.getc();
-    while (true) {
-        wifi.recv(buf, &to_rec);
-        dev.printf("To Receive: %d\n", to_rec);
-        to_rec = 7;
-        buf[7] = '\0';
-        dev.printf("%s", buf);
-        wait(15);
-    }
-    wifi.recv(buf, &to_rec);
-    dev.printf("%s\n", buf);
-    wifi.recv(buf, &to_rec);
-    int recv2 = tcp.receive_all(buf, 4);
-    dev.printf("Received (%d):\n%s\n", recv2, buf);
-    // Where are we & What time is it?
-    // Create the TCP connection, request from ipstack. Get the request,
-    // and store the current time (FORMAT?)
-    set_time(1256729737);
-    // Now that we know what time it is, set up our Time Thread
-    time_thread.start(time_updater);
+
     // Now, make a single request to nws and get the forecast link - we can 
     // store this link for later!
     
     // Start up weather service!
-    weather_thread.start(weather_updater);
+    //weather_thread.start(weather_updater);
     
     //weather_ticker.attach(&weather_tick, 900000.0f);
     // Listen on bluetooth.
     dev_thread.start(dev_recv);
+    dev.printf("Hello, World!\n");
+    time_t prev_time = time(NULL);
     while(true) {
+        time_t curr_time = time(NULL);
+        // if it's been an hour (divide by 3600 >= 60), pause and get weather:
+        if (((int)(curr_time - prev_time) / 3600) >= 60) {
+            //weather_updater();
+            prev_time = curr_time;
+        }
         err_led = !err_led;
         //dev.printf("Connection Status: %d\n", wifi.is_connected());
         wait(0.5);