Libreria basica para el manejo del ESP01
Dependents: Gateway_Cotosys Gateway_Cotosys_os5
ESP01.cpp@7:8c6f56470ba7, 2019-06-11 (annotated)
- Committer:
- Thrillex13
- Date:
- Tue Jun 11 00:22:45 2019 +0000
- Revision:
- 7:8c6f56470ba7
- Parent:
- 6:54b89962c798
- Child:
- 8:ef9014a530cf
Function SendTCPData was added
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 | 6:54b89962c798 | 45 | strcpy(r, ""); |
Thrillex13 | 6:54b89962c798 | 46 | t.start(); |
Thrillex13 | 7:8c6f56470ba7 | 47 | |
Thrillex13 | 6:54b89962c798 | 48 | do { |
Thrillex13 | 6:54b89962c798 | 49 | if(comm.readable()) { |
Thrillex13 | 6:54b89962c798 | 50 | AddChar(r, comm.getc()); |
Thrillex13 | 6:54b89962c798 | 51 | t.start(); |
Thrillex13 | 6:54b89962c798 | 52 | } |
Thrillex13 | 6:54b89962c798 | 53 | } while(t.read_ms() < to); |
Thrillex13 | 6:54b89962c798 | 54 | |
Thrillex13 | 6:54b89962c798 | 55 | AddChar(r, '\0'); |
Thrillex13 | 6:54b89962c798 | 56 | return r[0] != '\0'; |
Thrillex13 | 6:54b89962c798 | 57 | } |
Thrillex13 | 6:54b89962c798 | 58 | |
Thrillex13 | 6:54b89962c798 | 59 | // Gets the AP list. Parameter: the string to receive the list |
Thrillex13 | 7:8c6f56470ba7 | 60 | bool ESP01::GetList(char *l) |
Thrillex13 | 7:8c6f56470ba7 | 61 | { |
Thrillex13 | 6:54b89962c798 | 62 | SendCMD("AT+CWLAP"); |
Thrillex13 | 6:54b89962c798 | 63 | return RcvReply(l, 5000); // Needs big timeout because it takes long to start replying |
Thrillex13 | 6:54b89962c798 | 64 | } |
Thrillex13 | 6:54b89962c798 | 65 | |
Thrillex13 | 6:54b89962c798 | 66 | // Joins a Wifi AP. Parameters: SSID and Password (strings) |
Thrillex13 | 7:8c6f56470ba7 | 67 | void ESP01::Join(char *id, char *pwd) |
Thrillex13 | 7:8c6f56470ba7 | 68 | { |
Thrillex13 | 6:54b89962c798 | 69 | char cmd[255]; |
Thrillex13 | 6:54b89962c798 | 70 | sprintf(cmd, "AT+CWJAP=\"%s\",\"%s\"", id, pwd); |
Thrillex13 | 6:54b89962c798 | 71 | SendCMD(cmd); |
Thrillex13 | 6:54b89962c798 | 72 | } |
Thrillex13 | 6:54b89962c798 | 73 | |
Thrillex13 | 6:54b89962c798 | 74 | // Gets ESP IP. Parameter: string to contain IP |
Thrillex13 | 7:8c6f56470ba7 | 75 | bool ESP01::GetIP(char *ip) |
Thrillex13 | 7:8c6f56470ba7 | 76 | { |
Thrillex13 | 6:54b89962c798 | 77 | SendCMD("AT+CIFSR"); |
Thrillex13 | 6:54b89962c798 | 78 | return RcvReply(ip, 2000); |
Thrillex13 | 6:54b89962c798 | 79 | } |
Thrillex13 | 6:54b89962c798 | 80 | |
Thrillex13 | 6:54b89962c798 | 81 | //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both |
Thrillex13 | 7:8c6f56470ba7 | 82 | void ESP01::SetMode(WiFiMode mode) |
Thrillex13 | 7:8c6f56470ba7 | 83 | { |
Thrillex13 | 6:54b89962c798 | 84 | char cmd[15]; |
Thrillex13 | 6:54b89962c798 | 85 | strcpy(cmd, "AT+CWMODE="); |
Thrillex13 | 6:54b89962c798 | 86 | AddChar(cmd, mode + '0'); // Completes command string |
Thrillex13 | 6:54b89962c798 | 87 | SendCMD(cmd); |
Thrillex13 | 6:54b89962c798 | 88 | } |
Thrillex13 | 6:54b89962c798 | 89 | |
Thrillex13 | 6:54b89962c798 | 90 | // Quits the AP |
Thrillex13 | 7:8c6f56470ba7 | 91 | void ESP01::Quit(void) |
Thrillex13 | 7:8c6f56470ba7 | 92 | { |
Thrillex13 | 6:54b89962c798 | 93 | SendCMD("AT+CWQAP"); |
Thrillex13 | 6:54b89962c798 | 94 | } |
Thrillex13 | 6:54b89962c798 | 95 | |
Thrillex13 | 6:54b89962c798 | 96 | // Sets single connection |
Thrillex13 | 7:8c6f56470ba7 | 97 | void ESP01::SetSingle(void) |
Thrillex13 | 7:8c6f56470ba7 | 98 | { |
Thrillex13 | 6:54b89962c798 | 99 | SendCMD("AT+CIPMUX=0"); |
Thrillex13 | 6:54b89962c798 | 100 | } |
Thrillex13 | 6:54b89962c798 | 101 | |
Thrillex13 | 6:54b89962c798 | 102 | // Sets multiple connection |
Thrillex13 | 7:8c6f56470ba7 | 103 | void ESP01::SetMultiple(void) |
Thrillex13 | 7:8c6f56470ba7 | 104 | { |
Thrillex13 | 6:54b89962c798 | 105 | SendCMD("AT+CIPMUX=1"); |
Thrillex13 | 6:54b89962c798 | 106 | } |
Thrillex13 | 6:54b89962c798 | 107 | |
Thrillex13 | 6:54b89962c798 | 108 | // Gets connection status. Parameter: string to contain status |
Thrillex13 | 7:8c6f56470ba7 | 109 | bool ESP01::GetConnStatus(char * st) |
Thrillex13 | 7:8c6f56470ba7 | 110 | { |
Thrillex13 | 6:54b89962c798 | 111 | SendCMD("AT+CIPSTATUS"); |
Thrillex13 | 6:54b89962c798 | 112 | return RcvReply(st, 2000); |
Thrillex13 | 6:54b89962c798 | 113 | } |
Thrillex13 | 6:54b89962c798 | 114 | |
Thrillex13 | 6:54b89962c798 | 115 | // Starts server mode. Parameter: port to be used |
Thrillex13 | 7:8c6f56470ba7 | 116 | void ESP01::StartServerMode(int port) |
Thrillex13 | 7:8c6f56470ba7 | 117 | { |
Thrillex13 | 6:54b89962c798 | 118 | char rs[25]; |
Thrillex13 | 6:54b89962c798 | 119 | sprintf(rs, "AT+CIPSERVER=1,%d", port); |
Thrillex13 | 6:54b89962c798 | 120 | SendCMD(rs); |
Thrillex13 | 6:54b89962c798 | 121 | } |
Thrillex13 | 6:54b89962c798 | 122 | |
Thrillex13 | 6:54b89962c798 | 123 | // Close server mode. |
Thrillex13 | 7:8c6f56470ba7 | 124 | void ESP01::CloseServerMode(void) |
Thrillex13 | 7:8c6f56470ba7 | 125 | { |
Thrillex13 | 6:54b89962c798 | 126 | SendCMD("AT+CIPSERVER=0"); |
Thrillex13 | 6:54b89962c798 | 127 | } |
Thrillex13 | 6:54b89962c798 | 128 | |
Thrillex13 | 7:8c6f56470ba7 | 129 | void ESP01::setTransparent(void) |
Thrillex13 | 7:8c6f56470ba7 | 130 | { |
Thrillex13 | 6:54b89962c798 | 131 | SendCMD("AT+CIPMODE=0"); |
Thrillex13 | 6:54b89962c798 | 132 | } |
Thrillex13 | 6:54b89962c798 | 133 | |
Thrillex13 | 7:8c6f56470ba7 | 134 | void ESP01::startTCPConn(char *IP, int port) |
Thrillex13 | 7:8c6f56470ba7 | 135 | { |
Thrillex13 | 6:54b89962c798 | 136 | char rs[100]; |
Thrillex13 | 6:54b89962c798 | 137 | sprintf(rs, "AT+CIPSTART=0,\"TCP\",\"%s\",%d", IP, port); |
Thrillex13 | 6:54b89962c798 | 138 | SendCMD(rs); |
Thrillex13 | 6:54b89962c798 | 139 | } |
Thrillex13 | 6:54b89962c798 | 140 | |
Thrillex13 | 7:8c6f56470ba7 | 141 | void ESP01::sendURL(char *URL, char* IP, char *command) |
Thrillex13 | 7:8c6f56470ba7 | 142 | { |
Thrillex13 | 6:54b89962c798 | 143 | char snd[20], http_cmd[300]; |
Thrillex13 | 6:54b89962c798 | 144 | |
Thrillex13 | 6:54b89962c798 | 145 | 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 | 146 | sprintf(snd,"AT+CIPSEND=0,%d",strlen(http_cmd)); |
Thrillex13 | 6:54b89962c798 | 147 | strcpy(command, http_cmd); |
Thrillex13 | 7:8c6f56470ba7 | 148 | |
Thrillex13 | 6:54b89962c798 | 149 | SendCMD(snd); |
Thrillex13 | 6:54b89962c798 | 150 | wait(1); |
Thrillex13 | 6:54b89962c798 | 151 | SendCMD(http_cmd); |
Thrillex13 | 6:54b89962c798 | 152 | } |
Thrillex13 | 7:8c6f56470ba7 | 153 | |
Thrillex13 | 7:8c6f56470ba7 | 154 | void ESP01::deepsleep(size_t ms) |
Thrillex13 | 7:8c6f56470ba7 | 155 | { |
Thrillex13 | 6:54b89962c798 | 156 | char snd[20]; |
Thrillex13 | 6:54b89962c798 | 157 | sprintf(snd, "AT+GSLP=%d", ms); |
Thrillex13 | 6:54b89962c798 | 158 | SendCMD(snd); |
Thrillex13 | 6:54b89962c798 | 159 | } |
Thrillex13 | 6:54b89962c798 | 160 | |
Thrillex13 | 6:54b89962c798 | 161 | //------------------ FUNCIONES QUE YO HE AGREGADO ------------------// |
Thrillex13 | 6:54b89962c798 | 162 | // Starts SmartConfig |
Thrillex13 | 7:8c6f56470ba7 | 163 | void ESP01::StartSmartConfig(void) |
Thrillex13 | 7:8c6f56470ba7 | 164 | { |
Thrillex13 | 7:8c6f56470ba7 | 165 | SendCMD("AT+CWSTARTSMART=1"); |
Thrillex13 | 6:54b89962c798 | 166 | } |
Thrillex13 | 6:54b89962c798 | 167 | |
Thrillex13 | 6:54b89962c798 | 168 | //Disable Echo |
Thrillex13 | 7:8c6f56470ba7 | 169 | void ESP01::DisableEcho(void) |
Thrillex13 | 7:8c6f56470ba7 | 170 | { |
Thrillex13 | 6:54b89962c798 | 171 | SendCMD("ATE0"); |
Thrillex13 | 6:54b89962c798 | 172 | } |
Thrillex13 | 6:54b89962c798 | 173 | |
Thrillex13 | 6:54b89962c798 | 174 | //Enable DHCP |
Thrillex13 | 7:8c6f56470ba7 | 175 | void ESP01::EnableDHCP(void) |
Thrillex13 | 7:8c6f56470ba7 | 176 | { |
Thrillex13 | 6:54b89962c798 | 177 | SendCMD("AT+CWDHCP=1,1"); |
Thrillex13 | 6:54b89962c798 | 178 | } |
Thrillex13 | 6:54b89962c798 | 179 | |
Thrillex13 | 6:54b89962c798 | 180 | // Receive reply until no character is received after a given timeout in miliseconds |
Thrillex13 | 7:8c6f56470ba7 | 181 | void ESP01::RcvSingleReply(char * r) |
Thrillex13 | 7:8c6f56470ba7 | 182 | { |
Thrillex13 | 6:54b89962c798 | 183 | bool ended = 0; |
Thrillex13 | 6:54b89962c798 | 184 | char c; |
Thrillex13 | 7:8c6f56470ba7 | 185 | |
Thrillex13 | 6:54b89962c798 | 186 | strcpy(r, ""); |
Thrillex13 | 6:54b89962c798 | 187 | while(!ended) { |
Thrillex13 | 6:54b89962c798 | 188 | if(comm.readable()) { |
Thrillex13 | 6:54b89962c798 | 189 | c = comm.getc(); |
Thrillex13 | 6:54b89962c798 | 190 | AddChar(r, c); |
Thrillex13 | 6:54b89962c798 | 191 | } |
Thrillex13 | 6:54b89962c798 | 192 | if(c == 0x0D) { |
Thrillex13 | 7:8c6f56470ba7 | 193 | ended = 1; |
Thrillex13 | 6:54b89962c798 | 194 | } |
Thrillex13 | 6:54b89962c798 | 195 | } |
Thrillex13 | 6:54b89962c798 | 196 | //AddChar(r, 0x00); |
Thrillex13 | 6:54b89962c798 | 197 | } |
Thrillex13 | 6:54b89962c798 | 198 | |
Thrillex13 | 6:54b89962c798 | 199 | // Gets connection status code. Parameter: string to contain status |
Thrillex13 | 7:8c6f56470ba7 | 200 | void ESP01::GetConnStatusCode(char * st) |
Thrillex13 | 7:8c6f56470ba7 | 201 | { |
Thrillex13 | 6:54b89962c798 | 202 | char cmd[15]; |
Thrillex13 | 6:54b89962c798 | 203 | strcpy(cmd, "AT+CIPSTATUS"); |
Thrillex13 | 6:54b89962c798 | 204 | SendCMD(cmd); |
Thrillex13 | 6:54b89962c798 | 205 | RcvSingleReply(st); |
Thrillex13 | 6:54b89962c798 | 206 | } |
Thrillex13 | 7:8c6f56470ba7 | 207 | |
Thrillex13 | 7:8c6f56470ba7 | 208 | // Set the Maximum Connections Allowed by Server. Parameter: maximum number of connections |
Thrillex13 | 7:8c6f56470ba7 | 209 | void ESP01::SetMaxTCPConn(int maxconn) |
Thrillex13 | 7:8c6f56470ba7 | 210 | { |
Thrillex13 | 7:8c6f56470ba7 | 211 | char rs[50]; |
Thrillex13 | 7:8c6f56470ba7 | 212 | sprintf(rs, "AT+CIPSERVERMAXCONN=%d", maxconn); |
Thrillex13 | 7:8c6f56470ba7 | 213 | SendCMD(rs); |
Thrillex13 | 7:8c6f56470ba7 | 214 | } |
Thrillex13 | 7:8c6f56470ba7 | 215 | |
Thrillex13 | 7:8c6f56470ba7 | 216 | // Returns true if data has been received. Parameter: string to contain data received |
Thrillex13 | 7:8c6f56470ba7 | 217 | bool ESP01::TCPDataAvailable(char *data) |
Thrillex13 | 7:8c6f56470ba7 | 218 | { |
Thrillex13 | 7:8c6f56470ba7 | 219 | return RcvReply(data, 500); |
Thrillex13 | 7:8c6f56470ba7 | 220 | } |
Thrillex13 | 7:8c6f56470ba7 | 221 | |
Thrillex13 | 7:8c6f56470ba7 | 222 | // Send TCP Data. Parameter: |
Thrillex13 | 7:8c6f56470ba7 | 223 | void ESP01::SendTCPData(int socket,int len, char *data) |
Thrillex13 | 7:8c6f56470ba7 | 224 | { |
Thrillex13 | 7:8c6f56470ba7 | 225 | char i; |
Thrillex13 | 7:8c6f56470ba7 | 226 | char rs[50]; |
Thrillex13 | 7:8c6f56470ba7 | 227 | sprintf(rs, "AT+CIPSEND=%d,%d",socket,len); |
Thrillex13 | 7:8c6f56470ba7 | 228 | SendCMD(rs); |
Thrillex13 | 7:8c6f56470ba7 | 229 | wait(1); |
Thrillex13 | 7:8c6f56470ba7 | 230 | SendCMD(data); |
Thrillex13 | 7:8c6f56470ba7 | 231 | } |