now this shit works
Fork of ESP8266NodeMCUInterface by
Revision 49:a7741fe989ca, committed 2016-03-13
- Comitter:
- krbng4180
- Date:
- Sun Mar 13 21:09:31 2016 +0000
- Parent:
- 48:6031f70e3914
- Child:
- 50:7c4a5bdcb624
- Commit message:
- it works!
Changed in this revision
| ESP8266/ESP8266.cpp | Show annotated file Show diff for this revision Revisions of this file |
| ESP8266/ESP8266.h | Show annotated file Show diff for this revision Revisions of this file |
--- 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()))
--- a/ESP8266/ESP8266.h Thu Jun 04 20:45:00 2015 +0000
+++ b/ESP8266/ESP8266.h Sun Mar 13 21:09:31 2016 +0000
@@ -171,6 +171,16 @@
return _inst;
};
+ /**
+ * Send part of a command to the wifi module.
+ *
+ * @param cmd string to be sent
+ * @param len optional length of cmd
+ * @param sanitize flag indicating if cmd is actually payload and needs to be escaped
+ * @return true if successful
+ */
+ bool command(const char *cmd);
+
private:
/**
* Read a character with timeout
@@ -201,15 +211,7 @@
*/
bool flush();
- /**
- * Send part of a command to the wifi module.
- *
- * @param cmd string to be sent
- * @param len optional length of cmd
- * @param sanitize flag indicating if cmd is actually payload and needs to be escaped
- * @return true if successful
- */
- bool command(const char *cmd);
+
/**
* Execute the command sent by command
@@ -219,6 +221,7 @@
* @return true if successful
*/
bool execute(char *resp_buffer = 0, int *resp_len = 0);
+ bool execute_full_length(char *resp_buffer = 0, int *resp_len = 0);
protected:
BufferedSerial _serial;
