| API For Arduino developers

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

- File List
- File Members

ESP8266.h
Go to the documentation of this file.
1
21 #ifndef __ESP8266_H__
22 #define __ESP8266_H__
23
24 #include "Arduino.h"
25
26 #ifdef ESP8266_USE_SOFTWARE_SERIAL
27 #include "SoftwareSerial.h"
28 #endif
29
30 #define VERSION_18 0X18
31 #define VERSION_22 0X22
32
37 #define USER_SEL_VERSION VERSION_18
38
42 class ESP8266 {
43 public:
44
45 #ifdef ESP8266_USE_SOFTWARE_SERIAL
46 /*
47 * Constuctor.
48 *
49 * @param uart - an reference of SoftwareSerial object.
50 * @param baud - the buad rate to communicate with ESP8266(default:9600).
51 *
52 * @warning parameter baud depends on the AT firmware. 9600 is an common value.
53 */
54 #if (USER_SEL_VERSION == VERSION_22)
55 ESP8266(SoftwareSerial &uart, uint32_t baud = 115200);
56 #elif (USER_SEL_VERSION == VERSION_18)
57 ESP8266(SoftwareSerial &uart, uint32_t baud = 9600);
58 #endif /* #if(USER_SEL_VERSION==VERSION_22) */
59
60 #else /* HardwareSerial */
61 /*
62 * Constuctor.
63 *
64 * @param uart - an reference of HardwareSerial object.
65 * @param baud - the buad rate to communicate with ESP8266(default:9600).
66 *
67 * @warning parameter baud depends on the AT firmware. 9600 is an common value.
68 */
69 #if (USER_SEL_VERSION == VERSION_22)
70 ESP8266(HardwareSerial &uart, uint32_t baud = 115200);
71 #elif (USER_SEL_VERSION == VERSION_18)
72 ESP8266(HardwareSerial &uart, uint32_t baud = 9600);
73 #endif /* #if(USER_SEL_VERSION == VERSION_22) */
74
75
76 #endif /* #ifdef ESP8266_USE_SOFTWARE_SERIAL */
77
78
87 bool kick(void);
88
97 bool restart(void);
98
104 String getVersion(void);
105
114 bool deepSleep(uint32_t time);
115
124 bool setEcho(uint8_t mode);
125
132 bool restore(void);
133
143 bool setUart(uint32_t baudrate,uint8_t pattern);
144
154 bool setOprToStation(uint8_t pattern1=3,uint8_t pattern2=3);
155
161 String getWifiModeList(void);
162
171 bool setOprToSoftAP(uint8_t pattern1=3,uint8_t pattern2=3);
172
181 bool setOprToStationSoftAP(uint8_t pattern1=3,uint8_t pattern2=3);
182
190 String getAPList(void);
191
198 String getNowConecAp(uint8_t pattern=3);
199
210 bool joinAP(String ssid, String pwd,uint8_t pattern=3);
211
218 bool leaveAP(void);
219
233 bool setSoftAPParam(String ssid, String pwd, uint8_t chl = 7, uint8_t ecn = 4,uint8_t pattern=3);
234
241 String getSoftAPParam(uint8_t pattern=3);
242
249 String getJoinedDeviceIP(void);
250
258 String getDHCP(uint8_t pattern=3);
259
268 bool setDHCP(uint8_t mode, uint8_t en, uint8_t pattern=3);
269
276 bool setAutoConnect(uint8_t en);
277
284 String getStationMac(uint8_t pattern=3);
285
293 bool setStationMac(String mac,uint8_t pattern=3);
294
301 String getStationIp(uint8_t pattern=3);
302
313 bool setStationIp(String ip,String gateway,String netmask,uint8_t pattern=3);
314
322 String getAPIp(uint8_t pattern=3);
323
332 bool setAPIp(String ip,uint8_t pattern=3);
333
340 bool startSmartConfig(uint8_t type);
341
348 bool stopSmartConfig(void);
349
355 String getIPStatus(void);
356
362 String getLocalIP(void);
363
373 bool enableMUX(void);
374
383 bool disableMUX(void);
384
393 bool createTCP(String addr, uint32_t port);
394
401 bool releaseTCP(void);
402
411 bool registerUDP(String addr, uint32_t port);
412
419 bool unregisterUDP(void);
420
430 bool createTCP(uint8_t mux_id, String addr, uint32_t port);
431
439 bool releaseTCP(uint8_t mux_id);
440
450 bool registerUDP(uint8_t mux_id, String addr, uint32_t port);
451
459 bool unregisterUDP(uint8_t mux_id);
460
468 bool setTCPServerTimeout(uint32_t timeout = 180);
469
485 bool startTCPServer(uint32_t port = 333);
486
493 bool stopTCPServer(void);
494
501 bool setCIPMODE(uint8_t mode);
502
513 bool startServer(uint32_t port = 333);
514
521 bool stopServer(void);
528 bool saveTransLink (uint8_t mode,String ip,uint32_t port);
529
536 bool setPing(String ip);
537
546 bool send(const uint8_t *buffer, uint32_t len);
547
557 bool send(uint8_t mux_id, const uint8_t *buffer, uint32_t len);
558
567 uint32_t recv(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout = 1000);
568
578 uint32_t recv(uint8_t mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout = 1000);
579
592 uint32_t recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout = 1000);
593
594 private:
595
596 /*
597 * Empty the buffer or UART RX.
598 */
599 void rx_empty(void);
600
601 /*
602 * Recvive data from uart. Return all received data if target found or timeout.
603 */
604 String recvString(String target, uint32_t timeout = 1000);
605
606 /*
607 * Recvive data from uart. Return all received data if one of target1 and target2 found or timeout.
608 */
609 String recvString(String target1, String target2, uint32_t timeout = 1000);
610
611 /*
612 * Recvive data from uart. Return all received data if one of target1, target2 and target3 found or timeout.
613 */
614 String recvString(String target1, String target2, String target3, uint32_t timeout = 1000);
615
616 /*
617 * Recvive data from uart and search first target. Return true if target found, false for timeout.
618 */
619 bool recvFind(String target, uint32_t timeout = 1000);
620
621 /*
622 * Recvive data from uart and search first target and cut out the substring between begin and end(excluding begin and end self).
623 * Return true if target found, false for timeout.
624 */
625 bool recvFindAndFilter(String target, String begin, String end, String &data, uint32_t timeout = 1000);
626
627 /*
628 * Receive a package from uart.
629 *
630 * @param buffer - the buffer storing data.
631 * @param buffer_size - guess what!
632 * @param data_len - the length of data actually received(maybe more than buffer_size, the remained data will be abandoned).
633 * @param timeout - the duration waitting data comming.
634 * @param coming_mux_id - in single connection mode, should be NULL and not NULL in multiple.
635 */
636 uint32_t recvPkg(uint8_t *buffer, uint32_t buffer_size, uint32_t *data_len, uint32_t timeout, uint8_t *coming_mux_id);
637
638
639 bool eAT(void);
640 bool eATRST(void);
641 bool eATGMR(String &version);
642 bool eATGSLP(uint32_t time);
643 bool eATE(uint8_t mode);
644 bool eATRESTORE(void);
645 bool eATSETUART(uint32_t baudrate,uint8_t pattern);
646
647 bool qATCWMODE(uint8_t *mode,uint8_t pattern=3);
648 bool eATCWMODE(String &list) ;
649 bool sATCWMODE(uint8_t mode,uint8_t pattern=3);
650 bool qATCWJAP(String &ssid,uint8_t pattern=3) ;
651 bool sATCWJAP(String ssid, String pwd,uint8_t pattern=3);
652 bool eATCWLAP(String &list);
653 bool eATCWQAP(void);
654 bool qATCWSAP(String &List,uint8_t pattern=3);
655 bool sATCWSAP(String ssid, String pwd, uint8_t chl, uint8_t ecn,uint8_t pattern=3);
656 bool eATCWLIF(String &list);
657 bool qATCWDHCP(String &List,uint8_t pattern=3);
658 bool sATCWDHCP(uint8_t mode, uint8_t en, uint8_t pattern=3);
659 bool eATCWAUTOCONN(uint8_t en);
660 bool qATCIPSTAMAC(String &mac,uint8_t pattern=3);
661 bool eATCIPSTAMAC(String mac,uint8_t pattern=3);
662 bool qATCIPSTAIP(String &ip,uint8_t pattern=3);
663 bool eATCIPSTAIP(String ip,String gateway,String netmask,uint8_t pattern=3);
664 bool qATCIPAP(String &ip,uint8_t pattern=3);
665 bool eATCIPAP(String ip,uint8_t pattern=3);
666 bool eCWSTARTSMART(uint8_t type);
667 bool eCWSTOPSMART(void);
668
669
670 bool eATCIPSTATUS(String &list);
671 bool sATCIPSTARTSingle(String type, String addr, uint32_t port);
672 bool sATCIPSTARTMultiple(uint8_t mux_id, String type, String addr, uint32_t port);
673 bool sATCIPSENDSingle(const uint8_t *buffer, uint32_t len);
674 bool sATCIPSENDMultiple(uint8_t mux_id, const uint8_t *buffer, uint32_t len);
675 bool sATCIPCLOSEMulitple(uint8_t mux_id);
676 bool eATCIPCLOSESingle(void);
677 bool eATCIFSR(String &list);
678 bool sATCIPMUX(uint8_t mode);
679 bool sATCIPSERVER(uint8_t mode, uint32_t port = 333);
680 bool sATCIPMODE(uint8_t mode);
681 bool eATSAVETRANSLINK(uint8_t mode,String ip,uint32_t port);
682 bool eATPING(String ip);
683 bool sATCIPSTO(uint32_t timeout);
684
685 /*
686 * +IPD,len:data
687 * +IPD,id,len:data
688 */
689
690 #ifdef ESP8266_USE_SOFTWARE_SERIAL
691 SoftwareSerial *m_puart; /* The UART to communicate with ESP8266 */
692 #else
693 HardwareSerial *m_puart; /* The UART to communicate with ESP8266 */
694 #endif
695 };
696
697 #endif /* #ifndef __ESP8266_H__ */
698
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::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
Provide an easy-to-use way to manipulate ESP8266.
Definition: ESP8266.h:42
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