for testing wifi

Dependents:   ESP8266_Test_WIFI

Fork of ESP8266Interface by Steve Kim

Committer:
hank51017
Date:
Tue Jun 28 03:34:57 2016 +0000
Revision:
51:e37b1643f58a
Parent:
49:2c05d747bb91
merge

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