Init

Fork of ESP8266Interface by ESP8266

Committer:
mbedAustin
Date:
Sat May 02 04:05:35 2015 +0000
Revision:
38:86e75901efc1
Parent:
37:6887e61cf674
Parent:
35:22d30e936e4c
Child:
40:0a83315aea0a
trying to merge things together...

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 36:e1545c6c2cb3 26 #if 1
mbedAustin 36:e1545c6c2cb3 27 #define DBG(x, ...) printf("[ESP8266 : DBG]"x" [%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
mbedAustin 36:e1545c6c2cb3 28 #define WARN(x, ...) printf("[ESP8266 : WARN]"x" [%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
mbedAustin 36:e1545c6c2cb3 29 #define ERR(x, ...) printf("[ESP8266 : ERR]"x" [%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
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 36:e1545c6c2cb3 36 #if 1
mbedAustin 36:e1545c6c2cb3 37 #define INFO(x, ...) printf("[ESP8266 : INFO]"x" [%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
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):
mbedAustin 36:e1545c6c2cb3 51 wifi(tx, rx), reset_pin(_reset), buf_ESP8266(ESP_MBUFFE_MAX)
samux 1:fb4494783863 52 {
mbedAustin 36:e1545c6c2cb3 53 INFO("Initializing ESP8266 object");
samux 1:fb4494783863 54 memset(&state, 0, sizeof(state));
samux 1:fb4494783863 55
michaeljkoster 26:0d5bcb3903e2 56 // change all ' ' in '$' in the ssid and the passphrase
samux 1:fb4494783863 57 strcpy(this->ssid, ssid);
samux 1:fb4494783863 58 for (int i = 0; i < strlen(ssid); i++) {
samux 1:fb4494783863 59 if (this->ssid[i] == ' ')
samux 1:fb4494783863 60 this->ssid[i] = '$';
samux 1:fb4494783863 61 }
samux 1:fb4494783863 62 strcpy(this->phrase, phrase);
samux 1:fb4494783863 63 for (int i = 0; i < strlen(phrase); i++) {
samux 1:fb4494783863 64 if (this->phrase[i] == ' ')
samux 1:fb4494783863 65 this->phrase[i] = '$';
samux 1:fb4494783863 66 }
michaeljkoster 26:0d5bcb3903e2 67
samux 1:fb4494783863 68
samux 1:fb4494783863 69 inst = this;
samux 1:fb4494783863 70 attach_rx(false);
mbedAustin 28:91e65e22e63a 71
mbedAustin 28:91e65e22e63a 72 wifi.baud(baud); // initial baud rate of the ESP8266
mbedAustin 28:91e65e22e63a 73
michaeljkoster 22:c4360e61486a 74 state.associated = false;
michaeljkoster 22:c4360e61486a 75 state.cmdMode = false;
samux 1:fb4494783863 76 }
samux 1:fb4494783863 77
michaeljkoster 13:41098c907200 78 bool ESP8266::join()
samux 1:fb4494783863 79 {
michaeljkoster 23:de9221771e96 80 sendCommand( "AT+CWMODE=1", "change", NULL, 1000);
michaeljkoster 16:3f0efaa57a12 81 string cmd="AT+CWJAP=\""+(string)this->ssid+"\",\""+(string)this->phrase+"\"";
mbedAustin 28:91e65e22e63a 82 if( sendCommand( cmd.c_str(), "OK", NULL, 10000) ) {
michaeljkoster 16:3f0efaa57a12 83 // successfully joined the network
samux 1:fb4494783863 84 state.associated = true;
mbedAustin 37:6887e61cf674 85 INFO("ssid: %s, phrase: %s", this->ssid, this->phrase);
samux 1:fb4494783863 86 return true;
samux 1:fb4494783863 87 }
samux 1:fb4494783863 88 return false;
samux 1:fb4494783863 89 }
samux 1:fb4494783863 90
michaeljkoster 13:41098c907200 91 bool ESP8266::connect()
samux 1:fb4494783863 92 {
mbedAustin 28:91e65e22e63a 93 sendCommand("AT+CWDHCP=1,1","OK",NULL,1000); // DHCP Enabled in Station Mode
mbedAustin 28:91e65e22e63a 94 return ESP8266::join();
samux 1:fb4494783863 95 }
samux 1:fb4494783863 96
michaeljkoster 15:37a7a56a424f 97 bool ESP8266::is_connected()
michaeljkoster 15:37a7a56a424f 98 {
michaeljkoster 15:37a7a56a424f 99 return true;
michaeljkoster 15:37a7a56a424f 100 }
michaeljkoster 15:37a7a56a424f 101
mbedAustin 30:c035696b9397 102 bool ESP8266::start(bool type,char* ip, int port, int id)
mbedAustin 30:c035696b9397 103 {
mbedAustin 30:c035696b9397 104 // Error Check
mbedAustin 30:c035696b9397 105 if(id > ESP_MAXID) {
mbedAustin 30:c035696b9397 106 ERR("startUDPMulti: max id is: %d, id given is %d",ESP_MAXID,id);
mbedAustin 30:c035696b9397 107 return false;
mbedAustin 30:c035696b9397 108 }
mbedAustin 30:c035696b9397 109 // Single Connection Mode
mbedAustin 30:c035696b9397 110 if(id < 0) {
mbedAustin 30:c035696b9397 111 DBG("Start Single Connection Mode");
mbedAustin 30:c035696b9397 112 char portstr[5];
mbedAustin 30:c035696b9397 113 char idstr[2];
mbedAustin 30:c035696b9397 114 bool check [3] = {0};
mbedAustin 30:c035696b9397 115 sprintf(idstr,"%d",id);
mbedAustin 30:c035696b9397 116 sprintf(portstr, "%d", port);
mbedAustin 30:c035696b9397 117 switch(type) {
mbedAustin 30:c035696b9397 118 case ESP_UDP_TYPE : //UDP
mbedAustin 32:cf071dc33972 119 check[0] = sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000);
mbedAustin 30:c035696b9397 120 break;
mbedAustin 30:c035696b9397 121 case ESP_TCP_TYPE : //TCP
mbedAustin 32:cf071dc33972 122 check[0] = sendCommand(( "AT+CIPSTART=\"TCP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000);
mbedAustin 30:c035696b9397 123 break;
mbedAustin 30:c035696b9397 124 default:
mbedAustin 30:c035696b9397 125 ERR("Default hit for starting connection, this shouldnt be possible!!");
mbedAustin 30:c035696b9397 126 break;
mbedAustin 30:c035696b9397 127 }
mbedAustin 30:c035696b9397 128 check[1] = sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode
mbedAustin 30:c035696b9397 129 check[2] = sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode
mbedAustin 30:c035696b9397 130 // check that all commands were sucessful
mbedAustin 32:cf071dc33972 131 if(check[0] and check[1] and check[2]) {
mbedAustin 30:c035696b9397 132 state.cmdMode = false;
mbedAustin 30:c035696b9397 133 return true;
mbedAustin 30:c035696b9397 134 } else {
mbedAustin 36:e1545c6c2cb3 135 ERR("startUDPTransparent Failed for ip:%s, port:%d",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 38:86e75901efc1 173 //DBG("Data Mode");
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 36:e1545c6c2cb3 224 DBG("IP: %s",ipString);
mbedAustin 29:939372104145 225 ip = ipString;
mbedAustin 29:939372104145 226
mbedAustin 29:939372104145 227 } else {
mbedAustin 28:91e65e22e63a 228 // Failure
mbedAustin 36:e1545c6c2cb3 229 DBG("getIPAddress() failed");
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 }
mbedAustin 36:e1545c6c2cb3 404 DBG("check: %s", 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';
mbedAustin 36:e1545c6c2cb3 423 DBG("user str 1: %s", 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';
mbedAustin 36:e1545c6c2cb3 429 DBG("user str: %s", 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 }
mbedAustin 36:e1545c6c2cb3 444 DBG("user str: %s", 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);
mbedAustin 36:e1545c6c2cb3 452 DBG("result: %d", result)
samux 1:fb4494783863 453 return result;
michaeljkoster 15:37a7a56a424f 454 }