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:
- 40:0a83315aea0a
- Parent:
- 38:86e75901efc1
- Child:
- 41:264902b160ea
--- a/ESP8266/ESP8266.cpp Sat May 02 04:08:30 2015 +0000
+++ b/ESP8266/ESP8266.cpp Mon May 04 12:04:54 2015 +0000
@@ -42,13 +42,11 @@
#define ESP_MAX_TRY_JOIN 3
#define ESP_MAXID 4 // the largest possible ID Value (max num of sockets possible)
-extern Serial pc;
-
ESP8266 * ESP8266::inst;
char* ip = NULL;
-ESP8266::ESP8266( PinName tx, PinName rx, PinName _reset, const char * ssid, const char * phrase, uint32_t baud):
- wifi(tx, rx), reset_pin(_reset), buf_ESP8266(ESP_MBUFFE_MAX)
+ESP8266::ESP8266(PinName tx, PinName rx, PinName reset, const char *ssid, const char *phrase, uint32_t baud) :
+ wifi(tx, rx), reset_pin(reset), buf_ESP8266(ESP_MBUFFE_MAX)
{
INFO("Initializing ESP8266 object");
memset(&state, 0, sizeof(state));
@@ -59,13 +57,13 @@
if (this->ssid[i] == ' ')
this->ssid[i] = '$';
}
+
strcpy(this->phrase, phrase);
for (int i = 0; i < strlen(phrase); i++) {
if (this->phrase[i] == ' ')
this->phrase[i] = '$';
}
-
inst = this;
attach_rx(false);
@@ -167,7 +165,6 @@
char portstr[5];
sprintf(portstr, "%d", port);
sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000);
-
sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode
sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode
//DBG("Data Mode");
@@ -178,8 +175,9 @@
bool ESP8266::close()
{
+ wait(0.1f);
send("+++",3);
- wait(1);
+ wait(1.0f);
state.cmdMode = true;
sendCommand("AT+CIPCLOSE","OK", NULL, 10000);
return true;
@@ -257,9 +255,9 @@
void ESP8266::reset()
{
reset_pin = 0;
- wait(0.2);
+ wait_us(20);
reset_pin = 1;
- wait(1);
+ //wait(1);
//send("+++",3);
//wait(1);
@@ -289,10 +287,12 @@
void ESP8266::attach_rx(bool callback)
{
- if (!callback)
+ if (!callback) {
wifi.attach(NULL);
- else
+ }
+ else {
wifi.attach(this, &ESP8266::handler_rx);
+ }
}
int ESP8266::readable()
@@ -338,7 +338,7 @@
{
char read;
size_t found = string::npos;
- string checking;
+ string checking = "";
Timer tmr;
int result = 0;
@@ -353,10 +353,10 @@
if (!ACK || !strcmp(ACK, "NO")) {
for (int i = 0; i < strlen(cmd); i++) {
result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
- wait(.005); // prevents stuck recieve ready (?) need to let echoed character get read first
+ wait(0.005f); // prevents stuck recieve ready (?) need to let echoed character get read first
}
putc(13); //CR
- wait(.005); // wait for echo
+ wait(0.005f); // wait for echo
putc(10); //LF
} else {
@@ -370,7 +370,7 @@
wait(.005); // wait for echo
}
putc(13); //CR
- wait(.005); // wait for echo
+ wait(0.005f); // wait for echo
putc(10); //LF
while (1) {
@@ -385,17 +385,17 @@
return -1;
} else if (readable()) {
read = getc();
- printf("%c",read); //debug echo
+ //printf("%c",read); //debug echo
if ( read != '\r' && read != '\n') {
checking += read;
found = checking.find(ACK);
if (found != string::npos) {
- wait(0.01);
+ wait(0.01f);
//We flush the buffer
while (readable())
read = getc();
- printf("%c",read); //debug echo
+ //printf("%c",read); //debug echo
break;
}
}
ESP8266

Adafruit Huzzah