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:
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
+}