Socket interface for ESP8266. Implements the NetworkSocketAPI. Requires device to use the Espressif Firmware.

Dependencies:   ESP8266

Dependents:   ESP8266InterfaceTests HelloESP8266Interface

Fork of ESP8266Interface by NetworkSocketAPI

Note

This library assumes your ESP8266 is running the Espressif Firmware. For instructions on how to update your ESP8266 to use the correct firmware see the Firmware Update Wiki Page.

Currently the ESP8266Interface LIbrary has the following Abilities:

Working

  • TCP Client
  • UDP Client
  • Transparent mode (single connection of 1 type at a time)
  • Station Mode (connects to AP)

To be implimented

  • TCP Server
  • UDP Server
  • Multi Connection Mode (able to have up to 5 sockets at a time)
  • AP Mode (Make ESP Chip act like access point)
  • DNS Support (currently websites must be looked up by IP)
  • Error Recovery

Nice but not necessary

  • colorized text for ESP AT Commands in Command line (easier to differentiate from other text)
Revision:
19:783c46b13285
Parent:
18:9fc7976c7b27
Child:
20:5d0762aa4680
--- a/ESP8266Interface.cpp	Fri Jul 17 23:15:21 2015 +0000
+++ b/ESP8266Interface.cpp	Tue Jul 21 19:54:52 2015 +0000
@@ -35,13 +35,13 @@
 
 int32_t ESP8266Interface::init(void) 
 {   
-    if(!atParser.command("AT","OK"))
+    if (!(atParser.send("AT") && atParser.recv("OK")))
         return -1;
-    if(!atParser.command("AT+RST", "OK\r\nready"))
+    if (!(atParser.send("AT+RST") && atParser.recv("OK\r\nready")))
         return -1;
-    if(!atParser.command("AT+CWMODE=3", "OK"))
+    if (!(atParser.send("AT+CWMODE=3") && atParser.recv("OK")))
         return -1;
-    if(!atParser.command("AT+CIPMUX=1", "OK"))
+    if (!(atParser.send("AT+CIPMUX=1") && atParser.recv("OK")))
         return -1;
     return 0;
     
@@ -59,11 +59,11 @@
 
 int32_t ESP8266Interface::connect(const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms)
 {
-    if(!atParser.command("AT+CWDHCP=2,1","OK"))
+    if (!(atParser.send("AT+CWDHCP=2,1") && atParser.recv("OK")))
         return -1;
     string connect_command = "AT+CWJAP=\""+(string)ap+"\",\""+(string)pass_phrase+"\"";
     atParser.setTimeout(10000);
-    if(!atParser.command(connect_command.c_str(),"OK")){
+    if (!(atParser.send(connect_command.c_str()) && atParser.recv("OK"))){
         return -1;
     }
     return 0;    
@@ -77,7 +77,7 @@
 char *ESP8266Interface::getIPAddress(void)
 {
     char* ip;
-    if(!atParser.command("AT+CIFSR", "+CIFSR:%s OK", ip))
+    if (!(atParser.send("AT+CIPSTA?") && atParser.recv("+CIPSTA:\"%s\"\r\nOK", ip)))
         return NULL;
     return ip;
 }
@@ -194,7 +194,7 @@
     else if(_type == SOCK_TCP)
         sock_type = "TCP";
     string start_command = "AT+CIPSTART="+(string)idstr+",\""+sock_type+"\",\""+(string)_addr+"\","+(string)portstr;
-    if(!atParser->command(start_command.c_str(),"OK")){
+    if (!(atParser->send(start_command.c_str()) && atParser->recv("OK"))){
         return -1;//opening socket not succesful
     }
     return 0;
@@ -221,16 +221,13 @@
 uint32_t ESP8266Socket::recv(void *data, uint32_t amount, uint32_t timeout_ms)
 {
     atParser->setTimeout((int)timeout_ms);
-    char idstr[2];
-    sprintf(idstr,"%d",_id);
-    string matching = "+IPD,"+(string)idstr+",%d:";
     int length;
-    if(!atParser->recv(matching.c_str(),&length))
-        return 0;
-    
-    if(!atParser->read((char *)data,length))
-        return 0;
-    
+    int id;
+    if (!(atParser->recv("+IPD,%d,%d:", &id, &length) && 
+      atParser->read((char*)data, length) &&
+      atParser->recv("OK"))){  
+        return false;
+    }
     return length;   
 }
 
@@ -240,7 +237,7 @@
     sprintf(idstr,"%d",_id);
     string close_command = "AT+CIPCLOSE="+(string)idstr;  
     
-    if(!atParser->command(close_command.c_str(),"OK")){
+    if (!(atParser->send(close_command.c_str()) && atParser->recv("OK"))){
         return -1;//opening socket not succesful
     }
     return 0;