ESP8266 Socket Library. AT Thinker firmware.

Dependents:   ESP8266_MQTT_HelloWorld ESP8266_IFTTT_Test ECE_4180_Lab_4 websocketmbed ... more

Fork of ESP8266Interface by ESP8266

This repository has been superceded

This project has moved to https://developer.mbed.org/teams/ESP8266/code/esp8266-driver/

This library works with the AT Thinker firmware.

Note

This library is currently in Beta. It is not feature complete and has some bugs, proceed with caution! Fixes and patches are welcome and appreciated!

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 implemented

  • 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:
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;
                     }
                 }