Miguel Maldonado / ESP8266

Dependents:   ESP8266_Connect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP8266.cpp Source File

ESP8266.cpp

00001 #include "ESP8266.h"
00002 
00003 // Constructor
00004 ESP8266::ESP8266(PinName tx, PinName rx, int br) : comm(tx, rx) {
00005     comm.baud(br);
00006 }
00007 
00008 // Destructor
00009 ESP8266::~ESP8266() { }
00010 
00011 // Add <CR> + <LF> at the end of the string
00012 void ESP8266::AddEOL(char * s) {
00013     char k;
00014     k = strlen(s); // Finds position of NULL character
00015     s[k] = 0x0D; // switch NULL for <CR>
00016     s[k + 1] = 0x0A; // Add <LF>
00017     s[k + 2] = 0; // Add NULL at the end
00018 }
00019 
00020 // Add one ASCII character at the end of the string
00021 void ESP8266::AddChar(char * s, char c) {
00022     char k;
00023     k = strlen(s);
00024     s[k] = c;
00025     s[k + 1] = 0;
00026 }
00027 
00028 // Converts integer number to null-terminated string
00029 void ESP8266::itoa(int n, char * s) {
00030     char k = 0;
00031     char r[11];
00032     
00033     if(n == 0) {
00034         s[0] = '0';
00035         s[1] = 0;
00036     } else {
00037         while(n != 0) {
00038             r[k]= (n % 10) + '0';
00039             n = n / 10;
00040             k++;
00041         }
00042         while(k > 0) {
00043             s[n] = r[k - 1] + '0';
00044             n++;
00045             k--;
00046         }
00047         s[n] = 0;
00048     }
00049 }
00050 
00051 // Sends command to ESP8266. Receives the command string
00052 void ESP8266::SendCMD(char * s) {
00053     AddEOL(s);
00054     comm.printf("%s", s);
00055 }
00056 
00057 // Resets the ESP8266
00058 void ESP8266::Reset(void) {
00059     char rs[10];
00060     strcpy(rs, "AT+RST");
00061     SendCMD(rs);
00062 }
00063 
00064 // Receive reply until no character is received after a given timeout in miliseconds
00065 void ESP8266::RcvReply(char * r, int to) {
00066     Timer t;
00067     bool ended = 0;
00068     char c;
00069     
00070     strcpy(r, "");
00071     t.start();
00072     while(!ended) {
00073         if(comm.readable()) {
00074             c = comm.getc();
00075             AddChar(r, c);
00076             t.start();
00077         }
00078         if(t.read_ms() > to) {
00079                 ended = 1;
00080         }
00081     }
00082     AddChar(r, 0x00);
00083 }
00084 
00085 // Receive reply until no character is received after a given timeout in miliseconds
00086 void ESP8266::RcvSingleReply(char * r) {
00087     bool ended = 0;
00088     char c;
00089     
00090     strcpy(r, "");
00091     while(!ended) {
00092         if(comm.readable()) {
00093             c = comm.getc();
00094             AddChar(r, c);
00095         }
00096         if(c == 0x0D) {
00097                 ended = 1;
00098         }
00099     }
00100     //AddChar(r, 0x00);
00101 }
00102 
00103 // Gets the AP list. Parameter: the string to receive the list
00104 void ESP8266::GetList(char * l) {
00105     char rs[15];
00106     strcpy(rs, "AT+CWLAP");
00107     SendCMD(rs);
00108     RcvReply(l, 5000); // Needs big timeout because it takes long to start replying
00109 }
00110 
00111 // Joins a Wifi AP. Parameters: SSID and Password (strings)
00112 void ESP8266::Join(char * id, char * pwd) {
00113     char cmd[255];
00114     strcpy(cmd, "AT+CWJAP=");
00115     AddChar(cmd, 0x22);
00116     strcat(cmd, id);
00117     AddChar(cmd, 0x22);
00118     AddChar(cmd, 0x2C);
00119     AddChar(cmd, 0x22);
00120     strcat(cmd, pwd);
00121     AddChar(cmd, 0x22);
00122     SendCMD(cmd);
00123 }
00124 
00125 // Gets ESP IP. Parameter: string to contain IP
00126 void ESP8266::GetIP(char * ip) {
00127     char cmd[15];
00128     strcpy(cmd, "AT+CIFSR");
00129     SendCMD(cmd);
00130     RcvReply(ip, 2000);
00131 }
00132 
00133 //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both
00134 void ESP8266::SetMode(char mode) {
00135     char cmd[15];
00136     strcpy(cmd, "AT+CWMODE=");
00137     mode = mode + 0x30; // Converts number into corresponding ASCII character
00138     AddChar(cmd, mode); // Completes command string
00139     SendCMD(cmd);
00140 }
00141 
00142 // Quits the AP
00143 void ESP8266::Quit(void) {
00144     char cmd[15];
00145     strcpy(cmd, "AT+CWQAP");
00146     SendCMD(cmd);
00147 }
00148 
00149 // Sets single connection
00150 void ESP8266::SetSingle(void) {
00151     char cmd[15];
00152     strcpy(cmd, "AT+CIPMUX=0");
00153     SendCMD(cmd);
00154 }
00155 
00156 // Sets multiple connection
00157 void ESP8266::SetMultiple(void) {
00158     char rs[15];
00159     strcpy(rs, "AT+CIPMUX=1");
00160     SendCMD(rs);
00161 }
00162 
00163 // Gets connection status. Parameter: string to contain status
00164 void ESP8266::GetConnStatus(char * st) {
00165     char cmd[15];
00166     strcpy(cmd, "AT+CIPSTATUS");
00167     SendCMD(cmd);
00168     RcvSingleReply(st);
00169     //RcvReply(st, 2000);
00170 }
00171 
00172 // Starts server mode. Parameter: port to be used
00173 void ESP8266::StartServerMode(int port) {
00174     char rs[25];
00175     char t[4];
00176     strcpy(rs, "AT+CIPSERVER=1,");
00177     itoa(port, t);
00178     strcat(rs, t);
00179     SendCMD(rs);
00180 }
00181 
00182 // Close server mode.
00183 void ESP8266::CloseServerMode(void) {
00184     char rs[20];
00185     strcpy(rs, "AT+CIPSERVER=0");
00186     SendCMD(rs);
00187 }
00188 
00189 // Starts SmartConfig
00190 void ESP8266::StartSmartConfig(void){
00191     char rs[20];
00192     strcpy(rs, "AT+CWSTARTSMART=1");
00193     SendCMD(rs);    
00194 }
00195 
00196 //Disable Echo
00197 void ESP8266::DisableEcho(void) {
00198     char cmd[15];
00199     strcpy(cmd, "ATE0");
00200     SendCMD(cmd);
00201 }