now this shit works

Dependencies:   BufferedSerial

Dependents:   IoTWeatherStation

Fork of ESP8266NodeMCUInterface by ESP8266

Revision:
46:4abb80f0a920
Parent:
44:16da10e7b3f7
Child:
47:04632d22a723
--- a/ESP8266/ESP8266.cpp	Wed Jun 03 21:44:20 2015 +0000
+++ b/ESP8266/ESP8266.cpp	Thu Jun 04 20:14:52 2015 +0000
@@ -137,8 +137,14 @@
     // Setup functions for sending and recieving characters on the esp side
     if (!(command("cm='';") &&
           command("function cs(n) c:send(n) end;") &&
-          command("function cr(n) print(cm:sub(1,n)); cm=cm:sub(n+1,-1) 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) "
+                  "end;") && 
           command("c:on('receive',function(c,n) cm=cm..n end)") && 
           execute()))
         return false;
@@ -182,12 +188,26 @@
     return command("c:close();" "c=nil") && execute();
 }
 
-bool ESP8266::send(const char *buffer, int len) {
-    if (!(command("cs('") &&
-          payload(buffer, len) &&
-          command("')") &&
-          execute()))
-        return false;
+bool ESP8266::send(const char *buffer, int len) {     
+    for (int line = 0; line < len; line += ESP_MAX_LINE) {
+        if (!command("cs('"))
+            return false;
+        
+        for (int i = 0; i < ESP_MAX_LINE && line+i < len; i++) {
+            int a = buffer[line+i] / 100;
+            int b = (buffer[line+i] - a*100) / 10;
+            int c = (buffer[line+i] - a*100 - b*10);
+            
+            if (serialputc('\\') < 0 ||
+                serialputc(a + '0') < 0 ||
+                serialputc(b + '0') < 0 ||
+                serialputc(c + '0') < 0)
+                return false;
+        }
+        
+        if (!(command("')") && execute()))
+            return false;
+    }
     
     return true;
 }
@@ -198,11 +218,35 @@
     
     if (!(command("cr(") &&
           command(len_buf) &&
-          command(")") && 
-          execute(buffer, len)))
+          command(")")))
+        return false;
+        
+   if (!(command("\r\n") && discardEcho()))
         return false;
+        
+    // 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();
+        
+        if (a < 0 || b < 0 || c < 0)
+            return false;
+            
+        buffer[i] = (a-'0')*100 + (b-'0')*10 + (c-'0');
+    }
     
-    return true;
+    // Flush to next prompt
+    return flush();
 }
 
 int ESP8266::putc(char c) {
@@ -322,6 +366,17 @@
         
         if (c < 0)
             return false;
+        else if (c == '\n')
+            return true;
+    }
+}
+
+bool ESP8266::flush() {
+    while (true) {
+        int c = serialgetc();
+        
+        if (c < 0)
+            return false;
         else if (c == '>')
             return true;
     }
@@ -336,39 +391,13 @@
     }
     
     return true;
-}
-
-bool ESP8266::payload(const char *data, int len) {
-    for (int i = 0; i < len; i++) {
-        int a = data[i] / 100;
-        int b = (data[i] - a*100) / 10;
-        int c = (data[i] - a*100 - b*10);
-        
-        if (serialputc('\\') < 0 ||
-            serialputc(a + '0') < 0 ||
-            serialputc(b + '0') < 0 ||
-            serialputc(c + '0') < 0)
-            return false;
-    }
-    
-    return true;
-}   
+} 
 
 bool ESP8266::execute(char *resp_buf, int *resp_len) {
     // Finish command with a newline
-    if (serialputc('\r') < 0 || serialputc('\n') < 0)
+    if (!(command("\r\n") && discardEcho()))
         return false;
         
-    // Drop echoed string
-    while (true) {
-        int c = serialgetc();
-        
-        if (c < 0)
-            return false;
-        else if (c == '\n')
-            break;
-    }
-        
     // Read in response if any
     if (resp_buf && resp_len) {
         int i;
@@ -390,6 +419,5 @@
         DBG("command response:\t %.*s", *resp_len, resp_buf);
     }
     
-    // Flush to next prompt
-    return discardEcho();
+    return flush();
 }