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:
- 16:3f0efaa57a12
- Parent:
- 15:37a7a56a424f
- Child:
- 17:d11fa4c3ac65
--- a/ESP8266/ESP8266.cpp Sun Nov 30 21:56:03 2014 +0000
+++ b/ESP8266/ESP8266.cpp Mon Dec 01 06:22:00 2014 +0000
@@ -18,6 +18,7 @@
#include "mbed.h"
#include "ESP8266.h"
+#include "Endpoint.h"
#include <string>
#include <algorithm>
@@ -63,14 +64,16 @@
inst = this;
attach_rx(false);
+
+ wifi.baud(9600); // initial baud rate of the ESP8266
+ pc.printf("ESP8266 test\r\n");
}
bool ESP8266::join()
{
-
- for (int i= 0; i < MAX_TRY_JOIN; i++) {
-
- //join the network
+ string cmd="AT+CWJAP=\""+(string)this->ssid+"\",\""+(string)this->phrase+"\"";
+ if( sendCommand( cmd.c_str(), "OK", NULL, 10000) ){
+ // successfully joined the network
state.associated = true;
INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase);
return true;
@@ -80,7 +83,7 @@
bool ESP8266::connect()
{
- return join();
+ return true;
}
bool ESP8266::is_connected()
@@ -88,8 +91,20 @@
return true;
}
+bool ESP8266::startUDP(char* ip, int port){
+ char* portstr = "";
+ sprintf(portstr, "%d", port);
+
+ sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"" + (string) portstr + "\"").c_str(), "OK", NULL, 10000);
+
+ sendCommand("AT+CIPSEND", "OK", NULL, 1000);// go into transparent mode
+
+ return true;
+}
+
bool ESP8266::close()
{
+ sendCommand("AT+CIPCLOSE","OK", NULL, 10000);
return true;
}
@@ -99,13 +114,14 @@
if (!state.associated)
return true;
// send command to quit AP
- //
+ sendCommand("AT+CWQAP", "OK", NULL, 10000);
state.associated = false;
return true;
}
char* ESP8266::getIPAddress()
{
+ sendCommand("AT+CWLIF", "OK", NULL, 10000);
return ipString;
}
@@ -133,22 +149,30 @@
void ESP8266::reset()
{
+ sendCommand("AT+RST", "ready", NULL, 10000);
+ /*
reset_pin = 0;
wait(0.2);
reset_pin = 1;
wait(0.2);
+ */
}
bool ESP8266::reboot()
{
+ reset();
return true;
}
void ESP8266::handler_rx(void)
{
//read characters
- while (wifi.readable())
- buf_ESP8266.queue(wifi.getc());
+ char c;
+ while (wifi.readable()){
+ c=wifi.getc();
+ buf_ESP8266.queue(c);
+ //pc.printf("%c",c);
+ }
}
void ESP8266::attach_rx(bool callback)
@@ -188,12 +212,17 @@
buf_ESP8266.flush();
}
-bool ESP8266::sendCommand(const char * cmd, const char * ack, char * res, int timeout)
+int ESP8266::send(const char * buf, int len)
{
- return true;
+ const char* bufptr=buf;
+ while(!wifi.writeable()){}
+ for(int i=0; i<len; i++){
+ wifi.putc((int)*bufptr++);
+ while(!wifi.writeable()){}
+ }return true;
}
-int ESP8266::send(const char * str, int len, const char * ACK, char * res, int timeout)
+bool ESP8266::sendCommand(const char * cmd, const char * ACK, char * res, int timeout)
{
char read;
size_t found = string::npos;
@@ -201,25 +230,38 @@
Timer tmr;
int result = 0;
- DBG("will send: %s\r\n",str);
+ DBG("will send: %s\r\n",cmd);
attach_rx(false);
//We flush the buffer
while (wifi.readable())
wifi.getc();
+
+ while(!wifi.writeable()){};
if (!ACK || !strcmp(ACK, "NO")) {
- for (int i = 0; i < len; i++)
- result = (putc(str[i]) == str[i]) ? result + 1 : result;
+ for (int i = 0; i < sizeof(cmd); i++){
+ result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
+ while(!wifi.writeable()){};
+ }
+ putc(13); //CR
+ while(!wifi.writeable()){};
+ putc(10); //LF
+
} else {
//We flush the buffer
while (wifi.readable())
wifi.getc();
tmr.start();
- for (int i = 0; i < len; i++)
- result = (putc(str[i]) == str[i]) ? result + 1 : result;
+ for (int i = 0; i < sizeof(cmd); i++){
+ result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
+ while(!wifi.writeable()){};
+ }
+ putc(13); //CR
+ while(!wifi.writeable()){};
+ putc(10); //LF
while (1) {
if (tmr.read_ms() > timeout) {
@@ -233,6 +275,7 @@
return -1;
} else if (wifi.readable()) {
read = wifi.getc();
+ pc.putc(read);//debug
if ( read != '\r' && read != '\n') {
checking += read;
found = checking.find(ACK);
@@ -299,3 +342,16 @@
DBG("result: %d\r\n", result)
return result;
}
+
+void ESP8266::ATcommand(char* command){
+ char* cmd=command;
+ while(!wifi.writeable() || wifi.readable()){}
+ while(*cmd){
+ wifi.putc((int)*cmd++);
+ wait(.005); // wait for the echo
+ while(!wifi.writeable() || wifi.readable()){}
+ }
+ wifi.putc(13); //CR
+ while(!wifi.writeable() || wifi.readable()){}
+ wifi.putc(10); //LF
+}
ESP8266

Adafruit Huzzah