Example to connect to wifi automatically or start smartconfig if it is not connected
ESP8266.cpp@3:d52701d66b62, 2019-05-12 (annotated)
- Committer:
- Thrillex13
- Date:
- Sun May 12 16:40:02 2019 +0000
- Revision:
- 3:d52701d66b62
- Parent:
- 2:77388e8f0697
Primera version donde solo se se inicializa el wifi y se inicia startsmartconfig en caso de que no este conectado al wifi
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| quevedo | 0:e58f27687450 | 1 | #include "ESP8266.h" |
| quevedo | 0:e58f27687450 | 2 | |
| quevedo | 0:e58f27687450 | 3 | // Constructor |
| quevedo | 2:77388e8f0697 | 4 | ESP8266::ESP8266(PinName tx, PinName rx, int br) : comm(tx, rx) { |
| quevedo | 2:77388e8f0697 | 5 | comm.baud(br); |
| quevedo | 0:e58f27687450 | 6 | } |
| quevedo | 0:e58f27687450 | 7 | |
| quevedo | 0:e58f27687450 | 8 | // Destructor |
| quevedo | 0:e58f27687450 | 9 | ESP8266::~ESP8266() { } |
| quevedo | 0:e58f27687450 | 10 | |
| quevedo | 0:e58f27687450 | 11 | // Add <CR> + <LF> at the end of the string |
| quevedo | 0:e58f27687450 | 12 | void ESP8266::AddEOL(char * s) { |
| quevedo | 0:e58f27687450 | 13 | char k; |
| quevedo | 0:e58f27687450 | 14 | k = strlen(s); // Finds position of NULL character |
| quevedo | 0:e58f27687450 | 15 | s[k] = 0x0D; // switch NULL for <CR> |
| quevedo | 0:e58f27687450 | 16 | s[k + 1] = 0x0A; // Add <LF> |
| quevedo | 0:e58f27687450 | 17 | s[k + 2] = 0; // Add NULL at the end |
| quevedo | 0:e58f27687450 | 18 | } |
| quevedo | 0:e58f27687450 | 19 | |
| quevedo | 0:e58f27687450 | 20 | // Add one ASCII character at the end of the string |
| quevedo | 0:e58f27687450 | 21 | void ESP8266::AddChar(char * s, char c) { |
| quevedo | 0:e58f27687450 | 22 | char k; |
| quevedo | 0:e58f27687450 | 23 | k = strlen(s); |
| quevedo | 0:e58f27687450 | 24 | s[k] = c; |
| quevedo | 0:e58f27687450 | 25 | s[k + 1] = 0; |
| quevedo | 0:e58f27687450 | 26 | } |
| quevedo | 0:e58f27687450 | 27 | |
| quevedo | 2:77388e8f0697 | 28 | // Converts integer number to null-terminated string |
| quevedo | 2:77388e8f0697 | 29 | void ESP8266::itoa(int n, char * s) { |
| quevedo | 2:77388e8f0697 | 30 | char k = 0; |
| quevedo | 2:77388e8f0697 | 31 | char r[11]; |
| quevedo | 2:77388e8f0697 | 32 | |
| quevedo | 2:77388e8f0697 | 33 | if(n == 0) { |
| quevedo | 2:77388e8f0697 | 34 | s[0] = '0'; |
| quevedo | 2:77388e8f0697 | 35 | s[1] = 0; |
| quevedo | 2:77388e8f0697 | 36 | } else { |
| quevedo | 2:77388e8f0697 | 37 | while(n != 0) { |
| quevedo | 2:77388e8f0697 | 38 | r[k]= (n % 10) + '0'; |
| quevedo | 2:77388e8f0697 | 39 | n = n / 10; |
| quevedo | 2:77388e8f0697 | 40 | k++; |
| quevedo | 2:77388e8f0697 | 41 | } |
| quevedo | 2:77388e8f0697 | 42 | while(k > 0) { |
| quevedo | 2:77388e8f0697 | 43 | s[n] = r[k - 1] + '0'; |
| quevedo | 2:77388e8f0697 | 44 | n++; |
| quevedo | 2:77388e8f0697 | 45 | k--; |
| quevedo | 2:77388e8f0697 | 46 | } |
| quevedo | 2:77388e8f0697 | 47 | s[n] = 0; |
| quevedo | 2:77388e8f0697 | 48 | } |
| quevedo | 2:77388e8f0697 | 49 | } |
| quevedo | 2:77388e8f0697 | 50 | |
| quevedo | 0:e58f27687450 | 51 | // Sends command to ESP8266. Receives the command string |
| quevedo | 0:e58f27687450 | 52 | void ESP8266::SendCMD(char * s) { |
| quevedo | 0:e58f27687450 | 53 | AddEOL(s); |
| quevedo | 0:e58f27687450 | 54 | comm.printf("%s", s); |
| quevedo | 0:e58f27687450 | 55 | } |
| quevedo | 0:e58f27687450 | 56 | |
| quevedo | 0:e58f27687450 | 57 | // Resets the ESP8266 |
| quevedo | 0:e58f27687450 | 58 | void ESP8266::Reset(void) { |
| quevedo | 0:e58f27687450 | 59 | char rs[10]; |
| quevedo | 0:e58f27687450 | 60 | strcpy(rs, "AT+RST"); |
| quevedo | 0:e58f27687450 | 61 | SendCMD(rs); |
| quevedo | 0:e58f27687450 | 62 | } |
| quevedo | 0:e58f27687450 | 63 | |
| quevedo | 0:e58f27687450 | 64 | // Receive reply until no character is received after a given timeout in miliseconds |
| quevedo | 0:e58f27687450 | 65 | void ESP8266::RcvReply(char * r, int to) { |
| quevedo | 0:e58f27687450 | 66 | Timer t; |
| quevedo | 0:e58f27687450 | 67 | bool ended = 0; |
| quevedo | 0:e58f27687450 | 68 | char c; |
| quevedo | 0:e58f27687450 | 69 | |
| quevedo | 0:e58f27687450 | 70 | strcpy(r, ""); |
| quevedo | 0:e58f27687450 | 71 | t.start(); |
| quevedo | 0:e58f27687450 | 72 | while(!ended) { |
| quevedo | 0:e58f27687450 | 73 | if(comm.readable()) { |
| quevedo | 0:e58f27687450 | 74 | c = comm.getc(); |
| quevedo | 0:e58f27687450 | 75 | AddChar(r, c); |
| quevedo | 0:e58f27687450 | 76 | t.start(); |
| quevedo | 0:e58f27687450 | 77 | } |
| quevedo | 0:e58f27687450 | 78 | if(t.read_ms() > to) { |
| quevedo | 0:e58f27687450 | 79 | ended = 1; |
| quevedo | 0:e58f27687450 | 80 | } |
| quevedo | 0:e58f27687450 | 81 | } |
| quevedo | 0:e58f27687450 | 82 | AddChar(r, 0x00); |
| quevedo | 0:e58f27687450 | 83 | } |
| quevedo | 0:e58f27687450 | 84 | |
| Thrillex13 | 3:d52701d66b62 | 85 | // Receive reply until no character is received after a given timeout in miliseconds |
| Thrillex13 | 3:d52701d66b62 | 86 | void ESP8266::RcvSingleReply(char * r) { |
| Thrillex13 | 3:d52701d66b62 | 87 | bool ended = 0; |
| Thrillex13 | 3:d52701d66b62 | 88 | char c; |
| Thrillex13 | 3:d52701d66b62 | 89 | |
| Thrillex13 | 3:d52701d66b62 | 90 | strcpy(r, ""); |
| Thrillex13 | 3:d52701d66b62 | 91 | while(!ended) { |
| Thrillex13 | 3:d52701d66b62 | 92 | if(comm.readable()) { |
| Thrillex13 | 3:d52701d66b62 | 93 | c = comm.getc(); |
| Thrillex13 | 3:d52701d66b62 | 94 | AddChar(r, c); |
| Thrillex13 | 3:d52701d66b62 | 95 | } |
| Thrillex13 | 3:d52701d66b62 | 96 | if(c == 0x0D) { |
| Thrillex13 | 3:d52701d66b62 | 97 | ended = 1; |
| Thrillex13 | 3:d52701d66b62 | 98 | } |
| Thrillex13 | 3:d52701d66b62 | 99 | } |
| Thrillex13 | 3:d52701d66b62 | 100 | //AddChar(r, 0x00); |
| Thrillex13 | 3:d52701d66b62 | 101 | } |
| Thrillex13 | 3:d52701d66b62 | 102 | |
| quevedo | 0:e58f27687450 | 103 | // Gets the AP list. Parameter: the string to receive the list |
| quevedo | 0:e58f27687450 | 104 | void ESP8266::GetList(char * l) { |
| quevedo | 0:e58f27687450 | 105 | char rs[15]; |
| quevedo | 0:e58f27687450 | 106 | strcpy(rs, "AT+CWLAP"); |
| quevedo | 0:e58f27687450 | 107 | SendCMD(rs); |
| quevedo | 0:e58f27687450 | 108 | RcvReply(l, 5000); // Needs big timeout because it takes long to start replying |
| quevedo | 0:e58f27687450 | 109 | } |
| quevedo | 0:e58f27687450 | 110 | |
| quevedo | 0:e58f27687450 | 111 | // Joins a Wifi AP. Parameters: SSID and Password (strings) |
| quevedo | 0:e58f27687450 | 112 | void ESP8266::Join(char * id, char * pwd) { |
| quevedo | 0:e58f27687450 | 113 | char cmd[255]; |
| quevedo | 0:e58f27687450 | 114 | strcpy(cmd, "AT+CWJAP="); |
| quevedo | 0:e58f27687450 | 115 | AddChar(cmd, 0x22); |
| quevedo | 0:e58f27687450 | 116 | strcat(cmd, id); |
| quevedo | 0:e58f27687450 | 117 | AddChar(cmd, 0x22); |
| quevedo | 0:e58f27687450 | 118 | AddChar(cmd, 0x2C); |
| quevedo | 0:e58f27687450 | 119 | AddChar(cmd, 0x22); |
| quevedo | 0:e58f27687450 | 120 | strcat(cmd, pwd); |
| quevedo | 0:e58f27687450 | 121 | AddChar(cmd, 0x22); |
| quevedo | 0:e58f27687450 | 122 | SendCMD(cmd); |
| quevedo | 0:e58f27687450 | 123 | } |
| quevedo | 0:e58f27687450 | 124 | |
| quevedo | 0:e58f27687450 | 125 | // Gets ESP IP. Parameter: string to contain IP |
| quevedo | 0:e58f27687450 | 126 | void ESP8266::GetIP(char * ip) { |
| quevedo | 0:e58f27687450 | 127 | char cmd[15]; |
| quevedo | 0:e58f27687450 | 128 | strcpy(cmd, "AT+CIFSR"); |
| quevedo | 0:e58f27687450 | 129 | SendCMD(cmd); |
| quevedo | 0:e58f27687450 | 130 | RcvReply(ip, 2000); |
| quevedo | 1:399414d48048 | 131 | } |
| quevedo | 1:399414d48048 | 132 | |
| quevedo | 1:399414d48048 | 133 | //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both |
| quevedo | 1:399414d48048 | 134 | void ESP8266::SetMode(char mode) { |
| quevedo | 1:399414d48048 | 135 | char cmd[15]; |
| quevedo | 1:399414d48048 | 136 | strcpy(cmd, "AT+CWMODE="); |
| quevedo | 1:399414d48048 | 137 | mode = mode + 0x30; // Converts number into corresponding ASCII character |
| quevedo | 1:399414d48048 | 138 | AddChar(cmd, mode); // Completes command string |
| quevedo | 1:399414d48048 | 139 | SendCMD(cmd); |
| quevedo | 1:399414d48048 | 140 | } |
| quevedo | 1:399414d48048 | 141 | |
| quevedo | 1:399414d48048 | 142 | // Quits the AP |
| quevedo | 1:399414d48048 | 143 | void ESP8266::Quit(void) { |
| quevedo | 1:399414d48048 | 144 | char cmd[15]; |
| quevedo | 1:399414d48048 | 145 | strcpy(cmd, "AT+CWQAP"); |
| quevedo | 1:399414d48048 | 146 | SendCMD(cmd); |
| quevedo | 1:399414d48048 | 147 | } |
| quevedo | 1:399414d48048 | 148 | |
| quevedo | 1:399414d48048 | 149 | // Sets single connection |
| quevedo | 1:399414d48048 | 150 | void ESP8266::SetSingle(void) { |
| quevedo | 1:399414d48048 | 151 | char cmd[15]; |
| quevedo | 1:399414d48048 | 152 | strcpy(cmd, "AT+CIPMUX=0"); |
| quevedo | 1:399414d48048 | 153 | SendCMD(cmd); |
| quevedo | 1:399414d48048 | 154 | } |
| quevedo | 1:399414d48048 | 155 | |
| quevedo | 1:399414d48048 | 156 | // Sets multiple connection |
| quevedo | 1:399414d48048 | 157 | void ESP8266::SetMultiple(void) { |
| quevedo | 1:399414d48048 | 158 | char rs[15]; |
| quevedo | 1:399414d48048 | 159 | strcpy(rs, "AT+CIPMUX=1"); |
| quevedo | 1:399414d48048 | 160 | SendCMD(rs); |
| quevedo | 2:77388e8f0697 | 161 | } |
| quevedo | 2:77388e8f0697 | 162 | |
| quevedo | 2:77388e8f0697 | 163 | // Gets connection status. Parameter: string to contain status |
| quevedo | 2:77388e8f0697 | 164 | void ESP8266::GetConnStatus(char * st) { |
| quevedo | 2:77388e8f0697 | 165 | char cmd[15]; |
| quevedo | 2:77388e8f0697 | 166 | strcpy(cmd, "AT+CIPSTATUS"); |
| quevedo | 2:77388e8f0697 | 167 | SendCMD(cmd); |
| Thrillex13 | 3:d52701d66b62 | 168 | RcvSingleReply(st); |
| Thrillex13 | 3:d52701d66b62 | 169 | //RcvReply(st, 2000); |
| quevedo | 2:77388e8f0697 | 170 | } |
| quevedo | 2:77388e8f0697 | 171 | |
| quevedo | 2:77388e8f0697 | 172 | // Starts server mode. Parameter: port to be used |
| quevedo | 2:77388e8f0697 | 173 | void ESP8266::StartServerMode(int port) { |
| quevedo | 2:77388e8f0697 | 174 | char rs[25]; |
| quevedo | 2:77388e8f0697 | 175 | char t[4]; |
| quevedo | 2:77388e8f0697 | 176 | strcpy(rs, "AT+CIPSERVER=1,"); |
| quevedo | 2:77388e8f0697 | 177 | itoa(port, t); |
| quevedo | 2:77388e8f0697 | 178 | strcat(rs, t); |
| quevedo | 2:77388e8f0697 | 179 | SendCMD(rs); |
| quevedo | 2:77388e8f0697 | 180 | } |
| quevedo | 2:77388e8f0697 | 181 | |
| quevedo | 2:77388e8f0697 | 182 | // Close server mode. |
| quevedo | 2:77388e8f0697 | 183 | void ESP8266::CloseServerMode(void) { |
| quevedo | 2:77388e8f0697 | 184 | char rs[20]; |
| quevedo | 2:77388e8f0697 | 185 | strcpy(rs, "AT+CIPSERVER=0"); |
| quevedo | 2:77388e8f0697 | 186 | SendCMD(rs); |
| Thrillex13 | 3:d52701d66b62 | 187 | } |
| Thrillex13 | 3:d52701d66b62 | 188 | |
| Thrillex13 | 3:d52701d66b62 | 189 | // Starts SmartConfig |
| Thrillex13 | 3:d52701d66b62 | 190 | void ESP8266::StartSmartConfig(void){ |
| Thrillex13 | 3:d52701d66b62 | 191 | char rs[20]; |
| Thrillex13 | 3:d52701d66b62 | 192 | strcpy(rs, "AT+CWSTARTSMART=1"); |
| Thrillex13 | 3:d52701d66b62 | 193 | SendCMD(rs); |
| Thrillex13 | 3:d52701d66b62 | 194 | } |
| Thrillex13 | 3:d52701d66b62 | 195 | |
| Thrillex13 | 3:d52701d66b62 | 196 | //Disable Echo |
| Thrillex13 | 3:d52701d66b62 | 197 | void ESP8266::DisableEcho(void) { |
| Thrillex13 | 3:d52701d66b62 | 198 | char cmd[15]; |
| Thrillex13 | 3:d52701d66b62 | 199 | strcpy(cmd, "ATE0"); |
| Thrillex13 | 3:d52701d66b62 | 200 | SendCMD(cmd); |
| quevedo | 0:e58f27687450 | 201 | } |