A fork of the original interface for OS/2. Features a correctly-implemented recv (but retains the old behavior via recv2).

Dependencies:   BufferedSerial

Dependents:   weather_clock weather_clock

Committer:
michaeljkoster
Date:
Tue Dec 30 03:21:33 2014 +0000
Revision:
26:0d5bcb3903e2
Parent:
25:8e2b04473830
Child:
28:91e65e22e63a
set baud rate and remove printf from rx handler

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
screamer 10:131675c17372 26 #if (defined(DEBUG))
michaeljkoster 13:41098c907200 27 #define DBG(x, ...) std::printf("[ESP8266 : DBG]"x"\r\n", ##__VA_ARGS__);
michaeljkoster 13:41098c907200 28 #define WARN(x, ...) std::printf("[ESP8266 : WARN]"x"\r\n", ##__VA_ARGS__);
michaeljkoster 13:41098c907200 29 #define ERR(x, ...) std::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
screamer 10:131675c17372 36 #if defined(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
samux 1:fb4494783863 42 #define MAX_TRY_JOIN 3
samux 1:fb4494783863 43
michaeljkoster 12:c5f0eac67a8a 44 extern Serial pc;
michaeljkoster 12:c5f0eac67a8a 45
michaeljkoster 13:41098c907200 46 ESP8266 * ESP8266::inst;
samux 1:fb4494783863 47
michaeljkoster 13:41098c907200 48 ESP8266::ESP8266( PinName tx, PinName rx, PinName _reset, const char * ssid, const char * phrase ):
michaeljkoster 13:41098c907200 49 wifi(tx, rx), reset_pin(_reset), buf_ESP8266(256)
samux 1:fb4494783863 50 {
samux 1:fb4494783863 51 memset(&state, 0, sizeof(state));
samux 1:fb4494783863 52
michaeljkoster 26:0d5bcb3903e2 53 // change all ' ' in '$' in the ssid and the passphrase
samux 1:fb4494783863 54 strcpy(this->ssid, ssid);
samux 1:fb4494783863 55 for (int i = 0; i < strlen(ssid); i++) {
samux 1:fb4494783863 56 if (this->ssid[i] == ' ')
samux 1:fb4494783863 57 this->ssid[i] = '$';
samux 1:fb4494783863 58 }
samux 1:fb4494783863 59 strcpy(this->phrase, phrase);
samux 1:fb4494783863 60 for (int i = 0; i < strlen(phrase); i++) {
samux 1:fb4494783863 61 if (this->phrase[i] == ' ')
samux 1:fb4494783863 62 this->phrase[i] = '$';
samux 1:fb4494783863 63 }
michaeljkoster 26:0d5bcb3903e2 64
samux 1:fb4494783863 65
samux 1:fb4494783863 66 inst = this;
samux 1:fb4494783863 67 attach_rx(false);
michaeljkoster 16:3f0efaa57a12 68
michaeljkoster 26:0d5bcb3903e2 69 wifi.baud(115200); // initial baud rate of the ESP8266
michaeljkoster 22:c4360e61486a 70
michaeljkoster 22:c4360e61486a 71 state.associated = false;
michaeljkoster 22:c4360e61486a 72 state.cmdMode = false;
samux 1:fb4494783863 73 }
samux 1:fb4494783863 74
michaeljkoster 13:41098c907200 75 bool ESP8266::join()
samux 1:fb4494783863 76 {
michaeljkoster 23:de9221771e96 77 sendCommand( "AT+CWMODE=1", "change", NULL, 1000);
michaeljkoster 16:3f0efaa57a12 78 string cmd="AT+CWJAP=\""+(string)this->ssid+"\",\""+(string)this->phrase+"\"";
michaeljkoster 16:3f0efaa57a12 79 if( sendCommand( cmd.c_str(), "OK", NULL, 10000) ){
michaeljkoster 16:3f0efaa57a12 80 // successfully joined the network
samux 1:fb4494783863 81 state.associated = true;
michaeljkoster 13:41098c907200 82 INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase);
samux 1:fb4494783863 83 return true;
samux 1:fb4494783863 84 }
samux 1:fb4494783863 85 return false;
samux 1:fb4494783863 86 }
samux 1:fb4494783863 87
michaeljkoster 13:41098c907200 88 bool ESP8266::connect()
samux 1:fb4494783863 89 {
michaeljkoster 16:3f0efaa57a12 90 return true;
samux 1:fb4494783863 91 }
samux 1:fb4494783863 92
michaeljkoster 15:37a7a56a424f 93 bool ESP8266::is_connected()
michaeljkoster 15:37a7a56a424f 94 {
michaeljkoster 15:37a7a56a424f 95 return true;
michaeljkoster 15:37a7a56a424f 96 }
michaeljkoster 15:37a7a56a424f 97
michaeljkoster 16:3f0efaa57a12 98 bool ESP8266::startUDP(char* ip, int port){
michaeljkoster 17:d11fa4c3ac65 99 char portstr[5];
michaeljkoster 16:3f0efaa57a12 100 sprintf(portstr, "%d", port);
michaeljkoster 18:60422852e99c 101 sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000);
michaeljkoster 16:3f0efaa57a12 102
michaeljkoster 17:d11fa4c3ac65 103 sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode
michaeljkoster 17:d11fa4c3ac65 104 sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode
michaeljkoster 22:c4360e61486a 105 pc.printf("Data Mode\r\n");
michaeljkoster 22:c4360e61486a 106 state.cmdMode = false;
michaeljkoster 22:c4360e61486a 107
michaeljkoster 16:3f0efaa57a12 108 return true;
michaeljkoster 16:3f0efaa57a12 109 }
michaeljkoster 16:3f0efaa57a12 110
michaeljkoster 15:37a7a56a424f 111 bool ESP8266::close()
michaeljkoster 15:37a7a56a424f 112 {
michaeljkoster 17:d11fa4c3ac65 113 send("+++",3);
michaeljkoster 17:d11fa4c3ac65 114 wait(1);
michaeljkoster 22:c4360e61486a 115 state.cmdMode = true;
michaeljkoster 16:3f0efaa57a12 116 sendCommand("AT+CIPCLOSE","OK", NULL, 10000);
michaeljkoster 15:37a7a56a424f 117 return true;
michaeljkoster 15:37a7a56a424f 118 }
michaeljkoster 15:37a7a56a424f 119
michaeljkoster 15:37a7a56a424f 120 bool ESP8266::disconnect()
michaeljkoster 15:37a7a56a424f 121 {
michaeljkoster 15:37a7a56a424f 122 // if already disconnected, return
michaeljkoster 15:37a7a56a424f 123 if (!state.associated)
michaeljkoster 15:37a7a56a424f 124 return true;
michaeljkoster 15:37a7a56a424f 125 // send command to quit AP
michaeljkoster 16:3f0efaa57a12 126 sendCommand("AT+CWQAP", "OK", NULL, 10000);
michaeljkoster 15:37a7a56a424f 127 state.associated = false;
michaeljkoster 15:37a7a56a424f 128 return true;
michaeljkoster 15:37a7a56a424f 129 }
michaeljkoster 15:37a7a56a424f 130
michaeljkoster 15:37a7a56a424f 131 char* ESP8266::getIPAddress()
michaeljkoster 15:37a7a56a424f 132 {
michaeljkoster 16:3f0efaa57a12 133 sendCommand("AT+CWLIF", "OK", NULL, 10000);
michaeljkoster 15:37a7a56a424f 134 return ipString;
michaeljkoster 15:37a7a56a424f 135 }
michaeljkoster 15:37a7a56a424f 136
michaeljkoster 13:41098c907200 137 bool ESP8266::gethostbyname(const char * host, char * ip)
samux 1:fb4494783863 138 {
samux 1:fb4494783863 139 string h = host;
samux 1:fb4494783863 140 int nb_digits = 0;
samux 1:fb4494783863 141
samux 1:fb4494783863 142 // no dns needed
samux 1:fb4494783863 143 int pos = h.find(".");
samux 1:fb4494783863 144 if (pos != string::npos) {
samux 1:fb4494783863 145 string sub = h.substr(0, h.find("."));
samux 1:fb4494783863 146 nb_digits = atoi(sub.c_str());
samux 1:fb4494783863 147 }
samux 1:fb4494783863 148 //printf("substrL %s\r\n", sub.c_str());
samux 1:fb4494783863 149 if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) {
samux 1:fb4494783863 150 strcpy(ip, host);
michaeljkoster 13:41098c907200 151 return true;
samux 1:fb4494783863 152 }
samux 1:fb4494783863 153 else {
michaeljkoster 13:41098c907200 154 // dns needed, not currently available
michaeljkoster 13:41098c907200 155 return false;
michaeljkoster 13:41098c907200 156 }
michaeljkoster 13:41098c907200 157 }
samux 1:fb4494783863 158
michaeljkoster 13:41098c907200 159 void ESP8266::reset()
samux 1:fb4494783863 160 {
samux 1:fb4494783863 161 reset_pin = 0;
samux 1:fb4494783863 162 wait(0.2);
samux 1:fb4494783863 163 reset_pin = 1;
michaeljkoster 24:03585a13ff3b 164 wait(1);
michaeljkoster 24:03585a13ff3b 165
michaeljkoster 24:03585a13ff3b 166 //send("+++",3);
michaeljkoster 24:03585a13ff3b 167 //wait(1);
michaeljkoster 24:03585a13ff3b 168 state.cmdMode = true;
michaeljkoster 24:03585a13ff3b 169 sendCommand("AT", "OK", NULL, 1000);
michaeljkoster 24:03585a13ff3b 170 //sendCommand("AT+RST", "ready", NULL, 10000);
michaeljkoster 24:03585a13ff3b 171 state.associated = false;
michaeljkoster 24:03585a13ff3b 172
samux 1:fb4494783863 173 }
samux 1:fb4494783863 174
michaeljkoster 13:41098c907200 175 bool ESP8266::reboot()
samux 3:9aa05e19c62e 176 {
michaeljkoster 16:3f0efaa57a12 177 reset();
samux 3:9aa05e19c62e 178 return true;
samux 3:9aa05e19c62e 179 }
samux 3:9aa05e19c62e 180
michaeljkoster 15:37a7a56a424f 181 void ESP8266::handler_rx(void)
samux 1:fb4494783863 182 {
michaeljkoster 15:37a7a56a424f 183 //read characters
michaeljkoster 16:3f0efaa57a12 184 char c;
michaeljkoster 16:3f0efaa57a12 185 while (wifi.readable()){
michaeljkoster 16:3f0efaa57a12 186 c=wifi.getc();
michaeljkoster 16:3f0efaa57a12 187 buf_ESP8266.queue(c);
michaeljkoster 26:0d5bcb3903e2 188 //if (state.cmdMode) pc.printf("%c",c); //debug echo, needs fast serial console to prevent UART overruns
michaeljkoster 16:3f0efaa57a12 189 }
michaeljkoster 15:37a7a56a424f 190 }
michaeljkoster 15:37a7a56a424f 191
michaeljkoster 15:37a7a56a424f 192 void ESP8266::attach_rx(bool callback)
michaeljkoster 15:37a7a56a424f 193 {
michaeljkoster 15:37a7a56a424f 194 if (!callback)
michaeljkoster 15:37a7a56a424f 195 wifi.attach(NULL);
michaeljkoster 15:37a7a56a424f 196 else
michaeljkoster 15:37a7a56a424f 197 wifi.attach(this, &ESP8266::handler_rx);
samux 1:fb4494783863 198 }
samux 1:fb4494783863 199
michaeljkoster 13:41098c907200 200 int ESP8266::readable()
samux 1:fb4494783863 201 {
michaeljkoster 13:41098c907200 202 return buf_ESP8266.available();
samux 1:fb4494783863 203 }
samux 1:fb4494783863 204
michaeljkoster 13:41098c907200 205 int ESP8266::writeable()
samux 1:fb4494783863 206 {
samux 1:fb4494783863 207 return wifi.writeable();
samux 1:fb4494783863 208 }
samux 1:fb4494783863 209
michaeljkoster 13:41098c907200 210 char ESP8266::getc()
samux 1:fb4494783863 211 {
michaeljkoster 14:4d1128f72e00 212 char c=0;
michaeljkoster 13:41098c907200 213 while (!buf_ESP8266.available());
michaeljkoster 13:41098c907200 214 buf_ESP8266.dequeue(&c);
samux 1:fb4494783863 215 return c;
samux 1:fb4494783863 216 }
samux 1:fb4494783863 217
michaeljkoster 15:37a7a56a424f 218 int ESP8266::putc(char c)
samux 1:fb4494783863 219 {
michaeljkoster 20:d764237405c2 220 while (!wifi.writeable() || wifi.readable()); //wait for echoed command characters to be read first
michaeljkoster 15:37a7a56a424f 221 return wifi.putc(c);
samux 1:fb4494783863 222 }
samux 1:fb4494783863 223
michaeljkoster 15:37a7a56a424f 224 void ESP8266::flush()
samux 1:fb4494783863 225 {
michaeljkoster 15:37a7a56a424f 226 buf_ESP8266.flush();
samux 1:fb4494783863 227 }
samux 1:fb4494783863 228
michaeljkoster 16:3f0efaa57a12 229 int ESP8266::send(const char * buf, int len)
michaeljkoster 15:37a7a56a424f 230 {
michaeljkoster 18:60422852e99c 231
michaeljkoster 16:3f0efaa57a12 232 const char* bufptr=buf;
michaeljkoster 16:3f0efaa57a12 233 for(int i=0; i<len; i++){
michaeljkoster 17:d11fa4c3ac65 234 putc((int)*bufptr++);
michaeljkoster 18:60422852e99c 235 }
michaeljkoster 19:fb8d5bff2076 236 return len;
michaeljkoster 15:37a7a56a424f 237 }
samux 1:fb4494783863 238
michaeljkoster 16:3f0efaa57a12 239 bool ESP8266::sendCommand(const char * cmd, const char * ACK, char * res, int timeout)
samux 1:fb4494783863 240 {
samux 1:fb4494783863 241 char read;
samux 1:fb4494783863 242 size_t found = string::npos;
samux 1:fb4494783863 243 string checking;
samux 1:fb4494783863 244 Timer tmr;
samux 1:fb4494783863 245 int result = 0;
samux 1:fb4494783863 246
michaeljkoster 17:d11fa4c3ac65 247 //pc.printf("will send: %s\r\n",cmd);
samux 1:fb4494783863 248
michaeljkoster 17:d11fa4c3ac65 249 attach_rx(true);
samux 1:fb4494783863 250
samux 1:fb4494783863 251 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 252 while (readable())
michaeljkoster 17:d11fa4c3ac65 253 getc();
michaeljkoster 16:3f0efaa57a12 254
samux 1:fb4494783863 255 if (!ACK || !strcmp(ACK, "NO")) {
michaeljkoster 17:d11fa4c3ac65 256 for (int i = 0; i < strlen(cmd); i++){
michaeljkoster 16:3f0efaa57a12 257 result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
michaeljkoster 20:d764237405c2 258 wait(.005); // prevents stuck recieve ready (?) need to let echoed character get read first
michaeljkoster 16:3f0efaa57a12 259 }
michaeljkoster 16:3f0efaa57a12 260 putc(13); //CR
michaeljkoster 20:d764237405c2 261 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 262 putc(10); //LF
michaeljkoster 16:3f0efaa57a12 263
samux 1:fb4494783863 264 } else {
samux 1:fb4494783863 265 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 266 while (readable())
michaeljkoster 17:d11fa4c3ac65 267 getc();
samux 1:fb4494783863 268
samux 1:fb4494783863 269 tmr.start();
michaeljkoster 17:d11fa4c3ac65 270 for (int i = 0; i < strlen(cmd); i++){
michaeljkoster 16:3f0efaa57a12 271 result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
michaeljkoster 20:d764237405c2 272 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 273 }
michaeljkoster 16:3f0efaa57a12 274 putc(13); //CR
michaeljkoster 20:d764237405c2 275 wait(.005); // wait for echo
michaeljkoster 16:3f0efaa57a12 276 putc(10); //LF
samux 1:fb4494783863 277
samux 1:fb4494783863 278 while (1) {
samux 1:fb4494783863 279 if (tmr.read_ms() > timeout) {
samux 1:fb4494783863 280 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 281 while (readable())
michaeljkoster 17:d11fa4c3ac65 282 getc();
samux 1:fb4494783863 283
samux 1:fb4494783863 284 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 285
samux 1:fb4494783863 286 attach_rx(true);
samux 1:fb4494783863 287 return -1;
michaeljkoster 17:d11fa4c3ac65 288 } else if (readable()) {
michaeljkoster 17:d11fa4c3ac65 289 read = getc();
michaeljkoster 26:0d5bcb3903e2 290 pc.printf("%c",read); //debug echo
samux 1:fb4494783863 291 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 292 checking += read;
samux 1:fb4494783863 293 found = checking.find(ACK);
samux 1:fb4494783863 294 if (found != string::npos) {
samux 1:fb4494783863 295 wait(0.01);
samux 1:fb4494783863 296
samux 1:fb4494783863 297 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 298 while (readable())
michaeljkoster 26:0d5bcb3903e2 299 read = getc();
michaeljkoster 26:0d5bcb3903e2 300 pc.printf("%c",read); //debug echo
samux 1:fb4494783863 301 break;
samux 1:fb4494783863 302 }
samux 1:fb4494783863 303 }
samux 1:fb4494783863 304 }
samux 1:fb4494783863 305 }
samux 1:fb4494783863 306 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 307
samux 1:fb4494783863 308 attach_rx(true);
samux 1:fb4494783863 309 return result;
samux 1:fb4494783863 310 }
samux 1:fb4494783863 311
samux 1:fb4494783863 312 //the user wants the result from the command (ACK == NULL, res != NULL)
samux 1:fb4494783863 313 if ( res != NULL) {
samux 1:fb4494783863 314 int i = 0;
samux 1:fb4494783863 315 Timer timeout;
samux 1:fb4494783863 316 timeout.start();
samux 1:fb4494783863 317 tmr.reset();
samux 1:fb4494783863 318 while (1) {
samux 1:fb4494783863 319 if (timeout.read() > 2) {
samux 1:fb4494783863 320 if (i == 0) {
samux 1:fb4494783863 321 res = NULL;
samux 1:fb4494783863 322 break;
samux 1:fb4494783863 323 }
samux 1:fb4494783863 324 res[i] = '\0';
samux 1:fb4494783863 325 DBG("user str 1: %s\r\n", res);
samux 1:fb4494783863 326
samux 1:fb4494783863 327 break;
samux 1:fb4494783863 328 } else {
samux 1:fb4494783863 329 if (tmr.read_ms() > 300) {
samux 1:fb4494783863 330 res[i] = '\0';
samux 1:fb4494783863 331 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 332
samux 1:fb4494783863 333 break;
samux 1:fb4494783863 334 }
michaeljkoster 17:d11fa4c3ac65 335 if (readable()) {
samux 1:fb4494783863 336 tmr.start();
michaeljkoster 17:d11fa4c3ac65 337 read = getc();
samux 1:fb4494783863 338
samux 1:fb4494783863 339 // we drop \r and \n
samux 1:fb4494783863 340 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 341 res[i++] = read;
samux 1:fb4494783863 342 }
samux 1:fb4494783863 343 }
samux 1:fb4494783863 344 }
samux 1:fb4494783863 345 }
samux 1:fb4494783863 346 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 347 }
samux 1:fb4494783863 348
samux 1:fb4494783863 349 //We flush the buffer
michaeljkoster 17:d11fa4c3ac65 350 while (readable())
michaeljkoster 17:d11fa4c3ac65 351 getc();
samux 1:fb4494783863 352
samux 1:fb4494783863 353 attach_rx(true);
samux 1:fb4494783863 354 DBG("result: %d\r\n", result)
samux 1:fb4494783863 355 return result;
michaeljkoster 15:37a7a56a424f 356 }