Added monitoring feature of ESP8266's UART

Dependents:   ESP8266_W7500_Example DualNetworkInterface-Basic

Fork of ESP8266Interface by ESP8266

Committer:
michaeljkoster
Date:
Tue Dec 02 16:02:21 2014 +0000
Revision:
22:c4360e61486a
Parent:
20:d764237405c2
Child:
23:de9221771e96
Add state.cmdMode and some cleanup

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:fb4494783863 1 /* Copyright (C) 2012 mbed.org, MIT License
samux 1:fb4494783863 2 *
samux 1:fb4494783863 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 1:fb4494783863 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
samux 1:fb4494783863 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
samux 1:fb4494783863 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
samux 1:fb4494783863 7 * furnished to do so, subject to the following conditions:
samux 1:fb4494783863 8 *
samux 1:fb4494783863 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 1:fb4494783863 10 * substantial portions of the Software.
samux 1:fb4494783863 11 *
samux 1:fb4494783863 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 1:fb4494783863 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 1:fb4494783863 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 1:fb4494783863 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 1:fb4494783863 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 1:fb4494783863 17 */
samux 1:fb4494783863 18
samux 1:fb4494783863 19 #include "mbed.h"
michaeljkoster 13:41098c907200 20 #include "ESP8266.h"
michaeljkoster 16:3f0efaa57a12 21 #include "Endpoint.h"
samux 1:fb4494783863 22 #include <string>
samux 1:fb4494783863 23 #include <algorithm>
samux 1:fb4494783863 24
samux 1:fb4494783863 25 //Debug is disabled by default
screamer 10:131675c17372 26 #if (defined(DEBUG))
michaeljkoster 13:41098c907200 27 #define DBG(x, ...) std::printf("[ESP8266 : DBG]"x"\r\n", ##__VA_ARGS__);
michaeljkoster 13:41098c907200 28 #define WARN(x, ...) std::printf("[ESP8266 : WARN]"x"\r\n", ##__VA_ARGS__);
michaeljkoster 13:41098c907200 29 #define ERR(x, ...) std::printf("[ESP8266 : ERR]"x"\r\n", ##__VA_ARGS__);
samux 1:fb4494783863 30 #else
samux 1:fb4494783863 31 #define DBG(x, ...)
samux 1:fb4494783863 32 #define WARN(x, ...)
samux 1:fb4494783863 33 #define ERR(x, ...)
samux 1:fb4494783863 34 #endif
samux 1:fb4494783863 35
screamer 10:131675c17372 36 #if defined(DEBUG)
michaeljkoster 13:41098c907200 37 #define INFO(x, ...) printf("[ESP8266 : INFO]"x"\r\n", ##__VA_ARGS__);
samux 1:fb4494783863 38 #else
samux 1:fb4494783863 39 #define INFO(x, ...)
samux 1:fb4494783863 40 #endif
samux 1:fb4494783863 41
samux 1:fb4494783863 42 #define MAX_TRY_JOIN 3
samux 1:fb4494783863 43
michaeljkoster 12:c5f0eac67a8a 44 extern Serial pc;
michaeljkoster 12:c5f0eac67a8a 45
michaeljkoster 13:41098c907200 46 ESP8266 * ESP8266::inst;
samux 1:fb4494783863 47
michaeljkoster 13:41098c907200 48 ESP8266::ESP8266( PinName tx, PinName rx, PinName _reset, const char * ssid, const char * phrase ):
michaeljkoster 13:41098c907200 49 wifi(tx, rx), reset_pin(_reset), buf_ESP8266(256)
samux 1:fb4494783863 50 {
samux 1:fb4494783863 51 memset(&state, 0, sizeof(state));
samux 1:fb4494783863 52
samux 1:fb4494783863 53 // change all ' ' in '$' in the ssid and the passphrase
samux 1:fb4494783863 54 strcpy(this->ssid, ssid);
samux 1:fb4494783863 55 for (int i = 0; i < strlen(ssid); i++) {
samux 1:fb4494783863 56 if (this->ssid[i] == ' ')
samux 1:fb4494783863 57 this->ssid[i] = '$';
samux 1:fb4494783863 58 }
samux 1:fb4494783863 59 strcpy(this->phrase, phrase);
samux 1:fb4494783863 60 for (int i = 0; i < strlen(phrase); i++) {
samux 1:fb4494783863 61 if (this->phrase[i] == ' ')
samux 1:fb4494783863 62 this->phrase[i] = '$';
samux 1:fb4494783863 63 }
samux 1:fb4494783863 64
samux 1:fb4494783863 65 inst = this;
samux 1:fb4494783863 66 attach_rx(false);
michaeljkoster 16:3f0efaa57a12 67
michaeljkoster 16:3f0efaa57a12 68 wifi.baud(9600); // initial baud rate of the ESP8266
michaeljkoster 22:c4360e61486a 69
michaeljkoster 22:c4360e61486a 70 state.associated = false;
michaeljkoster 22:c4360e61486a 71 state.cmdMode = false;
samux 1:fb4494783863 72 }
samux 1:fb4494783863 73
michaeljkoster 13:41098c907200 74 bool ESP8266::join()
samux 1:fb4494783863 75 {
michaeljkoster 16:3f0efaa57a12 76 string cmd="AT+CWJAP=\""+(string)this->ssid+"\",\""+(string)this->phrase+"\"";
michaeljkoster 16:3f0efaa57a12 77 if( sendCommand( cmd.c_str(), "OK", NULL, 10000) ){
michaeljkoster 16:3f0efaa57a12 78 // successfully joined the network
samux 1:fb4494783863 79 state.associated = true;
michaeljkoster 13:41098c907200 80 INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase);
samux 1:fb4494783863 81 return true;
samux 1:fb4494783863 82 }
samux 1:fb4494783863 83 return false;
samux 1:fb4494783863 84 }
samux 1:fb4494783863 85
michaeljkoster 13:41098c907200 86 bool ESP8266::connect()
samux 1:fb4494783863 87 {
michaeljkoster 16:3f0efaa57a12 88 return true;
samux 1:fb4494783863 89 }
samux 1:fb4494783863 90
michaeljkoster 15:37a7a56a424f 91 bool ESP8266::is_connected()
michaeljkoster 15:37a7a56a424f 92 {
michaeljkoster 15:37a7a56a424f 93 return true;
michaeljkoster 15:37a7a56a424f 94 }
michaeljkoster 15:37a7a56a424f 95
michaeljkoster 16:3f0efaa57a12 96 bool ESP8266::startUDP(char* ip, int port){
michaeljkoster 17:d11fa4c3ac65 97 char portstr[5];
michaeljkoster 16:3f0efaa57a12 98 sprintf(portstr, "%d", port);
michaeljkoster 18:60422852e99c 99 sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000);
michaeljkoster 16:3f0efaa57a12 100
michaeljkoster 17:d11fa4c3ac65 101 sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode
michaeljkoster 17:d11fa4c3ac65 102 sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode
michaeljkoster 22:c4360e61486a 103 pc.printf("Data Mode\r\n");
michaeljkoster 22:c4360e61486a 104 state.cmdMode = false;
michaeljkoster 22:c4360e61486a 105
michaeljkoster 16:3f0efaa57a12 106 return true;
michaeljkoster 16:3f0efaa57a12 107 }
michaeljkoster 16:3f0efaa57a12 108
michaeljkoster 15:37a7a56a424f 109 bool ESP8266::close()
michaeljkoster 15:37a7a56a424f 110 {
michaeljkoster 17:d11fa4c3ac65 111 send("+++",3);
michaeljkoster 17:d11fa4c3ac65 112 wait(1);
michaeljkoster 22:c4360e61486a 113 state.cmdMode = true;
michaeljkoster 16:3f0efaa57a12 114 sendCommand("AT+CIPCLOSE","OK", NULL, 10000);
michaeljkoster 15:37a7a56a424f 115 return true;
michaeljkoster 15:37a7a56a424f 116 }
michaeljkoster 15:37a7a56a424f 117
michaeljkoster 15:37a7a56a424f 118 bool ESP8266::disconnect()
michaeljkoster 15:37a7a56a424f 119 {
michaeljkoster 15:37a7a56a424f 120 // if already disconnected, return
michaeljkoster 15:37a7a56a424f 121 if (!state.associated)
michaeljkoster 15:37a7a56a424f 122 return true;
michaeljkoster 15:37a7a56a424f 123 // send command to quit AP
michaeljkoster 16:3f0efaa57a12 124 sendCommand("AT+CWQAP", "OK", NULL, 10000);
michaeljkoster 15:37a7a56a424f 125 state.associated = false;
michaeljkoster 15:37a7a56a424f 126 return true;
michaeljkoster 15:37a7a56a424f 127 }
michaeljkoster 15:37a7a56a424f 128
michaeljkoster 15:37a7a56a424f 129 char* ESP8266::getIPAddress()
michaeljkoster 15:37a7a56a424f 130 {
michaeljkoster 16:3f0efaa57a12 131 sendCommand("AT+CWLIF", "OK", NULL, 10000);
michaeljkoster 15:37a7a56a424f 132 return ipString;
michaeljkoster 15:37a7a56a424f 133 }
michaeljkoster 15:37a7a56a424f 134
michaeljkoster 13:41098c907200 135 bool ESP8266::gethostbyname(const char * host, char * ip)
samux 1:fb4494783863 136 {
samux 1:fb4494783863 137 string h = host;
samux 1:fb4494783863 138 int nb_digits = 0;
samux 1:fb4494783863 139
samux 1:fb4494783863 140 // no dns needed
samux 1:fb4494783863 141 int pos = h.find(".");
samux 1:fb4494783863 142 if (pos != string::npos) {
samux 1:fb4494783863 143 string sub = h.substr(0, h.find("."));
samux 1:fb4494783863 144 nb_digits = atoi(sub.c_str());
samux 1:fb4494783863 145 }
samux 1:fb4494783863 146 //printf("substrL %s\r\n", sub.c_str());
samux 1:fb4494783863 147 if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) {
samux 1:fb4494783863 148 strcpy(ip, host);
michaeljkoster 13:41098c907200 149 return true;
samux 1:fb4494783863 150 }
samux 1:fb4494783863 151 else {
michaeljkoster 13:41098c907200 152 // dns needed, not currently available
michaeljkoster 13:41098c907200 153 return false;
michaeljkoster 13:41098c907200 154 }
michaeljkoster 13:41098c907200 155 }
samux 1:fb4494783863 156
michaeljkoster 13:41098c907200 157 void ESP8266::reset()
samux 1:fb4494783863 158 {
michaeljkoster 22:c4360e61486a 159 send("+++",3);
michaeljkoster 22:c4360e61486a 160 wait(1);
michaeljkoster 22:c4360e61486a 161 state.cmdMode = true;
michaeljkoster 22:c4360e61486a 162 sendCommand("AT", "OK", NULL, 1000);
michaeljkoster 16:3f0efaa57a12 163 sendCommand("AT+RST", "ready", NULL, 10000);
michaeljkoster 22:c4360e61486a 164 state.associated = false;
michaeljkoster 16:3f0efaa57a12 165 /*
samux 1:fb4494783863 166 reset_pin = 0;
samux 1:fb4494783863 167 wait(0.2);
samux 1:fb4494783863 168 reset_pin = 1;
samux 1:fb4494783863 169 wait(0.2);
michaeljkoster 16:3f0efaa57a12 170 */
samux 1:fb4494783863 171 }
samux 1:fb4494783863 172
michaeljkoster 13:41098c907200 173 bool ESP8266::reboot()
samux 3:9aa05e19c62e 174 {
michaeljkoster 16:3f0efaa57a12 175 reset();
samux 3:9aa05e19c62e 176 return true;
samux 3:9aa05e19c62e 177 }
samux 3:9aa05e19c62e 178
michaeljkoster 15:37a7a56a424f 179 void ESP8266::handler_rx(void)
samux 1:fb4494783863 180 {
michaeljkoster 15:37a7a56a424f 181 //read characters
michaeljkoster 16:3f0efaa57a12 182 char c;
michaeljkoster 16:3f0efaa57a12 183 while (wifi.readable()){
michaeljkoster 16:3f0efaa57a12 184 c=wifi.getc();
michaeljkoster 16:3f0efaa57a12 185 buf_ESP8266.queue(c);
michaeljkoster 22:c4360e61486a 186 if (state.cmdMode) pc.printf("%c",c); //debug echo
michaeljkoster 16:3f0efaa57a12 187 }
michaeljkoster 15:37a7a56a424f 188 }
michaeljkoster 15:37a7a56a424f 189
michaeljkoster 15:37a7a56a424f 190 void ESP8266::attach_rx(bool callback)
michaeljkoster 15:37a7a56a424f 191 {
michaeljkoster 15:37a7a56a424f 192 if (!callback)
michaeljkoster 15:37a7a56a424f 193 wifi.attach(NULL);
michaeljkoster 15:37a7a56a424f 194 else
michaeljkoster 15:37a7a56a424f 195 wifi.attach(this, &ESP8266::handler_rx);
samux 1:fb4494783863 196 }
samux 1:fb4494783863 197
michaeljkoster 13:41098c907200 198 int ESP8266::readable()
samux 1:fb4494783863 199 {
michaeljkoster 13:41098c907200 200 return buf_ESP8266.available();
samux 1:fb4494783863 201 }
samux 1:fb4494783863 202
michaeljkoster 13:41098c907200 203 int ESP8266::writeable()
samux 1:fb4494783863 204 {
samux 1:fb4494783863 205 return wifi.writeable();
samux 1:fb4494783863 206 }
samux 1:fb4494783863 207
michaeljkoster 13:41098c907200 208 char ESP8266::getc()
samux 1:fb4494783863 209 {
michaeljkoster 14:4d1128f72e00 210 char c=0;
michaeljkoster 13:41098c907200 211 while (!buf_ESP8266.available());
michaeljkoster 13:41098c907200 212 buf_ESP8266.dequeue(&c);
samux 1:fb4494783863 213 return c;
samux 1:fb4494783863 214 }
samux 1:fb4494783863 215
michaeljkoster 15:37a7a56a424f 216 int ESP8266::putc(char c)
samux 1:fb4494783863 217 {
michaeljkoster 20:d764237405c2 218 while (!wifi.writeable() || wifi.readable()); //wait for echoed command characters to be read first
michaeljkoster 15:37a7a56a424f 219 return wifi.putc(c);
samux 1:fb4494783863 220 }
samux 1:fb4494783863 221
michaeljkoster 15:37a7a56a424f 222 void ESP8266::flush()
samux 1:fb4494783863 223 {
michaeljkoster 15:37a7a56a424f 224 buf_ESP8266.flush();
samux 1:fb4494783863 225 }
samux 1:fb4494783863 226
michaeljkoster 16:3f0efaa57a12 227 int ESP8266::send(const char * buf, int len)
michaeljkoster 15:37a7a56a424f 228 {
michaeljkoster 18:60422852e99c 229
michaeljkoster 16:3f0efaa57a12 230 const char* bufptr=buf;
michaeljkoster 16:3f0efaa57a12 231 for(int i=0; i<len; i++){
michaeljkoster 17:d11fa4c3ac65 232 putc((int)*bufptr++);
michaeljkoster 18:60422852e99c 233 }
michaeljkoster 19:fb8d5bff2076 234 return len;
michaeljkoster 15:37a7a56a424f 235 }
samux 1:fb4494783863 236
michaeljkoster 16:3f0efaa57a12 237 bool ESP8266::sendCommand(const char * cmd, const char * ACK, char * res, int timeout)
samux 1:fb4494783863 238 {
samux 1:fb4494783863 239 char read;
samux 1:fb4494783863 240 size_t found = string::npos;
samux 1:fb4494783863 241 string checking;
samux 1:fb4494783863 242 Timer tmr;
samux 1:fb4494783863 243 int result = 0;
samux 1:fb4494783863 244
michaeljkoster 17:d11fa4c3ac65 245 //pc.printf("will send: %s\r\n",cmd);
samux 1:fb4494783863 246
michaeljkoster 17:d11fa4c3ac65 247 attach_rx(true);
samux 1:fb4494783863 248
samux 1:fb4494783863 249 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 250 while (readable())
michaeljkoster 17:d11fa4c3ac65 251 getc();
michaeljkoster 16:3f0efaa57a12 252
samux 1:fb4494783863 253 if (!ACK || !strcmp(ACK, "NO")) {
michaeljkoster 17:d11fa4c3ac65 254 for (int i = 0; i < strlen(cmd); i++){
michaeljkoster 16:3f0efaa57a12 255 result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
michaeljkoster 20:d764237405c2 256 wait(.005); // prevents stuck recieve ready (?) need to let echoed character get read first
michaeljkoster 16:3f0efaa57a12 257 }
michaeljkoster 16:3f0efaa57a12 258 putc(13); //CR
michaeljkoster 20:d764237405c2 259 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 260 putc(10); //LF
michaeljkoster 16:3f0efaa57a12 261
samux 1:fb4494783863 262 } else {
samux 1:fb4494783863 263 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 264 while (readable())
michaeljkoster 17:d11fa4c3ac65 265 getc();
samux 1:fb4494783863 266
samux 1:fb4494783863 267 tmr.start();
michaeljkoster 17:d11fa4c3ac65 268 for (int i = 0; i < strlen(cmd); i++){
michaeljkoster 16:3f0efaa57a12 269 result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
michaeljkoster 20:d764237405c2 270 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 271 }
michaeljkoster 16:3f0efaa57a12 272 putc(13); //CR
michaeljkoster 20:d764237405c2 273 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 274 putc(10); //LF
samux 1:fb4494783863 275
samux 1:fb4494783863 276 while (1) {
samux 1:fb4494783863 277 if (tmr.read_ms() > timeout) {
samux 1:fb4494783863 278 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 279 while (readable())
michaeljkoster 17:d11fa4c3ac65 280 getc();
samux 1:fb4494783863 281
samux 1:fb4494783863 282 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 283
samux 1:fb4494783863 284 attach_rx(true);
samux 1:fb4494783863 285 return -1;
michaeljkoster 17:d11fa4c3ac65 286 } else if (readable()) {
michaeljkoster 17:d11fa4c3ac65 287 read = getc();
samux 1:fb4494783863 288 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 289 checking += read;
samux 1:fb4494783863 290 found = checking.find(ACK);
samux 1:fb4494783863 291 if (found != string::npos) {
samux 1:fb4494783863 292 wait(0.01);
samux 1:fb4494783863 293
samux 1:fb4494783863 294 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 295 while (readable())
michaeljkoster 17:d11fa4c3ac65 296 getc();
samux 1:fb4494783863 297
samux 1:fb4494783863 298 break;
samux 1:fb4494783863 299 }
samux 1:fb4494783863 300 }
samux 1:fb4494783863 301 }
samux 1:fb4494783863 302 }
samux 1:fb4494783863 303 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 304
samux 1:fb4494783863 305 attach_rx(true);
samux 1:fb4494783863 306 return result;
samux 1:fb4494783863 307 }
samux 1:fb4494783863 308
samux 1:fb4494783863 309 //the user wants the result from the command (ACK == NULL, res != NULL)
samux 1:fb4494783863 310 if ( res != NULL) {
samux 1:fb4494783863 311 int i = 0;
samux 1:fb4494783863 312 Timer timeout;
samux 1:fb4494783863 313 timeout.start();
samux 1:fb4494783863 314 tmr.reset();
samux 1:fb4494783863 315 while (1) {
samux 1:fb4494783863 316 if (timeout.read() > 2) {
samux 1:fb4494783863 317 if (i == 0) {
samux 1:fb4494783863 318 res = NULL;
samux 1:fb4494783863 319 break;
samux 1:fb4494783863 320 }
samux 1:fb4494783863 321 res[i] = '\0';
samux 1:fb4494783863 322 DBG("user str 1: %s\r\n", res);
samux 1:fb4494783863 323
samux 1:fb4494783863 324 break;
samux 1:fb4494783863 325 } else {
samux 1:fb4494783863 326 if (tmr.read_ms() > 300) {
samux 1:fb4494783863 327 res[i] = '\0';
samux 1:fb4494783863 328 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 329
samux 1:fb4494783863 330 break;
samux 1:fb4494783863 331 }
michaeljkoster 17:d11fa4c3ac65 332 if (readable()) {
samux 1:fb4494783863 333 tmr.start();
michaeljkoster 17:d11fa4c3ac65 334 read = getc();
samux 1:fb4494783863 335
samux 1:fb4494783863 336 // we drop \r and \n
samux 1:fb4494783863 337 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 338 res[i++] = read;
samux 1:fb4494783863 339 }
samux 1:fb4494783863 340 }
samux 1:fb4494783863 341 }
samux 1:fb4494783863 342 }
samux 1:fb4494783863 343 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 344 }
samux 1:fb4494783863 345
samux 1:fb4494783863 346 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 347 while (readable())
michaeljkoster 17:d11fa4c3ac65 348 getc();
samux 1:fb4494783863 349
samux 1:fb4494783863 350 attach_rx(true);
samux 1:fb4494783863 351 DBG("result: %d\r\n", result)
samux 1:fb4494783863 352 return result;
michaeljkoster 15:37a7a56a424f 353 }