ESP8266 driver using the NodeMCU interface
Dependencies: BufferedSerial
Dependents: esp8266_nodeMCU1 esp8266_2_thingspeak1 Solarator_0-0-2 IoTBurglar_and_Fire_AlarmSystem ... more
Fork of ESP8266Interface by
This is an alternative implementation of the ESP8266 driver that uses the NodeMCU firmware. The NodeMCU firmware provides a slightly larger feature set than the default firmware through a Lua interpreter.
Note
This library is currently in Alpha. It is not feature complete and has some bugs, proceed with caution. Fixes and patches are welcome!
Interface changes
- SSID and passphrase moved out of ESP8266Interface constructor and to ESP8266Interface::connect
- ESP8266Interface constructor provides optional timeout parameter to specify how long to wait for network operations
Note
NodeMCU defaults to a baud rate of 9600 instead of 115200 used by the default firmware.
Firmware
To install the NodeMCU firmware, follow the instructions on the Firmware Update wiki page using the nodemcu_integer_0.9.6-dev_20150406.bin binary at address 0x00000 instead of boot_v1.1.bin and user1.bin.
Since the NodeMCU firmware defaults to a baud rate of 9600, the Serial Passthrough program can be used to get direct access to the Lua interpreter running on the ESP8266.
Status
Working features:
- TCP Client
- UDP Client Transmit (Currently only UDP Server can recieve messages)
- Single Connection at a time
- Station Mode (Connects to AP)
- DNS Lookups
To be implemented:
- TCP Server
- UDP Server
- UDP Client recieve
- Multiple Connections tracked through Lua variables
- AP Mode (Act as access point)
- IPV6 support (Existing issue with NodeMCU)
Diff: ESP8266/ESP8266.cpp
- 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();
}
ESP8266

Adafruit Huzzah