Added monitoring feature of ESP8266's UART

Dependents:   ESP8266_W7500_Example DualNetworkInterface-Basic

Fork of ESP8266Interface by ESP8266

Committer:
SteveKim
Date:
Tue Jul 14 09:09:55 2015 +0000
Revision:
49:2c05d747bb91
Parent:
48:03fd9333670d
Added monitoring-feature of ESP8266's UART.

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