Libreria basica para el manejo del ESP01
Dependents: Gateway_Cotosys Gateway_Cotosys_os5
ESP01.cpp@9:21290fc75c17, 2020-01-06 (annotated)
- Committer:
- Thrillex13
- Date:
- Mon Jan 06 04:39:26 2020 +0000
- Revision:
- 9:21290fc75c17
- Parent:
- 8:ef9014a530cf
Se arreglo la funcion ReceiveSingleReply para evitar que se quede atascado en un loop.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Thrillex13 | 6:54b89962c798 | 1 | #include "ESP01.h" |
Thrillex13 | 6:54b89962c798 | 2 | |
Thrillex13 | 6:54b89962c798 | 3 | // Constructor |
Thrillex13 | 7:8c6f56470ba7 | 4 | ESP01::ESP01(PinName tx, PinName rx, int br) : comm(tx, rx) |
Thrillex13 | 7:8c6f56470ba7 | 5 | { |
Thrillex13 | 6:54b89962c798 | 6 | comm.baud(br); |
Thrillex13 | 6:54b89962c798 | 7 | } |
Thrillex13 | 6:54b89962c798 | 8 | |
Thrillex13 | 6:54b89962c798 | 9 | // Destructor |
Thrillex13 | 6:54b89962c798 | 10 | ESP01::~ESP01() { } |
Thrillex13 | 6:54b89962c798 | 11 | |
Thrillex13 | 6:54b89962c798 | 12 | // Add <CR> + <LF> at the end of the string |
Thrillex13 | 7:8c6f56470ba7 | 13 | void ESP01::AddEOL(char * s) |
Thrillex13 | 7:8c6f56470ba7 | 14 | { |
Thrillex13 | 6:54b89962c798 | 15 | char k = strlen(s); // Finds position of NULL character |
Thrillex13 | 6:54b89962c798 | 16 | s[k] = '\r'; // switch NULL for <CR> |
Thrillex13 | 6:54b89962c798 | 17 | s[k + 1] = '\n'; // Add <LF> |
Thrillex13 | 6:54b89962c798 | 18 | s[k + 2] = '\0'; // Add NULL at the end |
Thrillex13 | 6:54b89962c798 | 19 | } |
Thrillex13 | 6:54b89962c798 | 20 | |
Thrillex13 | 6:54b89962c798 | 21 | // Add one ASCII character at the end of the string |
Thrillex13 | 7:8c6f56470ba7 | 22 | void ESP01::AddChar(char * s, char c) |
Thrillex13 | 7:8c6f56470ba7 | 23 | { |
Thrillex13 | 6:54b89962c798 | 24 | char k = strlen(s); |
Thrillex13 | 6:54b89962c798 | 25 | s[k] = c; |
Thrillex13 | 6:54b89962c798 | 26 | s[k + 1] = '\0'; |
Thrillex13 | 6:54b89962c798 | 27 | } |
Thrillex13 | 6:54b89962c798 | 28 | |
Thrillex13 | 6:54b89962c798 | 29 | // Sends command to ESP01. Receives the command string |
Thrillex13 | 7:8c6f56470ba7 | 30 | void ESP01::SendCMD(char* s) |
Thrillex13 | 7:8c6f56470ba7 | 31 | { |
Thrillex13 | 6:54b89962c798 | 32 | comm.printf("%s%s", s, CMD_END); |
Thrillex13 | 6:54b89962c798 | 33 | } |
Thrillex13 | 6:54b89962c798 | 34 | |
Thrillex13 | 6:54b89962c798 | 35 | // Resets the ESP01 |
Thrillex13 | 7:8c6f56470ba7 | 36 | void ESP01::Reset(void) |
Thrillex13 | 7:8c6f56470ba7 | 37 | { |
Thrillex13 | 6:54b89962c798 | 38 | SendCMD("AT+RST"); |
Thrillex13 | 6:54b89962c798 | 39 | } |
Thrillex13 | 6:54b89962c798 | 40 | |
Thrillex13 | 6:54b89962c798 | 41 | // Receive reply until no character is received after a given timeout in miliseconds |
Thrillex13 | 7:8c6f56470ba7 | 42 | bool ESP01::RcvReply(char* r, int to) |
Thrillex13 | 7:8c6f56470ba7 | 43 | { |
Thrillex13 | 6:54b89962c798 | 44 | Timer t; |
Thrillex13 | 8:ef9014a530cf | 45 | bool ended = 0; |
Thrillex13 | 8:ef9014a530cf | 46 | char c; |
Thrillex13 | 8:ef9014a530cf | 47 | |
Thrillex13 | 6:54b89962c798 | 48 | strcpy(r, ""); |
Thrillex13 | 6:54b89962c798 | 49 | t.start(); |
Thrillex13 | 8:ef9014a530cf | 50 | while(!ended) { |
Thrillex13 | 6:54b89962c798 | 51 | if(comm.readable()) { |
Thrillex13 | 8:ef9014a530cf | 52 | c = comm.getc(); |
Thrillex13 | 8:ef9014a530cf | 53 | AddChar(r, c); |
Thrillex13 | 6:54b89962c798 | 54 | t.start(); |
Thrillex13 | 6:54b89962c798 | 55 | } |
Thrillex13 | 8:ef9014a530cf | 56 | if(t.read_ms() > to) { |
Thrillex13 | 8:ef9014a530cf | 57 | ended = 1; |
Thrillex13 | 8:ef9014a530cf | 58 | } |
Thrillex13 | 8:ef9014a530cf | 59 | } |
Thrillex13 | 8:ef9014a530cf | 60 | |
Thrillex13 | 6:54b89962c798 | 61 | AddChar(r, '\0'); |
Thrillex13 | 6:54b89962c798 | 62 | return r[0] != '\0'; |
Thrillex13 | 6:54b89962c798 | 63 | } |
Thrillex13 | 6:54b89962c798 | 64 | |
Thrillex13 | 6:54b89962c798 | 65 | // Gets the AP list. Parameter: the string to receive the list |
Thrillex13 | 7:8c6f56470ba7 | 66 | bool ESP01::GetList(char *l) |
Thrillex13 | 7:8c6f56470ba7 | 67 | { |
Thrillex13 | 6:54b89962c798 | 68 | SendCMD("AT+CWLAP"); |
Thrillex13 | 6:54b89962c798 | 69 | return RcvReply(l, 5000); // Needs big timeout because it takes long to start replying |
Thrillex13 | 6:54b89962c798 | 70 | } |
Thrillex13 | 6:54b89962c798 | 71 | |
Thrillex13 | 6:54b89962c798 | 72 | // Joins a Wifi AP. Parameters: SSID and Password (strings) |
Thrillex13 | 7:8c6f56470ba7 | 73 | void ESP01::Join(char *id, char *pwd) |
Thrillex13 | 7:8c6f56470ba7 | 74 | { |
Thrillex13 | 6:54b89962c798 | 75 | char cmd[255]; |
Thrillex13 | 6:54b89962c798 | 76 | sprintf(cmd, "AT+CWJAP=\"%s\",\"%s\"", id, pwd); |
Thrillex13 | 6:54b89962c798 | 77 | SendCMD(cmd); |
Thrillex13 | 6:54b89962c798 | 78 | } |
Thrillex13 | 6:54b89962c798 | 79 | |
Thrillex13 | 6:54b89962c798 | 80 | // Gets ESP IP. Parameter: string to contain IP |
Thrillex13 | 7:8c6f56470ba7 | 81 | bool ESP01::GetIP(char *ip) |
Thrillex13 | 7:8c6f56470ba7 | 82 | { |
Thrillex13 | 6:54b89962c798 | 83 | SendCMD("AT+CIFSR"); |
Thrillex13 | 9:21290fc75c17 | 84 | return RcvReply(ip, 3000); |
Thrillex13 | 6:54b89962c798 | 85 | } |
Thrillex13 | 6:54b89962c798 | 86 | |
Thrillex13 | 6:54b89962c798 | 87 | //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both |
Thrillex13 | 7:8c6f56470ba7 | 88 | void ESP01::SetMode(WiFiMode mode) |
Thrillex13 | 7:8c6f56470ba7 | 89 | { |
Thrillex13 | 6:54b89962c798 | 90 | char cmd[15]; |
Thrillex13 | 6:54b89962c798 | 91 | strcpy(cmd, "AT+CWMODE="); |
Thrillex13 | 8:ef9014a530cf | 92 | AddChar(cmd, mode + '0'); // // Converts number into corresponding ASCII character |
Thrillex13 | 6:54b89962c798 | 93 | SendCMD(cmd); |
Thrillex13 | 6:54b89962c798 | 94 | } |
Thrillex13 | 6:54b89962c798 | 95 | |
Thrillex13 | 6:54b89962c798 | 96 | // Quits the AP |
Thrillex13 | 7:8c6f56470ba7 | 97 | void ESP01::Quit(void) |
Thrillex13 | 7:8c6f56470ba7 | 98 | { |
Thrillex13 | 6:54b89962c798 | 99 | SendCMD("AT+CWQAP"); |
Thrillex13 | 6:54b89962c798 | 100 | } |
Thrillex13 | 6:54b89962c798 | 101 | |
Thrillex13 | 6:54b89962c798 | 102 | // Sets single connection |
Thrillex13 | 7:8c6f56470ba7 | 103 | void ESP01::SetSingle(void) |
Thrillex13 | 7:8c6f56470ba7 | 104 | { |
Thrillex13 | 6:54b89962c798 | 105 | SendCMD("AT+CIPMUX=0"); |
Thrillex13 | 6:54b89962c798 | 106 | } |
Thrillex13 | 6:54b89962c798 | 107 | |
Thrillex13 | 6:54b89962c798 | 108 | // Sets multiple connection |
Thrillex13 | 7:8c6f56470ba7 | 109 | void ESP01::SetMultiple(void) |
Thrillex13 | 7:8c6f56470ba7 | 110 | { |
Thrillex13 | 6:54b89962c798 | 111 | SendCMD("AT+CIPMUX=1"); |
Thrillex13 | 6:54b89962c798 | 112 | } |
Thrillex13 | 6:54b89962c798 | 113 | |
Thrillex13 | 6:54b89962c798 | 114 | // Gets connection status. Parameter: string to contain status |
Thrillex13 | 7:8c6f56470ba7 | 115 | bool ESP01::GetConnStatus(char * st) |
Thrillex13 | 7:8c6f56470ba7 | 116 | { |
Thrillex13 | 6:54b89962c798 | 117 | SendCMD("AT+CIPSTATUS"); |
Thrillex13 | 6:54b89962c798 | 118 | return RcvReply(st, 2000); |
Thrillex13 | 6:54b89962c798 | 119 | } |
Thrillex13 | 6:54b89962c798 | 120 | |
Thrillex13 | 6:54b89962c798 | 121 | // Starts server mode. Parameter: port to be used |
Thrillex13 | 7:8c6f56470ba7 | 122 | void ESP01::StartServerMode(int port) |
Thrillex13 | 7:8c6f56470ba7 | 123 | { |
Thrillex13 | 6:54b89962c798 | 124 | char rs[25]; |
Thrillex13 | 6:54b89962c798 | 125 | sprintf(rs, "AT+CIPSERVER=1,%d", port); |
Thrillex13 | 6:54b89962c798 | 126 | SendCMD(rs); |
Thrillex13 | 6:54b89962c798 | 127 | } |
Thrillex13 | 6:54b89962c798 | 128 | |
Thrillex13 | 6:54b89962c798 | 129 | // Close server mode. |
Thrillex13 | 7:8c6f56470ba7 | 130 | void ESP01::CloseServerMode(void) |
Thrillex13 | 7:8c6f56470ba7 | 131 | { |
Thrillex13 | 6:54b89962c798 | 132 | SendCMD("AT+CIPSERVER=0"); |
Thrillex13 | 6:54b89962c798 | 133 | } |
Thrillex13 | 6:54b89962c798 | 134 | |
Thrillex13 | 7:8c6f56470ba7 | 135 | void ESP01::setTransparent(void) |
Thrillex13 | 7:8c6f56470ba7 | 136 | { |
Thrillex13 | 6:54b89962c798 | 137 | SendCMD("AT+CIPMODE=0"); |
Thrillex13 | 6:54b89962c798 | 138 | } |
Thrillex13 | 6:54b89962c798 | 139 | |
Thrillex13 | 7:8c6f56470ba7 | 140 | void ESP01::startTCPConn(char *IP, int port) |
Thrillex13 | 7:8c6f56470ba7 | 141 | { |
Thrillex13 | 6:54b89962c798 | 142 | char rs[100]; |
Thrillex13 | 6:54b89962c798 | 143 | sprintf(rs, "AT+CIPSTART=0,\"TCP\",\"%s\",%d", IP, port); |
Thrillex13 | 6:54b89962c798 | 144 | SendCMD(rs); |
Thrillex13 | 6:54b89962c798 | 145 | } |
Thrillex13 | 6:54b89962c798 | 146 | |
Thrillex13 | 7:8c6f56470ba7 | 147 | void ESP01::sendURL(char *URL, char* IP, char *command) |
Thrillex13 | 7:8c6f56470ba7 | 148 | { |
Thrillex13 | 6:54b89962c798 | 149 | char snd[20], http_cmd[300]; |
Thrillex13 | 6:54b89962c798 | 150 | |
Thrillex13 | 6:54b89962c798 | 151 | sprintf(http_cmd, "GET %s HTTP/1.1%sHost: %s%sConnection: close%s%s", URL, CMD_END, IP, CMD_END, CMD_END, CMD_END); |
Thrillex13 | 6:54b89962c798 | 152 | sprintf(snd,"AT+CIPSEND=0,%d",strlen(http_cmd)); |
Thrillex13 | 6:54b89962c798 | 153 | strcpy(command, http_cmd); |
Thrillex13 | 7:8c6f56470ba7 | 154 | |
Thrillex13 | 6:54b89962c798 | 155 | SendCMD(snd); |
Thrillex13 | 6:54b89962c798 | 156 | wait(1); |
Thrillex13 | 6:54b89962c798 | 157 | SendCMD(http_cmd); |
Thrillex13 | 6:54b89962c798 | 158 | } |
Thrillex13 | 7:8c6f56470ba7 | 159 | |
Thrillex13 | 7:8c6f56470ba7 | 160 | void ESP01::deepsleep(size_t ms) |
Thrillex13 | 7:8c6f56470ba7 | 161 | { |
Thrillex13 | 6:54b89962c798 | 162 | char snd[20]; |
Thrillex13 | 6:54b89962c798 | 163 | sprintf(snd, "AT+GSLP=%d", ms); |
Thrillex13 | 6:54b89962c798 | 164 | SendCMD(snd); |
Thrillex13 | 6:54b89962c798 | 165 | } |
Thrillex13 | 6:54b89962c798 | 166 | |
Thrillex13 | 6:54b89962c798 | 167 | //------------------ FUNCIONES QUE YO HE AGREGADO ------------------// |
Thrillex13 | 6:54b89962c798 | 168 | // Starts SmartConfig |
Thrillex13 | 7:8c6f56470ba7 | 169 | void ESP01::StartSmartConfig(void) |
Thrillex13 | 7:8c6f56470ba7 | 170 | { |
Thrillex13 | 7:8c6f56470ba7 | 171 | SendCMD("AT+CWSTARTSMART=1"); |
Thrillex13 | 6:54b89962c798 | 172 | } |
Thrillex13 | 6:54b89962c798 | 173 | |
Thrillex13 | 6:54b89962c798 | 174 | //Disable Echo |
Thrillex13 | 7:8c6f56470ba7 | 175 | void ESP01::DisableEcho(void) |
Thrillex13 | 7:8c6f56470ba7 | 176 | { |
Thrillex13 | 6:54b89962c798 | 177 | SendCMD("ATE0"); |
Thrillex13 | 6:54b89962c798 | 178 | } |
Thrillex13 | 6:54b89962c798 | 179 | |
Thrillex13 | 6:54b89962c798 | 180 | //Enable DHCP |
Thrillex13 | 7:8c6f56470ba7 | 181 | void ESP01::EnableDHCP(void) |
Thrillex13 | 7:8c6f56470ba7 | 182 | { |
Thrillex13 | 6:54b89962c798 | 183 | SendCMD("AT+CWDHCP=1,1"); |
Thrillex13 | 6:54b89962c798 | 184 | } |
Thrillex13 | 6:54b89962c798 | 185 | |
Thrillex13 | 6:54b89962c798 | 186 | // Receive reply until no character is received after a given timeout in miliseconds |
Thrillex13 | 7:8c6f56470ba7 | 187 | void ESP01::RcvSingleReply(char * r) |
Thrillex13 | 7:8c6f56470ba7 | 188 | { |
Thrillex13 | 6:54b89962c798 | 189 | bool ended = 0; |
Thrillex13 | 6:54b89962c798 | 190 | char c; |
Thrillex13 | 9:21290fc75c17 | 191 | Timer t; |
Thrillex13 | 7:8c6f56470ba7 | 192 | |
Thrillex13 | 6:54b89962c798 | 193 | strcpy(r, ""); |
Thrillex13 | 9:21290fc75c17 | 194 | t.start(); |
Thrillex13 | 6:54b89962c798 | 195 | while(!ended) { |
Thrillex13 | 6:54b89962c798 | 196 | if(comm.readable()) { |
Thrillex13 | 6:54b89962c798 | 197 | c = comm.getc(); |
Thrillex13 | 6:54b89962c798 | 198 | AddChar(r, c); |
Thrillex13 | 6:54b89962c798 | 199 | } |
Thrillex13 | 9:21290fc75c17 | 200 | if((c==0x0D)||(t.read_ms() > 5000)) { |
Thrillex13 | 7:8c6f56470ba7 | 201 | ended = 1; |
Thrillex13 | 6:54b89962c798 | 202 | } |
Thrillex13 | 6:54b89962c798 | 203 | } |
Thrillex13 | 6:54b89962c798 | 204 | //AddChar(r, 0x00); |
Thrillex13 | 6:54b89962c798 | 205 | } |
Thrillex13 | 6:54b89962c798 | 206 | |
Thrillex13 | 6:54b89962c798 | 207 | // Gets connection status code. Parameter: string to contain status |
Thrillex13 | 7:8c6f56470ba7 | 208 | void ESP01::GetConnStatusCode(char * st) |
Thrillex13 | 7:8c6f56470ba7 | 209 | { |
Thrillex13 | 8:ef9014a530cf | 210 | char cmd[25]; |
Thrillex13 | 6:54b89962c798 | 211 | strcpy(cmd, "AT+CIPSTATUS"); |
Thrillex13 | 6:54b89962c798 | 212 | SendCMD(cmd); |
Thrillex13 | 6:54b89962c798 | 213 | RcvSingleReply(st); |
Thrillex13 | 6:54b89962c798 | 214 | } |
Thrillex13 | 7:8c6f56470ba7 | 215 | |
Thrillex13 | 7:8c6f56470ba7 | 216 | // Set the Maximum Connections Allowed by Server. Parameter: maximum number of connections |
Thrillex13 | 7:8c6f56470ba7 | 217 | void ESP01::SetMaxTCPConn(int maxconn) |
Thrillex13 | 7:8c6f56470ba7 | 218 | { |
Thrillex13 | 7:8c6f56470ba7 | 219 | char rs[50]; |
Thrillex13 | 7:8c6f56470ba7 | 220 | sprintf(rs, "AT+CIPSERVERMAXCONN=%d", maxconn); |
Thrillex13 | 7:8c6f56470ba7 | 221 | SendCMD(rs); |
Thrillex13 | 7:8c6f56470ba7 | 222 | } |
Thrillex13 | 7:8c6f56470ba7 | 223 | |
Thrillex13 | 7:8c6f56470ba7 | 224 | // Returns true if data has been received. Parameter: string to contain data received |
Thrillex13 | 7:8c6f56470ba7 | 225 | bool ESP01::TCPDataAvailable(char *data) |
Thrillex13 | 7:8c6f56470ba7 | 226 | { |
Thrillex13 | 7:8c6f56470ba7 | 227 | return RcvReply(data, 500); |
Thrillex13 | 7:8c6f56470ba7 | 228 | } |
Thrillex13 | 7:8c6f56470ba7 | 229 | |
Thrillex13 | 7:8c6f56470ba7 | 230 | // Send TCP Data. Parameter: |
Thrillex13 | 7:8c6f56470ba7 | 231 | void ESP01::SendTCPData(int socket,int len, char *data) |
Thrillex13 | 7:8c6f56470ba7 | 232 | { |
Thrillex13 | 7:8c6f56470ba7 | 233 | char i; |
Thrillex13 | 7:8c6f56470ba7 | 234 | char rs[50]; |
Thrillex13 | 7:8c6f56470ba7 | 235 | sprintf(rs, "AT+CIPSEND=%d,%d",socket,len); |
Thrillex13 | 7:8c6f56470ba7 | 236 | SendCMD(rs); |
Thrillex13 | 7:8c6f56470ba7 | 237 | wait(1); |
Thrillex13 | 7:8c6f56470ba7 | 238 | SendCMD(data); |
Thrillex13 | 7:8c6f56470ba7 | 239 | } |