now this shit works

Dependencies:   BufferedSerial

Dependents:   IoTWeatherStation

Fork of ESP8266NodeMCUInterface by ESP8266

Revision:
49:a7741fe989ca
Parent:
47:04632d22a723
Child:
50:7c4a5bdcb624
--- a/ESP8266/ESP8266.cpp	Thu Jun 04 20:45:00 2015 +0000
+++ b/ESP8266/ESP8266.cpp	Sun Mar 13 21:09:31 2016 +0000
@@ -59,7 +59,8 @@
     _reset_pin = 1;
     
     // Send reboot command in case reset is not connected
-    return command("\x03\r\n" "node.restart()") && execute();
+    //return command("\x03\r\n" "node.restart()") && execute();
+    return true;
 }
 
 bool ESP8266::init() {
@@ -68,13 +69,27 @@
 }
 
 bool ESP8266::connect(const char *ssid, const char *phrase) {
+    int ip_len = 15;
+    // Check if already connected.
+    if (!(command("ip=wifi.sta.getip();") &&
+          command("print(ip);") &&
+          execute(_ip, &ip_len)))
+        return false;
+    
+    _ip[ip_len] = 0;
+    
+    if (strcmp(_ip, "nil") != 0)
+        return true;
+    
+    
     // Configure as station with passed ssid and passphrase
     if (!(command("wifi.setmode(wifi.STATION);") &&
           command("wifi.sta.config('") &&
           command(ssid) &&
           command("','") &&
           command(phrase) &&
-          command("')") &&
+          command("');") &&
+          command("wifi.sta.connect();") &&
           execute()))
         return false;
         
@@ -87,7 +102,7 @@
         int ip_len = 15;
         
         if (!(command("ip=wifi.sta.getip();") &&
-              command("print(ip)") &&
+              command("print(ip);") &&
               execute(_ip, &ip_len)))
             return false;
         
@@ -139,14 +154,10 @@
           command("function cs(n) c:send(n) end;") &&
           command("function ca() print(#cm) end;") &&
           command("function cr(n) " 
-                    "d=cm:sub(1,n):gsub('.', function(s) "
-                      "return s.format('\\\\%03d', s:byte(1)) "
-                    "end);"
-                    "cm=cm:sub(n+1,-1);" 
-                    "print(d) "
+                    "d=cm:sub(1,n):gsub('.', function(s) return s.format('\\\\%03d', s:byte(1)) end);"
+                    "cm=cm:sub(n+1,-1);"
                   "end;") && 
-          command("c:on('receive',function(c,n) cm=cm..n end)") && 
-          execute()))
+          command("c:on('receive', function(c,n) cm=cm..n;end);") && execute()))
         return false;
     
     // Convert port to a string    
@@ -213,9 +224,16 @@
 }
 
 bool ESP8266::recv(char *buffer, int *len) {
-    char len_buf[16];
+    // Nick
+    if (!(command("print(cm)") && execute_full_length(buffer, len))) {
+        return false;
+    }
+    
+    return true;
+    // /Nick
+    
+    char len_buf[16]; 
     sprintf(len_buf, "%d", *len);
-    
     if (!(command("cr(") &&
           command(len_buf) &&
           command(")")))
@@ -227,14 +245,13 @@
     // Read in response
     for (int i = 0; i < *len; i++) {
         int e = serialgetc();
-        
         if (e == '\r') {
             *len = i;
             break;
         } else if (e != '\\') {
             return false;
         }
-        
+       
         int a = serialgetc();
         int b = serialgetc();
         int c = serialgetc();
@@ -393,6 +410,37 @@
     return true;
 } 
 
+bool ESP8266::execute_full_length(char *resp_buf, int *resp_len) {
+    // Finish command with a newline
+    if (!(command("\r\n") && discardEcho()))
+        return false;
+        
+    int brace_count = 0; 
+    
+    // Read in response if any
+    if (resp_buf && resp_len) {
+        int i;
+        
+        for (i = 0; i < *resp_len; i++) {
+            int c = serialgetc();
+                
+            if (c < 0)
+                return false;
+                
+            if (c == '>') {
+                *resp_len = i;
+                break;
+            }  
+            
+            resp_buf[i] = c;
+        }
+        
+        DBG("command response:\t %.*s", *resp_len, resp_buf);
+    }
+    
+    return flush();
+}
+
 bool ESP8266::execute(char *resp_buf, int *resp_len) {
     // Finish command with a newline
     if (!(command("\r\n") && discardEcho()))