Added monitoring feature of ESP8266's UART

Dependents:   ESP8266_W7500_Example DualNetworkInterface-Basic

Fork of ESP8266Interface by ESP8266

Committer:
mbedAustin
Date:
Tue Apr 28 20:17:51 2015 +0000
Revision:
29:939372104145
Parent:
28:91e65e22e63a
Child:
30:c035696b9397
Added IP Function, made all Serials RAWSerials;

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
mbedAustin 28:91e65e22e63a 26 #ifdef DEBUG
mbedAustin 28:91e65e22e63a 27 #define DBG(x, ...) printf("[ESP8266 : DBG]"x"\r\n", ##__VA_ARGS__);
mbedAustin 28:91e65e22e63a 28 #define WARN(x, ...) printf("[ESP8266 : WARN]"x"\r\n", ##__VA_ARGS__);
mbedAustin 28:91e65e22e63a 29 #define ERR(x, ...) 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
mbedAustin 28:91e65e22e63a 36 #ifdef 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;
mbedAustin 29:939372104145 47 char* ip = NULL;
samux 1:fb4494783863 48
mbedAustin 28:91e65e22e63a 49 ESP8266::ESP8266( PinName tx, PinName rx, PinName _reset, const char * ssid, const char * phrase, uint32_t baud):
michaeljkoster 13:41098c907200 50 wifi(tx, rx), reset_pin(_reset), buf_ESP8266(256)
samux 1:fb4494783863 51 {
samux 1:fb4494783863 52 memset(&state, 0, sizeof(state));
samux 1:fb4494783863 53
michaeljkoster 26:0d5bcb3903e2 54 // change all ' ' in '$' in the ssid and the passphrase
samux 1:fb4494783863 55 strcpy(this->ssid, ssid);
samux 1:fb4494783863 56 for (int i = 0; i < strlen(ssid); i++) {
samux 1:fb4494783863 57 if (this->ssid[i] == ' ')
samux 1:fb4494783863 58 this->ssid[i] = '$';
samux 1:fb4494783863 59 }
samux 1:fb4494783863 60 strcpy(this->phrase, phrase);
samux 1:fb4494783863 61 for (int i = 0; i < strlen(phrase); i++) {
samux 1:fb4494783863 62 if (this->phrase[i] == ' ')
samux 1:fb4494783863 63 this->phrase[i] = '$';
samux 1:fb4494783863 64 }
michaeljkoster 26:0d5bcb3903e2 65
samux 1:fb4494783863 66
samux 1:fb4494783863 67 inst = this;
samux 1:fb4494783863 68 attach_rx(false);
mbedAustin 28:91e65e22e63a 69
mbedAustin 28:91e65e22e63a 70 wifi.baud(baud); // initial baud rate of the ESP8266
mbedAustin 28:91e65e22e63a 71
michaeljkoster 22:c4360e61486a 72 state.associated = false;
michaeljkoster 22:c4360e61486a 73 state.cmdMode = false;
samux 1:fb4494783863 74 }
samux 1:fb4494783863 75
michaeljkoster 13:41098c907200 76 bool ESP8266::join()
samux 1:fb4494783863 77 {
michaeljkoster 23:de9221771e96 78 sendCommand( "AT+CWMODE=1", "change", NULL, 1000);
michaeljkoster 16:3f0efaa57a12 79 string cmd="AT+CWJAP=\""+(string)this->ssid+"\",\""+(string)this->phrase+"\"";
mbedAustin 28:91e65e22e63a 80 if( sendCommand( cmd.c_str(), "OK", NULL, 10000) ) {
michaeljkoster 16:3f0efaa57a12 81 // successfully joined the network
samux 1:fb4494783863 82 state.associated = true;
michaeljkoster 13:41098c907200 83 INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase);
samux 1:fb4494783863 84 return true;
samux 1:fb4494783863 85 }
samux 1:fb4494783863 86 return false;
samux 1:fb4494783863 87 }
samux 1:fb4494783863 88
michaeljkoster 13:41098c907200 89 bool ESP8266::connect()
samux 1:fb4494783863 90 {
mbedAustin 28:91e65e22e63a 91 sendCommand("AT+CWDHCP=1,1","OK",NULL,1000); // DHCP Enabled in Station Mode
mbedAustin 28:91e65e22e63a 92 return ESP8266::join();
samux 1:fb4494783863 93 }
samux 1:fb4494783863 94
michaeljkoster 15:37a7a56a424f 95 bool ESP8266::is_connected()
michaeljkoster 15:37a7a56a424f 96 {
michaeljkoster 15:37a7a56a424f 97 return true;
michaeljkoster 15:37a7a56a424f 98 }
michaeljkoster 15:37a7a56a424f 99
mbedAustin 28:91e65e22e63a 100 bool ESP8266::startUDP(char* ip, int port)
mbedAustin 28:91e65e22e63a 101 {
michaeljkoster 17:d11fa4c3ac65 102 char portstr[5];
michaeljkoster 16:3f0efaa57a12 103 sprintf(portstr, "%d", port);
michaeljkoster 18:60422852e99c 104 sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000);
mbedAustin 28:91e65e22e63a 105
mbedAustin 28:91e65e22e63a 106 sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode
mbedAustin 28:91e65e22e63a 107 sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode
mbedAustin 28:91e65e22e63a 108 printf("Data Mode\r\n");
michaeljkoster 22:c4360e61486a 109 state.cmdMode = false;
mbedAustin 28:91e65e22e63a 110
michaeljkoster 16:3f0efaa57a12 111 return true;
michaeljkoster 16:3f0efaa57a12 112 }
michaeljkoster 16:3f0efaa57a12 113
michaeljkoster 15:37a7a56a424f 114 bool ESP8266::close()
michaeljkoster 15:37a7a56a424f 115 {
michaeljkoster 17:d11fa4c3ac65 116 send("+++",3);
michaeljkoster 17:d11fa4c3ac65 117 wait(1);
michaeljkoster 22:c4360e61486a 118 state.cmdMode = true;
michaeljkoster 16:3f0efaa57a12 119 sendCommand("AT+CIPCLOSE","OK", NULL, 10000);
michaeljkoster 15:37a7a56a424f 120 return true;
michaeljkoster 15:37a7a56a424f 121 }
michaeljkoster 15:37a7a56a424f 122
michaeljkoster 15:37a7a56a424f 123 bool ESP8266::disconnect()
michaeljkoster 15:37a7a56a424f 124 {
michaeljkoster 15:37a7a56a424f 125 // if already disconnected, return
michaeljkoster 15:37a7a56a424f 126 if (!state.associated)
michaeljkoster 15:37a7a56a424f 127 return true;
michaeljkoster 15:37a7a56a424f 128 // send command to quit AP
mbedAustin 28:91e65e22e63a 129 sendCommand("AT+CWQAP", "OK", NULL, 10000);
michaeljkoster 15:37a7a56a424f 130 state.associated = false;
michaeljkoster 15:37a7a56a424f 131 return true;
michaeljkoster 15:37a7a56a424f 132 }
michaeljkoster 15:37a7a56a424f 133
mbedAustin 28:91e65e22e63a 134 /*
mbedAustin 28:91e65e22e63a 135 Assuming Returned data looks like this:
mbedAustin 28:91e65e22e63a 136 +CIFSR:STAIP,"192.168.11.2"
mbedAustin 28:91e65e22e63a 137 +CIFSR:STAMAC,"18:fe:34:9f:3a:f5"
mbedAustin 28:91e65e22e63a 138 grabbing IP from first set of quotation marks
mbedAustin 28:91e65e22e63a 139 */
michaeljkoster 15:37a7a56a424f 140 char* ESP8266::getIPAddress()
michaeljkoster 15:37a7a56a424f 141 {
mbedAustin 28:91e65e22e63a 142 char result[30] = {0};
mbedAustin 28:91e65e22e63a 143 int check = 0;
mbedAustin 28:91e65e22e63a 144 check = sendCommand("AT+CIFSR", NULL, result, 1000);
mbedAustin 29:939372104145 145 //pc.printf("\r\nReceivedInfo for IP Command is: %s\r\n",result);
mbedAustin 29:939372104145 146 ip = ipString;
mbedAustin 29:939372104145 147 if(check) {
mbedAustin 28:91e65e22e63a 148 // Success
mbedAustin 28:91e65e22e63a 149 string resultString(result);
mbedAustin 28:91e65e22e63a 150 uint8_t pos1 = 0, pos2 = 0;
mbedAustin 28:91e65e22e63a 151 //uint8_t pos3 = 0, pos4 = 0;
mbedAustin 28:91e65e22e63a 152 pos1 = resultString.find("+CIFSR:STAIP");
mbedAustin 28:91e65e22e63a 153 pos1 = resultString.find('"',pos1);
mbedAustin 28:91e65e22e63a 154 pos2 = resultString.find('"',pos1+1);
mbedAustin 28:91e65e22e63a 155 //pos3 = resultString.find('"',pos2+1); //would find mac address
mbedAustin 28:91e65e22e63a 156 //pos4 = resultString.find('"',pos3+1);
mbedAustin 28:91e65e22e63a 157 strncpy(ipString,resultString.substr(pos1,pos2).c_str(),sizeof(ipString));
mbedAustin 28:91e65e22e63a 158 ipString[pos2 - pos1 +1] = 0; // null terminate string correctly.
mbedAustin 28:91e65e22e63a 159 DBG("IP: %s\r\n",ipString);
mbedAustin 29:939372104145 160 ip = ipString;
mbedAustin 29:939372104145 161
mbedAustin 29:939372104145 162 } else {
mbedAustin 28:91e65e22e63a 163 // Failure
mbedAustin 28:91e65e22e63a 164 DBG("getIPAddress() failed\r\n");
mbedAustin 29:939372104145 165 ip = NULL;
mbedAustin 28:91e65e22e63a 166 }
mbedAustin 29:939372104145 167 return ip;
michaeljkoster 15:37a7a56a424f 168 }
michaeljkoster 15:37a7a56a424f 169
michaeljkoster 13:41098c907200 170 bool ESP8266::gethostbyname(const char * host, char * ip)
samux 1:fb4494783863 171 {
samux 1:fb4494783863 172 string h = host;
samux 1:fb4494783863 173 int nb_digits = 0;
samux 1:fb4494783863 174
samux 1:fb4494783863 175 // no dns needed
samux 1:fb4494783863 176 int pos = h.find(".");
samux 1:fb4494783863 177 if (pos != string::npos) {
samux 1:fb4494783863 178 string sub = h.substr(0, h.find("."));
samux 1:fb4494783863 179 nb_digits = atoi(sub.c_str());
samux 1:fb4494783863 180 }
samux 1:fb4494783863 181 //printf("substrL %s\r\n", sub.c_str());
samux 1:fb4494783863 182 if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) {
samux 1:fb4494783863 183 strcpy(ip, host);
michaeljkoster 13:41098c907200 184 return true;
mbedAustin 28:91e65e22e63a 185 } else {
michaeljkoster 13:41098c907200 186 // dns needed, not currently available
michaeljkoster 13:41098c907200 187 return false;
michaeljkoster 13:41098c907200 188 }
michaeljkoster 13:41098c907200 189 }
samux 1:fb4494783863 190
michaeljkoster 13:41098c907200 191 void ESP8266::reset()
samux 1:fb4494783863 192 {
samux 1:fb4494783863 193 reset_pin = 0;
samux 1:fb4494783863 194 wait(0.2);
samux 1:fb4494783863 195 reset_pin = 1;
michaeljkoster 24:03585a13ff3b 196 wait(1);
mbedAustin 28:91e65e22e63a 197
michaeljkoster 24:03585a13ff3b 198 //send("+++",3);
michaeljkoster 24:03585a13ff3b 199 //wait(1);
michaeljkoster 24:03585a13ff3b 200 state.cmdMode = true;
michaeljkoster 24:03585a13ff3b 201 sendCommand("AT", "OK", NULL, 1000);
mbedAustin 28:91e65e22e63a 202 sendCommand("AT+RST", "ready", NULL, 10000);
michaeljkoster 24:03585a13ff3b 203 state.associated = false;
michaeljkoster 24:03585a13ff3b 204
samux 1:fb4494783863 205 }
samux 1:fb4494783863 206
michaeljkoster 13:41098c907200 207 bool ESP8266::reboot()
samux 3:9aa05e19c62e 208 {
michaeljkoster 16:3f0efaa57a12 209 reset();
samux 3:9aa05e19c62e 210 return true;
samux 3:9aa05e19c62e 211 }
samux 3:9aa05e19c62e 212
michaeljkoster 15:37a7a56a424f 213 void ESP8266::handler_rx(void)
samux 1:fb4494783863 214 {
michaeljkoster 15:37a7a56a424f 215 //read characters
michaeljkoster 16:3f0efaa57a12 216 char c;
mbedAustin 28:91e65e22e63a 217 while (wifi.readable()) {
michaeljkoster 16:3f0efaa57a12 218 c=wifi.getc();
michaeljkoster 16:3f0efaa57a12 219 buf_ESP8266.queue(c);
michaeljkoster 26:0d5bcb3903e2 220 //if (state.cmdMode) pc.printf("%c",c); //debug echo, needs fast serial console to prevent UART overruns
michaeljkoster 16:3f0efaa57a12 221 }
michaeljkoster 15:37a7a56a424f 222 }
michaeljkoster 15:37a7a56a424f 223
michaeljkoster 15:37a7a56a424f 224 void ESP8266::attach_rx(bool callback)
michaeljkoster 15:37a7a56a424f 225 {
michaeljkoster 15:37a7a56a424f 226 if (!callback)
michaeljkoster 15:37a7a56a424f 227 wifi.attach(NULL);
michaeljkoster 15:37a7a56a424f 228 else
michaeljkoster 15:37a7a56a424f 229 wifi.attach(this, &ESP8266::handler_rx);
samux 1:fb4494783863 230 }
samux 1:fb4494783863 231
michaeljkoster 13:41098c907200 232 int ESP8266::readable()
samux 1:fb4494783863 233 {
michaeljkoster 13:41098c907200 234 return buf_ESP8266.available();
samux 1:fb4494783863 235 }
samux 1:fb4494783863 236
michaeljkoster 13:41098c907200 237 int ESP8266::writeable()
samux 1:fb4494783863 238 {
samux 1:fb4494783863 239 return wifi.writeable();
samux 1:fb4494783863 240 }
samux 1:fb4494783863 241
michaeljkoster 13:41098c907200 242 char ESP8266::getc()
samux 1:fb4494783863 243 {
michaeljkoster 14:4d1128f72e00 244 char c=0;
michaeljkoster 13:41098c907200 245 while (!buf_ESP8266.available());
michaeljkoster 13:41098c907200 246 buf_ESP8266.dequeue(&c);
samux 1:fb4494783863 247 return c;
samux 1:fb4494783863 248 }
samux 1:fb4494783863 249
michaeljkoster 15:37a7a56a424f 250 int ESP8266::putc(char c)
samux 1:fb4494783863 251 {
michaeljkoster 20:d764237405c2 252 while (!wifi.writeable() || wifi.readable()); //wait for echoed command characters to be read first
michaeljkoster 15:37a7a56a424f 253 return wifi.putc(c);
samux 1:fb4494783863 254 }
samux 1:fb4494783863 255
michaeljkoster 15:37a7a56a424f 256 void ESP8266::flush()
samux 1:fb4494783863 257 {
michaeljkoster 15:37a7a56a424f 258 buf_ESP8266.flush();
samux 1:fb4494783863 259 }
samux 1:fb4494783863 260
michaeljkoster 16:3f0efaa57a12 261 int ESP8266::send(const char * buf, int len)
michaeljkoster 15:37a7a56a424f 262 {
michaeljkoster 18:60422852e99c 263
michaeljkoster 16:3f0efaa57a12 264 const char* bufptr=buf;
mbedAustin 28:91e65e22e63a 265 for(int i=0; i<len; i++) {
michaeljkoster 17:d11fa4c3ac65 266 putc((int)*bufptr++);
michaeljkoster 18:60422852e99c 267 }
michaeljkoster 19:fb8d5bff2076 268 return len;
michaeljkoster 15:37a7a56a424f 269 }
samux 1:fb4494783863 270
michaeljkoster 16:3f0efaa57a12 271 bool ESP8266::sendCommand(const char * cmd, const char * ACK, char * res, int timeout)
samux 1:fb4494783863 272 {
samux 1:fb4494783863 273 char read;
samux 1:fb4494783863 274 size_t found = string::npos;
samux 1:fb4494783863 275 string checking;
samux 1:fb4494783863 276 Timer tmr;
samux 1:fb4494783863 277 int result = 0;
samux 1:fb4494783863 278
mbedAustin 28:91e65e22e63a 279 DBG("will send: %s\r\n",cmd);
samux 1:fb4494783863 280
michaeljkoster 17:d11fa4c3ac65 281 attach_rx(true);
samux 1:fb4494783863 282
samux 1:fb4494783863 283 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 284 while (readable())
michaeljkoster 17:d11fa4c3ac65 285 getc();
mbedAustin 28:91e65e22e63a 286
samux 1:fb4494783863 287 if (!ACK || !strcmp(ACK, "NO")) {
mbedAustin 28:91e65e22e63a 288 for (int i = 0; i < strlen(cmd); i++) {
michaeljkoster 16:3f0efaa57a12 289 result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
michaeljkoster 20:d764237405c2 290 wait(.005); // prevents stuck recieve ready (?) need to let echoed character get read first
michaeljkoster 16:3f0efaa57a12 291 }
michaeljkoster 16:3f0efaa57a12 292 putc(13); //CR
michaeljkoster 20:d764237405c2 293 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 294 putc(10); //LF
michaeljkoster 16:3f0efaa57a12 295
samux 1:fb4494783863 296 } else {
samux 1:fb4494783863 297 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 298 while (readable())
michaeljkoster 17:d11fa4c3ac65 299 getc();
samux 1:fb4494783863 300
samux 1:fb4494783863 301 tmr.start();
mbedAustin 28:91e65e22e63a 302 for (int i = 0; i < strlen(cmd); i++) {
michaeljkoster 16:3f0efaa57a12 303 result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
michaeljkoster 20:d764237405c2 304 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 305 }
michaeljkoster 16:3f0efaa57a12 306 putc(13); //CR
michaeljkoster 20:d764237405c2 307 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 308 putc(10); //LF
samux 1:fb4494783863 309
samux 1:fb4494783863 310 while (1) {
samux 1:fb4494783863 311 if (tmr.read_ms() > timeout) {
samux 1:fb4494783863 312 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 313 while (readable())
michaeljkoster 17:d11fa4c3ac65 314 getc();
samux 1:fb4494783863 315
samux 1:fb4494783863 316 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 317
samux 1:fb4494783863 318 attach_rx(true);
samux 1:fb4494783863 319 return -1;
michaeljkoster 17:d11fa4c3ac65 320 } else if (readable()) {
michaeljkoster 17:d11fa4c3ac65 321 read = getc();
mbedAustin 28:91e65e22e63a 322 printf("%c",read); //debug echo
samux 1:fb4494783863 323 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 324 checking += read;
samux 1:fb4494783863 325 found = checking.find(ACK);
samux 1:fb4494783863 326 if (found != string::npos) {
samux 1:fb4494783863 327 wait(0.01);
samux 1:fb4494783863 328
samux 1:fb4494783863 329 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 330 while (readable())
michaeljkoster 26:0d5bcb3903e2 331 read = getc();
mbedAustin 28:91e65e22e63a 332 printf("%c",read); //debug echo
samux 1:fb4494783863 333 break;
samux 1:fb4494783863 334 }
samux 1:fb4494783863 335 }
samux 1:fb4494783863 336 }
samux 1:fb4494783863 337 }
samux 1:fb4494783863 338 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 339
samux 1:fb4494783863 340 attach_rx(true);
samux 1:fb4494783863 341 return result;
samux 1:fb4494783863 342 }
samux 1:fb4494783863 343
samux 1:fb4494783863 344 //the user wants the result from the command (ACK == NULL, res != NULL)
samux 1:fb4494783863 345 if ( res != NULL) {
samux 1:fb4494783863 346 int i = 0;
samux 1:fb4494783863 347 Timer timeout;
samux 1:fb4494783863 348 timeout.start();
samux 1:fb4494783863 349 tmr.reset();
samux 1:fb4494783863 350 while (1) {
samux 1:fb4494783863 351 if (timeout.read() > 2) {
samux 1:fb4494783863 352 if (i == 0) {
samux 1:fb4494783863 353 res = NULL;
samux 1:fb4494783863 354 break;
samux 1:fb4494783863 355 }
samux 1:fb4494783863 356 res[i] = '\0';
samux 1:fb4494783863 357 DBG("user str 1: %s\r\n", res);
samux 1:fb4494783863 358
samux 1:fb4494783863 359 break;
samux 1:fb4494783863 360 } else {
samux 1:fb4494783863 361 if (tmr.read_ms() > 300) {
samux 1:fb4494783863 362 res[i] = '\0';
samux 1:fb4494783863 363 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 364
samux 1:fb4494783863 365 break;
samux 1:fb4494783863 366 }
michaeljkoster 17:d11fa4c3ac65 367 if (readable()) {
samux 1:fb4494783863 368 tmr.start();
michaeljkoster 17:d11fa4c3ac65 369 read = getc();
samux 1:fb4494783863 370
samux 1:fb4494783863 371 // we drop \r and \n
samux 1:fb4494783863 372 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 373 res[i++] = read;
samux 1:fb4494783863 374 }
samux 1:fb4494783863 375 }
samux 1:fb4494783863 376 }
samux 1:fb4494783863 377 }
samux 1:fb4494783863 378 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 379 }
samux 1:fb4494783863 380
samux 1:fb4494783863 381 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 382 while (readable())
michaeljkoster 17:d11fa4c3ac65 383 getc();
samux 1:fb4494783863 384
samux 1:fb4494783863 385 attach_rx(true);
samux 1:fb4494783863 386 DBG("result: %d\r\n", result)
samux 1:fb4494783863 387 return result;
michaeljkoster 15:37a7a56a424f 388 }