Added monitoring feature of ESP8266's UART

Dependents:   ESP8266_W7500_Example DualNetworkInterface-Basic

Fork of ESP8266Interface by ESP8266

Committer:
sarahmarshy
Date:
Wed Jun 03 18:21:19 2015 +0000
Revision:
44:3a7b6083210b
Parent:
35:22d30e936e4c
Child:
45:c180905b5b79
-UDP Client working ; -Interfaces with WebSocketClient; -General improvements

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
sarahmarshy 44:3a7b6083210b 26 #if 1
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
sarahmarshy 44:3a7b6083210b 165 bool ESP8266::startUDP(char* ip, int port, int id, int length)
mbedAustin 28:91e65e22e63a 166 {
michaeljkoster 17:d11fa4c3ac65 167 char portstr[5];
sarahmarshy 44:3a7b6083210b 168 char idstr[1];
sarahmarshy 44:3a7b6083210b 169 char lenstr[2];
sarahmarshy 44:3a7b6083210b 170
michaeljkoster 16:3f0efaa57a12 171 sprintf(portstr, "%d", port);
sarahmarshy 44:3a7b6083210b 172 sprintf(idstr, "%d", id);
sarahmarshy 44:3a7b6083210b 173 sprintf(lenstr, "%d", length);
sarahmarshy 44:3a7b6083210b 174
sarahmarshy 44:3a7b6083210b 175 sendCommand("AT+CIPMUX=1", "OK", NULL, 1000);
sarahmarshy 44:3a7b6083210b 176 sendCommand(( "AT+CIPSTART=" + string(idstr) + ",\"UDP\",\"" + (string) ip + "\"," + (string) portstr + ",1112,0").c_str(), "OK", NULL, 10000);
sarahmarshy 44:3a7b6083210b 177 sendCommand(("AT+CIPSEND=" + (string)idstr + "," + (string)lenstr).c_str(), ">", NULL, 1000);// go into transparent mode
mbedAustin 35:22d30e936e4c 178 DBG("Data Mode\r\n");
michaeljkoster 22:c4360e61486a 179 state.cmdMode = false;
mbedAustin 28:91e65e22e63a 180
michaeljkoster 16:3f0efaa57a12 181 return true;
michaeljkoster 16:3f0efaa57a12 182 }
michaeljkoster 16:3f0efaa57a12 183
sarahmarshy 44:3a7b6083210b 184 bool ESP8266::startTCPServer(int port)
sarahmarshy 44:3a7b6083210b 185 {
sarahmarshy 44:3a7b6083210b 186 bool command_results[3];
sarahmarshy 44:3a7b6083210b 187 command_results[0]=sendCommand("AT+CWMODE=3", "OK", NULL, 1000);
sarahmarshy 44:3a7b6083210b 188 command_results[1]=sendCommand("AT+CIPMUX=1", "OK", NULL, 1000);
sarahmarshy 44:3a7b6083210b 189 if(port == 333){
sarahmarshy 44:3a7b6083210b 190 command_results[2]=sendCommand("AT+CIPSERVER=1", "OK", NULL, 1000);
sarahmarshy 44:3a7b6083210b 191 }
sarahmarshy 44:3a7b6083210b 192 else{
sarahmarshy 44:3a7b6083210b 193 char portstr[5];
sarahmarshy 44:3a7b6083210b 194 sprintf(portstr, "%d", port);
sarahmarshy 44:3a7b6083210b 195 command_results[2]=sendCommand(("AT+CIPSERVER=1," + (string)portstr).c_str(), "OK", NULL, 1000);
sarahmarshy 44:3a7b6083210b 196 }
sarahmarshy 44:3a7b6083210b 197 //sendCommand("AT+CIFSR", "OK", NULL, 1000);
sarahmarshy 44:3a7b6083210b 198 DBG("Data Mode\r\n");
sarahmarshy 44:3a7b6083210b 199 state.cmdMode = false;
sarahmarshy 44:3a7b6083210b 200 if (command_results[0] and command_results[1] and command_results[2]){
sarahmarshy 44:3a7b6083210b 201 return true;
sarahmarshy 44:3a7b6083210b 202 }
sarahmarshy 44:3a7b6083210b 203 else{
sarahmarshy 44:3a7b6083210b 204 return false;
sarahmarshy 44:3a7b6083210b 205 }
sarahmarshy 44:3a7b6083210b 206 }
sarahmarshy 44:3a7b6083210b 207
michaeljkoster 15:37a7a56a424f 208 bool ESP8266::close()
michaeljkoster 15:37a7a56a424f 209 {
michaeljkoster 17:d11fa4c3ac65 210 send("+++",3);
michaeljkoster 17:d11fa4c3ac65 211 wait(1);
michaeljkoster 22:c4360e61486a 212 state.cmdMode = true;
michaeljkoster 16:3f0efaa57a12 213 sendCommand("AT+CIPCLOSE","OK", NULL, 10000);
michaeljkoster 15:37a7a56a424f 214 return true;
michaeljkoster 15:37a7a56a424f 215 }
michaeljkoster 15:37a7a56a424f 216
michaeljkoster 15:37a7a56a424f 217 bool ESP8266::disconnect()
michaeljkoster 15:37a7a56a424f 218 {
michaeljkoster 15:37a7a56a424f 219 // if already disconnected, return
michaeljkoster 15:37a7a56a424f 220 if (!state.associated)
michaeljkoster 15:37a7a56a424f 221 return true;
michaeljkoster 15:37a7a56a424f 222 // send command to quit AP
mbedAustin 28:91e65e22e63a 223 sendCommand("AT+CWQAP", "OK", NULL, 10000);
michaeljkoster 15:37a7a56a424f 224 state.associated = false;
michaeljkoster 15:37a7a56a424f 225 return true;
michaeljkoster 15:37a7a56a424f 226 }
michaeljkoster 15:37a7a56a424f 227
mbedAustin 28:91e65e22e63a 228 /*
mbedAustin 28:91e65e22e63a 229 Assuming Returned data looks like this:
mbedAustin 28:91e65e22e63a 230 +CIFSR:STAIP,"192.168.11.2"
mbedAustin 28:91e65e22e63a 231 +CIFSR:STAMAC,"18:fe:34:9f:3a:f5"
mbedAustin 28:91e65e22e63a 232 grabbing IP from first set of quotation marks
mbedAustin 28:91e65e22e63a 233 */
michaeljkoster 15:37a7a56a424f 234 char* ESP8266::getIPAddress()
michaeljkoster 15:37a7a56a424f 235 {
mbedAustin 28:91e65e22e63a 236 char result[30] = {0};
mbedAustin 28:91e65e22e63a 237 int check = 0;
mbedAustin 28:91e65e22e63a 238 check = sendCommand("AT+CIFSR", NULL, result, 1000);
mbedAustin 29:939372104145 239 //pc.printf("\r\nReceivedInfo for IP Command is: %s\r\n",result);
mbedAustin 29:939372104145 240 ip = ipString;
mbedAustin 29:939372104145 241 if(check) {
mbedAustin 28:91e65e22e63a 242 // Success
mbedAustin 28:91e65e22e63a 243 string resultString(result);
mbedAustin 28:91e65e22e63a 244 uint8_t pos1 = 0, pos2 = 0;
mbedAustin 28:91e65e22e63a 245 //uint8_t pos3 = 0, pos4 = 0;
mbedAustin 28:91e65e22e63a 246 pos1 = resultString.find("+CIFSR:STAIP");
mbedAustin 28:91e65e22e63a 247 pos1 = resultString.find('"',pos1);
mbedAustin 28:91e65e22e63a 248 pos2 = resultString.find('"',pos1+1);
mbedAustin 28:91e65e22e63a 249 //pos3 = resultString.find('"',pos2+1); //would find mac address
mbedAustin 28:91e65e22e63a 250 //pos4 = resultString.find('"',pos3+1);
mbedAustin 28:91e65e22e63a 251 strncpy(ipString,resultString.substr(pos1,pos2).c_str(),sizeof(ipString));
mbedAustin 28:91e65e22e63a 252 ipString[pos2 - pos1 +1] = 0; // null terminate string correctly.
mbedAustin 28:91e65e22e63a 253 DBG("IP: %s\r\n",ipString);
mbedAustin 29:939372104145 254 ip = ipString;
mbedAustin 29:939372104145 255
mbedAustin 29:939372104145 256 } else {
mbedAustin 28:91e65e22e63a 257 // Failure
mbedAustin 28:91e65e22e63a 258 DBG("getIPAddress() failed\r\n");
mbedAustin 29:939372104145 259 ip = NULL;
mbedAustin 28:91e65e22e63a 260 }
mbedAustin 29:939372104145 261 return ip;
michaeljkoster 15:37a7a56a424f 262 }
michaeljkoster 15:37a7a56a424f 263
michaeljkoster 13:41098c907200 264 bool ESP8266::gethostbyname(const char * host, char * ip)
samux 1:fb4494783863 265 {
samux 1:fb4494783863 266 string h = host;
samux 1:fb4494783863 267 int nb_digits = 0;
samux 1:fb4494783863 268
samux 1:fb4494783863 269 // no dns needed
samux 1:fb4494783863 270 int pos = h.find(".");
samux 1:fb4494783863 271 if (pos != string::npos) {
samux 1:fb4494783863 272 string sub = h.substr(0, h.find("."));
samux 1:fb4494783863 273 nb_digits = atoi(sub.c_str());
samux 1:fb4494783863 274 }
samux 1:fb4494783863 275 //printf("substrL %s\r\n", sub.c_str());
samux 1:fb4494783863 276 if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) {
samux 1:fb4494783863 277 strcpy(ip, host);
michaeljkoster 13:41098c907200 278 return true;
mbedAustin 28:91e65e22e63a 279 } else {
michaeljkoster 13:41098c907200 280 // dns needed, not currently available
mbedAustin 30:c035696b9397 281 ERR("gethostbyname(): DNS Not currently available, only use IP Addresses!");
michaeljkoster 13:41098c907200 282 return false;
michaeljkoster 13:41098c907200 283 }
michaeljkoster 13:41098c907200 284 }
samux 1:fb4494783863 285
michaeljkoster 13:41098c907200 286 void ESP8266::reset()
samux 1:fb4494783863 287 {
samux 1:fb4494783863 288 reset_pin = 0;
samux 1:fb4494783863 289 wait(0.2);
samux 1:fb4494783863 290 reset_pin = 1;
michaeljkoster 24:03585a13ff3b 291 wait(1);
mbedAustin 28:91e65e22e63a 292
michaeljkoster 24:03585a13ff3b 293 //send("+++",3);
michaeljkoster 24:03585a13ff3b 294 //wait(1);
michaeljkoster 24:03585a13ff3b 295 state.cmdMode = true;
michaeljkoster 24:03585a13ff3b 296 sendCommand("AT", "OK", NULL, 1000);
mbedAustin 28:91e65e22e63a 297 sendCommand("AT+RST", "ready", NULL, 10000);
michaeljkoster 24:03585a13ff3b 298 state.associated = false;
michaeljkoster 24:03585a13ff3b 299
samux 1:fb4494783863 300 }
samux 1:fb4494783863 301
michaeljkoster 13:41098c907200 302 bool ESP8266::reboot()
samux 3:9aa05e19c62e 303 {
michaeljkoster 16:3f0efaa57a12 304 reset();
samux 3:9aa05e19c62e 305 return true;
samux 3:9aa05e19c62e 306 }
samux 3:9aa05e19c62e 307
michaeljkoster 15:37a7a56a424f 308 void ESP8266::handler_rx(void)
samux 1:fb4494783863 309 {
michaeljkoster 15:37a7a56a424f 310 //read characters
michaeljkoster 16:3f0efaa57a12 311 char c;
mbedAustin 28:91e65e22e63a 312 while (wifi.readable()) {
michaeljkoster 16:3f0efaa57a12 313 c=wifi.getc();
michaeljkoster 16:3f0efaa57a12 314 buf_ESP8266.queue(c);
michaeljkoster 26:0d5bcb3903e2 315 //if (state.cmdMode) pc.printf("%c",c); //debug echo, needs fast serial console to prevent UART overruns
michaeljkoster 16:3f0efaa57a12 316 }
michaeljkoster 15:37a7a56a424f 317 }
michaeljkoster 15:37a7a56a424f 318
michaeljkoster 15:37a7a56a424f 319 void ESP8266::attach_rx(bool callback)
michaeljkoster 15:37a7a56a424f 320 {
michaeljkoster 15:37a7a56a424f 321 if (!callback)
michaeljkoster 15:37a7a56a424f 322 wifi.attach(NULL);
michaeljkoster 15:37a7a56a424f 323 else
michaeljkoster 15:37a7a56a424f 324 wifi.attach(this, &ESP8266::handler_rx);
samux 1:fb4494783863 325 }
samux 1:fb4494783863 326
michaeljkoster 13:41098c907200 327 int ESP8266::readable()
samux 1:fb4494783863 328 {
michaeljkoster 13:41098c907200 329 return buf_ESP8266.available();
samux 1:fb4494783863 330 }
samux 1:fb4494783863 331
michaeljkoster 13:41098c907200 332 int ESP8266::writeable()
samux 1:fb4494783863 333 {
samux 1:fb4494783863 334 return wifi.writeable();
samux 1:fb4494783863 335 }
samux 1:fb4494783863 336
michaeljkoster 13:41098c907200 337 char ESP8266::getc()
samux 1:fb4494783863 338 {
michaeljkoster 14:4d1128f72e00 339 char c=0;
michaeljkoster 13:41098c907200 340 while (!buf_ESP8266.available());
michaeljkoster 13:41098c907200 341 buf_ESP8266.dequeue(&c);
samux 1:fb4494783863 342 return c;
samux 1:fb4494783863 343 }
samux 1:fb4494783863 344
michaeljkoster 15:37a7a56a424f 345 int ESP8266::putc(char c)
samux 1:fb4494783863 346 {
michaeljkoster 20:d764237405c2 347 while (!wifi.writeable() || wifi.readable()); //wait for echoed command characters to be read first
michaeljkoster 15:37a7a56a424f 348 return wifi.putc(c);
samux 1:fb4494783863 349 }
samux 1:fb4494783863 350
michaeljkoster 15:37a7a56a424f 351 void ESP8266::flush()
samux 1:fb4494783863 352 {
michaeljkoster 15:37a7a56a424f 353 buf_ESP8266.flush();
samux 1:fb4494783863 354 }
samux 1:fb4494783863 355
michaeljkoster 16:3f0efaa57a12 356 int ESP8266::send(const char * buf, int len)
michaeljkoster 15:37a7a56a424f 357 {
mbedAustin 32:cf071dc33972 358 //TODO: need to add handler for data > 2048B, this is the max packet size of the ESP8266.
michaeljkoster 16:3f0efaa57a12 359 const char* bufptr=buf;
mbedAustin 28:91e65e22e63a 360 for(int i=0; i<len; i++) {
michaeljkoster 17:d11fa4c3ac65 361 putc((int)*bufptr++);
michaeljkoster 18:60422852e99c 362 }
michaeljkoster 19:fb8d5bff2076 363 return len;
michaeljkoster 15:37a7a56a424f 364 }
samux 1:fb4494783863 365
michaeljkoster 16:3f0efaa57a12 366 bool ESP8266::sendCommand(const char * cmd, const char * ACK, char * res, int timeout)
samux 1:fb4494783863 367 {
samux 1:fb4494783863 368 char read;
samux 1:fb4494783863 369 size_t found = string::npos;
samux 1:fb4494783863 370 string checking;
samux 1:fb4494783863 371 Timer tmr;
samux 1:fb4494783863 372 int result = 0;
samux 1:fb4494783863 373
mbedAustin 28:91e65e22e63a 374 DBG("will send: %s\r\n",cmd);
samux 1:fb4494783863 375
michaeljkoster 17:d11fa4c3ac65 376 attach_rx(true);
samux 1:fb4494783863 377
samux 1:fb4494783863 378 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 379 while (readable())
michaeljkoster 17:d11fa4c3ac65 380 getc();
mbedAustin 28:91e65e22e63a 381
samux 1:fb4494783863 382 if (!ACK || !strcmp(ACK, "NO")) {
mbedAustin 28:91e65e22e63a 383 for (int i = 0; i < strlen(cmd); i++) {
michaeljkoster 16:3f0efaa57a12 384 result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
michaeljkoster 20:d764237405c2 385 wait(.005); // prevents stuck recieve ready (?) need to let echoed character get read first
michaeljkoster 16:3f0efaa57a12 386 }
michaeljkoster 16:3f0efaa57a12 387 putc(13); //CR
michaeljkoster 20:d764237405c2 388 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 389 putc(10); //LF
michaeljkoster 16:3f0efaa57a12 390
samux 1:fb4494783863 391 } else {
samux 1:fb4494783863 392 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 393 while (readable())
michaeljkoster 17:d11fa4c3ac65 394 getc();
samux 1:fb4494783863 395
samux 1:fb4494783863 396 tmr.start();
mbedAustin 28:91e65e22e63a 397 for (int i = 0; i < strlen(cmd); i++) {
michaeljkoster 16:3f0efaa57a12 398 result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
michaeljkoster 20:d764237405c2 399 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 400 }
michaeljkoster 16:3f0efaa57a12 401 putc(13); //CR
michaeljkoster 20:d764237405c2 402 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 403 putc(10); //LF
samux 1:fb4494783863 404
samux 1:fb4494783863 405 while (1) {
samux 1:fb4494783863 406 if (tmr.read_ms() > timeout) {
samux 1:fb4494783863 407 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 408 while (readable())
michaeljkoster 17:d11fa4c3ac65 409 getc();
samux 1:fb4494783863 410
samux 1:fb4494783863 411 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 412
samux 1:fb4494783863 413 attach_rx(true);
samux 1:fb4494783863 414 return -1;
michaeljkoster 17:d11fa4c3ac65 415 } else if (readable()) {
michaeljkoster 17:d11fa4c3ac65 416 read = getc();
mbedAustin 28:91e65e22e63a 417 printf("%c",read); //debug echo
samux 1:fb4494783863 418 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 419 checking += read;
samux 1:fb4494783863 420 found = checking.find(ACK);
samux 1:fb4494783863 421 if (found != string::npos) {
samux 1:fb4494783863 422 wait(0.01);
samux 1:fb4494783863 423
samux 1:fb4494783863 424 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 425 while (readable())
michaeljkoster 26:0d5bcb3903e2 426 read = getc();
mbedAustin 28:91e65e22e63a 427 printf("%c",read); //debug echo
samux 1:fb4494783863 428 break;
samux 1:fb4494783863 429 }
samux 1:fb4494783863 430 }
samux 1:fb4494783863 431 }
samux 1:fb4494783863 432 }
samux 1:fb4494783863 433 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 434
samux 1:fb4494783863 435 attach_rx(true);
samux 1:fb4494783863 436 return result;
samux 1:fb4494783863 437 }
samux 1:fb4494783863 438
samux 1:fb4494783863 439 //the user wants the result from the command (ACK == NULL, res != NULL)
samux 1:fb4494783863 440 if ( res != NULL) {
samux 1:fb4494783863 441 int i = 0;
samux 1:fb4494783863 442 Timer timeout;
samux 1:fb4494783863 443 timeout.start();
samux 1:fb4494783863 444 tmr.reset();
samux 1:fb4494783863 445 while (1) {
samux 1:fb4494783863 446 if (timeout.read() > 2) {
samux 1:fb4494783863 447 if (i == 0) {
samux 1:fb4494783863 448 res = NULL;
samux 1:fb4494783863 449 break;
samux 1:fb4494783863 450 }
samux 1:fb4494783863 451 res[i] = '\0';
samux 1:fb4494783863 452 DBG("user str 1: %s\r\n", res);
samux 1:fb4494783863 453
samux 1:fb4494783863 454 break;
samux 1:fb4494783863 455 } else {
samux 1:fb4494783863 456 if (tmr.read_ms() > 300) {
samux 1:fb4494783863 457 res[i] = '\0';
samux 1:fb4494783863 458 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 459
samux 1:fb4494783863 460 break;
samux 1:fb4494783863 461 }
michaeljkoster 17:d11fa4c3ac65 462 if (readable()) {
samux 1:fb4494783863 463 tmr.start();
michaeljkoster 17:d11fa4c3ac65 464 read = getc();
samux 1:fb4494783863 465
samux 1:fb4494783863 466 // we drop \r and \n
samux 1:fb4494783863 467 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 468 res[i++] = read;
samux 1:fb4494783863 469 }
samux 1:fb4494783863 470 }
samux 1:fb4494783863 471 }
samux 1:fb4494783863 472 }
samux 1:fb4494783863 473 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 474 }
samux 1:fb4494783863 475
samux 1:fb4494783863 476 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 477 while (readable())
michaeljkoster 17:d11fa4c3ac65 478 getc();
samux 1:fb4494783863 479
samux 1:fb4494783863 480 attach_rx(true);
samux 1:fb4494783863 481 DBG("result: %d\r\n", result)
samux 1:fb4494783863 482 return result;
michaeljkoster 15:37a7a56a424f 483 }