Added monitoring feature of ESP8266's UART

Dependents:   ESP8266_W7500_Example DualNetworkInterface-Basic

Fork of ESP8266Interface by ESP8266

Committer:
michaeljkoster
Date:
Mon Dec 08 00:53:07 2014 +0000
Revision:
23:de9221771e96
Parent:
22:c4360e61486a
Child:
24:03585a13ff3b
Added CWMODE command to join()

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 23:de9221771e96 76 sendCommand( "AT+CWMODE=1", "change", NULL, 1000);
michaeljkoster 16:3f0efaa57a12 77 string cmd="AT+CWJAP=\""+(string)this->ssid+"\",\""+(string)this->phrase+"\"";
michaeljkoster 16:3f0efaa57a12 78 if( sendCommand( cmd.c_str(), "OK", NULL, 10000) ){
michaeljkoster 16:3f0efaa57a12 79 // successfully joined the network
samux 1:fb4494783863 80 state.associated = true;
michaeljkoster 13:41098c907200 81 INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase);
samux 1:fb4494783863 82 return true;
samux 1:fb4494783863 83 }
samux 1:fb4494783863 84 return false;
samux 1:fb4494783863 85 }
samux 1:fb4494783863 86
michaeljkoster 13:41098c907200 87 bool ESP8266::connect()
samux 1:fb4494783863 88 {
michaeljkoster 16:3f0efaa57a12 89 return true;
samux 1:fb4494783863 90 }
samux 1:fb4494783863 91
michaeljkoster 15:37a7a56a424f 92 bool ESP8266::is_connected()
michaeljkoster 15:37a7a56a424f 93 {
michaeljkoster 15:37a7a56a424f 94 return true;
michaeljkoster 15:37a7a56a424f 95 }
michaeljkoster 15:37a7a56a424f 96
michaeljkoster 16:3f0efaa57a12 97 bool ESP8266::startUDP(char* ip, int port){
michaeljkoster 17:d11fa4c3ac65 98 char portstr[5];
michaeljkoster 16:3f0efaa57a12 99 sprintf(portstr, "%d", port);
michaeljkoster 18:60422852e99c 100 sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000);
michaeljkoster 16:3f0efaa57a12 101
michaeljkoster 17:d11fa4c3ac65 102 sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode
michaeljkoster 17:d11fa4c3ac65 103 sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode
michaeljkoster 22:c4360e61486a 104 pc.printf("Data Mode\r\n");
michaeljkoster 22:c4360e61486a 105 state.cmdMode = false;
michaeljkoster 22:c4360e61486a 106
michaeljkoster 16:3f0efaa57a12 107 return true;
michaeljkoster 16:3f0efaa57a12 108 }
michaeljkoster 16:3f0efaa57a12 109
michaeljkoster 15:37a7a56a424f 110 bool ESP8266::close()
michaeljkoster 15:37a7a56a424f 111 {
michaeljkoster 17:d11fa4c3ac65 112 send("+++",3);
michaeljkoster 17:d11fa4c3ac65 113 wait(1);
michaeljkoster 22:c4360e61486a 114 state.cmdMode = true;
michaeljkoster 16:3f0efaa57a12 115 sendCommand("AT+CIPCLOSE","OK", NULL, 10000);
michaeljkoster 15:37a7a56a424f 116 return true;
michaeljkoster 15:37a7a56a424f 117 }
michaeljkoster 15:37a7a56a424f 118
michaeljkoster 15:37a7a56a424f 119 bool ESP8266::disconnect()
michaeljkoster 15:37a7a56a424f 120 {
michaeljkoster 15:37a7a56a424f 121 // if already disconnected, return
michaeljkoster 15:37a7a56a424f 122 if (!state.associated)
michaeljkoster 15:37a7a56a424f 123 return true;
michaeljkoster 15:37a7a56a424f 124 // send command to quit AP
michaeljkoster 16:3f0efaa57a12 125 sendCommand("AT+CWQAP", "OK", NULL, 10000);
michaeljkoster 15:37a7a56a424f 126 state.associated = false;
michaeljkoster 15:37a7a56a424f 127 return true;
michaeljkoster 15:37a7a56a424f 128 }
michaeljkoster 15:37a7a56a424f 129
michaeljkoster 15:37a7a56a424f 130 char* ESP8266::getIPAddress()
michaeljkoster 15:37a7a56a424f 131 {
michaeljkoster 16:3f0efaa57a12 132 sendCommand("AT+CWLIF", "OK", NULL, 10000);
michaeljkoster 15:37a7a56a424f 133 return ipString;
michaeljkoster 15:37a7a56a424f 134 }
michaeljkoster 15:37a7a56a424f 135
michaeljkoster 13:41098c907200 136 bool ESP8266::gethostbyname(const char * host, char * ip)
samux 1:fb4494783863 137 {
samux 1:fb4494783863 138 string h = host;
samux 1:fb4494783863 139 int nb_digits = 0;
samux 1:fb4494783863 140
samux 1:fb4494783863 141 // no dns needed
samux 1:fb4494783863 142 int pos = h.find(".");
samux 1:fb4494783863 143 if (pos != string::npos) {
samux 1:fb4494783863 144 string sub = h.substr(0, h.find("."));
samux 1:fb4494783863 145 nb_digits = atoi(sub.c_str());
samux 1:fb4494783863 146 }
samux 1:fb4494783863 147 //printf("substrL %s\r\n", sub.c_str());
samux 1:fb4494783863 148 if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) {
samux 1:fb4494783863 149 strcpy(ip, host);
michaeljkoster 13:41098c907200 150 return true;
samux 1:fb4494783863 151 }
samux 1:fb4494783863 152 else {
michaeljkoster 13:41098c907200 153 // dns needed, not currently available
michaeljkoster 13:41098c907200 154 return false;
michaeljkoster 13:41098c907200 155 }
michaeljkoster 13:41098c907200 156 }
samux 1:fb4494783863 157
michaeljkoster 13:41098c907200 158 void ESP8266::reset()
samux 1:fb4494783863 159 {
michaeljkoster 22:c4360e61486a 160 send("+++",3);
michaeljkoster 22:c4360e61486a 161 wait(1);
michaeljkoster 22:c4360e61486a 162 state.cmdMode = true;
michaeljkoster 22:c4360e61486a 163 sendCommand("AT", "OK", NULL, 1000);
michaeljkoster 16:3f0efaa57a12 164 sendCommand("AT+RST", "ready", NULL, 10000);
michaeljkoster 22:c4360e61486a 165 state.associated = false;
michaeljkoster 16:3f0efaa57a12 166 /*
samux 1:fb4494783863 167 reset_pin = 0;
samux 1:fb4494783863 168 wait(0.2);
samux 1:fb4494783863 169 reset_pin = 1;
samux 1:fb4494783863 170 wait(0.2);
michaeljkoster 16:3f0efaa57a12 171 */
samux 1:fb4494783863 172 }
samux 1:fb4494783863 173
michaeljkoster 13:41098c907200 174 bool ESP8266::reboot()
samux 3:9aa05e19c62e 175 {
michaeljkoster 16:3f0efaa57a12 176 reset();
samux 3:9aa05e19c62e 177 return true;
samux 3:9aa05e19c62e 178 }
samux 3:9aa05e19c62e 179
michaeljkoster 15:37a7a56a424f 180 void ESP8266::handler_rx(void)
samux 1:fb4494783863 181 {
michaeljkoster 15:37a7a56a424f 182 //read characters
michaeljkoster 16:3f0efaa57a12 183 char c;
michaeljkoster 16:3f0efaa57a12 184 while (wifi.readable()){
michaeljkoster 16:3f0efaa57a12 185 c=wifi.getc();
michaeljkoster 16:3f0efaa57a12 186 buf_ESP8266.queue(c);
michaeljkoster 22:c4360e61486a 187 if (state.cmdMode) pc.printf("%c",c); //debug echo
michaeljkoster 16:3f0efaa57a12 188 }
michaeljkoster 15:37a7a56a424f 189 }
michaeljkoster 15:37a7a56a424f 190
michaeljkoster 15:37a7a56a424f 191 void ESP8266::attach_rx(bool callback)
michaeljkoster 15:37a7a56a424f 192 {
michaeljkoster 15:37a7a56a424f 193 if (!callback)
michaeljkoster 15:37a7a56a424f 194 wifi.attach(NULL);
michaeljkoster 15:37a7a56a424f 195 else
michaeljkoster 15:37a7a56a424f 196 wifi.attach(this, &ESP8266::handler_rx);
samux 1:fb4494783863 197 }
samux 1:fb4494783863 198
michaeljkoster 13:41098c907200 199 int ESP8266::readable()
samux 1:fb4494783863 200 {
michaeljkoster 13:41098c907200 201 return buf_ESP8266.available();
samux 1:fb4494783863 202 }
samux 1:fb4494783863 203
michaeljkoster 13:41098c907200 204 int ESP8266::writeable()
samux 1:fb4494783863 205 {
samux 1:fb4494783863 206 return wifi.writeable();
samux 1:fb4494783863 207 }
samux 1:fb4494783863 208
michaeljkoster 13:41098c907200 209 char ESP8266::getc()
samux 1:fb4494783863 210 {
michaeljkoster 14:4d1128f72e00 211 char c=0;
michaeljkoster 13:41098c907200 212 while (!buf_ESP8266.available());
michaeljkoster 13:41098c907200 213 buf_ESP8266.dequeue(&c);
samux 1:fb4494783863 214 return c;
samux 1:fb4494783863 215 }
samux 1:fb4494783863 216
michaeljkoster 15:37a7a56a424f 217 int ESP8266::putc(char c)
samux 1:fb4494783863 218 {
michaeljkoster 20:d764237405c2 219 while (!wifi.writeable() || wifi.readable()); //wait for echoed command characters to be read first
michaeljkoster 15:37a7a56a424f 220 return wifi.putc(c);
samux 1:fb4494783863 221 }
samux 1:fb4494783863 222
michaeljkoster 15:37a7a56a424f 223 void ESP8266::flush()
samux 1:fb4494783863 224 {
michaeljkoster 15:37a7a56a424f 225 buf_ESP8266.flush();
samux 1:fb4494783863 226 }
samux 1:fb4494783863 227
michaeljkoster 16:3f0efaa57a12 228 int ESP8266::send(const char * buf, int len)
michaeljkoster 15:37a7a56a424f 229 {
michaeljkoster 18:60422852e99c 230
michaeljkoster 16:3f0efaa57a12 231 const char* bufptr=buf;
michaeljkoster 16:3f0efaa57a12 232 for(int i=0; i<len; i++){
michaeljkoster 17:d11fa4c3ac65 233 putc((int)*bufptr++);
michaeljkoster 18:60422852e99c 234 }
michaeljkoster 19:fb8d5bff2076 235 return len;
michaeljkoster 15:37a7a56a424f 236 }
samux 1:fb4494783863 237
michaeljkoster 16:3f0efaa57a12 238 bool ESP8266::sendCommand(const char * cmd, const char * ACK, char * res, int timeout)
samux 1:fb4494783863 239 {
samux 1:fb4494783863 240 char read;
samux 1:fb4494783863 241 size_t found = string::npos;
samux 1:fb4494783863 242 string checking;
samux 1:fb4494783863 243 Timer tmr;
samux 1:fb4494783863 244 int result = 0;
samux 1:fb4494783863 245
michaeljkoster 17:d11fa4c3ac65 246 //pc.printf("will send: %s\r\n",cmd);
samux 1:fb4494783863 247
michaeljkoster 17:d11fa4c3ac65 248 attach_rx(true);
samux 1:fb4494783863 249
samux 1:fb4494783863 250 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 251 while (readable())
michaeljkoster 17:d11fa4c3ac65 252 getc();
michaeljkoster 16:3f0efaa57a12 253
samux 1:fb4494783863 254 if (!ACK || !strcmp(ACK, "NO")) {
michaeljkoster 17:d11fa4c3ac65 255 for (int i = 0; i < strlen(cmd); i++){
michaeljkoster 16:3f0efaa57a12 256 result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
michaeljkoster 20:d764237405c2 257 wait(.005); // prevents stuck recieve ready (?) need to let echoed character get read first
michaeljkoster 16:3f0efaa57a12 258 }
michaeljkoster 16:3f0efaa57a12 259 putc(13); //CR
michaeljkoster 20:d764237405c2 260 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 261 putc(10); //LF
michaeljkoster 16:3f0efaa57a12 262
samux 1:fb4494783863 263 } else {
samux 1:fb4494783863 264 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 265 while (readable())
michaeljkoster 17:d11fa4c3ac65 266 getc();
samux 1:fb4494783863 267
samux 1:fb4494783863 268 tmr.start();
michaeljkoster 17:d11fa4c3ac65 269 for (int i = 0; i < strlen(cmd); i++){
michaeljkoster 16:3f0efaa57a12 270 result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
michaeljkoster 20:d764237405c2 271 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 272 }
michaeljkoster 16:3f0efaa57a12 273 putc(13); //CR
michaeljkoster 20:d764237405c2 274 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 275 putc(10); //LF
samux 1:fb4494783863 276
samux 1:fb4494783863 277 while (1) {
samux 1:fb4494783863 278 if (tmr.read_ms() > timeout) {
samux 1:fb4494783863 279 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 280 while (readable())
michaeljkoster 17:d11fa4c3ac65 281 getc();
samux 1:fb4494783863 282
samux 1:fb4494783863 283 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 284
samux 1:fb4494783863 285 attach_rx(true);
samux 1:fb4494783863 286 return -1;
michaeljkoster 17:d11fa4c3ac65 287 } else if (readable()) {
michaeljkoster 17:d11fa4c3ac65 288 read = getc();
samux 1:fb4494783863 289 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 290 checking += read;
samux 1:fb4494783863 291 found = checking.find(ACK);
samux 1:fb4494783863 292 if (found != string::npos) {
samux 1:fb4494783863 293 wait(0.01);
samux 1:fb4494783863 294
samux 1:fb4494783863 295 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 296 while (readable())
michaeljkoster 17:d11fa4c3ac65 297 getc();
samux 1:fb4494783863 298
samux 1:fb4494783863 299 break;
samux 1:fb4494783863 300 }
samux 1:fb4494783863 301 }
samux 1:fb4494783863 302 }
samux 1:fb4494783863 303 }
samux 1:fb4494783863 304 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 305
samux 1:fb4494783863 306 attach_rx(true);
samux 1:fb4494783863 307 return result;
samux 1:fb4494783863 308 }
samux 1:fb4494783863 309
samux 1:fb4494783863 310 //the user wants the result from the command (ACK == NULL, res != NULL)
samux 1:fb4494783863 311 if ( res != NULL) {
samux 1:fb4494783863 312 int i = 0;
samux 1:fb4494783863 313 Timer timeout;
samux 1:fb4494783863 314 timeout.start();
samux 1:fb4494783863 315 tmr.reset();
samux 1:fb4494783863 316 while (1) {
samux 1:fb4494783863 317 if (timeout.read() > 2) {
samux 1:fb4494783863 318 if (i == 0) {
samux 1:fb4494783863 319 res = NULL;
samux 1:fb4494783863 320 break;
samux 1:fb4494783863 321 }
samux 1:fb4494783863 322 res[i] = '\0';
samux 1:fb4494783863 323 DBG("user str 1: %s\r\n", res);
samux 1:fb4494783863 324
samux 1:fb4494783863 325 break;
samux 1:fb4494783863 326 } else {
samux 1:fb4494783863 327 if (tmr.read_ms() > 300) {
samux 1:fb4494783863 328 res[i] = '\0';
samux 1:fb4494783863 329 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 330
samux 1:fb4494783863 331 break;
samux 1:fb4494783863 332 }
michaeljkoster 17:d11fa4c3ac65 333 if (readable()) {
samux 1:fb4494783863 334 tmr.start();
michaeljkoster 17:d11fa4c3ac65 335 read = getc();
samux 1:fb4494783863 336
samux 1:fb4494783863 337 // we drop \r and \n
samux 1:fb4494783863 338 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 339 res[i++] = read;
samux 1:fb4494783863 340 }
samux 1:fb4494783863 341 }
samux 1:fb4494783863 342 }
samux 1:fb4494783863 343 }
samux 1:fb4494783863 344 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 345 }
samux 1:fb4494783863 346
samux 1:fb4494783863 347 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 348 while (readable())
michaeljkoster 17:d11fa4c3ac65 349 getc();
samux 1:fb4494783863 350
samux 1:fb4494783863 351 attach_rx(true);
samux 1:fb4494783863 352 DBG("result: %d\r\n", result)
samux 1:fb4494783863 353 return result;
michaeljkoster 15:37a7a56a424f 354 }