API
For Arduino developers
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 
27 //#define ESP8266_USE_SOFTWARE_SERIAL
28 
29 
30 #ifdef ESP8266_USE_SOFTWARE_SERIAL
31 #include "SoftwareSerial.h"
32 #endif
33 
34 
38 class ESP8266 {
39  public:
40 
41 #ifdef ESP8266_USE_SOFTWARE_SERIAL
42  /*
43  * Constuctor.
44  *
45  * @param uart - an reference of SoftwareSerial object.
46  * @param baud - the buad rate to communicate with ESP8266(default:9600).
47  *
48  * @warning parameter baud depends on the AT firmware. 9600 is an common value.
49  */
50  ESP8266(SoftwareSerial &uart, uint32_t baud = 9600);
51 #else /* HardwareSerial */
52  /*
53  * Constuctor.
54  *
55  * @param uart - an reference of HardwareSerial object.
56  * @param baud - the buad rate to communicate with ESP8266(default:9600).
57  *
58  * @warning parameter baud depends on the AT firmware. 9600 is an common value.
59  */
60  ESP8266(HardwareSerial &uart, uint32_t baud = 9600);
61 #endif
62 
63 
72  bool kick(void);
73 
82  bool restart(void);
83 
89  String getVersion(void);
90 
97  bool setOprToStation(void);
98 
105  bool setOprToSoftAP(void);
106 
113  bool setOprToStationSoftAP(void);
114 
122  String getAPList(void);
123 
133  bool joinAP(String ssid, String pwd);
134 
141  bool leaveAP(void);
142 
153  bool setSoftAPParam(String ssid, String pwd, uint8_t chl = 7, uint8_t ecn = 4);
154 
161  String getJoinedDeviceIP(void);
162 
168  String getIPStatus(void);
169 
175  String getLocalIP(void);
176 
186  bool enableMUX(void);
187 
196  bool disableMUX(void);
197 
198 
207  bool createTCP(String addr, uint32_t port);
208 
215  bool releaseTCP(void);
216 
225  bool registerUDP(String addr, uint32_t port);
226 
233  bool unregisterUDP(void);
234 
244  bool createTCP(uint8_t mux_id, String addr, uint32_t port);
245 
253  bool releaseTCP(uint8_t mux_id);
254 
264  bool registerUDP(uint8_t mux_id, String addr, uint32_t port);
265 
273  bool unregisterUDP(uint8_t mux_id);
274 
275 
283  bool setTCPServerTimeout(uint32_t timeout = 180);
284 
300  bool startTCPServer(uint32_t port = 333);
301 
308  bool stopTCPServer(void);
309 
320  bool startServer(uint32_t port = 333);
321 
328  bool stopServer(void);
329 
338  bool send(const uint8_t *buffer, uint32_t len);
339 
349  bool send(uint8_t mux_id, const uint8_t *buffer, uint32_t len);
350 
359  uint32_t recv(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout = 1000);
360 
370  uint32_t recv(uint8_t mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout = 1000);
371 
384  uint32_t recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout = 1000);
385 
386  private:
387 
388  /*
389  * Empty the buffer or UART RX.
390  */
391  void rx_empty(void);
392 
393  /*
394  * Recvive data from uart. Return all received data if target found or timeout.
395  */
396  String recvString(String target, uint32_t timeout = 1000);
397 
398  /*
399  * Recvive data from uart. Return all received data if one of target1 and target2 found or timeout.
400  */
401  String recvString(String target1, String target2, uint32_t timeout = 1000);
402 
403  /*
404  * Recvive data from uart. Return all received data if one of target1, target2 and target3 found or timeout.
405  */
406  String recvString(String target1, String target2, String target3, uint32_t timeout = 1000);
407 
408  /*
409  * Recvive data from uart and search first target. Return true if target found, false for timeout.
410  */
411  bool recvFind(String target, uint32_t timeout = 1000);
412 
413  /*
414  * Recvive data from uart and search first target and cut out the substring between begin and end(excluding begin and end self).
415  * Return true if target found, false for timeout.
416  */
417  bool recvFindAndFilter(String target, String begin, String end, String &data, uint32_t timeout = 1000);
418 
419  /*
420  * Receive a package from uart.
421  *
422  * @param buffer - the buffer storing data.
423  * @param buffer_size - guess what!
424  * @param data_len - the length of data actually received(maybe more than buffer_size, the remained data will be abandoned).
425  * @param timeout - the duration waitting data comming.
426  * @param coming_mux_id - in single connection mode, should be NULL and not NULL in multiple.
427  */
428  uint32_t recvPkg(uint8_t *buffer, uint32_t buffer_size, uint32_t *data_len, uint32_t timeout, uint8_t *coming_mux_id);
429 
430 
431  bool eAT(void);
432  bool eATRST(void);
433  bool eATGMR(String &version);
434 
435  bool qATCWMODE(uint8_t *mode);
436  bool sATCWMODE(uint8_t mode);
437  bool sATCWJAP(String ssid, String pwd);
438  bool eATCWLAP(String &list);
439  bool eATCWQAP(void);
440  bool sATCWSAP(String ssid, String pwd, uint8_t chl, uint8_t ecn);
441  bool eATCWLIF(String &list);
442 
443  bool eATCIPSTATUS(String &list);
444  bool sATCIPSTARTSingle(String type, String addr, uint32_t port);
445  bool sATCIPSTARTMultiple(uint8_t mux_id, String type, String addr, uint32_t port);
446  bool sATCIPSENDSingle(const uint8_t *buffer, uint32_t len);
447  bool sATCIPSENDMultiple(uint8_t mux_id, const uint8_t *buffer, uint32_t len);
448  bool sATCIPCLOSEMulitple(uint8_t mux_id);
449  bool eATCIPCLOSESingle(void);
450  bool eATCIFSR(String &list);
451  bool sATCIPMUX(uint8_t mode);
452  bool sATCIPSERVER(uint8_t mode, uint32_t port = 333);
453  bool sATCIPSTO(uint32_t timeout);
454 
455  /*
456  * +IPD,len:data
457  * +IPD,id,len:data
458  */
459 
460 #ifdef ESP8266_USE_SOFTWARE_SERIAL
461  SoftwareSerial *m_puart; /* The UART to communicate with ESP8266 */
462 #else
463  HardwareSerial *m_puart; /* The UART to communicate with ESP8266 */
464 #endif
465 };
466 
467 #endif /* #ifndef __ESP8266_H__ */
468 
bool send(const uint8_t *buffer, uint32_t len)
Send data based on TCP or UDP builded already in single mode.
Definition: ESP8266.cpp:261
String getVersion(void)
Get the version of AT Command Set.
Definition: ESP8266.cpp:80
bool releaseTCP(void)
Release TCP connection in single mode.
Definition: ESP8266.cpp:196
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:271
bool enableMUX(void)
Enable IP MUX(multiple connection mode).
Definition: ESP8266.cpp:181
String getLocalIP(void)
Get the IP address of ESP8266.
Definition: ESP8266.cpp:174
bool setOprToSoftAP(void)
Set operation mode to softap.
Definition: ESP8266.cpp:104
bool setOprToStationSoftAP(void)
Set operation mode to station + softap.
Definition: ESP8266.cpp:121
bool createTCP(String addr, uint32_t port)
Create TCP connection in single mode.
Definition: ESP8266.cpp:191
bool unregisterUDP(void)
Unregister UDP port number in single mode.
Definition: ESP8266.cpp:206
bool startServer(uint32_t port=333)
Start Server(Only in multiple mode).
Definition: ESP8266.cpp:251
bool stopTCPServer(void)
Stop TCP Server(Only in multiple mode).
Definition: ESP8266.cpp:244
bool stopServer(void)
Stop Server(Only in multiple mode).
Definition: ESP8266.cpp:256
bool disableMUX(void)
Disable IP MUX(single connection mode).
Definition: ESP8266.cpp:186
bool restart(void)
Restart ESP8266 by "AT+RST".
Definition: ESP8266.cpp:63
bool kick(void)
Verify ESP8266 whether live or not.
Definition: ESP8266.cpp:58
bool joinAP(String ssid, String pwd)
Join in AP.
Definition: ESP8266.cpp:145
bool registerUDP(String addr, uint32_t port)
Register UDP port number in single mode.
Definition: ESP8266.cpp:201
bool leaveAP(void)
Leave AP joined before.
Definition: ESP8266.cpp:150
bool setTCPServerTimeout(uint32_t timeout=180)
Set the timeout of TCP Server.
Definition: ESP8266.cpp:231
Provide an easy-to-use way to manipulate ESP8266.
Definition: ESP8266.h:38
bool startTCPServer(uint32_t port=333)
Start TCP Server(Only in multiple mode).
Definition: ESP8266.cpp:236
bool setSoftAPParam(String ssid, String pwd, uint8_t chl=7, uint8_t ecn=4)
Set SoftAP parameters.
Definition: ESP8266.cpp:155
bool setOprToStation(void)
Set operation mode to staion.
Definition: ESP8266.cpp:87
String getAPList(void)
Search available AP list and return it.
Definition: ESP8266.cpp:138
String getIPStatus(void)
Get the current status of connection(UDP and TCP).
Definition: ESP8266.cpp:167
String getJoinedDeviceIP(void)
Get the IP list of devices connected to SoftAP.
Definition: ESP8266.cpp:160