Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Gateway_Cotosys Gateway_Cotosys_os5
ESP01.cpp
00001 #include "ESP01.h" 00002 00003 // Constructor 00004 ESP01::ESP01(PinName tx, PinName rx, int br) : comm(tx, rx) 00005 { 00006 comm.baud(br); 00007 } 00008 00009 // Destructor 00010 ESP01::~ESP01() { } 00011 00012 // Add <CR> + <LF> at the end of the string 00013 void ESP01::AddEOL(char * s) 00014 { 00015 char k = strlen(s); // Finds position of NULL character 00016 s[k] = '\r'; // switch NULL for <CR> 00017 s[k + 1] = '\n'; // Add <LF> 00018 s[k + 2] = '\0'; // Add NULL at the end 00019 } 00020 00021 // Add one ASCII character at the end of the string 00022 void ESP01::AddChar(char * s, char c) 00023 { 00024 char k = strlen(s); 00025 s[k] = c; 00026 s[k + 1] = '\0'; 00027 } 00028 00029 // Sends command to ESP01. Receives the command string 00030 void ESP01::SendCMD(char* s) 00031 { 00032 comm.printf("%s%s", s, CMD_END); 00033 } 00034 00035 // Resets the ESP01 00036 void ESP01::Reset(void) 00037 { 00038 SendCMD("AT+RST"); 00039 } 00040 00041 // Receive reply until no character is received after a given timeout in miliseconds 00042 bool ESP01::RcvReply(char* r, int to) 00043 { 00044 Timer t; 00045 bool ended = 0; 00046 char c; 00047 00048 strcpy(r, ""); 00049 t.start(); 00050 while(!ended) { 00051 if(comm.readable()) { 00052 c = comm.getc(); 00053 AddChar(r, c); 00054 t.start(); 00055 } 00056 if(t.read_ms() > to) { 00057 ended = 1; 00058 } 00059 } 00060 00061 AddChar(r, '\0'); 00062 return r[0] != '\0'; 00063 } 00064 00065 // Gets the AP list. Parameter: the string to receive the list 00066 bool ESP01::GetList(char *l) 00067 { 00068 SendCMD("AT+CWLAP"); 00069 return RcvReply(l, 5000); // Needs big timeout because it takes long to start replying 00070 } 00071 00072 // Joins a Wifi AP. Parameters: SSID and Password (strings) 00073 void ESP01::Join(char *id, char *pwd) 00074 { 00075 char cmd[255]; 00076 sprintf(cmd, "AT+CWJAP=\"%s\",\"%s\"", id, pwd); 00077 SendCMD(cmd); 00078 } 00079 00080 // Gets ESP IP. Parameter: string to contain IP 00081 bool ESP01::GetIP(char *ip) 00082 { 00083 SendCMD("AT+CIFSR"); 00084 return RcvReply(ip, 3000); 00085 } 00086 00087 //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both 00088 void ESP01::SetMode(WiFiMode mode) 00089 { 00090 char cmd[15]; 00091 strcpy(cmd, "AT+CWMODE="); 00092 AddChar(cmd, mode + '0'); // // Converts number into corresponding ASCII character 00093 SendCMD(cmd); 00094 } 00095 00096 // Quits the AP 00097 void ESP01::Quit(void) 00098 { 00099 SendCMD("AT+CWQAP"); 00100 } 00101 00102 // Sets single connection 00103 void ESP01::SetSingle(void) 00104 { 00105 SendCMD("AT+CIPMUX=0"); 00106 } 00107 00108 // Sets multiple connection 00109 void ESP01::SetMultiple(void) 00110 { 00111 SendCMD("AT+CIPMUX=1"); 00112 } 00113 00114 // Gets connection status. Parameter: string to contain status 00115 bool ESP01::GetConnStatus(char * st) 00116 { 00117 SendCMD("AT+CIPSTATUS"); 00118 return RcvReply(st, 2000); 00119 } 00120 00121 // Starts server mode. Parameter: port to be used 00122 void ESP01::StartServerMode(int port) 00123 { 00124 char rs[25]; 00125 sprintf(rs, "AT+CIPSERVER=1,%d", port); 00126 SendCMD(rs); 00127 } 00128 00129 // Close server mode. 00130 void ESP01::CloseServerMode(void) 00131 { 00132 SendCMD("AT+CIPSERVER=0"); 00133 } 00134 00135 void ESP01::setTransparent(void) 00136 { 00137 SendCMD("AT+CIPMODE=0"); 00138 } 00139 00140 void ESP01::startTCPConn(char *IP, int port) 00141 { 00142 char rs[100]; 00143 sprintf(rs, "AT+CIPSTART=0,\"TCP\",\"%s\",%d", IP, port); 00144 SendCMD(rs); 00145 } 00146 00147 void ESP01::sendURL(char *URL, char* IP, char *command) 00148 { 00149 char snd[20], http_cmd[300]; 00150 00151 sprintf(http_cmd, "GET %s HTTP/1.1%sHost: %s%sConnection: close%s%s", URL, CMD_END, IP, CMD_END, CMD_END, CMD_END); 00152 sprintf(snd,"AT+CIPSEND=0,%d",strlen(http_cmd)); 00153 strcpy(command, http_cmd); 00154 00155 SendCMD(snd); 00156 wait(1); 00157 SendCMD(http_cmd); 00158 } 00159 00160 void ESP01::deepsleep(size_t ms) 00161 { 00162 char snd[20]; 00163 sprintf(snd, "AT+GSLP=%d", ms); 00164 SendCMD(snd); 00165 } 00166 00167 //------------------ FUNCIONES QUE YO HE AGREGADO ------------------// 00168 // Starts SmartConfig 00169 void ESP01::StartSmartConfig(void) 00170 { 00171 SendCMD("AT+CWSTARTSMART=1"); 00172 } 00173 00174 //Disable Echo 00175 void ESP01::DisableEcho(void) 00176 { 00177 SendCMD("ATE0"); 00178 } 00179 00180 //Enable DHCP 00181 void ESP01::EnableDHCP(void) 00182 { 00183 SendCMD("AT+CWDHCP=1,1"); 00184 } 00185 00186 // Receive reply until no character is received after a given timeout in miliseconds 00187 void ESP01::RcvSingleReply(char * r) 00188 { 00189 bool ended = 0; 00190 char c; 00191 Timer t; 00192 00193 strcpy(r, ""); 00194 t.start(); 00195 while(!ended) { 00196 if(comm.readable()) { 00197 c = comm.getc(); 00198 AddChar(r, c); 00199 } 00200 if((c==0x0D)||(t.read_ms() > 5000)) { 00201 ended = 1; 00202 } 00203 } 00204 //AddChar(r, 0x00); 00205 } 00206 00207 // Gets connection status code. Parameter: string to contain status 00208 void ESP01::GetConnStatusCode(char * st) 00209 { 00210 char cmd[25]; 00211 strcpy(cmd, "AT+CIPSTATUS"); 00212 SendCMD(cmd); 00213 RcvSingleReply(st); 00214 } 00215 00216 // Set the Maximum Connections Allowed by Server. Parameter: maximum number of connections 00217 void ESP01::SetMaxTCPConn(int maxconn) 00218 { 00219 char rs[50]; 00220 sprintf(rs, "AT+CIPSERVERMAXCONN=%d", maxconn); 00221 SendCMD(rs); 00222 } 00223 00224 // Returns true if data has been received. Parameter: string to contain data received 00225 bool ESP01::TCPDataAvailable(char *data) 00226 { 00227 return RcvReply(data, 500); 00228 } 00229 00230 // Send TCP Data. Parameter: 00231 void ESP01::SendTCPData(int socket,int len, char *data) 00232 { 00233 char i; 00234 char rs[50]; 00235 sprintf(rs, "AT+CIPSEND=%d,%d",socket,len); 00236 SendCMD(rs); 00237 wait(1); 00238 SendCMD(data); 00239 }
Generated on Sun Jul 17 2022 22:08:45 by
1.7.2