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:
23:fd0f3197c30b
Parent:
22:312453862371
Child:
24:37504440f296
Child:
25:91c4e9d34b77
--- a/ESP8266Interface.cpp	Wed Jul 22 20:53:09 2015 +0000
+++ b/ESP8266Interface.cpp	Thu Jul 23 21:25:30 2015 +0000
@@ -18,30 +18,23 @@
 #include <string>
 
 ESP8266Interface::ESP8266Interface(PinName tx, PinName rx)
-: serial(tx, rx), atParser(&serial)
+:esp8266(tx,rx)
 {
     uuidCounter = 0;
-    serial.baud(115200);
     for(int i = 0; i<numSockets; i++){
         availableID[i] = -1;
     }
 }
 
-ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms)
-: serial(tx, rx), atParser(&serial)
-{
-
-}
-
 int32_t ESP8266Interface::init(void) 
 {   
-    if (!(atParser.send("AT") && atParser.recv("OK")))
+    if (!esp8266.startup())
         return -1;
-    if (!(atParser.send("AT+RST") && atParser.recv("OK\r\nready")))
+    if (!esp8266.reset())
         return -1;
-    if (!(atParser.send("AT+CWMODE=3") && atParser.recv("OK")))
+    if (!esp8266.wifiMode(3))
         return -1;
-    if (!(atParser.send("AT+CIPMUX=1") && atParser.recv("OK")))
+    if (!esp8266.multipleConnections(true))
         return -1;
     return 0;
     
@@ -49,7 +42,7 @@
 
 int32_t ESP8266Interface::init(const char *ip, const char *mask, const char *gateway) 
 {
-
+    return -1;
 }
 
 int32_t ESP8266Interface::connect(uint32_t timeout_ms) 
@@ -59,11 +52,11 @@
 
 int32_t ESP8266Interface::connect(const char *ap, const char *pass_phrase, wifi_security_t security, uint32_t timeout_ms)
 {
-    if (!(atParser.send("AT+CWDHCP=2,1") && atParser.recv("OK")))
+    esp8266.setTimeout(timeout_ms);
+    
+    if (!esp8266.dhcp(1,true))
         return -1;
-    string connect_command = "AT+CWJAP=\""+(string)ap+"\",\""+(string)pass_phrase+"\"";
-    atParser.setTimeout(10000);
-    if (!(atParser.send(connect_command.c_str()) && atParser.recv("OK"))){
+    if (!esp8266.connect(ap,pass_phrase)){
         return -1;
     }
     return 0;    
@@ -71,7 +64,7 @@
 
 int32_t ESP8266Interface::disconnect(void)
 {
-    if (!(atParser.send("AT+CWQAP") && atParser.recv("OK")))
+    if (!esp8266.disconnect())
         return -1;
     for(int i=0; i<numSockets; i++){
         SocketInterface *socket = sockets[availableID[i]];
@@ -83,7 +76,7 @@
 
 char *ESP8266Interface::getIPAddress(void)
 {
-    if (!(atParser.send("AT+CIPSTA?") && atParser.recv("+CIPSTA:\"%[^\"]\"", ip)))
+    if(!esp8266.getIPAddress(ip))
         return NULL;
     return ip;
 }
@@ -122,7 +115,7 @@
     if (id == -1){
         return NULL;//tried  to allocate more than the maximum 5 sockets   
     }
-    ESP8266Socket *socket = new ESP8266Socket(uuidCounter++, &atParser, socketProtocol, (uint8_t)id);
+    ESP8266Socket *socket = new ESP8266Socket(uuidCounter++, &esp8266, socketProtocol, (uint8_t)id);
     sockets[socket->getHandle()] = socket;
     return socket;
 }
@@ -148,8 +141,8 @@
     }
 }
 
-ESP8266Socket::ESP8266Socket(uint32_t handle, ATParser *atParser, socket_protocol_t type, uint8_t id)
-: atParser(atParser), _id(id)
+ESP8266Socket::ESP8266Socket(uint32_t handle, ESP8266 *driver, socket_protocol_t type, uint8_t id)
+: driver(driver), _id(id)
 {
     _handle = handle;
     SocketInterface::_type = type;
@@ -204,18 +197,13 @@
 
 int32_t ESP8266Socket::open() 
 {
-    char portstr[5];
-    char idstr[2];
-    sprintf(idstr,"%d",_id);
-    sprintf(portstr, "%d", _port);
-
     string sock_type;
     if(_type == SOCK_UDP)
         sock_type = "UDP";
     else if(_type == SOCK_TCP)
         sock_type = "TCP";
-    string start_command = "AT+CIPSTART="+(string)idstr+",\""+sock_type+"\",\""+(string)_addr+"\","+(string)portstr;
-    if (!(atParser->send(start_command.c_str()) && atParser->recv("OK"))){
+        
+    if (!driver->openSocket(sock_type, _id, _port, _addr)){
         return -1;//opening socket not succesful
     }
     return 0;
@@ -224,42 +212,26 @@
 
 int32_t ESP8266Socket::send(const void *data, uint32_t amount, uint32_t timeout_ms) 
 {
-    char idstr[2];
-    sprintf(idstr,"%d",_id);
-    char lenstr[5];
-    sprintf(lenstr,"%d",(int)amount);
     
-    atParser->setTimeout((int)timeout_ms);
-    
-    string send_command = "AT+CIPSEND="+(string)idstr+","+(string)lenstr;
-    if(!atParser->send(send_command.c_str())){ 
+    driver->setTimeout((int)timeout_ms);
+    if(!driver->sendData(_id, data, amount)){ 
         return -1;
     }
-    atParser->write((char*)data, (int)amount);
     return 0;
 }
 
 uint32_t ESP8266Socket::recv(void *data, uint32_t amount, uint32_t timeout_ms)
 {
-    atParser->setTimeout((int)timeout_ms);
-    int length;
-    int id;
-    if (!(atParser->recv("+IPD,%d,%d:", &id, &length) && 
-      atParser->read((char*)data, length) &&
-      atParser->recv("OK"))){  
-        return false;
-    }
-    return length;   
+    
+    return driver->recv(data, amount);
+        
 }
 
 int32_t ESP8266Socket::close() const
 {
-    char idstr[2];
-    sprintf(idstr,"%d",_id);
-    string close_command = "AT+CIPCLOSE="+(string)idstr;  
     
-    if (!(atParser->send(close_command.c_str()) && atParser->recv("OK"))){
-        return -1;//opening socket not succesful
+    if (!driver->close(_id)){
+        return -1;//closing socket not succesful
     }
     return 0;
 }