Update ESP8266.h to increase size of ssid & phrase to Wifi standards of 32 & 64
Fork of ESP8266Interface by
ESP8266/ESP8266.cpp@24:03585a13ff3b, 2014-12-11 (annotated)
- Committer:
- michaeljkoster
- Date:
- Thu Dec 11 23:24:55 2014 +0000
- Revision:
- 24:03585a13ff3b
- Parent:
- 23:de9221771e96
- Child:
- 25:8e2b04473830
Added hardware reset
Who changed what in which revision?
User | Revision | Line number | New 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 | { |
samux | 1:fb4494783863 | 160 | reset_pin = 0; |
samux | 1:fb4494783863 | 161 | wait(0.2); |
samux | 1:fb4494783863 | 162 | reset_pin = 1; |
michaeljkoster | 24:03585a13ff3b | 163 | wait(1); |
michaeljkoster | 24:03585a13ff3b | 164 | |
michaeljkoster | 24:03585a13ff3b | 165 | //send("+++",3); |
michaeljkoster | 24:03585a13ff3b | 166 | //wait(1); |
michaeljkoster | 24:03585a13ff3b | 167 | state.cmdMode = true; |
michaeljkoster | 24:03585a13ff3b | 168 | sendCommand("AT", "OK", NULL, 1000); |
michaeljkoster | 24:03585a13ff3b | 169 | //sendCommand("AT+RST", "ready", NULL, 10000); |
michaeljkoster | 24:03585a13ff3b | 170 | state.associated = false; |
michaeljkoster | 24:03585a13ff3b | 171 | |
michaeljkoster | 24:03585a13ff3b | 172 | //pc.printf("setting baud rate\r\n"); |
michaeljkoster | 24:03585a13ff3b | 173 | //sendCommand("AT+CIOBAUD=9600", "OK", NULL, 2000); |
michaeljkoster | 24:03585a13ff3b | 174 | //wifi.baud(9600); |
michaeljkoster | 24:03585a13ff3b | 175 | //sendCommand("AT", "OK", NULL, 1000); |
samux | 1:fb4494783863 | 176 | } |
samux | 1:fb4494783863 | 177 | |
michaeljkoster | 13:41098c907200 | 178 | bool ESP8266::reboot() |
samux | 3:9aa05e19c62e | 179 | { |
michaeljkoster | 16:3f0efaa57a12 | 180 | reset(); |
samux | 3:9aa05e19c62e | 181 | return true; |
samux | 3:9aa05e19c62e | 182 | } |
samux | 3:9aa05e19c62e | 183 | |
michaeljkoster | 15:37a7a56a424f | 184 | void ESP8266::handler_rx(void) |
samux | 1:fb4494783863 | 185 | { |
michaeljkoster | 15:37a7a56a424f | 186 | //read characters |
michaeljkoster | 16:3f0efaa57a12 | 187 | char c; |
michaeljkoster | 16:3f0efaa57a12 | 188 | while (wifi.readable()){ |
michaeljkoster | 16:3f0efaa57a12 | 189 | c=wifi.getc(); |
michaeljkoster | 16:3f0efaa57a12 | 190 | buf_ESP8266.queue(c); |
michaeljkoster | 22:c4360e61486a | 191 | if (state.cmdMode) pc.printf("%c",c); //debug echo |
michaeljkoster | 24:03585a13ff3b | 192 | //pc.printf("%c",c); //debug echo |
michaeljkoster | 16:3f0efaa57a12 | 193 | } |
michaeljkoster | 15:37a7a56a424f | 194 | } |
michaeljkoster | 15:37a7a56a424f | 195 | |
michaeljkoster | 15:37a7a56a424f | 196 | void ESP8266::attach_rx(bool callback) |
michaeljkoster | 15:37a7a56a424f | 197 | { |
michaeljkoster | 15:37a7a56a424f | 198 | if (!callback) |
michaeljkoster | 15:37a7a56a424f | 199 | wifi.attach(NULL); |
michaeljkoster | 15:37a7a56a424f | 200 | else |
michaeljkoster | 15:37a7a56a424f | 201 | wifi.attach(this, &ESP8266::handler_rx); |
samux | 1:fb4494783863 | 202 | } |
samux | 1:fb4494783863 | 203 | |
michaeljkoster | 13:41098c907200 | 204 | int ESP8266::readable() |
samux | 1:fb4494783863 | 205 | { |
michaeljkoster | 13:41098c907200 | 206 | return buf_ESP8266.available(); |
samux | 1:fb4494783863 | 207 | } |
samux | 1:fb4494783863 | 208 | |
michaeljkoster | 13:41098c907200 | 209 | int ESP8266::writeable() |
samux | 1:fb4494783863 | 210 | { |
samux | 1:fb4494783863 | 211 | return wifi.writeable(); |
samux | 1:fb4494783863 | 212 | } |
samux | 1:fb4494783863 | 213 | |
michaeljkoster | 13:41098c907200 | 214 | char ESP8266::getc() |
samux | 1:fb4494783863 | 215 | { |
michaeljkoster | 14:4d1128f72e00 | 216 | char c=0; |
michaeljkoster | 13:41098c907200 | 217 | while (!buf_ESP8266.available()); |
michaeljkoster | 13:41098c907200 | 218 | buf_ESP8266.dequeue(&c); |
samux | 1:fb4494783863 | 219 | return c; |
samux | 1:fb4494783863 | 220 | } |
samux | 1:fb4494783863 | 221 | |
michaeljkoster | 15:37a7a56a424f | 222 | int ESP8266::putc(char c) |
samux | 1:fb4494783863 | 223 | { |
michaeljkoster | 20:d764237405c2 | 224 | while (!wifi.writeable() || wifi.readable()); //wait for echoed command characters to be read first |
michaeljkoster | 15:37a7a56a424f | 225 | return wifi.putc(c); |
samux | 1:fb4494783863 | 226 | } |
samux | 1:fb4494783863 | 227 | |
michaeljkoster | 15:37a7a56a424f | 228 | void ESP8266::flush() |
samux | 1:fb4494783863 | 229 | { |
michaeljkoster | 15:37a7a56a424f | 230 | buf_ESP8266.flush(); |
samux | 1:fb4494783863 | 231 | } |
samux | 1:fb4494783863 | 232 | |
michaeljkoster | 16:3f0efaa57a12 | 233 | int ESP8266::send(const char * buf, int len) |
michaeljkoster | 15:37a7a56a424f | 234 | { |
michaeljkoster | 18:60422852e99c | 235 | |
michaeljkoster | 16:3f0efaa57a12 | 236 | const char* bufptr=buf; |
michaeljkoster | 16:3f0efaa57a12 | 237 | for(int i=0; i<len; i++){ |
michaeljkoster | 17:d11fa4c3ac65 | 238 | putc((int)*bufptr++); |
michaeljkoster | 18:60422852e99c | 239 | } |
michaeljkoster | 19:fb8d5bff2076 | 240 | return len; |
michaeljkoster | 15:37a7a56a424f | 241 | } |
samux | 1:fb4494783863 | 242 | |
michaeljkoster | 16:3f0efaa57a12 | 243 | bool ESP8266::sendCommand(const char * cmd, const char * ACK, char * res, int timeout) |
samux | 1:fb4494783863 | 244 | { |
samux | 1:fb4494783863 | 245 | char read; |
samux | 1:fb4494783863 | 246 | size_t found = string::npos; |
samux | 1:fb4494783863 | 247 | string checking; |
samux | 1:fb4494783863 | 248 | Timer tmr; |
samux | 1:fb4494783863 | 249 | int result = 0; |
samux | 1:fb4494783863 | 250 | |
michaeljkoster | 17:d11fa4c3ac65 | 251 | //pc.printf("will send: %s\r\n",cmd); |
samux | 1:fb4494783863 | 252 | |
michaeljkoster | 17:d11fa4c3ac65 | 253 | attach_rx(true); |
samux | 1:fb4494783863 | 254 | |
samux | 1:fb4494783863 | 255 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 256 | while (readable()) |
michaeljkoster | 17:d11fa4c3ac65 | 257 | getc(); |
michaeljkoster | 16:3f0efaa57a12 | 258 | |
samux | 1:fb4494783863 | 259 | if (!ACK || !strcmp(ACK, "NO")) { |
michaeljkoster | 17:d11fa4c3ac65 | 260 | for (int i = 0; i < strlen(cmd); i++){ |
michaeljkoster | 16:3f0efaa57a12 | 261 | result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result; |
michaeljkoster | 20:d764237405c2 | 262 | wait(.005); // prevents stuck recieve ready (?) need to let echoed character get read first |
michaeljkoster | 16:3f0efaa57a12 | 263 | } |
michaeljkoster | 16:3f0efaa57a12 | 264 | putc(13); //CR |
michaeljkoster | 20:d764237405c2 | 265 | wait(.005); // wait for echo |
michaeljkoster | 16:3f0efaa57a12 | 266 | putc(10); //LF |
michaeljkoster | 16:3f0efaa57a12 | 267 | |
samux | 1:fb4494783863 | 268 | } else { |
samux | 1:fb4494783863 | 269 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 270 | while (readable()) |
michaeljkoster | 17:d11fa4c3ac65 | 271 | getc(); |
samux | 1:fb4494783863 | 272 | |
samux | 1:fb4494783863 | 273 | tmr.start(); |
michaeljkoster | 17:d11fa4c3ac65 | 274 | for (int i = 0; i < strlen(cmd); i++){ |
michaeljkoster | 16:3f0efaa57a12 | 275 | result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result; |
michaeljkoster | 20:d764237405c2 | 276 | wait(.005); // wait for echo |
michaeljkoster | 16:3f0efaa57a12 | 277 | } |
michaeljkoster | 16:3f0efaa57a12 | 278 | putc(13); //CR |
michaeljkoster | 20:d764237405c2 | 279 | wait(.005); // wait for echo |
michaeljkoster | 16:3f0efaa57a12 | 280 | putc(10); //LF |
samux | 1:fb4494783863 | 281 | |
samux | 1:fb4494783863 | 282 | while (1) { |
samux | 1:fb4494783863 | 283 | if (tmr.read_ms() > timeout) { |
samux | 1:fb4494783863 | 284 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 285 | while (readable()) |
michaeljkoster | 17:d11fa4c3ac65 | 286 | getc(); |
samux | 1:fb4494783863 | 287 | |
samux | 1:fb4494783863 | 288 | DBG("check: %s\r\n", checking.c_str()); |
samux | 1:fb4494783863 | 289 | |
samux | 1:fb4494783863 | 290 | attach_rx(true); |
samux | 1:fb4494783863 | 291 | return -1; |
michaeljkoster | 17:d11fa4c3ac65 | 292 | } else if (readable()) { |
michaeljkoster | 17:d11fa4c3ac65 | 293 | read = getc(); |
samux | 1:fb4494783863 | 294 | if ( read != '\r' && read != '\n') { |
samux | 1:fb4494783863 | 295 | checking += read; |
samux | 1:fb4494783863 | 296 | found = checking.find(ACK); |
samux | 1:fb4494783863 | 297 | if (found != string::npos) { |
samux | 1:fb4494783863 | 298 | wait(0.01); |
samux | 1:fb4494783863 | 299 | |
samux | 1:fb4494783863 | 300 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 301 | while (readable()) |
michaeljkoster | 17:d11fa4c3ac65 | 302 | getc(); |
samux | 1:fb4494783863 | 303 | |
samux | 1:fb4494783863 | 304 | break; |
samux | 1:fb4494783863 | 305 | } |
samux | 1:fb4494783863 | 306 | } |
samux | 1:fb4494783863 | 307 | } |
samux | 1:fb4494783863 | 308 | } |
samux | 1:fb4494783863 | 309 | DBG("check: %s\r\n", checking.c_str()); |
samux | 1:fb4494783863 | 310 | |
samux | 1:fb4494783863 | 311 | attach_rx(true); |
samux | 1:fb4494783863 | 312 | return result; |
samux | 1:fb4494783863 | 313 | } |
samux | 1:fb4494783863 | 314 | |
samux | 1:fb4494783863 | 315 | //the user wants the result from the command (ACK == NULL, res != NULL) |
samux | 1:fb4494783863 | 316 | if ( res != NULL) { |
samux | 1:fb4494783863 | 317 | int i = 0; |
samux | 1:fb4494783863 | 318 | Timer timeout; |
samux | 1:fb4494783863 | 319 | timeout.start(); |
samux | 1:fb4494783863 | 320 | tmr.reset(); |
samux | 1:fb4494783863 | 321 | while (1) { |
samux | 1:fb4494783863 | 322 | if (timeout.read() > 2) { |
samux | 1:fb4494783863 | 323 | if (i == 0) { |
samux | 1:fb4494783863 | 324 | res = NULL; |
samux | 1:fb4494783863 | 325 | break; |
samux | 1:fb4494783863 | 326 | } |
samux | 1:fb4494783863 | 327 | res[i] = '\0'; |
samux | 1:fb4494783863 | 328 | DBG("user str 1: %s\r\n", res); |
samux | 1:fb4494783863 | 329 | |
samux | 1:fb4494783863 | 330 | break; |
samux | 1:fb4494783863 | 331 | } else { |
samux | 1:fb4494783863 | 332 | if (tmr.read_ms() > 300) { |
samux | 1:fb4494783863 | 333 | res[i] = '\0'; |
samux | 1:fb4494783863 | 334 | DBG("user str: %s\r\n", res); |
samux | 1:fb4494783863 | 335 | |
samux | 1:fb4494783863 | 336 | break; |
samux | 1:fb4494783863 | 337 | } |
michaeljkoster | 17:d11fa4c3ac65 | 338 | if (readable()) { |
samux | 1:fb4494783863 | 339 | tmr.start(); |
michaeljkoster | 17:d11fa4c3ac65 | 340 | read = getc(); |
samux | 1:fb4494783863 | 341 | |
samux | 1:fb4494783863 | 342 | // we drop \r and \n |
samux | 1:fb4494783863 | 343 | if ( read != '\r' && read != '\n') { |
samux | 1:fb4494783863 | 344 | res[i++] = read; |
samux | 1:fb4494783863 | 345 | } |
samux | 1:fb4494783863 | 346 | } |
samux | 1:fb4494783863 | 347 | } |
samux | 1:fb4494783863 | 348 | } |
samux | 1:fb4494783863 | 349 | DBG("user str: %s\r\n", res); |
samux | 1:fb4494783863 | 350 | } |
samux | 1:fb4494783863 | 351 | |
samux | 1:fb4494783863 | 352 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 353 | while (readable()) |
michaeljkoster | 17:d11fa4c3ac65 | 354 | getc(); |
samux | 1:fb4494783863 | 355 | |
samux | 1:fb4494783863 | 356 | attach_rx(true); |
samux | 1:fb4494783863 | 357 | DBG("result: %d\r\n", result) |
samux | 1:fb4494783863 | 358 | return result; |
michaeljkoster | 15:37a7a56a424f | 359 | } |