| API For Arduino developers

- Main Page
- Related Pages
- Classes
- Files
- Examples

- File List
- File Members

ESP8266.cpp
Go to the documentation of this file.
1
21 #include "ESP8266.h"
22
23 #define LOG_OUTPUT_DEBUG (1)
24 #define LOG_OUTPUT_DEBUG_PREFIX (1)
25
26 #define logDebug(arg)\
27 do {\
28 if (LOG_OUTPUT_DEBUG)\
29 {\
30 if (LOG_OUTPUT_DEBUG_PREFIX)\
31 {\
32 Serial.print("[LOG Debug: ");\
33 Serial.print((const char*)__FILE__);\
34 Serial.print(",");\
35 Serial.print((unsigned int)__LINE__);\
36 Serial.print(",");\
37 Serial.print((const char*)__FUNCTION__);\
38 Serial.print("] ");\
39 }\
40 Serial.print(arg);\
41 }\
42 } while(0)
43
44 #ifdef ESP8266_USE_SOFTWARE_SERIAL
45 ESP8266::ESP8266(SoftwareSerial &uart, uint32_t baud): m_puart(&uart)
46 {
47 m_puart->begin(baud);
48 rx_empty();
49 }
50 #else
51 ESP8266::ESP8266(HardwareSerial &uart, uint32_t baud): m_puart(&uart)
52 {
53 m_puart->begin(baud);
54 rx_empty();
55 }
56 #endif
57
58 bool ESP8266::kick(void)
59 {
60 return eAT();
61 }
62
63 bool ESP8266::restart(void)
64 {
65 unsigned long start;
66 if (eATRST()) {
67 delay(2000);
68 start = millis();
69 while (millis() - start < 3000) {
70 if (eAT()) {
71 delay(1500); /* Waiting for stable */
72 return true;
73 }
74 delay(100);
75 }
76 }
77 return false;
78 }
79
80 String ESP8266::getVersion(void)
81 {
82 String version;
83 eATGMR(version);
84 return version;
85 }
86
87 bool ESP8266::setEcho(uint8_t mode)
88 {
89 return eATE(mode);
90 }
91
92 bool ESP8266::restore(void)
93 {
94 return eATRESTORE();
95 }
96 bool ESP8266::setUart(uint32_t baudrate,uint8_t pattern)
97 {
98 return eATSETUART(baudrate,pattern);
99 }
100
101 bool ESP8266::deepSleep(uint32_t time)
102 {
103 return eATGSLP(time);
104 }
105
106
107 bool ESP8266::setOprToStation(uint8_t pattern1,uint8_t pattern2)
108 {
109 uint8_t mode;
110 if (!qATCWMODE(&mode,pattern1)) {
111 return false;
112 }
113 if (mode == 1) {
114 return true;
115 } else {
116 if (sATCWMODE(1,pattern2)){
117 return true;
118 } else {
119 return false;
120 }
121 }
122 }
123 String ESP8266::getWifiModeList(void)
124 {
125 String list;
126 eATCWMODE(list);
127 return list;
128 }
129 bool ESP8266::setOprToSoftAP(uint8_t pattern1,uint8_t pattern2)
130 {
131 uint8_t mode;
132 if (!qATCWMODE(&mode,pattern1)) {
133 return false;
134 }
135 if (mode == 2) {
136 return true;
137 } else {
138 if (sATCWMODE(2,pattern2) ){
139 return true;
140 } else {
141 return false;
142 }
143 }
144 }
145
146 bool ESP8266::setOprToStationSoftAP(uint8_t pattern1,uint8_t pattern2)
147 {
148 uint8_t mode;
149 if (!qATCWMODE(&mode,pattern1)) {
150 return false;
151 }
152 if (mode == 3) {
153 return true;
154 } else {
155 if (sATCWMODE(3,pattern2) ){
156 return true;
157 } else {
158 return false;
159 }
160 }
161 }
162
163 String ESP8266::getNowConecAp(uint8_t pattern)
164 {
165 String ssid;
166 qATCWJAP(ssid,pattern);
167 return ssid;
168 }
169
170
171 String ESP8266::getAPList(void)
172 {
173 String list;
174 eATCWLAP(list);
175 return list;
176 }
177
178 bool ESP8266::joinAP(String ssid, String pwd,uint8_t pattern)
179 {
180 return sATCWJAP(ssid, pwd,pattern);
181 }
182
183 bool ESP8266::leaveAP(void)
184 {
185 return eATCWQAP();
186 }
187
188 String ESP8266::getSoftAPParam(uint8_t pattern)
189 {
190 String list;
191 qATCWSAP(list,pattern);
192 return list;
193 }
194
195
196
197 bool ESP8266::setSoftAPParam(String ssid, String pwd, uint8_t chl, uint8_t ecn,uint8_t pattern)
198 {
199 return sATCWSAP(ssid, pwd, chl, ecn,pattern);
200 }
201
202 String ESP8266::getJoinedDeviceIP(void)
203 {
204 String list;
205 eATCWLIF(list);
206 return list;
207 }
208
209 String ESP8266::getDHCP(uint8_t pattern)
210 {
211 String dhcp;
212 qATCWDHCP(dhcp,pattern);
213 return dhcp;
214 }
215 bool ESP8266::setDHCP(uint8_t mode, uint8_t en, uint8_t pattern)
216 {
217 return sATCWDHCP(mode, en, pattern);
218 }
219
220 bool ESP8266::setAutoConnect(uint8_t en)
221 {
222 return eATCWAUTOCONN(en);
223 }
224 String ESP8266::getStationMac(uint8_t pattern)
225 {
226 String mac;
227 qATCIPSTAMAC(mac,pattern);
228 return mac;
229 }
230
231
232 bool ESP8266::setStationMac(String mac,uint8_t pattern)
233 {
234 return eATCIPSTAMAC(mac,pattern);
235 }
236
237 String ESP8266::getStationIp(uint8_t pattern)
238 {
239 String ip;
240 qATCIPSTAIP(ip,pattern);
241 return ip;
242 }
243
244 bool ESP8266::setStationIp(String ip,String gateway,String netmask,uint8_t pattern)
245 {
246 return eATCIPSTAIP(ip,gateway,netmask,pattern);
247 }
248
249 String ESP8266::getAPIp(uint8_t pattern)
250 {
251 String ip;
252 qATCIPAP(ip,pattern);
253 return ip;
254 }
255
256 bool ESP8266::setAPIp(String ip,uint8_t pattern)
257 {
258 return eATCIPAP(ip,pattern);
259 }
260
261 bool ESP8266::startSmartConfig(uint8_t type)
262 {
263 return eCWSTARTSMART(type);
264 }
265
266 bool ESP8266::stopSmartConfig(void)
267 {
268 return eCWSTOPSMART();
269 }
270
271
272
273
274 String ESP8266::getIPStatus(void)
275 {
276 String list;
277 eATCIPSTATUS(list);
278 return list;
279 }
280
281 String ESP8266::getLocalIP(void)
282 {
283 String list;
284 eATCIFSR(list);
285 return list;
286 }
287
288 bool ESP8266::enableMUX(void)
289 {
290 return sATCIPMUX(1);
291 }
292
293 bool ESP8266::disableMUX(void)
294 {
295 return sATCIPMUX(0);
296 }
297
298 bool ESP8266::createTCP(String addr, uint32_t port)
299 {
300 return sATCIPSTARTSingle("TCP", addr, port);
301 }
302
303 bool ESP8266::releaseTCP(void)
304 {
305 return eATCIPCLOSESingle();
306 }
307
308 bool ESP8266::registerUDP(String addr, uint32_t port)
309 {
310 return sATCIPSTARTSingle("UDP", addr, port);
311 }
312
313 bool ESP8266::unregisterUDP(void)
314 {
315 return eATCIPCLOSESingle();
316 }
317
318 bool ESP8266::createTCP(uint8_t mux_id, String addr, uint32_t port)
319 {
320 return sATCIPSTARTMultiple(mux_id, "TCP", addr, port);
321 }
322
323 bool ESP8266::releaseTCP(uint8_t mux_id)
324 {
325 return sATCIPCLOSEMulitple(mux_id);
326 }
327
328 bool ESP8266::registerUDP(uint8_t mux_id, String addr, uint32_t port)
329 {
330 return sATCIPSTARTMultiple(mux_id, "UDP", addr, port);
331 }
332
333 bool ESP8266::unregisterUDP(uint8_t mux_id)
334 {
335 return sATCIPCLOSEMulitple(mux_id);
336 }
337
338 bool ESP8266::setTCPServerTimeout(uint32_t timeout)
339 {
340 return sATCIPSTO(timeout);
341 }
342
343 bool ESP8266::startTCPServer(uint32_t port)
344 {
345 if (sATCIPSERVER(1, port)) {
346 return true;
347 }
348 return false;
349 }
350
351 bool ESP8266::stopTCPServer(void)
352 {
353 sATCIPSERVER(0);
354 restart();
355 return false;
356 }
357
358 bool ESP8266::setCIPMODE(uint8_t mode)
359 {
360 return sATCIPMODE(mode);
361 }
362
363 bool ESP8266::saveTransLink (uint8_t mode,String ip,uint32_t port)
364 {
365 return eATSAVETRANSLINK(mode,ip,port);
366 }
367
368 bool ESP8266::setPing(String ip)
369 {
370 return eATPING(ip);
371 }
372
373
374
375
376 bool ESP8266::startServer(uint32_t port)
377 {
378 return startTCPServer(port);
379 }
380
381 bool ESP8266::stopServer(void)
382 {
383 return stopTCPServer();
384 }
385
386 bool ESP8266::send(const uint8_t *buffer, uint32_t len)
387 {
388 return sATCIPSENDSingle(buffer, len);
389 }
390
391 bool ESP8266::send(uint8_t mux_id, const uint8_t *buffer, uint32_t len)
392 {
393 return sATCIPSENDMultiple(mux_id, buffer, len);
394 }
395
396 uint32_t ESP8266::recv(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
397 {
398 return recvPkg(buffer, buffer_size, NULL, timeout, NULL);
399 }
400
401 uint32_t ESP8266::recv(uint8_t mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
402 {
403 uint8_t id;
404 uint32_t ret;
405 ret = recvPkg(buffer, buffer_size, NULL, timeout, &id);
406 if (ret > 0 && id == mux_id) {
407 return ret;
408 }
409 return 0;
410 }
411
412 uint32_t ESP8266::recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
413 {
414 return recvPkg(buffer, buffer_size, NULL, timeout, coming_mux_id);
415 }
416
417 /*----------------------------------------------------------------------------*/
418 /* +IPD,<id>,<len>:<data> */
419 /* +IPD,<len>:<data> */
420
421 uint32_t ESP8266::recvPkg(uint8_t *buffer, uint32_t buffer_size, uint32_t *data_len, uint32_t timeout, uint8_t *coming_mux_id)
422 {
423 String data;
424 char a;
425 int32_t index_PIPDcomma = -1;
426 int32_t index_colon = -1; /* : */
427 int32_t index_comma = -1; /* , */
428 int32_t len = -1;
429 int8_t id = -1;
430 bool has_data = false;
431 uint32_t ret;
432 unsigned long start;
433 uint32_t i;
434
435 if (buffer == NULL) {
436 return 0;
437 }
438
439 start = millis();
440 while (millis() - start < timeout) {
441 if(m_puart->available() > 0) {
442 a = m_puart->read();
443 data += a;
444 }
445
446 index_PIPDcomma = data.indexOf("+IPD,");
447 if (index_PIPDcomma != -1) {
448 index_colon = data.indexOf(':', index_PIPDcomma + 5);
449 if (index_colon != -1) {
450 index_comma = data.indexOf(',', index_PIPDcomma + 5);
451 /* +IPD,id,len:data */
452 if (index_comma != -1 && index_comma < index_colon) {
453 id = data.substring(index_PIPDcomma + 5, index_comma).toInt();
454 if (id < 0 || id > 4) {
455 return 0;
456 }
457 len = data.substring(index_comma + 1, index_colon).toInt();
458 if (len <= 0) {
459 return 0;
460 }
461 } else { /* +IPD,len:data */
462 len = data.substring(index_PIPDcomma + 5, index_colon).toInt();
463 if (len <= 0) {
464 return 0;
465 }
466 }
467 has_data = true;
468 break;
469 }
470 }
471 }
472
473 if (has_data) {
474 i = 0;
475 ret = len > buffer_size ? buffer_size : len;
476 start = millis();
477 while (millis() - start < 3000) {
478 while(m_puart->available() > 0 && i < ret) {
479 a = m_puart->read();
480 buffer[i++] = a;
481 }
482 if (i == ret) {
483 rx_empty();
484 if (data_len) {
485 *data_len = len;
486 }
487 if (index_comma != -1 && coming_mux_id) {
488 *coming_mux_id = id;
489 }
490 return ret;
491 }
492 }
493 }
494 return 0;
495 }
496
497 void ESP8266::rx_empty(void)
498 {
499 while(m_puart->available() > 0) {
500 m_puart->read();
501 }
502 }
503
504 String ESP8266::recvString(String target, uint32_t timeout)
505 {
506 String data;
507 char a;
508 unsigned long start = millis();
509 while (millis() - start < timeout) {
510 while(m_puart->available() > 0) {
511 a = m_puart->read();
512 if(a == '\0') continue;
513 data += a;
514 }
515 if (data.indexOf(target) != -1) {
516 break;
517 }
518 }
519
520 return data;
521 }
522
523 String ESP8266::recvString(String target1, String target2, uint32_t timeout)
524 {
525 String data;
526 char a;
527 unsigned long start = millis();
528 while (millis() - start < timeout) {
529 while(m_puart->available() > 0) {
530 a = m_puart->read();
531 if(a == '\0') continue;
532 data += a;
533 }
534 if (data.indexOf(target1) != -1) {
535 break;
536 } else if (data.indexOf(target2) != -1) {
537 break;
538 }
539 }
540 return data;
541 }
542
543 String ESP8266::recvString(String target1, String target2, String target3, uint32_t timeout)
544 {
545 String data;
546 char a;
547 unsigned long start = millis();
548 while (millis() - start < timeout) {
549 while(m_puart->available() > 0) {
550 a = m_puart->read();
551 if(a == '\0') continue;
552 data += a;
553 }
554 if (data.indexOf(target1) != -1) {
555 break;
556 } else if (data.indexOf(target2) != -1) {
557 break;
558 } else if (data.indexOf(target3) != -1) {
559 break;
560 }
561 }
562 return data;
563 }
564
565 bool ESP8266::recvFind(String target, uint32_t timeout)
566 {
567 String data_tmp;
568 data_tmp = recvString(target, timeout);
569 if (data_tmp.indexOf(target) != -1) {
570 return true;
571 }
572 return false;
573 }
574
575 bool ESP8266::recvFindAndFilter(String target, String begin, String end, String &data, uint32_t timeout)
576 {
577 String data_tmp;
578 data_tmp = recvString(target, timeout);
579 if (data_tmp.indexOf(target) != -1) {
580 int32_t index1 = data_tmp.indexOf(begin);
581 int32_t index2 = data_tmp.indexOf(end);
582 if (index1 != -1 && index2 != -1) {
583 index1 += begin.length();
584 data = data_tmp.substring(index1, index2);
585 return true;
586 }
587 }
588 data = "";
589 return false;
590 }
591
592 bool ESP8266::eAT(void)
593 {
594 rx_empty();
595 m_puart->println("AT");
596 return recvFind("OK");
597 }
598
599 bool ESP8266::eATRST(void)
600 {
601 rx_empty();
602 m_puart->println("AT+RST");
603 return recvFind("OK");
604 }
605
606 bool ESP8266::eATGMR(String &version)
607 {
608 rx_empty();
609 delay(3000);
610 m_puart->println("AT+GMR");
611 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", version,10000);
612 }
613
614 bool ESP8266::eATGSLP(uint32_t time)
615 {
616 rx_empty();
617 m_puart->print("AT+GSLP=");
618 m_puart->println(time);
619 return recvFind("OK");
620 }
621
622
623 bool ESP8266::eATE(uint8_t mode)
624 {
625 rx_empty();
626 m_puart->print("ATE");
627 m_puart->println(mode);
628 return recvFind("OK");
629 }
630
631 bool ESP8266::eATRESTORE(void)
632 {
633 rx_empty();
634 m_puart->println("AT+RESTORE");
635 return recvFind("OK");
636 }
637
638
639 bool ESP8266::eATSETUART(uint32_t baudrate,uint8_t pattern)
640 {
641 rx_empty();
642 if(pattern>3||pattern<1){
643 return false;
644 }
645 switch(pattern){
646 case 1:
647 m_puart->print("AT+UART=");
648 break;
649 case 2:
650 m_puart->print("AT+UART_CUR=");
651 break;
652 case 3:
653 m_puart->print("AT+UART_DEF=");
654 break;
655 }
656 m_puart->print(baudrate);
657 m_puart->print(",");
658 m_puart->print(8);
659 m_puart->print(",");
660 m_puart->print(1);
661 m_puart->print(",");
662 m_puart->print(0);
663 m_puart->print(",");
664 m_puart->println(0);
665 if(recvFind("OK",5000)){
666
667 m_puart->begin(baudrate);
668 return true;
669 }
670 else{
671 return false;
672 }
673
674 }
675
676
677 bool ESP8266::qATCWMODE(uint8_t *mode,uint8_t pattern)
678 {
679 String str_mode;
680 bool ret;
681 if (!mode||!pattern) {
682 return false;
683 }
684 rx_empty();
685 switch(pattern)
686 {
687 case 1 :
688 m_puart->println("AT+CWMODE_DEF?");
689 break;
690 case 2:
691 m_puart->println("AT+CWMODE_CUR?");
692 break;
693 default:
694 m_puart->println("AT+CWMODE?");
695 }
696 ret = recvFindAndFilter("OK", ":", "\r\n\r\nOK", str_mode);
697 if (ret) {
698 *mode = (uint8_t)str_mode.toInt();
699 return true;
700 } else {
701 return false;
702 }
703 }
704 bool ESP8266::eATCWMODE(String &list)
705 {
706 rx_empty();
707 m_puart->println("AT+CWMODE=?");
708 return recvFindAndFilter("OK", "+CWMODE:(", ")\r\n\r\nOK", list);
709 }
710
711 bool ESP8266::sATCWMODE(uint8_t mode,uint8_t pattern)
712 {
713 if(!pattern){
714 return false;
715 }
716 String data;
717 rx_empty();
718 switch(pattern)
719 {
720 case 1 :
721 m_puart->print("AT+CWMODE_DEF=");
722 break;
723 case 2:
724 m_puart->print("AT+CWMODE_CUR=");
725 break;
726 default:
727 m_puart->print("AT+CWMODE=");
728 }
729 m_puart->println(mode);
730 data = recvString("OK", "no change");
731
732 if (data.indexOf("OK") != -1 || data.indexOf("no change") != -1) {
733 return true;
734 }
735 return false;
736 }
737
738
739 bool ESP8266::qATCWJAP(String &ssid,uint8_t pattern)
740 {
741
742 bool ret;
743 if (!pattern) {
744 return false;
745 }
746 rx_empty();
747 switch(pattern)
748 {
749 case 1 :
750 m_puart->println("AT+CWJAP_DEF?");
751 break;
752 case 2:
753 m_puart->println("AT+CWJAP_CUR?");
754 break;
755 default:
756 m_puart->println("AT+CWJAP?");
757 }
758 ssid = recvString("OK", "No AP");
759 if (ssid.indexOf("OK") != -1 || ssid.indexOf("No AP") != -1) {
760 return true;
761 }
762 return false;
763
764 }
765
766 bool ESP8266::sATCWJAP(String ssid, String pwd,uint8_t pattern)
767 {
768 String data;
769 if (!pattern) {
770 return false;
771 }
772 rx_empty();
773 switch(pattern)
774 {
775 case 1 :
776 m_puart->print("AT+CWJAP_DEF=\"");
777
778 break;
779 case 2:
780 m_puart->print("AT+CWJAP_CUR=\"");
781 break;
782 default:
783 m_puart->print("AT+CWJAP=\"");
784 }
785
786 m_puart->print(ssid);
787 m_puart->print("\",\"");
788 m_puart->print(pwd);
789 m_puart->println("\"");
790
791 data = recvString("OK", "FAIL", 10000);
792 if (data.indexOf("OK") != -1) {
793 return true;
794 }
795 return false;
796 }
797
798 bool ESP8266::eATCWLAP(String &list)
799 {
800 String data;
801 rx_empty();
802 m_puart->println("AT+CWLAP");
803 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list, 15000);
804 }
805
806
807
808
809 bool ESP8266::eATCWQAP(void)
810 {
811 String data;
812 rx_empty();
813 m_puart->println("AT+CWQAP");
814 return recvFind("OK");
815 }
816
817
818 bool ESP8266::qATCWSAP(String &List,uint8_t pattern)
819 {
820 if (!pattern) {
821 return false;
822 }
823 rx_empty();
824 switch(pattern)
825 {
826 case 1 :
827 m_puart->println("AT+CWSAP_DEF?");
828
829 break;
830 case 2:
831 m_puart->println("AT+CWSAP_CUR?");
832 break;
833 default:
834 m_puart->println("AT+CWSAP?");
835 }
836 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", List,10000);
837
838 }
839
840 bool ESP8266::sATCWSAP(String ssid, String pwd, uint8_t chl, uint8_t ecn,uint8_t pattern)
841 {
842 String data;
843 if (!pattern) {
844 return false;
845 }
846 rx_empty();
847 switch(pattern){
848 case 1 :
849 m_puart->print("AT+CWSAP_DEF=\"");
850
851 break;
852 case 2:
853 m_puart->print("AT+CWSAP_CUR=\"");
854 break;
855 default:
856 m_puart->print("AT+CWSAP=\"");
857
858 }
859 m_puart->print(ssid);
860 m_puart->print("\",\"");
861 m_puart->print(pwd);
862 m_puart->print("\",");
863 m_puart->print(chl);
864 m_puart->print(",");
865 m_puart->println(ecn);
866
867 data = recvString("OK", "ERROR", 5000);
868 if (data.indexOf("OK") != -1) {
869 return true;
870 }
871 return false;
872 }
873
874 bool ESP8266::eATCWLIF(String &list)
875 {
876 String data;
877 rx_empty();
878 m_puart->println("AT+CWLIF");
879 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
880 }
881
882 bool ESP8266::qATCWDHCP(String &List,uint8_t pattern)
883 {
884 if (!pattern) {
885 return false;
886 }
887 rx_empty();
888 switch(pattern)
889 {
890 case 1 :
891 m_puart->println("AT+CWDHCP_DEF?");
892 break;
893 case 2:
894 m_puart->println("AT+CWDHCP_CUR?");
895 break;
896 default:
897 m_puart->println("AT+CWDHCP?");
898 }
899
900 return recvFindAndFilter("OK", "\r\r\n", "\r\nOK", List,10000);
901
902 }
903
904
905 bool ESP8266::sATCWDHCP(uint8_t mode, uint8_t en, uint8_t pattern)
906 {
907 String data;
908 if (!pattern) {
909 return false;
910 }
911 rx_empty();
912 switch(pattern){
913 case 1 :
914 m_puart->print("AT+CWDHCP_DEF=");
915
916 break;
917 case 2:
918 m_puart->print("AT+CWDHCP_CUR=");
919 break;
920 default:
921 m_puart->print("AT+CWDHCP=");
922
923 }
924 m_puart->print(mode);
925 m_puart->print(",");
926 m_puart->println(en);
927 data = recvString("OK", "ERROR", 2000);
928
929 if (data.indexOf("OK") != -1) {
930 return true;
931 }
932 return false;
933 }
934
935
936 bool ESP8266::eATCWAUTOCONN(uint8_t en)
937 {
938
939 rx_empty();
940 if(en>1||en<0){
941 return false;
942 }
943 m_puart->print("AT+CWAUTOCONN=");
944 m_puart->println(en);
945 return recvFind("OK");
946
947 }
948
949 bool ESP8266::qATCIPSTAMAC(String &mac,uint8_t pattern)
950 {
951
952 rx_empty();
953 if (!pattern) {
954 return false;
955 }
956 switch(pattern){
957 case 1 :
958 m_puart->println("AT+CIPSTAMAC_DEF?");
959
960 break;
961 case 2:
962 m_puart->println("AT+CIPSTAMAC_CUR?");
963 break;
964 default:
965 m_puart->println("AT+CIPSTAMAC?");
966
967 }
968 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", mac,2000);
969
970 }
971
972
973
974 bool ESP8266::eATCIPSTAMAC(String mac,uint8_t pattern)
975 {
976
977 rx_empty();
978 if (!pattern) {
979 return false;
980 }
981 switch(pattern){
982 case 1 :
983 m_puart->print("AT+CIPSTAMAC_DEF=");
984
985 break;
986 case 2:
987 m_puart->print("AT+CIPSTAMAC_CUR=");
988 break;
989 default:
990 m_puart->print("AT+CIPSTAMAC=");
991
992 }
993 m_puart->print("\"");
994 m_puart->print(mac);
995 m_puart->println("\"");
996 return recvFind("OK");
997
998 }
999
1000 bool ESP8266::qATCIPSTAIP(String &ip,uint8_t pattern)
1001 {
1002
1003 rx_empty();
1004 if (!pattern) {
1005 return false;
1006 }
1007 switch(pattern){
1008 case 1 :
1009 m_puart->println("AT+CIPSTA_DEF?");
1010
1011 break;
1012 case 2:
1013 m_puart->println("AT+CIPSTA_CUR?");
1014 break;
1015 default:
1016 m_puart->println("AT+CIPSTA?");
1017
1018 }
1019 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", ip,2000);
1020
1021 }
1022
1023 bool ESP8266::eATCIPSTAIP(String ip,String gateway,String netmask,uint8_t pattern)
1024 {
1025
1026 rx_empty();
1027 if (!pattern) {
1028 return false;
1029 }
1030 switch(pattern){
1031 case 1 :
1032 m_puart->print("AT+CIPSTA_DEF=");
1033
1034 break;
1035 case 2:
1036 m_puart->print("AT+CIPSTA_CUR=");
1037 break;
1038 default:
1039 m_puart->print("AT+CIPSTA=");
1040
1041 }
1042 m_puart->print("\"");
1043 m_puart->print(ip);
1044 m_puart->print("\",\"");
1045 m_puart->print(gateway);
1046 m_puart->print("\",\"");
1047 m_puart->print(netmask);
1048 m_puart->println("\"");
1049 return recvFind("OK");
1050
1051 }
1052
1053
1054 bool ESP8266::qATCIPAP(String &ip,uint8_t pattern)
1055 {
1056
1057 rx_empty();
1058 if (!pattern) {
1059 return false;
1060 }
1061 switch(pattern){
1062 case 1 :
1063 m_puart->println("AT+CIPAP_DEF?");
1064
1065 break;
1066 case 2:
1067 m_puart->println("AT+CIPAP_CUR?");
1068 break;
1069 default:
1070 m_puart->println("AT+CIPAP?");
1071
1072 }
1073 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", ip,2000);
1074
1075 }
1076
1077
1078 bool ESP8266::eATCIPAP(String ip,uint8_t pattern)
1079 {
1080
1081 rx_empty();
1082 if (!pattern) {
1083 return false;
1084 }
1085 switch(pattern){
1086 case 1 :
1087 m_puart->print("AT+CIPAP_DEF=");
1088
1089 break;
1090 case 2:
1091 m_puart->print("AT+CIPAP_CUR=");
1092 break;
1093 default:
1094 m_puart->print("AT+CIPAP=");
1095
1096 }
1097 m_puart->print("\"");
1098 m_puart->print(ip);
1099 m_puart->println("\"");
1100 return recvFind("OK");
1101
1102 }
1103
1104
1105 bool ESP8266::eCWSTARTSMART(uint8_t type)
1106 {
1107 rx_empty();
1108 m_puart->print("AT+CWSTARTSMART=");
1109 m_puart->println(type);
1110 return recvFind("OK");
1111 }
1112
1113 bool ESP8266::eCWSTOPSMART(void)
1114 {
1115 rx_empty();
1116 m_puart->println("AT+CWSTOPSMART");
1117 return recvFind("OK");
1118 }
1119
1120 bool ESP8266::eATCIPSTATUS(String &list)
1121 {
1122 String data;
1123 delay(100);
1124 rx_empty();
1125 m_puart->println("AT+CIPSTATUS");
1126 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
1127 }
1128 bool ESP8266::sATCIPSTARTSingle(String type, String addr, uint32_t port)
1129 {
1130 String data;
1131 rx_empty();
1132 m_puart->print("AT+CIPSTART=\"");
1133 m_puart->print(type);
1134 m_puart->print("\",\"");
1135 m_puart->print(addr);
1136 m_puart->print("\",");
1137 m_puart->println(port);
1138
1139 data = recvString("OK", "ERROR", "ALREADY CONNECT", 10000);
1140 if (data.indexOf("OK") != -1 || data.indexOf("ALREADY CONNECT") != -1) {
1141 return true;
1142 }
1143 return false;
1144 }
1145 bool ESP8266::sATCIPSTARTMultiple(uint8_t mux_id, String type, String addr, uint32_t port)
1146 {
1147 String data;
1148 rx_empty();
1149 m_puart->print("AT+CIPSTART=");
1150 m_puart->print(mux_id);
1151 m_puart->print(",\"");
1152 m_puart->print(type);
1153 m_puart->print("\",\"");
1154 m_puart->print(addr);
1155 m_puart->print("\",");
1156 m_puart->println(port);
1157
1158 data = recvString("OK", "ERROR", "ALREADY CONNECT", 10000);
1159 if (data.indexOf("OK") != -1 || data.indexOf("ALREADY CONNECT") != -1) {
1160 return true;
1161 }
1162 return false;
1163 }
1164 bool ESP8266::sATCIPSENDSingle(const uint8_t *buffer, uint32_t len)
1165 {
1166 rx_empty();
1167 m_puart->print("AT+CIPSEND=");
1168 m_puart->println(len);
1169 if (recvFind(">", 5000)) {
1170 rx_empty();
1171 for (uint32_t i = 0; i < len; i++) {
1172 m_puart->write(buffer[i]);
1173 }
1174 return recvFind("SEND OK", 10000);
1175 }
1176 return false;
1177 }
1178 bool ESP8266::sATCIPSENDMultiple(uint8_t mux_id, const uint8_t *buffer, uint32_t len)
1179 {
1180 rx_empty();
1181 m_puart->print("AT+CIPSEND=");
1182 m_puart->print(mux_id);
1183 m_puart->print(",");
1184 m_puart->println(len);
1185 if (recvFind(">", 5000)) {
1186 rx_empty();
1187 for (uint32_t i = 0; i < len; i++) {
1188 m_puart->write(buffer[i]);
1189 }
1190 return recvFind("SEND OK", 10000);
1191 }
1192 return false;
1193 }
1194 bool ESP8266::sATCIPCLOSEMulitple(uint8_t mux_id)
1195 {
1196 String data;
1197 rx_empty();
1198 m_puart->print("AT+CIPCLOSE=");
1199 m_puart->println(mux_id);
1200
1201 data = recvString("OK", "link is not", 5000);
1202 if (data.indexOf("OK") != -1 || data.indexOf("link is not") != -1) {
1203 return true;
1204 }
1205 return false;
1206 }
1207 bool ESP8266::eATCIPCLOSESingle(void)
1208 {
1209 rx_empty();
1210 m_puart->println("AT+CIPCLOSE");
1211 return recvFind("OK", 5000);
1212 }
1213 bool ESP8266::eATCIFSR(String &list)
1214 {
1215 rx_empty();
1216 m_puart->println("AT+CIFSR");
1217 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
1218 }
1219 bool ESP8266::sATCIPMUX(uint8_t mode)
1220 {
1221 String data;
1222 rx_empty();
1223 m_puart->print("AT+CIPMUX=");
1224 m_puart->println(mode);
1225
1226 data = recvString("OK", "Link is builded");
1227 if (data.indexOf("OK") != -1) {
1228 return true;
1229 }
1230 return false;
1231 }
1232 bool ESP8266::sATCIPSERVER(uint8_t mode, uint32_t port)
1233 {
1234 String data;
1235 if (mode) {
1236 rx_empty();
1237 m_puart->print("AT+CIPSERVER=1,");
1238 m_puart->println(port);
1239
1240 data = recvString("OK", "no change");
1241 if (data.indexOf("OK") != -1 || data.indexOf("no change") != -1) {
1242 return true;
1243 }
1244 return false;
1245 } else {
1246 rx_empty();
1247 m_puart->println("AT+CIPSERVER=0");
1248 return recvFind("\r\r\n");
1249 }
1250 }
1251
1252
1253 bool ESP8266::sATCIPMODE(uint8_t mode)
1254 {
1255 String data;
1256 if(mode>1||mode<0){
1257 return false;
1258 }
1259 rx_empty();
1260 m_puart->print("AT+CIPMODE=");
1261 m_puart->println(mode);
1262
1263 data = recvString("OK", "Link is builded",2000);
1264 if (data.indexOf("OK") != -1 ) {
1265 return true;
1266 }
1267 return false;
1268 }
1269
1270
1271
1272
1273 bool ESP8266::eATSAVETRANSLINK(uint8_t mode,String ip,uint32_t port)
1274 {
1275
1276 String data;
1277 rx_empty();
1278 m_puart->print("AT+SAVETRANSLINK=");
1279 m_puart->print(mode);
1280 m_puart->print(",\"");
1281 m_puart->print(ip);
1282 m_puart->print("\",");
1283 m_puart->println(port);
1284 data = recvString("OK", "ERROR",2000);
1285 if (data.indexOf("OK") != -1 ) {
1286 return true;
1287 }
1288 return false;
1289 }
1290
1291
1292
1293 bool ESP8266::eATPING(String ip)
1294 {
1295 rx_empty();
1296 m_puart->print("AT+PING=");
1297 m_puart->print("\"");
1298 m_puart->print(ip);
1299 m_puart->println("\"");
1300 return recvFind("OK",2000);
1301 }
1302
1303
1304
1305 bool ESP8266::sATCIPSTO(uint32_t timeout)
1306 {
1307 rx_empty();
1308 m_puart->print("AT+CIPSTO=");
1309 m_puart->println(timeout);
1310 return recvFind("OK");
1311 }
1312
ESP8266::setAutoConnect
bool setAutoConnect(uint8_t en)
make boot automatically connected.
Definition: ESP8266.cpp:220
ESP8266::send
bool send(const uint8_t *buffer, uint32_t len)
Send data based on TCP or UDP builded already in single mode.
Definition: ESP8266.cpp:386
ESP8266::getStationIp
String getStationIp(uint8_t pattern=3)
Get the station's IP.
Definition: ESP8266.cpp:237
ESP8266::setPing
bool setPing(String ip)
PING COMMAND.
Definition: ESP8266.cpp:368
ESP8266::joinAP
bool joinAP(String ssid, String pwd, uint8_t pattern=3)
Join in AP.
Definition: ESP8266.cpp:178
ESP8266::getVersion
String getVersion(void)
Get the version of AT Command Set.
Definition: ESP8266.cpp:80
ESP8266::releaseTCP
bool releaseTCP(void)
Release TCP connection in single mode.
Definition: ESP8266.cpp:303
ESP8266::recv
uint32_t recv(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout=1000)
Receive data from TCP or UDP builded already in single mode.
Definition: ESP8266.cpp:396
ESP8266::setSoftAPParam
bool setSoftAPParam(String ssid, String pwd, uint8_t chl=7, uint8_t ecn=4, uint8_t pattern=3)
Set SoftAP parameters.
Definition: ESP8266.cpp:197
ESP8266::getDHCP
String getDHCP(uint8_t pattern=3)
Get the current state of DHCP.
Definition: ESP8266.cpp:209
ESP8266::setOprToSoftAP
bool setOprToSoftAP(uint8_t pattern1=3, uint8_t pattern2=3)
Set operation mode to softap.
Definition: ESP8266.cpp:129
ESP8266::setStationMac
bool setStationMac(String mac, uint8_t pattern=3)
Set the station's MAC address.
Definition: ESP8266.cpp:232
ESP8266::getNowConecAp
String getNowConecAp(uint8_t pattern=3)
Search and returns the current connect AP.
Definition: ESP8266.cpp:163
ESP8266::setStationIp
bool setStationIp(String ip, String gateway, String netmask, uint8_t pattern=3)
Set the station's IP.
Definition: ESP8266.cpp:244
ESP8266::enableMUX
bool enableMUX(void)
Enable IP MUX(multiple connection mode).
Definition: ESP8266.cpp:288
ESP8266::setUart
bool setUart(uint32_t baudrate, uint8_t pattern)
Set up a serial port configuration.
Definition: ESP8266.cpp:96
ESP8266::getWifiModeList
String getWifiModeList(void)
Get the model values list.
Definition: ESP8266.cpp:123
ESP8266::setAPIp
bool setAPIp(String ip, uint8_t pattern=3)
Set the AP IP.
Definition: ESP8266.cpp:256
ESP8266::getLocalIP
String getLocalIP(void)
Get the IP address of ESP8266.
Definition: ESP8266.cpp:281
ESP8266::deepSleep
bool deepSleep(uint32_t time)
Start function of deep sleep.
Definition: ESP8266.cpp:101
ESP8266.h
The definition of class ESP8266.
ESP8266::getSoftAPParam
String getSoftAPParam(uint8_t pattern=3)
get SoftAP parameters.
Definition: ESP8266.cpp:188
ESP8266::createTCP
bool createTCP(String addr, uint32_t port)
Create TCP connection in single mode.
Definition: ESP8266.cpp:298
ESP8266::unregisterUDP
bool unregisterUDP(void)
Unregister UDP port number in single mode.
Definition: ESP8266.cpp:313
ESP8266::startServer
bool startServer(uint32_t port=333)
Start Server(Only in multiple mode).
Definition: ESP8266.cpp:376
ESP8266::saveTransLink
bool saveTransLink(uint8_t mode, String ip, uint32_t port)
Save the passthrough links.
Definition: ESP8266.cpp:363
ESP8266::stopTCPServer
bool stopTCPServer(void)
Stop TCP Server(Only in multiple mode).
Definition: ESP8266.cpp:351
ESP8266::setEcho
bool setEcho(uint8_t mode)
Switch the echo function.
Definition: ESP8266.cpp:87
ESP8266::startSmartConfig
bool startSmartConfig(uint8_t type)
start smartconfig.
Definition: ESP8266.cpp:261
ESP8266::stopServer
bool stopServer(void)
Stop Server(Only in multiple mode).
Definition: ESP8266.cpp:381
ESP8266::disableMUX
bool disableMUX(void)
Disable IP MUX(single connection mode).
Definition: ESP8266.cpp:293
ESP8266::setDHCP
bool setDHCP(uint8_t mode, uint8_t en, uint8_t pattern=3)
Set the state of DHCP.
Definition: ESP8266.cpp:215
ESP8266::restart
bool restart(void)
Restart ESP8266 by "AT+RST".
Definition: ESP8266.cpp:63
ESP8266::kick
bool kick(void)
Verify ESP8266 whether live or not.
Definition: ESP8266.cpp:58
ESP8266::setCIPMODE
bool setCIPMODE(uint8_t mode)
Set the module transfer mode.
Definition: ESP8266.cpp:358
ESP8266::registerUDP
bool registerUDP(String addr, uint32_t port)
Register UDP port number in single mode.
Definition: ESP8266.cpp:308
ESP8266::restore
bool restore(void)
Restore factory.
Definition: ESP8266.cpp:92
ESP8266::setOprToStationSoftAP
bool setOprToStationSoftAP(uint8_t pattern1=3, uint8_t pattern2=3)
Set operation mode to station + softap.
Definition: ESP8266.cpp:146
ESP8266::leaveAP
bool leaveAP(void)
Leave AP joined before.
Definition: ESP8266.cpp:183
ESP8266::setTCPServerTimeout
bool setTCPServerTimeout(uint32_t timeout=180)
Set the timeout of TCP Server.
Definition: ESP8266.cpp:338
ESP8266::getAPIp
String getAPIp(uint8_t pattern=3)
Get the AP's IP.
Definition: ESP8266.cpp:249
ESP8266::stopSmartConfig
bool stopSmartConfig(void)
stop smartconfig.
Definition: ESP8266.cpp:266
ESP8266::startTCPServer
bool startTCPServer(uint32_t port=333)
Start TCP Server(Only in multiple mode).
Definition: ESP8266.cpp:343
ESP8266::getAPList
String getAPList(void)
Search available AP list and return it.
Definition: ESP8266.cpp:171
ESP8266::getStationMac
String getStationMac(uint8_t pattern=3)
Get the station's MAC address.
Definition: ESP8266.cpp:224
ESP8266::getIPStatus
String getIPStatus(void)
Get the current status of connection(UDP and TCP).
Definition: ESP8266.cpp:274
ESP8266::setOprToStation
bool setOprToStation(uint8_t pattern1=3, uint8_t pattern2=3)
Set operation mode to station.
Definition: ESP8266.cpp:107
ESP8266::getJoinedDeviceIP
String getJoinedDeviceIP(void)
Get the IP list of devices connected to SoftAP.
Definition: ESP8266.cpp:202