23 #define LOG_OUTPUT_DEBUG (1)
24 #define LOG_OUTPUT_DEBUG_PREFIX (1)
26 #define logDebug(arg)\
28 if (LOG_OUTPUT_DEBUG)\
30 if (LOG_OUTPUT_DEBUG_PREFIX)\
32 Serial.print("[LOG Debug: ");\
33 Serial.print((const char*)__FILE__);\
35 Serial.print((unsigned int)__LINE__);\
37 Serial.print((const char*)__FUNCTION__);\
44 #ifdef ESP8266_USE_SOFTWARE_SERIAL
45 ESP8266::ESP8266(SoftwareSerial &uart, uint32_t baud): m_puart(&uart)
51 ESP8266::ESP8266(HardwareSerial &uart, uint32_t baud): m_puart(&uart)
69 while (millis() - start < 3000) {
98 return eATSETUART(baudrate,pattern);
103 return eATGSLP(time);
110 if (!qATCWMODE(&mode,pattern1)) {
116 if (sATCWMODE(1,pattern2)){
132 if (!qATCWMODE(&mode,pattern1)) {
138 if (sATCWMODE(2,pattern2) ){
149 if (!qATCWMODE(&mode,pattern1)) {
155 if (sATCWMODE(3,pattern2) ){
166 qATCWJAP(ssid,pattern);
180 return sATCWJAP(ssid, pwd,pattern);
191 qATCWSAP(list,pattern);
199 return sATCWSAP(ssid, pwd, chl, ecn,pattern);
212 qATCWDHCP(dhcp,pattern);
217 return sATCWDHCP(mode, en, pattern);
222 return eATCWAUTOCONN(en);
227 qATCIPSTAMAC(mac,pattern);
234 return eATCIPSTAMAC(mac,pattern);
240 qATCIPSTAIP(ip,pattern);
246 return eATCIPSTAIP(ip,gateway,netmask,pattern);
252 qATCIPAP(ip,pattern);
258 return eATCIPAP(ip,pattern);
263 return eCWSTARTSMART(type);
268 return eCWSTOPSMART();
300 return sATCIPSTARTSingle(
"TCP", addr, port);
305 return eATCIPCLOSESingle();
310 return sATCIPSTARTSingle(
"UDP", addr, port);
315 return eATCIPCLOSESingle();
320 return sATCIPSTARTMultiple(mux_id,
"TCP", addr, port);
325 return sATCIPCLOSEMulitple(mux_id);
330 return sATCIPSTARTMultiple(mux_id,
"UDP", addr, port);
335 return sATCIPCLOSEMulitple(mux_id);
340 return sATCIPSTO(timeout);
345 if (sATCIPSERVER(1, port)) {
360 return sATCIPMODE(mode);
365 return eATSAVETRANSLINK(mode,ip,port);
388 return sATCIPSENDSingle(buffer, len);
393 return sATCIPSENDMultiple(mux_id, buffer, len);
396 uint32_t
ESP8266::recv(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
398 return recvPkg(buffer, buffer_size, NULL, timeout, NULL);
401 uint32_t
ESP8266::recv(uint8_t mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
405 ret = recvPkg(buffer, buffer_size, NULL, timeout, &
id);
406 if (ret > 0 &&
id == mux_id) {
412 uint32_t
ESP8266::recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
414 return recvPkg(buffer, buffer_size, NULL, timeout, coming_mux_id);
421 uint32_t ESP8266::recvPkg(uint8_t *buffer, uint32_t buffer_size, uint32_t *data_len, uint32_t timeout, uint8_t *coming_mux_id)
425 int32_t index_PIPDcomma = -1;
426 int32_t index_colon = -1;
427 int32_t index_comma = -1;
430 bool has_data =
false;
435 if (buffer == NULL) {
440 while (millis() - start < timeout) {
441 if(m_puart->available() > 0) {
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);
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) {
457 len = data.substring(index_comma + 1, index_colon).toInt();
462 len = data.substring(index_PIPDcomma + 5, index_colon).toInt();
475 ret = len > buffer_size ? buffer_size : len;
477 while (millis() - start < 3000) {
478 while(m_puart->available() > 0 && i < ret) {
487 if (index_comma != -1 && coming_mux_id) {
497 void ESP8266::rx_empty(
void)
499 while(m_puart->available() > 0) {
504 String ESP8266::recvString(String target, uint32_t timeout)
508 unsigned long start = millis();
509 while (millis() - start < timeout) {
510 while(m_puart->available() > 0) {
512 if(a ==
'\0')
continue;
515 if (data.indexOf(target) != -1) {
523 String ESP8266::recvString(String target1, String target2, uint32_t timeout)
527 unsigned long start = millis();
528 while (millis() - start < timeout) {
529 while(m_puart->available() > 0) {
531 if(a ==
'\0')
continue;
534 if (data.indexOf(target1) != -1) {
536 }
else if (data.indexOf(target2) != -1) {
543 String ESP8266::recvString(String target1, String target2, String target3, uint32_t timeout)
547 unsigned long start = millis();
548 while (millis() - start < timeout) {
549 while(m_puart->available() > 0) {
551 if(a ==
'\0')
continue;
554 if (data.indexOf(target1) != -1) {
556 }
else if (data.indexOf(target2) != -1) {
558 }
else if (data.indexOf(target3) != -1) {
565 bool ESP8266::recvFind(String target, uint32_t timeout)
568 data_tmp = recvString(target, timeout);
569 if (data_tmp.indexOf(target) != -1) {
575 bool ESP8266::recvFindAndFilter(String target, String begin, String end, String &data, uint32_t timeout)
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);
592 bool ESP8266::eAT(
void)
595 m_puart->println(
"AT");
596 return recvFind(
"OK");
599 bool ESP8266::eATRST(
void)
602 m_puart->println(
"AT+RST");
603 return recvFind(
"OK");
606 bool ESP8266::eATGMR(String &version)
610 m_puart->println(
"AT+GMR");
611 return recvFindAndFilter(
"OK",
"\r\r\n",
"\r\n\r\nOK", version,10000);
614 bool ESP8266::eATGSLP(uint32_t time)
617 m_puart->print(
"AT+GSLP=");
618 m_puart->println(time);
619 return recvFind(
"OK");
623 bool ESP8266::eATE(uint8_t mode)
626 m_puart->print(
"ATE");
627 m_puart->println(mode);
628 return recvFind(
"OK");
631 bool ESP8266::eATRESTORE(
void)
634 m_puart->println(
"AT+RESTORE");
635 return recvFind(
"OK");
639 bool ESP8266::eATSETUART(uint32_t baudrate,uint8_t pattern)
642 if(pattern>3||pattern<1){
647 m_puart->print(
"AT+UART=");
650 m_puart->print(
"AT+UART_CUR=");
653 m_puart->print(
"AT+UART_DEF=");
656 m_puart->print(baudrate);
665 if(recvFind(
"OK",5000)){
667 m_puart->begin(baudrate);
677 bool ESP8266::qATCWMODE(uint8_t *mode,uint8_t pattern)
681 if (!mode||!pattern) {
688 m_puart->println(
"AT+CWMODE_DEF?");
691 m_puart->println(
"AT+CWMODE_CUR?");
694 m_puart->println(
"AT+CWMODE?");
696 ret = recvFindAndFilter(
"OK",
":",
"\r\n\r\nOK", str_mode);
698 *mode = (uint8_t)str_mode.toInt();
704 bool ESP8266::eATCWMODE(String &list)
707 m_puart->println(
"AT+CWMODE=?");
708 return recvFindAndFilter(
"OK",
"+CWMODE:(",
")\r\n\r\nOK", list);
711 bool ESP8266::sATCWMODE(uint8_t mode,uint8_t pattern)
721 m_puart->print(
"AT+CWMODE_DEF=");
724 m_puart->print(
"AT+CWMODE_CUR=");
727 m_puart->print(
"AT+CWMODE=");
729 m_puart->println(mode);
730 data = recvString(
"OK",
"no change");
732 if (data.indexOf(
"OK") != -1 || data.indexOf(
"no change") != -1) {
739 bool ESP8266::qATCWJAP(String &ssid,uint8_t pattern)
750 m_puart->println(
"AT+CWJAP_DEF?");
753 m_puart->println(
"AT+CWJAP_CUR?");
756 m_puart->println(
"AT+CWJAP?");
758 ssid = recvString(
"OK",
"No AP");
759 if (ssid.indexOf(
"OK") != -1 || ssid.indexOf(
"No AP") != -1) {
766 bool ESP8266::sATCWJAP(String ssid, String pwd,uint8_t pattern)
776 m_puart->print(
"AT+CWJAP_DEF=\"");
780 m_puart->print(
"AT+CWJAP_CUR=\"");
783 m_puart->print(
"AT+CWJAP=\"");
786 m_puart->print(ssid);
787 m_puart->print(
"\",\"");
789 m_puart->println(
"\"");
791 data = recvString(
"OK",
"FAIL", 10000);
792 if (data.indexOf(
"OK") != -1) {
798 bool ESP8266::eATCWLAP(String &list)
802 m_puart->println(
"AT+CWLAP");
803 return recvFindAndFilter(
"OK",
"\r\r\n",
"\r\n\r\nOK", list, 15000);
809 bool ESP8266::eATCWQAP(
void)
813 m_puart->println(
"AT+CWQAP");
814 return recvFind(
"OK");
818 bool ESP8266::qATCWSAP(String &List,uint8_t pattern)
827 m_puart->println(
"AT+CWSAP_DEF?");
831 m_puart->println(
"AT+CWSAP_CUR?");
834 m_puart->println(
"AT+CWSAP?");
836 return recvFindAndFilter(
"OK",
"\r\r\n",
"\r\n\r\nOK", List,10000);
840 bool ESP8266::sATCWSAP(String ssid, String pwd, uint8_t chl, uint8_t ecn,uint8_t pattern)
849 m_puart->print(
"AT+CWSAP_DEF=\"");
853 m_puart->print(
"AT+CWSAP_CUR=\"");
856 m_puart->print(
"AT+CWSAP=\"");
859 m_puart->print(ssid);
860 m_puart->print(
"\",\"");
862 m_puart->print(
"\",");
865 m_puart->println(ecn);
867 data = recvString(
"OK",
"ERROR", 5000);
868 if (data.indexOf(
"OK") != -1) {
874 bool ESP8266::eATCWLIF(String &list)
878 m_puart->println(
"AT+CWLIF");
879 return recvFindAndFilter(
"OK",
"\r\r\n",
"\r\n\r\nOK", list);
882 bool ESP8266::qATCWDHCP(String &List,uint8_t pattern)
891 m_puart->println(
"AT+CWDHCP_DEF?");
894 m_puart->println(
"AT+CWDHCP_CUR?");
897 m_puart->println(
"AT+CWDHCP?");
900 return recvFindAndFilter(
"OK",
"\r\r\n",
"\r\nOK", List,10000);
905 bool ESP8266::sATCWDHCP(uint8_t mode, uint8_t en, uint8_t pattern)
914 m_puart->print(
"AT+CWDHCP_DEF=");
918 m_puart->print(
"AT+CWDHCP_CUR=");
921 m_puart->print(
"AT+CWDHCP=");
924 m_puart->print(mode);
926 m_puart->println(en);
927 data = recvString(
"OK",
"ERROR", 2000);
929 if (data.indexOf(
"OK") != -1) {
936 bool ESP8266::eATCWAUTOCONN(uint8_t en)
943 m_puart->print(
"AT+CWAUTOCONN=");
944 m_puart->println(en);
945 return recvFind(
"OK");
949 bool ESP8266::qATCIPSTAMAC(String &mac,uint8_t pattern)
958 m_puart->println(
"AT+CIPSTAMAC_DEF?");
962 m_puart->println(
"AT+CIPSTAMAC_CUR?");
965 m_puart->println(
"AT+CIPSTAMAC?");
968 return recvFindAndFilter(
"OK",
"\r\r\n",
"\r\n\r\nOK", mac,2000);
974 bool ESP8266::eATCIPSTAMAC(String mac,uint8_t pattern)
983 m_puart->print(
"AT+CIPSTAMAC_DEF=");
987 m_puart->print(
"AT+CIPSTAMAC_CUR=");
990 m_puart->print(
"AT+CIPSTAMAC=");
993 m_puart->print(
"\"");
995 m_puart->println(
"\"");
996 return recvFind(
"OK");
1000 bool ESP8266::qATCIPSTAIP(String &ip,uint8_t pattern)
1009 m_puart->println(
"AT+CIPSTA_DEF?");
1013 m_puart->println(
"AT+CIPSTA_CUR?");
1016 m_puart->println(
"AT+CIPSTA?");
1019 return recvFindAndFilter(
"OK",
"\r\r\n",
"\r\n\r\nOK", ip,2000);
1023 bool ESP8266::eATCIPSTAIP(String ip,String gateway,String netmask,uint8_t pattern)
1032 m_puart->print(
"AT+CIPSTA_DEF=");
1036 m_puart->print(
"AT+CIPSTA_CUR=");
1039 m_puart->print(
"AT+CIPSTA=");
1042 m_puart->print(
"\"");
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");
1054 bool ESP8266::qATCIPAP(String &ip,uint8_t pattern)
1063 m_puart->println(
"AT+CIPAP_DEF?");
1067 m_puart->println(
"AT+CIPAP_CUR?");
1070 m_puart->println(
"AT+CIPAP?");
1073 return recvFindAndFilter(
"OK",
"\r\r\n",
"\r\n\r\nOK", ip,2000);
1078 bool ESP8266::eATCIPAP(String ip,uint8_t pattern)
1087 m_puart->print(
"AT+CIPAP_DEF=");
1091 m_puart->print(
"AT+CIPAP_CUR=");
1094 m_puart->print(
"AT+CIPAP=");
1097 m_puart->print(
"\"");
1099 m_puart->println(
"\"");
1100 return recvFind(
"OK");
1105 bool ESP8266::eCWSTARTSMART(uint8_t type)
1108 m_puart->print(
"AT+CWSTARTSMART=");
1109 m_puart->println(type);
1110 return recvFind(
"OK");
1113 bool ESP8266::eCWSTOPSMART(
void)
1116 m_puart->println(
"AT+CWSTOPSMART");
1117 return recvFind(
"OK");
1120 bool ESP8266::eATCIPSTATUS(String &list)
1125 m_puart->println(
"AT+CIPSTATUS");
1126 return recvFindAndFilter(
"OK",
"\r\r\n",
"\r\n\r\nOK", list);
1128 bool ESP8266::sATCIPSTARTSingle(String type, String addr, uint32_t port)
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);
1139 data = recvString(
"OK",
"ERROR",
"ALREADY CONNECT", 10000);
1140 if (data.indexOf(
"OK") != -1 || data.indexOf(
"ALREADY CONNECT") != -1) {
1145 bool ESP8266::sATCIPSTARTMultiple(uint8_t mux_id, String type, String addr, uint32_t port)
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);
1158 data = recvString(
"OK",
"ERROR",
"ALREADY CONNECT", 10000);
1159 if (data.indexOf(
"OK") != -1 || data.indexOf(
"ALREADY CONNECT") != -1) {
1164 bool ESP8266::sATCIPSENDSingle(
const uint8_t *buffer, uint32_t len)
1167 m_puart->print(
"AT+CIPSEND=");
1168 m_puart->println(len);
1169 if (recvFind(
">", 5000)) {
1171 for (uint32_t i = 0; i < len; i++) {
1172 m_puart->write(buffer[i]);
1174 return recvFind(
"SEND OK", 10000);
1178 bool ESP8266::sATCIPSENDMultiple(uint8_t mux_id,
const uint8_t *buffer, uint32_t len)
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)) {
1187 for (uint32_t i = 0; i < len; i++) {
1188 m_puart->write(buffer[i]);
1190 return recvFind(
"SEND OK", 10000);
1194 bool ESP8266::sATCIPCLOSEMulitple(uint8_t mux_id)
1198 m_puart->print(
"AT+CIPCLOSE=");
1199 m_puart->println(mux_id);
1201 data = recvString(
"OK",
"link is not", 5000);
1202 if (data.indexOf(
"OK") != -1 || data.indexOf(
"link is not") != -1) {
1207 bool ESP8266::eATCIPCLOSESingle(
void)
1210 m_puart->println(
"AT+CIPCLOSE");
1211 return recvFind(
"OK", 5000);
1213 bool ESP8266::eATCIFSR(String &list)
1216 m_puart->println(
"AT+CIFSR");
1217 return recvFindAndFilter(
"OK",
"\r\r\n",
"\r\n\r\nOK", list);
1219 bool ESP8266::sATCIPMUX(uint8_t mode)
1223 m_puart->print(
"AT+CIPMUX=");
1224 m_puart->println(mode);
1226 data = recvString(
"OK",
"Link is builded");
1227 if (data.indexOf(
"OK") != -1) {
1232 bool ESP8266::sATCIPSERVER(uint8_t mode, uint32_t port)
1237 m_puart->print(
"AT+CIPSERVER=1,");
1238 m_puart->println(port);
1240 data = recvString(
"OK",
"no change");
1241 if (data.indexOf(
"OK") != -1 || data.indexOf(
"no change") != -1) {
1247 m_puart->println(
"AT+CIPSERVER=0");
1248 return recvFind(
"\r\r\n");
1253 bool ESP8266::sATCIPMODE(uint8_t mode)
1260 m_puart->print(
"AT+CIPMODE=");
1261 m_puart->println(mode);
1263 data = recvString(
"OK",
"Link is builded",2000);
1264 if (data.indexOf(
"OK") != -1 ) {
1273 bool ESP8266::eATSAVETRANSLINK(uint8_t mode,String ip,uint32_t port)
1278 m_puart->print(
"AT+SAVETRANSLINK=");
1279 m_puart->print(mode);
1280 m_puart->print(
",\"");
1282 m_puart->print(
"\",");
1283 m_puart->println(port);
1284 data = recvString(
"OK",
"ERROR",2000);
1285 if (data.indexOf(
"OK") != -1 ) {
1293 bool ESP8266::eATPING(String ip)
1296 m_puart->print(
"AT+PING=");
1297 m_puart->print(
"\"");
1299 m_puart->println(
"\"");
1300 return recvFind(
"OK",2000);
1305 bool ESP8266::sATCIPSTO(uint32_t timeout)
1308 m_puart->print(
"AT+CIPSTO=");
1309 m_puart->println(timeout);
1310 return recvFind(
"OK");
bool setAutoConnect(uint8_t en)
make boot automatically connected.
bool send(const uint8_t *buffer, uint32_t len)
Send data based on TCP or UDP builded already in single mode.
String getStationIp(uint8_t pattern=3)
Get the station's IP.
bool setPing(String ip)
PING COMMAND.
bool joinAP(String ssid, String pwd, uint8_t pattern=3)
Join in AP.
String getVersion(void)
Get the version of AT Command Set.
bool releaseTCP(void)
Release TCP connection in single mode.
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.
bool setSoftAPParam(String ssid, String pwd, uint8_t chl=7, uint8_t ecn=4, uint8_t pattern=3)
Set SoftAP parameters.
String getDHCP(uint8_t pattern=3)
Get the current state of DHCP.
bool setOprToSoftAP(uint8_t pattern1=3, uint8_t pattern2=3)
Set operation mode to softap.
bool setStationMac(String mac, uint8_t pattern=3)
Set the station's MAC address.
String getNowConecAp(uint8_t pattern=3)
Search and returns the current connect AP.
bool setStationIp(String ip, String gateway, String netmask, uint8_t pattern=3)
Set the station's IP.
bool enableMUX(void)
Enable IP MUX(multiple connection mode).
bool setUart(uint32_t baudrate, uint8_t pattern)
Set up a serial port configuration.
String getWifiModeList(void)
Get the model values list.
bool setAPIp(String ip, uint8_t pattern=3)
Set the AP IP.
String getLocalIP(void)
Get the IP address of ESP8266.
bool deepSleep(uint32_t time)
Start function of deep sleep.
The definition of class ESP8266.
String getSoftAPParam(uint8_t pattern=3)
get SoftAP parameters.
bool createTCP(String addr, uint32_t port)
Create TCP connection in single mode.
bool unregisterUDP(void)
Unregister UDP port number in single mode.
bool startServer(uint32_t port=333)
Start Server(Only in multiple mode).
bool saveTransLink(uint8_t mode, String ip, uint32_t port)
Save the passthrough links.
bool stopTCPServer(void)
Stop TCP Server(Only in multiple mode).
bool setEcho(uint8_t mode)
Switch the echo function.
bool startSmartConfig(uint8_t type)
start smartconfig.
bool stopServer(void)
Stop Server(Only in multiple mode).
bool disableMUX(void)
Disable IP MUX(single connection mode).
bool setDHCP(uint8_t mode, uint8_t en, uint8_t pattern=3)
Set the state of DHCP.
bool restart(void)
Restart ESP8266 by "AT+RST".
bool kick(void)
Verify ESP8266 whether live or not.
bool setCIPMODE(uint8_t mode)
Set the module transfer mode.
bool registerUDP(String addr, uint32_t port)
Register UDP port number in single mode.
bool restore(void)
Restore factory.
bool setOprToStationSoftAP(uint8_t pattern1=3, uint8_t pattern2=3)
Set operation mode to station + softap.
bool leaveAP(void)
Leave AP joined before.
bool setTCPServerTimeout(uint32_t timeout=180)
Set the timeout of TCP Server.
String getAPIp(uint8_t pattern=3)
Get the AP's IP.
bool stopSmartConfig(void)
stop smartconfig.
bool startTCPServer(uint32_t port=333)
Start TCP Server(Only in multiple mode).
String getAPList(void)
Search available AP list and return it.
String getStationMac(uint8_t pattern=3)
Get the station's MAC address.
String getIPStatus(void)
Get the current status of connection(UDP and TCP).
bool setOprToStation(uint8_t pattern1=3, uint8_t pattern2=3)
Set operation mode to station.
String getJoinedDeviceIP(void)
Get the IP list of devices connected to SoftAP.