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