Voor Arne

Fork of ESP8266Interface by ESP8266

Committer:
blownelco
Date:
Mon Dec 05 15:36:40 2016 +0000
Revision:
49:5a4798ccba07
Parent:
47:1f4dd0e91837
Child:
50:3727afa4c1b5
wifi ok

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