├── .gitattributes ├── .gitignore ├── ESP8266_TCP.cpp ├── ESP8266_TCP.h ├── README.md ├── examples ├── Enable_Access_Point │ └── Enable_Access_Point.ino ├── Light_Station_Client_Arduino_Uno │ └── Light_Station_Client_Arduino_Uno.ino ├── Light_Station_Server_IPST_SE │ └── Light_Station_Server_IPST_SE.ino ├── Once_TCP_Client │ └── Once_TCP_Client.ino ├── Once_TCP_Server │ └── Once_TCP_Server.ino ├── Simple_TCP_Client │ └── Simple_TCP_Client.ino ├── Simple_TCP_Server │ └── Simple_TCP_Server.ino ├── WiFi_Connection │ └── WiFi_Connection.ino └── WiFi_Connection_Loop │ └── WiFi_Connection_Loop.ino └── keywords.txt /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # ========================= 18 | # Operating System Files 19 | # ========================= 20 | 21 | # OSX 22 | # ========================= 23 | 24 | .DS_Store 25 | .AppleDouble 26 | .LSOverride 27 | 28 | # Icon must end with two \r 29 | Icon 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /ESP8266_TCP.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | WiFlyTCP.h - WiFly Library for Arduino 3 | */ 4 | 5 | 6 | #include "ESP8266_TCP.h" 7 | 8 | /* 9 | * two-wire constructor. 10 | * Sets which wires should control the motor. 11 | */ 12 | ESP8266_TCP::ESP8266_TCP() { 13 | 14 | } 15 | 16 | void ESP8266_TCP::begin(Stream *serial, Stream *serialDebug, int pinReset) 17 | { 18 | pinMode(pinReset, OUTPUT); 19 | digitalWrite(pinReset, HIGH); 20 | this->pinReset = pinReset; 21 | this->serial = serial; 22 | this->serialDebug = serialDebug; 23 | this->isDebug = true; 24 | this->TCPConnected = false; 25 | this->clientId = -1; 26 | this->clientMessage = ""; 27 | this->runningState = WIFI_STATE_UNAVAILABLE; 28 | delay(2000); 29 | //debug.begin(serialport); 30 | //this->baudrate = baudrate; // which step the motor is on 31 | //this->serialport = serialport; 32 | //this->serialport.begin(serialport); 33 | } 34 | 35 | void ESP8266_TCP::begin(Stream *serial, int pinReset) 36 | { 37 | pinMode(pinReset, OUTPUT); 38 | digitalWrite(pinReset, HIGH); 39 | this->pinReset = pinReset; 40 | this->serial = serial; 41 | this->isDebug = false; 42 | this->TCPConnected = false; 43 | this->clientId = -1; 44 | this->clientMessage = ""; 45 | this->runningState = WIFI_STATE_UNAVAILABLE; 46 | delay(2000); 47 | //debug.begin(serialport); 48 | //this->baudrate = baudrate; // which step the motor is on 49 | //this->serialport = serialport; 50 | //this->serialport.begin(serialport); 51 | } 52 | 53 | bool ESP8266_TCP::test() { 54 | clearBuffer(); 55 | write("AT"); 56 | String data = readData(); 57 | debugPrintln(data); 58 | if(data.equals("")) { 59 | debugPrintln("RESET"); 60 | hardReset(); 61 | return true; 62 | } 63 | data = readData(); 64 | debugPrintln(data); 65 | if(data.equals("busy now ...")) { 66 | debugPrintln("RESET"); 67 | hardReset(); 68 | return true; 69 | } 70 | return readData().equals("OK"); 71 | } 72 | 73 | void ESP8266_TCP::reset() { 74 | clearBuffer(); 75 | write("AT+RST"); 76 | waitingForReset(); 77 | } 78 | 79 | void ESP8266_TCP::hardReset() { 80 | digitalWrite(this->pinReset, LOW); 81 | delay(1000); 82 | digitalWrite(this->pinReset, HIGH); 83 | waitingForHardReset(); 84 | } 85 | 86 | void ESP8266_TCP::waitingForReset() { 87 | while(true) { 88 | if(available() > 0) { 89 | String data = readData(1000); 90 | debugPrintln(data); 91 | if(data.substring(1, 4).equals("ets")) { 92 | debugPrintln("RESET"); 93 | delay(2000); 94 | clearBuffer(); 95 | return; 96 | } 97 | } 98 | } 99 | } 100 | 101 | void ESP8266_TCP::waitingForReset(unsigned long timeout) { 102 | unsigned long t = millis(); 103 | while(millis() - t < timeout) { 104 | if(available() > 0) { 105 | String data = readData(1000); 106 | if(data.substring(1, 4).equals("ets")) { 107 | debugPrintln("RESET"); 108 | delay(2000); 109 | clearBuffer(); 110 | return; 111 | } 112 | } 113 | } 114 | } 115 | 116 | void ESP8266_TCP::waitingForHardReset() { 117 | unsigned long t = millis(); 118 | while(true) { 119 | if(available() > 0) { 120 | String data = readData(1000); 121 | if(data.equals("ready")) { 122 | debugPrintln("RESET"); 123 | delay(2000); 124 | clearBuffer(); 125 | return; 126 | } 127 | } 128 | } 129 | } 130 | 131 | String ESP8266_TCP::connectAccessPoint(String ssid, String pass) { 132 | setMode(WIFI_MODE_STATION); 133 | write("AT+CWJAP=\"" + ssid + "\",\"" + pass + "\""); 134 | delay(2000); 135 | debugPrintln(readData()); 136 | debugPrintln(readData()); 137 | debugPrintln(readData()); 138 | waitingForReset(1000); 139 | String ip = waitingForJoin(); 140 | debugPrintln(readData()); 141 | debugPrintln(ip); 142 | return ip; 143 | } 144 | 145 | String ESP8266_TCP::waitingForJoin() { 146 | unsigned long timeout = 20000; 147 | unsigned long t = millis(); 148 | while(millis() - t < timeout) { 149 | clearBuffer(); 150 | write("AT+CIFSR"); 151 | readData(); 152 | String data = readData(); 153 | if(!data.equals("") && !data.equals("ERROR") && !data.equals("busy now ...")) { 154 | debugPrintln(data); 155 | return data; 156 | } 157 | debugPrint("."); 158 | delay(500); 159 | } 160 | return "0.0.0.0"; 161 | } 162 | 163 | bool ESP8266_TCP::setMode(int mode) { 164 | clearBuffer(); 165 | write("AT+CWMODE=" + String(mode)); 166 | delay(500); 167 | debugPrintln(readData()); 168 | String data = readData(); 169 | if(data.equals("no change")) { 170 | debugPrintln("No Change"); 171 | return true; 172 | } else if(data.equals("OK")) { 173 | reset(); 174 | debugPrintln("OK"); 175 | return true; 176 | } 177 | data = readData(); 178 | debugPrintln(data); 179 | if(data.equals("OK")) { 180 | reset(); 181 | debugPrintln("OK"); 182 | return true; 183 | } 184 | return false; 185 | } 186 | 187 | bool ESP8266_TCP::setAP(String ssid, String pass, int channel) { 188 | clearBuffer(); 189 | write("AT+CWSAP=\"" + ssid + "\",\"" + pass + "\"," + String(channel) + ",4"); 190 | debugPrintln(readData()); 191 | delay(4000); 192 | debugPrintln(readData()); 193 | String data = readData(); 194 | debugPrintln(data); 195 | return data.equals("OK"); 196 | } 197 | 198 | bool ESP8266_TCP::isNewAPSetting(String ssid, String pass, int channel) { 199 | clearBuffer(); 200 | write("AT+CWSAP?"); 201 | debugPrintln(readData()); 202 | String data = readData(); 203 | debugPrintln(data); 204 | data = data.substring(8, data.length()); 205 | 206 | byte ssidLength = findChar(data, 0, '\"'); 207 | String ssidTmp = data.substring(0, ssidLength); 208 | byte passLength = findChar(data, ssidLength + 3, '\"'); 209 | String passTmp = data.substring(ssidLength + 3, passLength); 210 | byte channelLength = findChar(data, passLength + 2, ','); 211 | int channelTmp = data.substring(passLength + 2, channelLength).toInt(); 212 | 213 | if(ssidTmp.equals(ssid) && passTmp.equals(pass) && channelTmp == channel) { 214 | return true; 215 | } 216 | return false; 217 | } 218 | 219 | bool ESP8266_TCP::setMux(int mux) { 220 | clearBuffer(); 221 | write("AT+CIPMUX=" + String(mux)); 222 | debugPrintln(readData()); 223 | String data = readData(); 224 | debugPrintln(data); 225 | if(data.equals("no change") || data.equals("OK")) 226 | return true; 227 | data = readData(); 228 | debugPrintln(data); 229 | return data.equals("OK"); 230 | } 231 | 232 | bool ESP8266_TCP::enableTCPServer(int port) { 233 | clearBuffer(); 234 | write("AT+CIPSERVER=1," + String(port)); 235 | debugPrintln(readData()); 236 | String data = readData(); 237 | debugPrintln(data); 238 | if(data.equals("no change")) 239 | return true; 240 | data = readData(); 241 | debugPrintln(data); 242 | return data.equals("OK"); 243 | } 244 | 245 | void ESP8266_TCP::openAccessPoint(String ssid, String pass, int channel) { 246 | if(!isNewAPSetting(ssid, pass, channel)) { 247 | debugPrintln("SET NEW AP"); 248 | setAP(ssid, pass, channel); 249 | reset(); 250 | } 251 | setMode(WIFI_MODE_AP); 252 | } 253 | 254 | bool ESP8266_TCP::closeTCPServer() { 255 | clearBuffer(); 256 | write("AT+CIPSERVER=0"); 257 | debugPrintln(readData()); 258 | debugPrintln(readData()); 259 | String data = readData(); 260 | debugPrintln(data); 261 | return data.equals("OK"); 262 | } 263 | /* 264 | bool ESP8266_TCP::isTCPEnabled() { 265 | clearBuffer(); 266 | write("AT+CIPMUX?"); 267 | debugPrintln(readData()); 268 | String data = readData(); 269 | debugPrintln(data); 270 | return data.equals("+CIPMUX:1"); 271 | }*/ 272 | 273 | void ESP8266_TCP::closeTCPConnection() { 274 | delay(1000); 275 | clearBuffer(); 276 | write("AT+CIPCLOSE"); 277 | debugPrintln(readData()); 278 | } 279 | 280 | void ESP8266_TCP::closeTCPConnection(int id) { 281 | delay(1000); 282 | clearBuffer(); 283 | write("AT+CIPCLOSE=" + String(id)); 284 | debugPrintln(readData()); 285 | } 286 | 287 | void ESP8266_TCP::openTCPServer(int port, int timeout) { 288 | setMux(WIFI_MUX_MULTI); 289 | enableTCPServer(port); 290 | setTCPTimeout(timeout); 291 | } 292 | 293 | bool ESP8266_TCP::setTCPTimeout(int timeout) { 294 | clearBuffer(); 295 | write("AT+CIPSTO=" + String(timeout)); 296 | debugPrintln(readData()); 297 | debugPrintln(readData()); 298 | String data = readData(); 299 | debugPrintln(data); 300 | return data.equals("OK"); 301 | } 302 | 303 | void ESP8266_TCP::connectTCP(String ip, int port) { 304 | clearBuffer(); 305 | write("AT+CIPSTART=\"TCP\",\"" + ip + "\"," + String(port) ); 306 | debugPrintln(readData()); 307 | 308 | setRunningState(WIFI_STATE_CONNECT); 309 | } 310 | 311 | void ESP8266_TCP::waitingForTCPConnection() { 312 | 313 | } 314 | 315 | void ESP8266_TCP::setRunningState(int state) { 316 | this->runningState = state; 317 | } 318 | 319 | int ESP8266_TCP::getRunningState() { 320 | return this->runningState; 321 | } 322 | 323 | void ESP8266_TCP::flush() { 324 | this->serial->flush(); 325 | } 326 | 327 | int ESP8266_TCP::isNewDataComing(byte type) { 328 | if(type == WIFI_CLIENT) { 329 | String data = read(); 330 | if(!data.equals("")) { 331 | if(data.substring(2, 6).equals("+IPD")) { 332 | int lastPosition = findChar(data, 7, ':'); 333 | int length = data.substring(7, lastPosition).toInt() - 1; 334 | lastPosition += 2; 335 | this->clientMessage = data.substring(lastPosition, lastPosition + length); 336 | return WIFI_NEW_MESSAGE; 337 | } else if(data.substring(3, 6).equals("ets")) { 338 | //debugPrintln("RESET"); 339 | delay(2000); 340 | clear(); 341 | return WIFI_NEW_RESET; 342 | } else if(data.substring(6, 12).equals("Unlink") 343 | || (data.substring(9, 15).equals("Unlink") && data.substring(2, 7).equals("ERROR"))) { 344 | //debugPrintln("Disconnected"); 345 | this->TCPConnected = false; 346 | setRunningState(WIFI_STATE_UNAVAILABLE); 347 | return WIFI_NEW_DISCONNECTED; 348 | } else if(data.substring(2, 4).equals("OK") && data.substring(6, 12).equals("Linked")) { 349 | setRunningState(WIFI_STATE_IDLE); 350 | //debugPrintln("Connected"); 351 | this->TCPConnected = true; 352 | return WIFI_NEW_CONNECTED; 353 | } else if(data.substring(2, 9).equals("SEND OK")) { 354 | setRunningState(WIFI_STATE_IDLE); 355 | //debugPrintln("Sent!!"); 356 | return WIFI_NEW_SEND_OK; 357 | } else if(data.substring(0, 14).equals("ALREAY CONNECT") 358 | && data.substring(16, 17).equals("OK") 359 | && data.substring(19, 24).equals("Unlink")) { 360 | this->TCPConnected = false; 361 | setRunningState(WIFI_STATE_UNAVAILABLE); 362 | return WIFI_NEW_DISCONNECTED; 363 | } else if(data.substring(0, 14).equals("ALREAY CONNECT")) { 364 | return WIFI_NEW_ALREADY_CONNECT; 365 | } else { 366 | //debugPrintln("******"); 367 | //debugPrintln(data); 368 | this->clientMessage = data; 369 | return WIFI_NEW_ETC; 370 | } 371 | } 372 | return WIFI_NEW_NONE; 373 | } else if(type == WIFI_SERVER) { 374 | String data = read(); 375 | if(!data.equals("")) { 376 | if(data.substring(2, 6).equals("+IPD")) { 377 | this->clientId = data.substring(7, 8).toInt(); 378 | int lastPosition = findChar(data, 9, ':'); 379 | int length = data.substring(9, lastPosition).toInt() + 1; 380 | 381 | if(data.charAt(lastPosition + 1) == '\n') { 382 | this->clientMessage = data.substring(lastPosition + 2, lastPosition + length + 1); 383 | } else { 384 | this->clientMessage = data.substring(lastPosition + 1, lastPosition + length); 385 | } 386 | 387 | //this->clientMessage = data.substring(lastPosition + 1, lastPosition + length); 388 | return WIFI_NEW_MESSAGE; 389 | } else if(data.substring(3, 6).equals("ets")) { 390 | //debugPrintln("RESET"); 391 | delay(2000); 392 | clear(); 393 | return WIFI_NEW_RESET; 394 | } else if((data.length() == 1 && data.charAt(0) == 0x0A) 395 | || (data.substring(2, 4).equals("OK") && data.substring(6, 12).equals("Unlink"))) { 396 | //debugPrintln("Disconnected"); 397 | this->TCPConnected = false; 398 | setRunningState(WIFI_STATE_UNAVAILABLE); 399 | return WIFI_NEW_DISCONNECTED; 400 | } else if(data.substring(0, 4).equals("Link") 401 | || data.substring(2, 4).equals("Link") 402 | || data.substring(5, 11).equals("Link")) { 403 | setRunningState(WIFI_STATE_IDLE); 404 | //debugPrintln("Connected"); 405 | this->TCPConnected = true; 406 | return WIFI_NEW_CONNECTED; 407 | } else if(data.substring(2, 9).equals("SEND OK")) { 408 | delay(2000); 409 | setRunningState(WIFI_STATE_IDLE); 410 | //debugPrintln("Sent!!"); 411 | return WIFI_NEW_SEND_OK; 412 | } else { 413 | //debugPrintln("******"); 414 | //debugPrintln(data); 415 | //debugPrintln(data.substring(2, 9)); 416 | debugPrintln("******"); 417 | //debugPrintln(data); 418 | this->clientMessage = data; 419 | debugPrintln(data.substring(2, 4)); 420 | debugPrintln(data.substring(6, 12)); 421 | return WIFI_NEW_ETC; 422 | } 423 | } 424 | return WIFI_NEW_NONE; 425 | 426 | /* 427 | String data = read(); 428 | if(!data.equals("")) { 429 | if(data.substring(2, 6).equals("+IPD")) { 430 | this->clientId = data.substring(7, 8).toInt(); 431 | int lastPosition = findChar(data, 9, ':'); 432 | int length = data.substring(9, lastPosition).toInt(); 433 | lastPosition += 1; 434 | this->clientMessage = data.substring(lastPosition + 1, lastPosition + length); 435 | return true; 436 | } else if(data.substring(3, 6).equals("ets")) { 437 | debugPrintln("RESET"); 438 | delay(2000); 439 | clear(); 440 | return true; 441 | } else if(data.substring(6,12).equals("Unlink")) { 442 | debugPrintln("Disconnected"); 443 | this->TCPConnected = false; 444 | return true; 445 | } else if(data.substring(0, 4).equals("Link")) { 446 | debugPrintln("Connected"); 447 | this->TCPConnected = true; 448 | //getClientList(); 449 | return true; 450 | } else { 451 | debugPrintln("ETC"); 452 | debugPrintln(data); 453 | debugPrintln(data.substring(0, 3)); 454 | return true; 455 | 456 | } 457 | } 458 | */ 459 | return false; 460 | } 461 | } 462 | 463 | int ESP8266_TCP::findChar(String str, int start, char c) { 464 | for(int i = start ; i < str.length() ; i++) { 465 | if(str.charAt(i) == c) 466 | return i; 467 | } 468 | return -1; 469 | } 470 | 471 | int ESP8266_TCP::getId() { 472 | return this->clientId; 473 | } 474 | 475 | String ESP8266_TCP::getMessage() { 476 | return this->clientMessage; 477 | } 478 | 479 | void ESP8266_TCP::clearBuffer() { 480 | while(available() > 0) { 481 | serial->read(); 482 | } 483 | } 484 | 485 | void ESP8266_TCP::clear() { 486 | clearBuffer(); 487 | this->clientId = -1; 488 | this->clientMessage = ""; 489 | this->TCPConnected = false; 490 | } 491 | 492 | void ESP8266_TCP::debugPrintln(String str) { 493 | if(this->isDebug) 494 | serialDebug->println(str); 495 | } 496 | 497 | void ESP8266_TCP::debugPrint(String str) { 498 | if(this->isDebug) 499 | serialDebug->print(str); 500 | } 501 | 502 | String ESP8266_TCP::readData() { 503 | String data = ""; 504 | while(available() > 0) { 505 | char r = serial->read(); 506 | if (r == '\n') { 507 | return data; 508 | } else if(r == '\r') { 509 | } else { 510 | data += r; 511 | } 512 | } 513 | return ""; 514 | } 515 | 516 | void ESP8266_TCP::write(String str) { 517 | this->serial->println(str); 518 | flush(); 519 | delay(50); 520 | } 521 | 522 | bool ESP8266_TCP::send(String message) { 523 | if(getRunningState() == WIFI_STATE_IDLE) { 524 | debugPrintln("Send : " + message); 525 | write("AT+CIPSEND=" + String(message.length() + 1)); 526 | serial->print(message + " "); 527 | flush(); 528 | debugPrintln(readData()); 529 | debugPrintln(readData()); 530 | setRunningState(WIFI_STATE_SEND); 531 | return true; 532 | } 533 | return false; 534 | } 535 | 536 | bool ESP8266_TCP::send(int id, String message) { 537 | if(getRunningState() == WIFI_STATE_IDLE) { 538 | debugPrintln("ID : " + String(id) + " Send : " + message); 539 | clearBuffer(); 540 | write("AT+CIPSEND=" + String(id) + "," + String(message.length())); 541 | serial->print(message); 542 | flush(); 543 | debugPrintln(readData()); 544 | debugPrintln(readData()); 545 | setRunningState(WIFI_STATE_SEND); 546 | return true; 547 | } 548 | return false; 549 | //waitForSendStatus(); 550 | } 551 | 552 | /* 553 | bool ESP8266_TCP::waitForSendStatus() { 554 | unsigned long timeout = 10000; 555 | unsigned long t = millis(); 556 | while(millis() - t < timeout) { 557 | if(available()) { 558 | String status = readData(100); 559 | if(status.equals("SEND OK") || status.equals("OK")) { 560 | return true; 561 | } else if(!status.equals("") && !status.substring(0, 2).equals("AT") 562 | && !status.substring(0, 1).equals(">")) { 563 | return false; 564 | } 565 | } 566 | } 567 | return false; 568 | }*/ 569 | 570 | void ESP8266_TCP::printClientList() { 571 | clearBuffer(); 572 | write("AT+CWLIF"); 573 | delay(1000); 574 | debugPrintln(readData(100)); 575 | debugPrintln(readData(100)); 576 | debugPrintln(readData(100)); 577 | debugPrintln(readData(100)); 578 | debugPrintln(readData(100)); 579 | debugPrintln(readData(100)); 580 | } 581 | 582 | int ESP8266_TCP::available() { 583 | return this->serial->available(); 584 | } 585 | 586 | String ESP8266_TCP::read() { 587 | String data = readTCPData(); 588 | if(data.equals("Unlink")) { 589 | this->TCPConnected = false; 590 | clearBuffer(); 591 | return ""; 592 | } 593 | return data; 594 | } 595 | 596 | String ESP8266_TCP::readData(unsigned long timeout) { 597 | String data = ""; 598 | unsigned long t = millis(); 599 | while(millis() - t < timeout) { 600 | if(available() > 0) { 601 | char r = serial->read(); 602 | if (r == '\n') { 603 | return data; 604 | } else if(r == '\r') { 605 | } else { 606 | data += r; 607 | t = millis(); 608 | } 609 | } 610 | } 611 | return ""; 612 | } 613 | 614 | String ESP8266_TCP::readTCPData() { 615 | unsigned long timeout = 100; 616 | unsigned long t = millis(); 617 | String data = ""; 618 | while(millis() - t < timeout) { 619 | if(available() > 0) { 620 | char r = serial->read(); 621 | if(data.equals("Unlink")) { 622 | return data; 623 | } else { 624 | data += r; 625 | t = millis(); 626 | } 627 | } 628 | } 629 | return data; 630 | } 631 | -------------------------------------------------------------------------------- /ESP8266_TCP.h: -------------------------------------------------------------------------------- 1 | /* 2 | WiFlyTCP.h - WiFly Library for Arduino 3 | */ 4 | // ensure this library description is only included once 5 | #ifndef _ESP8266_TCP_H_ 6 | #define _ESP8266_TCP_H_ 7 | 8 | 9 | #if defined(ARDUINO) && ARDUINO >= 100 10 | #include "Arduino.h" 11 | #else 12 | #include "WProgram.h" 13 | #endif 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #define WIFI_MODE_STATION 0x01 20 | #define WIFI_MODE_AP 0x02 21 | #define WIFI_MODE_BOTH 0x03 22 | 23 | #define WIFI_MUX_SINGLE 0x00 24 | #define WIFI_MUX_MULTI 0x01 25 | 26 | #define WIFI_TCP_DISABLE 0x00 27 | #define WIFI_TCP_ENABLE 0x01 28 | 29 | #define WIFI_CLIENT 0x00 30 | #define WIFI_SERVER 0x01 31 | 32 | #define WIFI_STATE_IDLE 0x00 33 | #define WIFI_STATE_UNAVAILABLE 0x01 34 | #define WIFI_STATE_SEND 0x02 35 | #define WIFI_STATE_CONNECT 0x03 36 | 37 | #define WIFI_NEW_NONE 0x00 38 | #define WIFI_NEW_MESSAGE 0x01 39 | #define WIFI_NEW_CONNECTED 0x02 40 | #define WIFI_NEW_DISCONNECTED 0x03 41 | #define WIFI_NEW_SEND_OK 0x04 42 | #define WIFI_NEW_SEND_ERROR 0x05 43 | #define WIFI_NEW_RESET 0x06 44 | #define WIFI_NEW_ALREADY_CONNECT 0x07 45 | #define WIFI_NEW_ETC 0x08 46 | 47 | 48 | // library interface description 49 | 50 | class ESP8266_TCP { 51 | 52 | public: 53 | 54 | ESP8266_TCP(); 55 | void begin(Stream *serial, Stream *serialDebug, int pinReset); 56 | void begin(Stream *serial, int pinReset); 57 | 58 | 59 | bool test(); 60 | void reset(); 61 | void hardReset(); 62 | 63 | bool closeTCPServer(); 64 | //bool isTCPEnabled(); 65 | void openTCPServer(int port, int timeout); 66 | void connectTCP(String ip, int port); 67 | void closeTCPConnection(); 68 | void closeTCPConnection(int id); 69 | 70 | int getRunningState(); 71 | 72 | int getId(); 73 | String getMessage(); 74 | void clearNewMessage(); 75 | 76 | String connectAccessPoint(String ssid, String pass); 77 | void openAccessPoint(String ssid, String pass, int channel); 78 | 79 | int isNewDataComing(byte type); 80 | 81 | bool send(String message); 82 | bool send(int id, String message); 83 | 84 | void printClientList(); 85 | 86 | private: 87 | 88 | void waitingForReset(); 89 | void waitingForReset(unsigned long timeout); 90 | void waitingForHardReset(); 91 | 92 | String waitingForJoin(); 93 | void waitingForTCPConnection(); 94 | 95 | bool setMode(int mode); 96 | bool setMux(int mux); 97 | 98 | bool enableTCPServer(int port); 99 | bool setTCPTimeout(int timeout); 100 | 101 | void flush(); 102 | 103 | //bool waitForSendStatus(); 104 | 105 | void setRunningState(int state); 106 | 107 | void debugPrintln(String str); 108 | void debugPrint(String str); 109 | 110 | bool setAP(String ssid, String pass, int channel); 111 | bool isNewAPSetting(String ssid, String pass, int channel); 112 | 113 | 114 | void clearBuffer(); 115 | int findChar(String str, int start, char c); 116 | void clear(); 117 | 118 | void write(String str); 119 | 120 | int available(); 121 | 122 | String read(); 123 | String readData(); 124 | String readData(unsigned long timeout); 125 | String readTCPData(); 126 | 127 | bool TCPConnected; 128 | bool TCPEnable; 129 | bool isDebug; 130 | 131 | int pinReset; 132 | 133 | int clientId; 134 | String clientMessage; 135 | 136 | Stream *serial; 137 | Stream *serialDebug; 138 | 139 | int runningState; 140 | }; 141 | 142 | #endif 143 | 144 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Arduino-ESP8266_libs 2 | ==================== 3 | 4 | Library that make easy to using ESP8266 Serial WiFi Module (Available only TCP now) 5 | 6 | RESET_PIN in this library is mean CH_PD pin 7 | 8 | (This is beta version now) 9 | -------------------------------------------------------------------------------- /examples/Enable_Access_Point/Enable_Access_Point.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Make an Access Point 4 | * 5 | */ 6 | 7 | #include 8 | 9 | // ESP8266 Class 10 | ESP8266_TCP wifi; 11 | 12 | // Define SSID, Password and Channel that provide for Access Point 13 | #define ssid "ESP_AP" 14 | #define pass "123456789" 15 | #define channel 5 16 | 17 | // Connect this pin to CH_PD pin on ESP8266 18 | #define PIN_RESET 6 19 | 20 | void setup() 21 | { 22 | delay(3000); 23 | 24 | // We use Serial1 to interface with ESP8266 25 | // and use Serial to debugging 26 | Serial.begin(9600); 27 | Serial1.begin(115200); 28 | wifi.begin(&Serial1, &Serial, PIN_RESET); 29 | 30 | /* If your board has only 1 serial port 31 | * or you didn't need to debugging, try this. 32 | * 33 | * Serial.begin(115200); 34 | * wifi.begin(&Serial, PIN_RESET); 35 | * 36 | */ 37 | 38 | // Check that ESP8266 is available 39 | if(wifi.test()) 40 | { 41 | // Open Access Point (WiFi Hotspot) 42 | wifi.openAccessPoint(ssid, pass, channel); 43 | } 44 | else 45 | { 46 | // ESP8266 isn't available 47 | Serial.println("Check module connection and restart to try again..."); 48 | } 49 | } 50 | 51 | void loop() 52 | { 53 | 54 | } 55 | -------------------------------------------------------------------------------- /examples/Light_Station_Client_Arduino_Uno/Light_Station_Client_Arduino_Uno.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | ESP8266_TCP wifi; 4 | 5 | #define ssid "ESP8266_AP" 6 | #define pass "00000000" 7 | 8 | #define serverIP "192.168.4.1" 9 | #define serverPort 2000 10 | 11 | #define PIN_RESET 2 12 | 13 | #define SW_0 13 14 | #define SW_1 12 15 | #define SW_2 11 16 | #define SW_3 10 17 | #define SW_4 9 18 | #define SW_5 8 19 | #define SW_6 7 20 | #define SW_7 6 21 | 22 | #define LED_0 A2 23 | #define LED_1 A3 24 | #define LED_2 A4 25 | #define LED_3 A5 26 | 27 | int sw_pin[8] = { 28 | SW_0, SW_1, SW_2, SW_3, SW_4, SW_5, SW_6, SW_7 }; 29 | 30 | boolean sw[8] = { 31 | false, false, false, false, false, false, false, false }; 32 | 33 | void setup() 34 | { 35 | delay(3000); 36 | 37 | pinMode(SW_0, INPUT); 38 | pinMode(SW_1, INPUT); 39 | pinMode(SW_2, INPUT); 40 | pinMode(SW_3, INPUT); 41 | pinMode(SW_4, INPUT); 42 | pinMode(SW_5, INPUT); 43 | pinMode(SW_6, INPUT); 44 | pinMode(SW_7, INPUT); 45 | 46 | pinMode(LED_0, OUTPUT); 47 | pinMode(LED_1, OUTPUT); 48 | pinMode(LED_2, OUTPUT); 49 | pinMode(LED_3, OUTPUT); 50 | 51 | digitalWrite(LED_0, HIGH); 52 | digitalWrite(LED_1, HIGH); 53 | digitalWrite(LED_2, HIGH); 54 | digitalWrite(LED_3, HIGH); 55 | 56 | Serial.begin(115200); 57 | wifi.begin(&Serial, PIN_RESET); 58 | 59 | if(wifi.test()) 60 | { 61 | digitalWrite(LED_0, LOW); 62 | String ip = connectAP(); 63 | digitalWrite(LED_0, HIGH); 64 | } 65 | else 66 | { 67 | digitalWrite(LED_3, LOW); 68 | while(true); 69 | } 70 | } 71 | 72 | void loop() 73 | { 74 | int dataState = wifi.isNewDataComing(WIFI_CLIENT); 75 | if(dataState != WIFI_NEW_NONE) { 76 | if(dataState == WIFI_NEW_CONNECTED) { 77 | digitalWrite(LED_1, LOW); 78 | } 79 | else if(dataState == WIFI_NEW_DISCONNECTED) { 80 | digitalWrite(LED_1, HIGH); 81 | } 82 | else if(dataState == WIFI_NEW_MESSAGE) { 83 | if(wifi.getMessage().equals("update")) { 84 | update(); 85 | } 86 | } 87 | else if(dataState == WIFI_NEW_SEND_OK) { 88 | digitalWrite(LED_2, HIGH); 89 | } 90 | } 91 | 92 | if(isSwitchChanged()) { 93 | update(); 94 | } 95 | 96 | if(wifi.getRunningState() == WIFI_STATE_UNAVAILABLE) { 97 | delay(500); 98 | wifi.connectTCP(serverIP, serverPort); 99 | delay(500); 100 | } 101 | 102 | delay(50); 103 | } 104 | 105 | boolean isSwitchChanged() { 106 | for(int i = 0 ; i < 8 ; i++) { 107 | if(digitalRead(sw_pin[i]) != sw[i]) { 108 | digitalWrite(LED_2, LOW); 109 | return true; 110 | } 111 | } 112 | return false; 113 | } 114 | 115 | void update() { 116 | String str = ""; 117 | for(int i = 0 ; i < 8 ; i++) { 118 | boolean state = digitalRead(sw_pin[i]); 119 | sw[i] = state; 120 | str += state; 121 | if(i < (8 - 1)) { 122 | str += ","; 123 | } 124 | } 125 | wifi.send(str); 126 | delay(500); 127 | } 128 | 129 | String connectAP() 130 | { 131 | String ip = "0.0.0.0"; 132 | while(ip.equals("0.0.0.0")) 133 | { 134 | ip = wifi.connectAccessPoint(ssid, pass); 135 | if(!ip.equals("0.0.0.0")) 136 | { 137 | break; 138 | } 139 | } 140 | return ip; 141 | } 142 | 143 | -------------------------------------------------------------------------------- /examples/Light_Station_Server_IPST_SE/Light_Station_Server_IPST_SE.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | ESP8266_TCP wifi; 5 | 6 | #define ssid "ESP8266_AP" 7 | #define pass "00000000" 8 | #define channel 5 9 | 10 | #define PIN_RESET 17 11 | 12 | void setup() 13 | { 14 | delay(3000); 15 | 16 | glcdClear(); 17 | glcdMode(3); 18 | for(int i = 0 ; i < 8 ; i++) { 19 | glcdFillCircle((20 * i) + 10 , 50, 5, GLCD_WHITE); 20 | } 21 | 22 | Serial.begin(9600); 23 | Serial1.begin(115200); 24 | wifi.begin(&Serial1, &Serial, PIN_RESET); 25 | 26 | if(wifi.test()) 27 | { 28 | wifi.openAccessPoint(ssid, pass, channel); 29 | //String ip = connectAP(); 30 | wifi.openTCPServer(2000, 30); 31 | } 32 | else 33 | { 34 | Serial.println("Check module connection and restart to try again..."); 35 | while(true); 36 | } 37 | } 38 | 39 | void loop() 40 | { 41 | int dataState = wifi.isNewDataComing(WIFI_SERVER); 42 | if(dataState != WIFI_NEW_NONE) { 43 | if(dataState == WIFI_NEW_CONNECTED) { 44 | wifi.send(0, "update"); 45 | } 46 | else if(dataState == WIFI_NEW_MESSAGE) { 47 | setDisplay(wifi.getMessage()); 48 | } 49 | } 50 | } 51 | 52 | String connectAP() 53 | { 54 | String ip = "0.0.0.0"; 55 | while(ip.equals("0.0.0.0")) 56 | { 57 | ip = wifi.connectAccessPoint(ssid, pass); 58 | if(!ip.equals("0.0.0.0")) 59 | { 60 | break; 61 | } 62 | } 63 | return ip; 64 | } 65 | 66 | void setDisplay(String str) { 67 | char state[8]; 68 | 69 | for(int i = 0 ; i < 8 ; i++) { 70 | state[i] = str.charAt(2 * i); 71 | } 72 | 73 | for(int i = 0 ; i < 8 ; i++) { 74 | glcdFillCircle((20 * i) + 10 , 50, 5, (state[i] == '1') ? GLCD_RED : GLCD_WHITE); 75 | } 76 | } 77 | 78 | 79 | -------------------------------------------------------------------------------- /examples/Once_TCP_Client/Once_TCP_Client.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Client side for Once Connection per message (Connect > Send > Close) 4 | * 5 | */ 6 | 7 | #include 8 | 9 | // ESP8266 Class 10 | ESP8266_TCP wifi; 11 | 12 | // Target Access Point 13 | #define ssid "ESP_AP" 14 | #define pass "123456789" 15 | 16 | // TCP Server IP and port 17 | #define serverIP "192.168.43.1" 18 | #define serverPort 2000 19 | 20 | // Connect this pin to CH_PD pin on ESP8266 21 | #define PIN_RESET 6 22 | 23 | // Pin that connected to button to send any message 24 | #define PIN_SEND 8 25 | 26 | void setup() 27 | { 28 | delay(3000); 29 | 30 | // Set pin for send command to input mode 31 | pinMode(PIN_SEND, INPUT); 32 | 33 | 34 | // We use Serial1 to interface with ESP8266 35 | // and use Serial to debugging 36 | Serial.begin(9600); 37 | Serial1.begin(115200); 38 | wifi.begin(&Serial1, &Serial, PIN_RESET); 39 | 40 | /* If your board has only 1 serial port 41 | * or you didn't need to debugging, try this. 42 | * 43 | * Serial.begin(115200); 44 | * wifi.begin(&Serial, PIN_RESET); 45 | * 46 | */ 47 | 48 | // Check that ESP8266 is available 49 | if(wifi.test()) 50 | { 51 | // Connect to target Access Point 52 | String ip = connectAP(); 53 | } 54 | else 55 | { 56 | // ESP8266 isn't available 57 | Serial.println("Check module connection and restart to try again..."); 58 | while(true); 59 | } 60 | } 61 | 62 | void loop() 63 | { 64 | // Check for any data has coming to ESP8266 65 | int dataState = wifi.isNewDataComing(WIFI_CLIENT); 66 | if(dataState != WIFI_NEW_NONE) { 67 | if(dataState == WIFI_NEW_CONNECTED) { 68 | // Connected with TCP Server Side 69 | // Send a message to TCP Server Side 70 | wifi.send("12345678"); 71 | } else if(dataState == WIFI_NEW_DISCONNECTED) { 72 | // Disconnected from TCP Server Side 73 | Serial.println("Disconnected"); 74 | } else if(dataState == WIFI_NEW_SEND_OK) { 75 | // Message transfer has successfully 76 | Serial.println("SENT!!!!"); 77 | } else if(dataState == WIFI_NEW_ALREADY_CONNECT) { 78 | // Already connected with TCP Server Side 79 | Serial.println("Already Connect!!"); 80 | } 81 | } 82 | 83 | // When button for connect to TCP Server Side was pressed 84 | if(!digitalRead(PIN_SEND)) { 85 | // Send message to TCP Server Side and waiting for 1 sec 86 | Serial.println("Connect!!"); 87 | wifi.connectTCP(serverIP, serverPort); 88 | delay(1000); 89 | } 90 | 91 | delay(50); 92 | } 93 | 94 | // Access Point Connection Function that you can loop connect to Access Point until successful 95 | String connectAP() 96 | { 97 | String ip = "0.0.0.0"; 98 | while(ip.equals("0.0.0.0")) 99 | { 100 | ip = wifi.connectAccessPoint(ssid, pass); 101 | if(!ip.equals("0.0.0.0")) 102 | { 103 | break; 104 | } 105 | } 106 | return ip; 107 | } 108 | -------------------------------------------------------------------------------- /examples/Once_TCP_Server/Once_TCP_Server.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Server side for Once Connection per message (Connect > read > Close) 4 | * 5 | */ 6 | 7 | #include 8 | 9 | // ESP8266 Class 10 | ESP8266_TCP wifi; 11 | 12 | // Target Access Point 13 | #define ssid "ESP_AP" 14 | #define pass "123456789" 15 | 16 | // Connect this pin to CH_PD pin on ESP8266 17 | #define PIN_RESET 6 18 | 19 | void setup() 20 | { 21 | delay(3000); 22 | 23 | // We use Serial1 to interface with ESP8266 24 | // and use Serial to debugging 25 | Serial.begin(9600); 26 | Serial1.begin(115200); 27 | wifi.begin(&Serial1, &Serial, PIN_RESET); 28 | 29 | /* If your board has only 1 serial port 30 | * or you didn't need to debugging, try this. 31 | * 32 | * Serial.begin(115200); 33 | * wifi.begin(&Serial, PIN_RESET); 34 | * 35 | */ 36 | 37 | // Check that ESP8266 is available 38 | if(wifi.test()) 39 | { 40 | // Connect to target Access Point 41 | String ip = connectAP(); 42 | 43 | // Open TCP Server on port 2000 and 30 seconds for connection timeout (Max 2880) 44 | wifi.openTCPServer(2000, 30); 45 | } 46 | else 47 | { 48 | // ESP8266 isn't available 49 | Serial.println("Check module connection and restart to try again..."); 50 | while(true); 51 | } 52 | } 53 | 54 | void loop() 55 | { 56 | // Check for any data has coming to ESP8266 57 | int dataState = wifi.isNewDataComing(WIFI_SERVER); 58 | if(dataState != WIFI_NEW_NONE) { 59 | if(dataState == WIFI_NEW_CONNECTED) { 60 | // Connected with TCP Client Side 61 | Serial.println("Status : Connected"); 62 | } else if(dataState == WIFI_NEW_DISCONNECTED) { 63 | // Disconnected from TCP Client Side 64 | Serial.println("Status : Disconnected"); 65 | } else if(dataState == WIFI_NEW_MESSAGE) { 66 | // Got a message from TCP Client Side 67 | Serial.println("ID : " + String(wifi.getId())); 68 | Serial.println("Message : " + wifi.getMessage()); 69 | wifi.closeTCPConnection(0); 70 | } else if(dataState == WIFI_NEW_SEND_OK) { 71 | // Message transfer has successful 72 | Serial.println("SENT!!!!"); 73 | } 74 | } 75 | } 76 | 77 | // Access Point Connection Function that you can loop connect to Access Point until successful 78 | String connectAP() 79 | { 80 | String ip = "0.0.0.0"; 81 | while(ip.equals("0.0.0.0")) 82 | { 83 | ip = wifi.connectAccessPoint(ssid, pass); 84 | if(!ip.equals("0.0.0.0")) 85 | { 86 | break; 87 | } 88 | } 89 | return ip; 90 | } 91 | 92 | -------------------------------------------------------------------------------- /examples/Simple_TCP_Client/Simple_TCP_Client.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Client side for Simple Connection (Always Connected) 4 | * 5 | */ 6 | 7 | #include 8 | 9 | // ESP8266 Class 10 | ESP8266_TCP wifi; 11 | 12 | // Target Access Point 13 | #define ssid "ESP_AP" 14 | #define pass "123456789" 15 | 16 | // TCP Server IP and port 17 | #define serverIP "192.168.43.96" 18 | #define serverPort 2000 19 | 20 | // Connect this pin to CH_PD pin on ESP8266 21 | #define PIN_RESET 6 22 | 23 | // Pin that connected to button to send any message 24 | #define PIN_SEND 8 25 | 26 | void setup() 27 | { 28 | delay(3000); 29 | 30 | // Set pin for send command to input mode 31 | pinMode(PIN_SEND, INPUT); 32 | 33 | // We use Serial1 to interface with ESP8266 34 | // and use Serial to debugging 35 | Serial.begin(9600); 36 | Serial1.begin(115200); 37 | wifi.begin(&Serial1, &Serial, PIN_RESET); 38 | 39 | /* If your board has only 1 serial port 40 | * or you didn't need to debugging, try this. 41 | * 42 | * Serial.begin(115200); 43 | * wifi.begin(&Serial, PIN_RESET); 44 | * 45 | */ 46 | 47 | // Check that ESP8266 is available 48 | if(wifi.test()) 49 | { 50 | // Connect to target Access Point 51 | String ip = connectAP(); 52 | } 53 | else 54 | { 55 | // Open TCP Server on port 2000 and 30 seconds for timeout 56 | Serial.println("Check module connection and restart to try again..."); 57 | while(true); 58 | } 59 | } 60 | 61 | void loop() 62 | { 63 | // Check for any data has coming to ESP8266 64 | int dataState = wifi.isNewDataComing(WIFI_CLIENT); 65 | if(dataState != WIFI_NEW_NONE) { 66 | if(dataState == WIFI_NEW_CONNECTED) { 67 | // Connected with TCP Server Side 68 | Serial.println("Connected"); 69 | } else if(dataState == WIFI_NEW_DISCONNECTED) { 70 | // Disconnected from TCP Server Side 71 | Serial.println("Disconnected"); 72 | } else if(dataState == WIFI_NEW_MESSAGE) { 73 | // Got a message from TCP Server Side 74 | Serial.println("Message : " + wifi.getMessage()); 75 | } else if(dataState == WIFI_NEW_SEND_OK) { 76 | // Message transfer has successful 77 | Serial.println("SENT!!!!"); 78 | } 79 | } 80 | 81 | // When button for send message was pressed 82 | if(!digitalRead(PIN_SEND)) { 83 | // Send a message 84 | wifi.send("12345678"); 85 | delay(1000); 86 | } 87 | 88 | // Auto connect to TCP Server Side when connection timeout 89 | if(wifi.getRunningState() == WIFI_STATE_UNAVAILABLE) { 90 | // Connect to TCP Server Side 91 | Serial.println("Connect!!"); 92 | delay(500); 93 | wifi.connectTCP(serverIP, serverPort); 94 | delay(500); 95 | } 96 | 97 | delay(50); 98 | } 99 | 100 | // Access Point Connection Function that you can loop connect to Access Point until successful 101 | String connectAP() 102 | { 103 | String ip = "0.0.0.0"; 104 | while(ip.equals("0.0.0.0")) 105 | { 106 | ip = wifi.connectAccessPoint(ssid, pass); 107 | if(!ip.equals("0.0.0.0")) 108 | { 109 | break; 110 | } 111 | } 112 | return ip; 113 | } 114 | -------------------------------------------------------------------------------- /examples/Simple_TCP_Server/Simple_TCP_Server.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Server side for Simple Connection (Always Connected) 4 | * 5 | */ 6 | 7 | #include 8 | 9 | // ESP8266 Class 10 | ESP8266_TCP wifi; 11 | 12 | // Target Access Point 13 | #define ssid "ESP_AP" 14 | #define pass "123456789" 15 | 16 | // Connect this pin to CH_PD pin on ESP8266 17 | #define PIN_RESET 6 18 | 19 | void setup() 20 | { 21 | delay(3000); 22 | 23 | // We use Serial1 to interface with ESP8266 24 | // and use Serial to debugging 25 | Serial.begin(9600); 26 | Serial1.begin(115200); 27 | wifi.begin(&Serial1, &Serial, PIN_RESET); 28 | 29 | /* If your board has only 1 serial port 30 | * or you didn't need to debugging, try this. 31 | * 32 | * Serial.begin(115200); 33 | * wifi.begin(&Serial, PIN_RESET); 34 | * 35 | */ 36 | 37 | // Check that ESP8266 is available 38 | if(wifi.test()) 39 | { 40 | // Connect to target Access Point 41 | String ip = connectAP(); 42 | 43 | // Open TCP Server on port 2000 and 30 seconds for connection timeout (Max 2880) 44 | wifi.openTCPServer(2000, 30); 45 | } 46 | else 47 | { 48 | // ESP8266 isn't available 49 | Serial.println("Check module connection and restart to try again..."); 50 | while(true); 51 | } 52 | } 53 | 54 | void loop() 55 | { 56 | // Check for any data has coming to ESP8266 57 | int dataState = wifi.isNewDataComing(WIFI_SERVER); 58 | if(dataState != WIFI_NEW_NONE) { 59 | if(dataState == WIFI_NEW_CONNECTED) { 60 | // Connected with TCP Client Side 61 | Serial.println("Status : Connected"); 62 | } else if(dataState == WIFI_NEW_DISCONNECTED) { 63 | // Disconnected from TCP Client Side 64 | Serial.println("Status : Disconnected"); 65 | } else if(dataState == WIFI_NEW_MESSAGE) { 66 | // Got a message from TCP Client Side 67 | Serial.println("ID : " + String(wifi.getId())); 68 | Serial.println("Message : " + wifi.getMessage()); 69 | } else if(dataState == WIFI_NEW_SEND_OK) { 70 | // Message transfer has successful 71 | Serial.println("SENT!!!!"); 72 | } 73 | } 74 | } 75 | 76 | // Access Point Connection Function that you can loop connect to Access Point until successful 77 | String connectAP() 78 | { 79 | String ip = "0.0.0.0"; 80 | while(ip.equals("0.0.0.0")) 81 | { 82 | ip = wifi.connectAccessPoint(ssid, pass); 83 | if(!ip.equals("0.0.0.0")) 84 | { 85 | break; 86 | } 87 | } 88 | return ip; 89 | } 90 | 91 | -------------------------------------------------------------------------------- /examples/WiFi_Connection/WiFi_Connection.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Once connect to target Access Point 4 | * 5 | */ 6 | 7 | #include 8 | 9 | // ESP8266 Class 10 | ESP8266_TCP wifi; 11 | 12 | // Target Access Point 13 | #define ssid "ESP_AP" 14 | #define pass "123456789" 15 | 16 | // Connect this pin to CH_PD pin on ESP8266 17 | #define PIN_RESET 6 18 | 19 | void setup() 20 | { 21 | delay(3000); 22 | 23 | // We use Serial1 to interface with ESP8266 24 | // and use Serial to debugging 25 | Serial.begin(9600); 26 | Serial1.begin(115200); 27 | wifi.begin(&Serial1, &Serial, PIN_RESET); 28 | 29 | /* If your board has only 1 serial port 30 | * or you didn't need to debugging, try this. 31 | * 32 | * Serial.begin(115200); 33 | * wifi.begin(&Serial, PIN_RESET); 34 | * 35 | */ 36 | 37 | // Check that ESP8266 is available 38 | if(wifi.test()) 39 | { 40 | // Connect to Access Point 41 | wifi.connectAccessPoint(ssid, pass); 42 | } 43 | else 44 | { 45 | // ESP8266 isn't available 46 | Serial.println("Check module connection and restart to try again..."); 47 | } 48 | } 49 | 50 | void loop() 51 | { 52 | 53 | } 54 | 55 | -------------------------------------------------------------------------------- /examples/WiFi_Connection_Loop/WiFi_Connection_Loop.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Reconnect to target Access Point until successful 4 | * 5 | */ 6 | 7 | #include 8 | 9 | // ESP8266 Class 10 | ESP8266_TCP wifi; 11 | 12 | // Target Access Point 13 | #define ssid "ESP_AP" 14 | #define pass "123456789" 15 | 16 | // Connect this pin to CH_PD pin on ESP8266 17 | #define PIN_RESET 6 18 | 19 | void setup() 20 | { 21 | delay(3000); 22 | 23 | // We use Serial1 to interface with ESP8266 24 | // and use Serial to debugging 25 | Serial.begin(9600); 26 | Serial1.begin(115200); 27 | wifi.begin(&Serial1, &Serial, PIN_RESET); 28 | 29 | /* If your board has only 1 serial port 30 | * or you didn't need to debugging, try this. 31 | * 32 | * Serial.begin(115200); 33 | * wifi.begin(&Serial, PIN_RESET); 34 | * 35 | */ 36 | 37 | // Check that ESP8266 is available 38 | if(wifi.test()) 39 | { 40 | // Connect to Access Point 41 | String ip = connectAP(); 42 | } 43 | else 44 | { 45 | // ESP8266 isn't available 46 | Serial.println("Check module connection and restart to try again..."); 47 | } 48 | } 49 | 50 | void loop() 51 | { 52 | 53 | } 54 | 55 | // Access Point Connection Function that you can loop connect to Access Point until successful 56 | String connectAP() 57 | { 58 | String ip = "0.0.0.0"; 59 | while(ip.equals("0.0.0.0")) 60 | { 61 | ip = wifi.connectAccessPoint(ssid,pass); 62 | if(!ip.equals("0.0.0.0")) 63 | { 64 | break; 65 | } 66 | } 67 | return ip; 68 | } 69 | 70 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For Test 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | ####################################### 10 | # Methods and Functions (KEYWORD2) 11 | ####################################### 12 | 13 | begin KEYWORD2 14 | test KEYWORD2 15 | hardReset KEYWORD2 16 | reset KEYWORD2 17 | closeTCPServer KEYWORD2 18 | openTCPServer KEYWORD2 19 | connectTCP KEYWORD2 20 | closeTCPConnection KEYWORD2 21 | getRunningState KEYWORD2 22 | getId KEYWORD2 23 | getMessage KEYWORD2 24 | clearNewMessage KEYWORD2 25 | connectAccessPoint KEYWORD2 26 | openAccessPoint KEYWORD2 27 | isNewDataComing KEYWORD2 28 | send KEYWORD2 29 | 30 | ###################################### 31 | # Instances (KEYWORD2) 32 | ####################################### 33 | 34 | ####################################### 35 | # Constants (LITERAL1) 36 | ####################################### 37 | 38 | WIFI_CLIENT LITERAL1 39 | WIFI_SERVER LITERAL1 40 | 41 | WIFI_STATE_IDLE LITERAL1 42 | WIFI_STATE_UNAVAILABLE LITERAL1 43 | WIFI_STATE_SEND LITERAL1 44 | WIFI_STATE_CONNECT LITERAL1 45 | WIFI_NEW_NONE LITERAL1 46 | WIFI_NEW_MESSAGE LITERAL1 47 | WIFI_NEW_CONNECTED LITERAL1 48 | WIFI_NEW_DISCONNECTED LITERAL1 49 | WIFI_NEW_SEND_OK LITERAL1 50 | WIFI_NEW_SEND_ERROR LITERAL1 51 | WIFI_NEW_RESET LITERAL1 52 | WIFI_NEW_ALREADY_CONNECT LITERAL1 53 | WIFI_NEW_ETC LITERAL1 --------------------------------------------------------------------------------