Fork from Alex

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

Revision:
2:f7d19812bdc5
Parent:
1:0620ba35d2e8
Child:
3:7bf41989ff8f
--- a/main.cpp	Fri Mar 29 13:12:49 2019 +0000
+++ b/main.cpp	Sun Mar 31 00:53:59 2019 +0000
@@ -115,26 +115,53 @@
         //if (strcmp(buf, "reset") != 0) {
         Thread::wait(0.01);
     }
-}       
+}
 
 int main() {
     // Set up bluetooth
     dev.baud(9600);
     pc.baud(9600);
+    
     // Tell user we're initializing...
     lcd_lock.lock();
     uLCD.cls();
     uLCD.text_height(2);
     uLCD.text_string("PLEASE WAIT", 0, 2, FONT_7X8, WHITE);
     lcd_lock.unlock();
+    
     // Need to get wifi settings. If we don't have local file, then ask!
     FILE* fid = NULL;//fopen("/local/settings.txt", "r");
-    // TODO: Get username and password
     char ssid[256];
     char pass[256];
-    if (fid == NULL) {
+    char api_key[256];
+    if (true) {
         
-        //fclose(fid);
+    } else if (fid != NULL) {
+        // Read WiFi Settings
+        char settings_buf[1024];
+        // Guaranteed to be 256, 256, 512 AT MOST + two new lines
+        // 
+        fgets(settings_buf, 1024, fid);
+        // find \n
+        int settings_ind = 0;
+        int counter = 0;
+        while (settings_buf[counter] != '\n') {
+            ssid[settings_ind++] = settings_buf[counter++];
+        }
+        ssid[settings_ind] = '\0';
+        settings_ind = 0;
+        counter++;
+        while (settings_buf[counter] != '\n') {
+            pass[settings_ind++] = settings_buf[counter++];
+        }
+        pass[settings_ind] = '\0';
+        settings_ind = 0;
+        counter++;
+        while (settings_buf[counter] != '\n') {
+            api_key[settings_ind++] = settings_buf[counter++];
+        }
+        api_key[settings_ind] = '\0';
+        fclose(fid);
     } else {
         lcd_lock.lock();
         uLCD.cls();
@@ -143,13 +170,16 @@
         uLCD.text_string("SEE", 0, 2, FONT_7X8, RED);
         uLCD.text_string("DEVICE", 0, 5, FONT_7X8, RED);
         lcd_lock.unlock();
+        
         // Ask!
         dev.printf("Please provide the name of a WiFi Network\n");
+        
         // Wait for them to respond
         while (!dev.readable()) {
             wait(0.001);
         }
         int ind = 0;
+        
         // Read response
         while (ind < 255) {
             char tmp = dev.getc();
@@ -167,6 +197,7 @@
             wait(0.01);
         }
         
+        // Get the password
         dev.printf("Please provide the password\n");
         while (!dev.readable()) {
             wait(0.001);
@@ -181,11 +212,27 @@
             wait(0.01);
         }
         pass[ind] = '\0';
+        
+        // Get the API key
+        dev.printf("Please provide the API key\n");
+        while (!dev.readable()) {
+            wait(0.001);
+        }
+        ind = 0;
+        while (ind < 255) {
+            char tmp = dev.getc();
+            if (tmp == '\n' || tmp == '\r') {
+                break;
+            }
+            api_key[ind++] = tmp;
+            wait(0.01);
+        }
+        api_key[ind] = '\0';
         // Because this is a simple proof of concept, we store the password in 
         // plaintext. It should be noted, however, that you **should never do 
         // this in "real life"**
         //fid = fopen("/local/settings.txt", "w");
-        //fprintf(fid, "ssid: %s\npass: %s\n", ssid, pass);
+        //fprintf(fid, "%s\n%s\n%s\n", ssid, pass, api_key);
         //fclose(fid);
     }
     
@@ -194,15 +241,58 @@
     // Set up the WiFi Access Point
     dev.printf("Connecting to WiFi %s with Password %s\n", ssid, pass);
     res = wifi.connect("Alex's iPhone", "mbedlookhere");
-    //if (!res) {
-    //    dev.printf("Connection Failed... Resetting Device\n");
-        // mbed_reset();
-    //}
-    dev.printf("Is Connected: %d\n", wifi.is_connected());
-    dev.printf("IP Address: %s\n", wifi.getIPAddress());
+    if (!res) {
+        dev.printf("Connection Failed... Resetting Device\n");
+        err_led = 1;
+        mbed_reset();
+    }
+    dev.printf("Connected with IP Address: %s\n", wifi.getIPAddress());
+    
+    // Get the time & location
+    dev.printf("Getting the current time & location...\n");
+    
+    set_time(1256729737);
+    
+    // Clear the LCD Screen first
+    lcd_lock.lock();
+    uLCD.cls();
+    lcd_lock.unlock();
+    
+    // Now that we know what time it is, set up our Time Thread
+    time_thread.start(time_updater);
+    
+    // Make the request to get the forecast link
     TCPSocketConnection tcp;
-    int tcp_res = tcp.connect("13.58.105.158", 80);
+    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?)
@@ -211,9 +301,6 @@
     time_thread.start(time_updater);
     // Now, make a single request to nws and get the forecast link - we can 
     // store this link for later!
-    lcd_lock.lock();
-    uLCD.cls();
-    lcd_lock.unlock();
     
     // Start up weather service!
     weather_thread.start(weather_updater);