A fork of the original interface for OS/2. Features a correctly-implemented recv (but retains the old behavior via recv2).

Dependencies:   BufferedSerial

Dependents:   weather_clock weather_clock

Committer:
mbedAustin
Date:
Thu Apr 30 19:07:37 2015 +0000
Revision:
32:cf071dc33972
Parent:
30:c035696b9397
Child:
34:7ccda5d68a00
Child:
36:e1545c6c2cb3
TCP Send / Recieve Working

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