Library for using the Wifi module ESP8266, first version with basic commands, soon new commands will be added

Dependents:   PIR_Sensor_wifi myESP8266forLOstFound Motin_Activated_Temperature_Humidity_BluetoothHC-06_ESP8266_IOT 1Project3_Robot1_Final ... more

Library updated in Dec 28, 2014. Now the ESP baudrate is passed as a parameter in the constructor. Also, some new functions were added. Here is an example code:

ESP8266 example code

#include "mbed.h"
#include <string>
#include "ESP8266.h"

// Objects
Serial pc(USBTX, USBRX);
ESP8266 esp(PTE0, PTE1, 115200);

// Global variables
char snd[255], rcv[1000]; // Strings for sending and receiving commands / data / replies

int main() {
    pc.baud(115200);
    pc.printf("START\r\n");
    wait(3);
    
    pc.printf("Reset ESP\r\n");
    esp.Reset();
    esp.RcvReply(rcv, 400);
    pc.printf("%s", rcv);
    wait(2);
    pc.printf("Sending AT\r\n");
    strcpy(snd, "AT");
    esp.SendCMD(snd);
    esp.RcvReply(rcv, 400);
    pc.printf("%s", rcv);
    wait(2);
    pc.printf("Set mode to AP\r\n");
    esp.SetMode(1);
    esp.RcvReply(rcv, 1000);
    pc.printf("%s", rcv);
    
    pc.printf("Receiving Wifi List\r\n");
    esp.GetList(rcv);
    pc.printf("%s", rcv);
    
    pc.printf("Connecting to AP\r\n");
    esp.Join("MyAP", "MyPasswd"); // Replace MyAP and MyPasswd with your SSID and password
    esp.RcvReply(rcv, 1000);
    pc.printf("%s", rcv);
    wait(8);
    pc.printf("Getting IP\r\n");
    esp.GetIP(rcv);
    pc.printf("%s", rcv);
    pc.printf("Setting multiple connections\r\n");
    esp.SetMultiple();
    esp.RcvReply(rcv, 1000);
    pc.printf("%s", rcv);
    pc.printf("Getting Connection Status\r\n");
    esp.GetConnStatus(rcv);
    pc.printf("%s", rcv);
    pc.printf("Start server mode on port 80\r\n");
    esp.StartServerMode(80);
    esp.RcvReply(rcv, 1000);
    pc.printf("%s", rcv);
    wait(4);
    pc.printf("Close server mode\r\n");
    esp.CloseServerMode();
    esp.RcvReply(rcv, 1000);
    pc.printf("THE END");
    while(1);
}

Files at this revision

API Documentation at this revision

Comitter:
quevedo
Date:
Sun Dec 28 21:58:49 2014 +0000
Parent:
1:399414d48048
Commit message:
Added functions: Get connection status, Start server mode, Close server mode; now the ESP serial baudrate is passed as a parameter in the constructor

Changed in this revision

ESP8266.cpp Show annotated file Show diff for this revision Revisions of this file
ESP8266.h Show annotated file Show diff for this revision Revisions of this file
--- a/ESP8266.cpp	Wed Dec 17 13:54:34 2014 +0000
+++ b/ESP8266.cpp	Sun Dec 28 21:58:49 2014 +0000
@@ -1,8 +1,8 @@
 #include "ESP8266.h"
 
 // Constructor
-ESP8266::ESP8266(PinName tx, PinName rx) : comm(tx, rx) {
-    comm.baud(115200);
+ESP8266::ESP8266(PinName tx, PinName rx, int br) : comm(tx, rx) {
+    comm.baud(br);
 }
 
 // Destructor
@@ -25,6 +25,29 @@
     s[k + 1] = 0;
 }
 
+// Converts integer number to null-terminated string
+void ESP8266::itoa(int n, char * s) {
+    char k = 0;
+    char r[11];
+    
+    if(n == 0) {
+        s[0] = '0';
+        s[1] = 0;
+    } else {
+        while(n != 0) {
+            r[k]= (n % 10) + '0';
+            n = n / 10;
+            k++;
+        }
+        while(k > 0) {
+            s[n] = r[k - 1] + '0';
+            n++;
+            k--;
+        }
+        s[n] = 0;
+    }
+}
+
 // Sends command to ESP8266. Receives the command string
 void ESP8266::SendCMD(char * s) {
     AddEOL(s);
@@ -117,4 +140,29 @@
     char rs[15];
     strcpy(rs, "AT+CIPMUX=1");
     SendCMD(rs);
+}
+
+// Gets connection status. Parameter: string to contain status
+void ESP8266::GetConnStatus(char * st) {
+    char cmd[15];
+    strcpy(cmd, "AT+CIPSTATUS");
+    SendCMD(cmd);
+    RcvReply(st, 2000);
+}
+
+// Starts server mode. Parameter: port to be used
+void ESP8266::StartServerMode(int port) {
+    char rs[25];
+    char t[4];
+    strcpy(rs, "AT+CIPSERVER=1,");
+    itoa(port, t);
+    strcat(rs, t);
+    SendCMD(rs);
+}
+
+// Close server mode.
+void ESP8266::CloseServerMode(void) {
+    char rs[20];
+    strcpy(rs, "AT+CIPSERVER=0");
+    SendCMD(rs);
 }
\ No newline at end of file
--- a/ESP8266.h	Wed Dec 17 13:54:34 2014 +0000
+++ b/ESP8266.h	Sun Dec 28 21:58:49 2014 +0000
@@ -12,8 +12,9 @@
   *
   * @param tx TX pin
   * @param rx RX pin
+  * @param br Baud Rate
   */
-  ESP8266(PinName tx, PinName rx);
+  ESP8266(PinName tx, PinName rx, int br);
 
   /**
   * ESP8266 destructor
@@ -30,11 +31,15 @@
 void Quit(void);
 void SetSingle(void);
 void SetMultiple(void);
+void GetConnStatus(char * st);
+void StartServerMode(int port);
+void CloseServerMode(void);
 
 private:
 Serial comm;
 void AddEOL(char * s);
 void AddChar(char * s, char c);
+void itoa(int c, char s[]);
 
 };
   
@@ -51,12 +56,12 @@
     AT+CWQAP: quit the AP wifi; Inquiry: AT+CWQAP=?  
     * AT+CWSAP: set the parameters of AP; AT+CWSAP= <ssid>,<pwd>,<chl>,<ecn> - ssid, pwd, chl = channel, ecn = encryption; Inquiry: AT+CWJAP?
     TCP/IP:
-    * AT+CIPSTATUS: get the connection status
+    AT+CIPSTATUS: get the connection status
     * AT+CIPSTART: set up TCP or UDP connection 1)single connection (+CIPMUX=0) AT+CIPSTART= <type>,<addr>,<port>; 2) multiple connection (+CIPMUX=1) AT+CIPSTART= <id><type>,<addr>, <port> - id = 0-4, type = TCP/UDP, addr = IP address, port= port; Inquiry: AT+CIPSTART=?
     * AT+CIPSEND: send data; 1)single connection(+CIPMUX=0) AT+CIPSEND=<length>; 2) multiple connection (+CIPMUX=1) AT+CIPSEND= <id>,<length>; Inquiry: AT+CIPSEND=?
     * AT+CIPCLOSE: close TCP or UDP connection; AT+CIPCLOSE=<id> or AT+CIPCLOSE; Inquiry: AT+CIPCLOSE=?
     AT+CIFSR: Get IP address; Inquiry: AT+ CIFSR=?
     AT+CIPMUX:  set mutiple connection; AT+ CIPMUX=<mode> - 0 for single connection 1 for mutiple connection; Inquiry: AT+CIPMUX?
-    * AT+CIPSERVER: set as server; AT+ CIPSERVER= <mode>[,<port> ] - mode 0 to close server mode, mode 1 to open; port = port; Inquiry: AT+CIFSR=?
+    AT+CIPSERVER: set as server; AT+ CIPSERVER= <mode>[,<port> ] - mode 0 to close server mode, mode 1 to open; port = port; Inquiry: AT+CIFSR=?
     * +IPD: received data
 */
\ No newline at end of file