now this shit works
Fork of ESP8266NodeMCUInterface by
Revision 46:4abb80f0a920, committed 2015-06-04
- Comitter:
- geky
- Date:
- Thu Jun 04 20:14:52 2015 +0000
- Parent:
- 44:16da10e7b3f7
- Child:
- 47:04632d22a723
- Commit message:
- Seperated sent messages into chunks to keep from overflowing the Lua buffer
Changed in this revision
--- 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();
}
--- a/ESP8266/ESP8266.h Wed Jun 03 21:44:20 2015 +0000
+++ b/ESP8266/ESP8266.h Thu Jun 04 20:14:52 2015 +0000
@@ -33,6 +33,7 @@
#define ESP_TCP_TYPE 1
#define ESP_UDP_TYPE 0
+#define ESP_MAX_LINE 62
/**
* The ESP8266 class
@@ -194,6 +195,13 @@
bool discardEcho();
/**
+ * Flushes to next prompt
+ *
+ * @return true if successful
+ */
+ bool flush();
+
+ /**
* Send part of a command to the wifi module.
*
* @param cmd string to be sent
@@ -204,15 +212,6 @@
bool command(const char *cmd);
/**
- * Sanitizes and sends payload to wifi module
- *
- * @param data data to send
- * @param len length of data
- * @return true if successful
- */
- bool payload(const char *data, int len);
-
- /**
* Execute the command sent by command
*
* @param resp_buf pointer to buffer to store response from the wifi module
--- a/Socket/Socket.cpp Wed Jun 03 21:44:20 2015 +0000
+++ b/Socket/Socket.cpp Thu Jun 04 20:14:52 2015 +0000
@@ -28,7 +28,7 @@
}
void Socket::set_blocking(bool blocking, unsigned int timeout) {
- printf("set blocking: %d %d\r\n", blocking, timeout);
+ //printf("set blocking: %d %d\r\n", blocking, timeout);
_blocking = blocking;
_timeout = timeout;
}
