ESP8266 Socket Library. AT Thinker firmware.
Dependents: ESP8266_MQTT_HelloWorld ESP8266_IFTTT_Test ECE_4180_Lab_4 websocketmbed ... more
Fork of ESP8266Interface by
This repository has been superceded
This project has moved to https://developer.mbed.org/teams/ESP8266/code/esp8266-driver/
This library works with the AT Thinker firmware.
Note
This library is currently in Beta. It is not feature complete and has some bugs, proceed with caution! Fixes and patches are welcome and appreciated!
Currently the ESP8266Interface Library has the following Abilities:
Working
- TCP Client
- UDP Client
- Transparent mode (single connection of 1 type at a time)
- Station Mode (connects to AP)
To be implemented
- TCP Server
- UDP Server
- Multi Connection Mode (able to have up to 5 sockets at a time)
- AP Mode (Make ESP Chip act like access point)
- DNS Support (currently websites must be looked up by IP)
- Error Recovery
Nice but not necessary
- colorized text for ESP AT Commands in Command line (easier to differentiate from other text)
ESP8266/ESP8266.cpp@25:8e2b04473830, 2014-12-30 (annotated)
- Committer:
- michaeljkoster
- Date:
- Tue Dec 30 00:16:21 2014 +0000
- Revision:
- 25:8e2b04473830
- Parent:
- 24:03585a13ff3b
- Child:
- 26:0d5bcb3903e2
Starting AT v0.2 support for ESP8266
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 | |
michaeljkoster | 25:8e2b04473830 | 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 | } |
michaeljkoster | 25:8e2b04473830 | 64 | */ |
samux | 1:fb4494783863 | 65 | |
samux | 1:fb4494783863 | 66 | inst = this; |
samux | 1:fb4494783863 | 67 | attach_rx(false); |
michaeljkoster | 16:3f0efaa57a12 | 68 | |
michaeljkoster | 16:3f0efaa57a12 | 69 | wifi.baud(9600); // initial baud rate of the ESP8266 |
michaeljkoster | 22:c4360e61486a | 70 | |
michaeljkoster | 22:c4360e61486a | 71 | state.associated = false; |
michaeljkoster | 22:c4360e61486a | 72 | state.cmdMode = false; |
samux | 1:fb4494783863 | 73 | } |
samux | 1:fb4494783863 | 74 | |
michaeljkoster | 13:41098c907200 | 75 | bool ESP8266::join() |
samux | 1:fb4494783863 | 76 | { |
michaeljkoster | 23:de9221771e96 | 77 | sendCommand( "AT+CWMODE=1", "change", NULL, 1000); |
michaeljkoster | 16:3f0efaa57a12 | 78 | string cmd="AT+CWJAP=\""+(string)this->ssid+"\",\""+(string)this->phrase+"\""; |
michaeljkoster | 16:3f0efaa57a12 | 79 | if( sendCommand( cmd.c_str(), "OK", NULL, 10000) ){ |
michaeljkoster | 16:3f0efaa57a12 | 80 | // successfully joined the network |
samux | 1:fb4494783863 | 81 | state.associated = true; |
michaeljkoster | 13:41098c907200 | 82 | INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase); |
samux | 1:fb4494783863 | 83 | return true; |
samux | 1:fb4494783863 | 84 | } |
samux | 1:fb4494783863 | 85 | return false; |
samux | 1:fb4494783863 | 86 | } |
samux | 1:fb4494783863 | 87 | |
michaeljkoster | 13:41098c907200 | 88 | bool ESP8266::connect() |
samux | 1:fb4494783863 | 89 | { |
michaeljkoster | 16:3f0efaa57a12 | 90 | return true; |
samux | 1:fb4494783863 | 91 | } |
samux | 1:fb4494783863 | 92 | |
michaeljkoster | 15:37a7a56a424f | 93 | bool ESP8266::is_connected() |
michaeljkoster | 15:37a7a56a424f | 94 | { |
michaeljkoster | 15:37a7a56a424f | 95 | return true; |
michaeljkoster | 15:37a7a56a424f | 96 | } |
michaeljkoster | 15:37a7a56a424f | 97 | |
michaeljkoster | 16:3f0efaa57a12 | 98 | bool ESP8266::startUDP(char* ip, int port){ |
michaeljkoster | 17:d11fa4c3ac65 | 99 | char portstr[5]; |
michaeljkoster | 16:3f0efaa57a12 | 100 | sprintf(portstr, "%d", port); |
michaeljkoster | 18:60422852e99c | 101 | sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000); |
michaeljkoster | 16:3f0efaa57a12 | 102 | |
michaeljkoster | 17:d11fa4c3ac65 | 103 | sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode |
michaeljkoster | 17:d11fa4c3ac65 | 104 | sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode |
michaeljkoster | 22:c4360e61486a | 105 | pc.printf("Data Mode\r\n"); |
michaeljkoster | 22:c4360e61486a | 106 | state.cmdMode = false; |
michaeljkoster | 22:c4360e61486a | 107 | |
michaeljkoster | 16:3f0efaa57a12 | 108 | return true; |
michaeljkoster | 16:3f0efaa57a12 | 109 | } |
michaeljkoster | 16:3f0efaa57a12 | 110 | |
michaeljkoster | 15:37a7a56a424f | 111 | bool ESP8266::close() |
michaeljkoster | 15:37a7a56a424f | 112 | { |
michaeljkoster | 17:d11fa4c3ac65 | 113 | send("+++",3); |
michaeljkoster | 17:d11fa4c3ac65 | 114 | wait(1); |
michaeljkoster | 22:c4360e61486a | 115 | state.cmdMode = true; |
michaeljkoster | 16:3f0efaa57a12 | 116 | sendCommand("AT+CIPCLOSE","OK", NULL, 10000); |
michaeljkoster | 15:37a7a56a424f | 117 | return true; |
michaeljkoster | 15:37a7a56a424f | 118 | } |
michaeljkoster | 15:37a7a56a424f | 119 | |
michaeljkoster | 15:37a7a56a424f | 120 | bool ESP8266::disconnect() |
michaeljkoster | 15:37a7a56a424f | 121 | { |
michaeljkoster | 15:37a7a56a424f | 122 | // if already disconnected, return |
michaeljkoster | 15:37a7a56a424f | 123 | if (!state.associated) |
michaeljkoster | 15:37a7a56a424f | 124 | return true; |
michaeljkoster | 15:37a7a56a424f | 125 | // send command to quit AP |
michaeljkoster | 16:3f0efaa57a12 | 126 | sendCommand("AT+CWQAP", "OK", NULL, 10000); |
michaeljkoster | 15:37a7a56a424f | 127 | state.associated = false; |
michaeljkoster | 15:37a7a56a424f | 128 | return true; |
michaeljkoster | 15:37a7a56a424f | 129 | } |
michaeljkoster | 15:37a7a56a424f | 130 | |
michaeljkoster | 15:37a7a56a424f | 131 | char* ESP8266::getIPAddress() |
michaeljkoster | 15:37a7a56a424f | 132 | { |
michaeljkoster | 16:3f0efaa57a12 | 133 | sendCommand("AT+CWLIF", "OK", NULL, 10000); |
michaeljkoster | 15:37a7a56a424f | 134 | return ipString; |
michaeljkoster | 15:37a7a56a424f | 135 | } |
michaeljkoster | 15:37a7a56a424f | 136 | |
michaeljkoster | 13:41098c907200 | 137 | bool ESP8266::gethostbyname(const char * host, char * ip) |
samux | 1:fb4494783863 | 138 | { |
samux | 1:fb4494783863 | 139 | string h = host; |
samux | 1:fb4494783863 | 140 | int nb_digits = 0; |
samux | 1:fb4494783863 | 141 | |
samux | 1:fb4494783863 | 142 | // no dns needed |
samux | 1:fb4494783863 | 143 | int pos = h.find("."); |
samux | 1:fb4494783863 | 144 | if (pos != string::npos) { |
samux | 1:fb4494783863 | 145 | string sub = h.substr(0, h.find(".")); |
samux | 1:fb4494783863 | 146 | nb_digits = atoi(sub.c_str()); |
samux | 1:fb4494783863 | 147 | } |
samux | 1:fb4494783863 | 148 | //printf("substrL %s\r\n", sub.c_str()); |
samux | 1:fb4494783863 | 149 | if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) { |
samux | 1:fb4494783863 | 150 | strcpy(ip, host); |
michaeljkoster | 13:41098c907200 | 151 | return true; |
samux | 1:fb4494783863 | 152 | } |
samux | 1:fb4494783863 | 153 | else { |
michaeljkoster | 13:41098c907200 | 154 | // dns needed, not currently available |
michaeljkoster | 13:41098c907200 | 155 | return false; |
michaeljkoster | 13:41098c907200 | 156 | } |
michaeljkoster | 13:41098c907200 | 157 | } |
samux | 1:fb4494783863 | 158 | |
michaeljkoster | 13:41098c907200 | 159 | void ESP8266::reset() |
samux | 1:fb4494783863 | 160 | { |
samux | 1:fb4494783863 | 161 | reset_pin = 0; |
samux | 1:fb4494783863 | 162 | wait(0.2); |
samux | 1:fb4494783863 | 163 | reset_pin = 1; |
michaeljkoster | 24:03585a13ff3b | 164 | wait(1); |
michaeljkoster | 24:03585a13ff3b | 165 | |
michaeljkoster | 24:03585a13ff3b | 166 | //send("+++",3); |
michaeljkoster | 24:03585a13ff3b | 167 | //wait(1); |
michaeljkoster | 24:03585a13ff3b | 168 | state.cmdMode = true; |
michaeljkoster | 24:03585a13ff3b | 169 | sendCommand("AT", "OK", NULL, 1000); |
michaeljkoster | 24:03585a13ff3b | 170 | //sendCommand("AT+RST", "ready", NULL, 10000); |
michaeljkoster | 24:03585a13ff3b | 171 | state.associated = false; |
michaeljkoster | 24:03585a13ff3b | 172 | |
michaeljkoster | 24:03585a13ff3b | 173 | //pc.printf("setting baud rate\r\n"); |
michaeljkoster | 24:03585a13ff3b | 174 | //sendCommand("AT+CIOBAUD=9600", "OK", NULL, 2000); |
michaeljkoster | 24:03585a13ff3b | 175 | //wifi.baud(9600); |
michaeljkoster | 24:03585a13ff3b | 176 | //sendCommand("AT", "OK", NULL, 1000); |
samux | 1:fb4494783863 | 177 | } |
samux | 1:fb4494783863 | 178 | |
michaeljkoster | 13:41098c907200 | 179 | bool ESP8266::reboot() |
samux | 3:9aa05e19c62e | 180 | { |
michaeljkoster | 16:3f0efaa57a12 | 181 | reset(); |
samux | 3:9aa05e19c62e | 182 | return true; |
samux | 3:9aa05e19c62e | 183 | } |
samux | 3:9aa05e19c62e | 184 | |
michaeljkoster | 15:37a7a56a424f | 185 | void ESP8266::handler_rx(void) |
samux | 1:fb4494783863 | 186 | { |
michaeljkoster | 15:37a7a56a424f | 187 | //read characters |
michaeljkoster | 16:3f0efaa57a12 | 188 | char c; |
michaeljkoster | 16:3f0efaa57a12 | 189 | while (wifi.readable()){ |
michaeljkoster | 16:3f0efaa57a12 | 190 | c=wifi.getc(); |
michaeljkoster | 16:3f0efaa57a12 | 191 | buf_ESP8266.queue(c); |
michaeljkoster | 22:c4360e61486a | 192 | if (state.cmdMode) pc.printf("%c",c); //debug echo |
michaeljkoster | 24:03585a13ff3b | 193 | //pc.printf("%c",c); //debug echo |
michaeljkoster | 16:3f0efaa57a12 | 194 | } |
michaeljkoster | 15:37a7a56a424f | 195 | } |
michaeljkoster | 15:37a7a56a424f | 196 | |
michaeljkoster | 15:37a7a56a424f | 197 | void ESP8266::attach_rx(bool callback) |
michaeljkoster | 15:37a7a56a424f | 198 | { |
michaeljkoster | 15:37a7a56a424f | 199 | if (!callback) |
michaeljkoster | 15:37a7a56a424f | 200 | wifi.attach(NULL); |
michaeljkoster | 15:37a7a56a424f | 201 | else |
michaeljkoster | 15:37a7a56a424f | 202 | wifi.attach(this, &ESP8266::handler_rx); |
samux | 1:fb4494783863 | 203 | } |
samux | 1:fb4494783863 | 204 | |
michaeljkoster | 13:41098c907200 | 205 | int ESP8266::readable() |
samux | 1:fb4494783863 | 206 | { |
michaeljkoster | 13:41098c907200 | 207 | return buf_ESP8266.available(); |
samux | 1:fb4494783863 | 208 | } |
samux | 1:fb4494783863 | 209 | |
michaeljkoster | 13:41098c907200 | 210 | int ESP8266::writeable() |
samux | 1:fb4494783863 | 211 | { |
samux | 1:fb4494783863 | 212 | return wifi.writeable(); |
samux | 1:fb4494783863 | 213 | } |
samux | 1:fb4494783863 | 214 | |
michaeljkoster | 13:41098c907200 | 215 | char ESP8266::getc() |
samux | 1:fb4494783863 | 216 | { |
michaeljkoster | 14:4d1128f72e00 | 217 | char c=0; |
michaeljkoster | 13:41098c907200 | 218 | while (!buf_ESP8266.available()); |
michaeljkoster | 13:41098c907200 | 219 | buf_ESP8266.dequeue(&c); |
samux | 1:fb4494783863 | 220 | return c; |
samux | 1:fb4494783863 | 221 | } |
samux | 1:fb4494783863 | 222 | |
michaeljkoster | 15:37a7a56a424f | 223 | int ESP8266::putc(char c) |
samux | 1:fb4494783863 | 224 | { |
michaeljkoster | 20:d764237405c2 | 225 | while (!wifi.writeable() || wifi.readable()); //wait for echoed command characters to be read first |
michaeljkoster | 15:37a7a56a424f | 226 | return wifi.putc(c); |
samux | 1:fb4494783863 | 227 | } |
samux | 1:fb4494783863 | 228 | |
michaeljkoster | 15:37a7a56a424f | 229 | void ESP8266::flush() |
samux | 1:fb4494783863 | 230 | { |
michaeljkoster | 15:37a7a56a424f | 231 | buf_ESP8266.flush(); |
samux | 1:fb4494783863 | 232 | } |
samux | 1:fb4494783863 | 233 | |
michaeljkoster | 16:3f0efaa57a12 | 234 | int ESP8266::send(const char * buf, int len) |
michaeljkoster | 15:37a7a56a424f | 235 | { |
michaeljkoster | 18:60422852e99c | 236 | |
michaeljkoster | 16:3f0efaa57a12 | 237 | const char* bufptr=buf; |
michaeljkoster | 16:3f0efaa57a12 | 238 | for(int i=0; i<len; i++){ |
michaeljkoster | 17:d11fa4c3ac65 | 239 | putc((int)*bufptr++); |
michaeljkoster | 18:60422852e99c | 240 | } |
michaeljkoster | 19:fb8d5bff2076 | 241 | return len; |
michaeljkoster | 15:37a7a56a424f | 242 | } |
samux | 1:fb4494783863 | 243 | |
michaeljkoster | 16:3f0efaa57a12 | 244 | bool ESP8266::sendCommand(const char * cmd, const char * ACK, char * res, int timeout) |
samux | 1:fb4494783863 | 245 | { |
samux | 1:fb4494783863 | 246 | char read; |
samux | 1:fb4494783863 | 247 | size_t found = string::npos; |
samux | 1:fb4494783863 | 248 | string checking; |
samux | 1:fb4494783863 | 249 | Timer tmr; |
samux | 1:fb4494783863 | 250 | int result = 0; |
samux | 1:fb4494783863 | 251 | |
michaeljkoster | 17:d11fa4c3ac65 | 252 | //pc.printf("will send: %s\r\n",cmd); |
samux | 1:fb4494783863 | 253 | |
michaeljkoster | 17:d11fa4c3ac65 | 254 | attach_rx(true); |
samux | 1:fb4494783863 | 255 | |
samux | 1:fb4494783863 | 256 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 257 | while (readable()) |
michaeljkoster | 17:d11fa4c3ac65 | 258 | getc(); |
michaeljkoster | 16:3f0efaa57a12 | 259 | |
samux | 1:fb4494783863 | 260 | if (!ACK || !strcmp(ACK, "NO")) { |
michaeljkoster | 17:d11fa4c3ac65 | 261 | for (int i = 0; i < strlen(cmd); i++){ |
michaeljkoster | 16:3f0efaa57a12 | 262 | result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result; |
michaeljkoster | 20:d764237405c2 | 263 | wait(.005); // prevents stuck recieve ready (?) need to let echoed character get read first |
michaeljkoster | 16:3f0efaa57a12 | 264 | } |
michaeljkoster | 16:3f0efaa57a12 | 265 | putc(13); //CR |
michaeljkoster | 20:d764237405c2 | 266 | wait(.005); // wait for echo |
michaeljkoster | 16:3f0efaa57a12 | 267 | putc(10); //LF |
michaeljkoster | 16:3f0efaa57a12 | 268 | |
samux | 1:fb4494783863 | 269 | } else { |
samux | 1:fb4494783863 | 270 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 271 | while (readable()) |
michaeljkoster | 17:d11fa4c3ac65 | 272 | getc(); |
samux | 1:fb4494783863 | 273 | |
samux | 1:fb4494783863 | 274 | tmr.start(); |
michaeljkoster | 17:d11fa4c3ac65 | 275 | for (int i = 0; i < strlen(cmd); i++){ |
michaeljkoster | 16:3f0efaa57a12 | 276 | result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result; |
michaeljkoster | 20:d764237405c2 | 277 | wait(.005); // wait for echo |
michaeljkoster | 16:3f0efaa57a12 | 278 | } |
michaeljkoster | 16:3f0efaa57a12 | 279 | putc(13); //CR |
michaeljkoster | 20:d764237405c2 | 280 | wait(.005); // wait for echo |
michaeljkoster | 16:3f0efaa57a12 | 281 | putc(10); //LF |
samux | 1:fb4494783863 | 282 | |
samux | 1:fb4494783863 | 283 | while (1) { |
samux | 1:fb4494783863 | 284 | if (tmr.read_ms() > timeout) { |
samux | 1:fb4494783863 | 285 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 286 | while (readable()) |
michaeljkoster | 17:d11fa4c3ac65 | 287 | getc(); |
samux | 1:fb4494783863 | 288 | |
samux | 1:fb4494783863 | 289 | DBG("check: %s\r\n", checking.c_str()); |
samux | 1:fb4494783863 | 290 | |
samux | 1:fb4494783863 | 291 | attach_rx(true); |
samux | 1:fb4494783863 | 292 | return -1; |
michaeljkoster | 17:d11fa4c3ac65 | 293 | } else if (readable()) { |
michaeljkoster | 17:d11fa4c3ac65 | 294 | read = getc(); |
samux | 1:fb4494783863 | 295 | if ( read != '\r' && read != '\n') { |
samux | 1:fb4494783863 | 296 | checking += read; |
samux | 1:fb4494783863 | 297 | found = checking.find(ACK); |
samux | 1:fb4494783863 | 298 | if (found != string::npos) { |
samux | 1:fb4494783863 | 299 | wait(0.01); |
samux | 1:fb4494783863 | 300 | |
samux | 1:fb4494783863 | 301 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 302 | while (readable()) |
michaeljkoster | 17:d11fa4c3ac65 | 303 | getc(); |
samux | 1:fb4494783863 | 304 | |
samux | 1:fb4494783863 | 305 | break; |
samux | 1:fb4494783863 | 306 | } |
samux | 1:fb4494783863 | 307 | } |
samux | 1:fb4494783863 | 308 | } |
samux | 1:fb4494783863 | 309 | } |
samux | 1:fb4494783863 | 310 | DBG("check: %s\r\n", checking.c_str()); |
samux | 1:fb4494783863 | 311 | |
samux | 1:fb4494783863 | 312 | attach_rx(true); |
samux | 1:fb4494783863 | 313 | return result; |
samux | 1:fb4494783863 | 314 | } |
samux | 1:fb4494783863 | 315 | |
samux | 1:fb4494783863 | 316 | //the user wants the result from the command (ACK == NULL, res != NULL) |
samux | 1:fb4494783863 | 317 | if ( res != NULL) { |
samux | 1:fb4494783863 | 318 | int i = 0; |
samux | 1:fb4494783863 | 319 | Timer timeout; |
samux | 1:fb4494783863 | 320 | timeout.start(); |
samux | 1:fb4494783863 | 321 | tmr.reset(); |
samux | 1:fb4494783863 | 322 | while (1) { |
samux | 1:fb4494783863 | 323 | if (timeout.read() > 2) { |
samux | 1:fb4494783863 | 324 | if (i == 0) { |
samux | 1:fb4494783863 | 325 | res = NULL; |
samux | 1:fb4494783863 | 326 | break; |
samux | 1:fb4494783863 | 327 | } |
samux | 1:fb4494783863 | 328 | res[i] = '\0'; |
samux | 1:fb4494783863 | 329 | DBG("user str 1: %s\r\n", res); |
samux | 1:fb4494783863 | 330 | |
samux | 1:fb4494783863 | 331 | break; |
samux | 1:fb4494783863 | 332 | } else { |
samux | 1:fb4494783863 | 333 | if (tmr.read_ms() > 300) { |
samux | 1:fb4494783863 | 334 | res[i] = '\0'; |
samux | 1:fb4494783863 | 335 | DBG("user str: %s\r\n", res); |
samux | 1:fb4494783863 | 336 | |
samux | 1:fb4494783863 | 337 | break; |
samux | 1:fb4494783863 | 338 | } |
michaeljkoster | 17:d11fa4c3ac65 | 339 | if (readable()) { |
samux | 1:fb4494783863 | 340 | tmr.start(); |
michaeljkoster | 17:d11fa4c3ac65 | 341 | read = getc(); |
samux | 1:fb4494783863 | 342 | |
samux | 1:fb4494783863 | 343 | // we drop \r and \n |
samux | 1:fb4494783863 | 344 | if ( read != '\r' && read != '\n') { |
samux | 1:fb4494783863 | 345 | res[i++] = read; |
samux | 1:fb4494783863 | 346 | } |
samux | 1:fb4494783863 | 347 | } |
samux | 1:fb4494783863 | 348 | } |
samux | 1:fb4494783863 | 349 | } |
samux | 1:fb4494783863 | 350 | DBG("user str: %s\r\n", res); |
samux | 1:fb4494783863 | 351 | } |
samux | 1:fb4494783863 | 352 | |
samux | 1:fb4494783863 | 353 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 354 | while (readable()) |
michaeljkoster | 17:d11fa4c3ac65 | 355 | getc(); |
samux | 1:fb4494783863 | 356 | |
samux | 1:fb4494783863 | 357 | attach_rx(true); |
samux | 1:fb4494783863 | 358 | DBG("result: %d\r\n", result) |
samux | 1:fb4494783863 | 359 | return result; |
michaeljkoster | 15:37a7a56a424f | 360 | } |