1 | /**
|
---|
2 | * @file ESP8266.h
|
---|
3 | * @brief The definition of class ESP8266.
|
---|
4 | * @author Wu Pengfei<pengfei.wu@itead.cc>
|
---|
5 | * @date 2015.02
|
---|
6 | *
|
---|
7 | * @par Copyright:
|
---|
8 | * Copyright (c) 2015 ITEAD Intelligent Systems Co., Ltd. \n\n
|
---|
9 | * Copyright (C) 2015 Embedded and Real-Time Systems Laboratory
|
---|
10 | * Graduate School of Information Science, Nagoya Univ., JAPAN \n\n
|
---|
11 | * This program is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU General Public License as
|
---|
13 | * published by the Free Software Foundation; either version 2 of
|
---|
14 | * the License, or (at your option) any later version. \n\n
|
---|
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
---|
21 | * THE SOFTWARE.
|
---|
22 | */
|
---|
23 | #ifndef __ESP8266_H__
|
---|
24 | #define __ESP8266_H__
|
---|
25 |
|
---|
26 | #include "Arduino.h"
|
---|
27 |
|
---|
28 |
|
---|
29 | //#define ESP8266_USE_SOFTWARE_SERIAL
|
---|
30 |
|
---|
31 |
|
---|
32 | #ifdef ESP8266_USE_SOFTWARE_SERIAL
|
---|
33 | #include "SoftwareSerial.h"
|
---|
34 | #endif
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Provide an easy-to-use way to manipulate ESP8266.
|
---|
39 | */
|
---|
40 | class ESP8266 {
|
---|
41 | public:
|
---|
42 |
|
---|
43 | #ifdef ESP8266_USE_SOFTWARE_SERIAL
|
---|
44 | /*
|
---|
45 | * Begin.
|
---|
46 | *
|
---|
47 | * @param uart - an reference of SoftwareSerial object.
|
---|
48 | * @param baud - the buad rate to communicate with ESP8266(default:115200).
|
---|
49 | *
|
---|
50 | * @warning parameter baud depends on the AT firmware. 115200 is an common value.
|
---|
51 | */
|
---|
52 | void begin(SoftwareSerial &uart, uint32_t baud = 115200);
|
---|
53 | #else /* HardwareSerial */
|
---|
54 | /*
|
---|
55 | * Begin.
|
---|
56 | *
|
---|
57 | * @param uart - an reference of HardwareSerial object.
|
---|
58 | * @param baud - the buad rate to communicate with ESP8266(default:115200).
|
---|
59 | *
|
---|
60 | * @warning parameter baud depends on the AT firmware. 115200 is an common value.
|
---|
61 | */
|
---|
62 | void begin(HardwareSerial &uart, uint32_t baud = 115200);
|
---|
63 | #endif
|
---|
64 |
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Verify ESP8266 whether live or not.
|
---|
68 | *
|
---|
69 | * Actually, this method will send command "AT" to ESP8266 and waiting for "OK".
|
---|
70 | *
|
---|
71 | * @retval true - alive.
|
---|
72 | * @retval false - dead.
|
---|
73 | */
|
---|
74 | bool kick(void);
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Restart ESP8266 by "AT+RST".
|
---|
78 | *
|
---|
79 | * This method will take 3 seconds or more.
|
---|
80 | *
|
---|
81 | * @retval true - success.
|
---|
82 | * @retval false - failure.
|
---|
83 | */
|
---|
84 | bool restart(void);
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Get the version of AT Command Set.
|
---|
88 | *
|
---|
89 | * @return the string of version.
|
---|
90 | */
|
---|
91 | String getVersion(void);
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Set operation mode to staion.
|
---|
95 | *
|
---|
96 | * @retval true - success.
|
---|
97 | * @retval false - failure.
|
---|
98 | */
|
---|
99 | bool setOprToStation(void);
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Set operation mode to softap.
|
---|
103 | *
|
---|
104 | * @retval true - success.
|
---|
105 | * @retval false - failure.
|
---|
106 | */
|
---|
107 | bool setOprToSoftAP(void);
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Set operation mode to station + softap.
|
---|
111 | *
|
---|
112 | * @retval true - success.
|
---|
113 | * @retval false - failure.
|
---|
114 | */
|
---|
115 | bool setOprToStationSoftAP(void);
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Search available AP list and return it.
|
---|
119 | *
|
---|
120 | * @return the list of available APs.
|
---|
121 | * @note This method will occupy a lot of memeory(hundreds of Bytes to a couple of KBytes).
|
---|
122 | * Do not call this method unless you must and ensure that your board has enough memery left.
|
---|
123 | */
|
---|
124 | String getAPList(void);
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Join in AP.
|
---|
128 | *
|
---|
129 | * @param ssid - SSID of AP to join in.
|
---|
130 | * @param pwd - Password of AP to join in.
|
---|
131 | * @retval true - success.
|
---|
132 | * @retval false - failure.
|
---|
133 | * @note This method will take a couple of seconds.
|
---|
134 | */
|
---|
135 | bool joinAP(String ssid, String pwd);
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Leave AP joined before.
|
---|
139 | *
|
---|
140 | * @retval true - success.
|
---|
141 | * @retval false - failure.
|
---|
142 | */
|
---|
143 | bool leaveAP(void);
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Set SoftAP parameters.
|
---|
147 | *
|
---|
148 | * @param ssid - SSID of SoftAP.
|
---|
149 | * @param pwd - PASSWORD of SoftAP.
|
---|
150 | * @param chl - the channel (1 - 13, default: 7).
|
---|
151 | * @param ecn - the way of encrypstion (0 - OPEN, 1 - WEP,
|
---|
152 | * 2 - WPA_PSK, 3 - WPA2_PSK, 4 - WPA_WPA2_PSK, default: 4).
|
---|
153 | * @note This method should not be called when station mode.
|
---|
154 | */
|
---|
155 | bool setSoftAPParam(String ssid, String pwd, uint8_t chl = 7, uint8_t ecn = 4);
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Get the IP list of devices connected to SoftAP.
|
---|
159 | *
|
---|
160 | * @return the list of IP.
|
---|
161 | * @note This method should not be called when station mode.
|
---|
162 | */
|
---|
163 | String getJoinedDeviceIP(void);
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Get the current status of connection(UDP and TCP).
|
---|
167 | *
|
---|
168 | * @return the status.
|
---|
169 | */
|
---|
170 | String getIPStatus(void);
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Get the IP address of ESP8266.
|
---|
174 | *
|
---|
175 | * @return the IP list.
|
---|
176 | */
|
---|
177 | String getLocalIP(void);
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Enable IP MUX(multiple connection mode).
|
---|
181 | *
|
---|
182 | * In multiple connection mode, a couple of TCP and UDP communication can be builded.
|
---|
183 | * They can be distinguished by the identifier of TCP or UDP named mux_id.
|
---|
184 | *
|
---|
185 | * @retval true - success.
|
---|
186 | * @retval false - failure.
|
---|
187 | */
|
---|
188 | bool enableMUX(void);
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Disable IP MUX(single connection mode).
|
---|
192 | *
|
---|
193 | * In single connection mode, only one TCP or UDP communication can be builded.
|
---|
194 | *
|
---|
195 | * @retval true - success.
|
---|
196 | * @retval false - failure.
|
---|
197 | */
|
---|
198 | bool disableMUX(void);
|
---|
199 |
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Create TCP connection in single mode.
|
---|
203 | *
|
---|
204 | * @param addr - the IP or domain name of the target host.
|
---|
205 | * @param port - the port number of the target host.
|
---|
206 | * @retval true - success.
|
---|
207 | * @retval false - failure.
|
---|
208 | */
|
---|
209 | bool createTCP(String addr, uint32_t port);
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Release TCP connection in single mode.
|
---|
213 | *
|
---|
214 | * @retval true - success.
|
---|
215 | * @retval false - failure.
|
---|
216 | */
|
---|
217 | bool releaseTCP(void);
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Register UDP port number in single mode.
|
---|
221 | *
|
---|
222 | * @param addr - the IP or domain name of the target host.
|
---|
223 | * @param port - the port number of the target host.
|
---|
224 | * @retval true - success.
|
---|
225 | * @retval false - failure.
|
---|
226 | */
|
---|
227 | bool registerUDP(String addr, uint32_t port);
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Unregister UDP port number in single mode.
|
---|
231 | *
|
---|
232 | * @retval true - success.
|
---|
233 | * @retval false - failure.
|
---|
234 | */
|
---|
235 | bool unregisterUDP(void);
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Create TCP connection in multiple mode.
|
---|
239 | *
|
---|
240 | * @param mux_id - the identifier of this TCP(available value: 0 - 4).
|
---|
241 | * @param addr - the IP or domain name of the target host.
|
---|
242 | * @param port - the port number of the target host.
|
---|
243 | * @retval true - success.
|
---|
244 | * @retval false - failure.
|
---|
245 | */
|
---|
246 | bool createTCP(uint8_t mux_id, String addr, uint32_t port);
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Release TCP connection in multiple mode.
|
---|
250 | *
|
---|
251 | * @param mux_id - the identifier of this TCP(available value: 0 - 4).
|
---|
252 | * @retval true - success.
|
---|
253 | * @retval false - failure.
|
---|
254 | */
|
---|
255 | bool releaseTCP(uint8_t mux_id);
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Register UDP port number in multiple mode.
|
---|
259 | *
|
---|
260 | * @param mux_id - the identifier of this TCP(available value: 0 - 4).
|
---|
261 | * @param addr - the IP or domain name of the target host.
|
---|
262 | * @param port - the port number of the target host.
|
---|
263 | * @retval true - success.
|
---|
264 | * @retval false - failure.
|
---|
265 | */
|
---|
266 | bool registerUDP(uint8_t mux_id, String addr, uint32_t port);
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Unregister UDP port number in multiple mode.
|
---|
270 | *
|
---|
271 | * @param mux_id - the identifier of this TCP(available value: 0 - 4).
|
---|
272 | * @retval true - success.
|
---|
273 | * @retval false - failure.
|
---|
274 | */
|
---|
275 | bool unregisterUDP(uint8_t mux_id);
|
---|
276 |
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Set the timeout of TCP Server.
|
---|
280 | *
|
---|
281 | * @param timeout - the duration for timeout by second(0 ~ 28800, default:180).
|
---|
282 | * @retval true - success.
|
---|
283 | * @retval false - failure.
|
---|
284 | */
|
---|
285 | bool setTCPServerTimeout(uint32_t timeout = 180);
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Start TCP Server(Only in multiple mode).
|
---|
289 | *
|
---|
290 | * After started, user should call method: getIPStatus to know the status of TCP connections.
|
---|
291 | * The methods of receiving data can be called for user's any purpose. After communication,
|
---|
292 | * release the TCP connection is needed by calling method: releaseTCP with mux_id.
|
---|
293 | *
|
---|
294 | * @param port - the port number to listen(default: 333).
|
---|
295 | * @retval true - success.
|
---|
296 | * @retval false - failure.
|
---|
297 | *
|
---|
298 | * @see String getIPStatus(void);
|
---|
299 | * @see uint32_t recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t len, uint32_t timeout);
|
---|
300 | * @see bool releaseTCP(uint8_t mux_id);
|
---|
301 | */
|
---|
302 | bool startTCPServer(uint32_t port = 333);
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Stop TCP Server(Only in multiple mode).
|
---|
306 | *
|
---|
307 | * @retval true - success.
|
---|
308 | * @retval false - failure.
|
---|
309 | */
|
---|
310 | bool stopTCPServer(void);
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Start Server(Only in multiple mode).
|
---|
314 | *
|
---|
315 | * @param port - the port number to listen(default: 333).
|
---|
316 | * @retval true - success.
|
---|
317 | * @retval false - failure.
|
---|
318 | *
|
---|
319 | * @see String getIPStatus(void);
|
---|
320 | * @see uint32_t recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t len, uint32_t timeout);
|
---|
321 | */
|
---|
322 | bool startServer(uint32_t port = 333);
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Stop Server(Only in multiple mode).
|
---|
326 | *
|
---|
327 | * @retval true - success.
|
---|
328 | * @retval false - failure.
|
---|
329 | */
|
---|
330 | bool stopServer(void);
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * Send data based on TCP or UDP builded already in single mode.
|
---|
334 | *
|
---|
335 | * @param buffer - the buffer of data to send.
|
---|
336 | * @param len - the length of data to send.
|
---|
337 | * @retval true - success.
|
---|
338 | * @retval false - failure.
|
---|
339 | */
|
---|
340 | bool send(const uint8_t *buffer, uint32_t len);
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Send data based on one of TCP or UDP builded already in multiple mode.
|
---|
344 | *
|
---|
345 | * @param mux_id - the identifier of this TCP(available value: 0 - 4).
|
---|
346 | * @param buffer - the buffer of data to send.
|
---|
347 | * @param len - the length of data to send.
|
---|
348 | * @retval true - success.
|
---|
349 | * @retval false - failure.
|
---|
350 | */
|
---|
351 | bool send(uint8_t mux_id, const uint8_t *buffer, uint32_t len);
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Send data based on TCP or UDP builded already in single mode.
|
---|
355 | *
|
---|
356 | * @param str - String to send.
|
---|
357 | * @retval true - success.
|
---|
358 | * @retval false - failure.
|
---|
359 | */
|
---|
360 | bool send(String &str);
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * Send data based on one of TCP or UDP builded already in multiple mode.
|
---|
364 | *
|
---|
365 | * @param mux_id - the identifier of this TCP(available value: 0 - 4).
|
---|
366 | * @param str - String to send.
|
---|
367 | * @retval true - success.
|
---|
368 | * @retval false - failure.
|
---|
369 | */
|
---|
370 | bool send(uint8_t mux_id, String &str);
|
---|
371 |
|
---|
372 | /**
|
---|
373 | * Receive data from TCP or UDP builded already in single mode.
|
---|
374 | *
|
---|
375 | * @param buffer - the buffer for storing data.
|
---|
376 | * @param buffer_size - the length of the buffer.
|
---|
377 | * @param timeout - the time waiting data.
|
---|
378 | * @return the length of data received actually.
|
---|
379 | */
|
---|
380 | uint32_t recv(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout = 1000);
|
---|
381 |
|
---|
382 | /**
|
---|
383 | * Receive data from one of TCP or UDP builded already in multiple mode.
|
---|
384 | *
|
---|
385 | * @param mux_id - the identifier of this TCP(available value: 0 - 4).
|
---|
386 | * @param buffer - the buffer for storing data.
|
---|
387 | * @param buffer_size - the length of the buffer.
|
---|
388 | * @param timeout - the time waiting data.
|
---|
389 | * @return the length of data received actually.
|
---|
390 | */
|
---|
391 | uint32_t recv(uint8_t mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout = 1000);
|
---|
392 |
|
---|
393 | /**
|
---|
394 | * Receive data from all of TCP or UDP builded already in multiple mode.
|
---|
395 | *
|
---|
396 | * After return, coming_mux_id store the id of TCP or UDP from which data coming.
|
---|
397 | * User should read the value of coming_mux_id and decide what next to do.
|
---|
398 | *
|
---|
399 | * @param coming_mux_id - the identifier of TCP or UDP.
|
---|
400 | * @param buffer - the buffer for storing data.
|
---|
401 | * @param buffer_size - the length of the buffer.
|
---|
402 | * @param timeout - the time waiting data.
|
---|
403 | * @return the length of data received actually.
|
---|
404 | */
|
---|
405 | uint32_t recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout = 1000);
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Check data is available.
|
---|
409 | *
|
---|
410 | * @return 1 : data is available, 0 : data is not available
|
---|
411 | */
|
---|
412 | int dataAvailable(void);
|
---|
413 |
|
---|
414 | private:
|
---|
415 |
|
---|
416 | /*
|
---|
417 | * Empty the buffer or UART RX.
|
---|
418 | */
|
---|
419 | void rx_empty(void);
|
---|
420 |
|
---|
421 | /*
|
---|
422 | * Recvive data from uart. Return all received data if target found or timeout.
|
---|
423 | */
|
---|
424 | String recvString(String target, uint32_t timeout = 1000);
|
---|
425 |
|
---|
426 | /*
|
---|
427 | * Recvive data from uart. Return all received data if one of target1 and target2 found or timeout.
|
---|
428 | */
|
---|
429 | String recvString(String target1, String target2, uint32_t timeout = 1000);
|
---|
430 |
|
---|
431 | /*
|
---|
432 | * Recvive data from uart. Return all received data if one of target1, target2 and target3 found or timeout.
|
---|
433 | */
|
---|
434 | String recvString(String target1, String target2, String target3, uint32_t timeout = 1000);
|
---|
435 |
|
---|
436 | /*
|
---|
437 | * Recvive data from uart and search first target. Return true if target found, false for timeout.
|
---|
438 | */
|
---|
439 | bool recvFind(String target, uint32_t timeout = 1000);
|
---|
440 |
|
---|
441 | /*
|
---|
442 | * Recvive data from uart and search first target and cut out the substring between begin and end(excluding begin and end self).
|
---|
443 | * Return true if target found, false for timeout.
|
---|
444 | */
|
---|
445 | bool recvFindAndFilter(String target, String begin, String end, String &data, uint32_t timeout = 1000);
|
---|
446 |
|
---|
447 | /*
|
---|
448 | * Receive a package from uart.
|
---|
449 | *
|
---|
450 | * @param buffer - the buffer storing data.
|
---|
451 | * @param buffer_size - guess what!
|
---|
452 | * @param data_len - the length of data actually received(maybe more than buffer_size, the remained data will be abandoned).
|
---|
453 | * @param timeout - the duration waitting data comming.
|
---|
454 | * @param coming_mux_id - in single connection mode, should be NULL and not NULL in multiple.
|
---|
455 | */
|
---|
456 | uint32_t recvPkg(uint8_t *buffer, uint32_t buffer_size, uint32_t *data_len, uint32_t timeout, uint8_t *coming_mux_id);
|
---|
457 |
|
---|
458 |
|
---|
459 | bool eAT(void);
|
---|
460 | bool eATRST(void);
|
---|
461 | bool eATGMR(String &version);
|
---|
462 |
|
---|
463 | bool qATCWMODE(uint8_t *mode);
|
---|
464 | bool sATCWMODE(uint8_t mode);
|
---|
465 | bool sATCWJAP(String ssid, String pwd);
|
---|
466 | bool eATCWLAP(String &list);
|
---|
467 | bool eATCWQAP(void);
|
---|
468 | bool sATCWSAP(String ssid, String pwd, uint8_t chl, uint8_t ecn);
|
---|
469 | bool eATCWLIF(String &list);
|
---|
470 |
|
---|
471 | bool eATCIPSTATUS(String &list);
|
---|
472 | bool sATCIPSTARTSingle(String type, String addr, uint32_t port);
|
---|
473 | bool sATCIPSTARTMultiple(uint8_t mux_id, String type, String addr, uint32_t port);
|
---|
474 | bool sATCIPSENDSingle(const uint8_t *buffer, uint32_t len);
|
---|
475 | bool sATCIPSENDMultiple(uint8_t mux_id, const uint8_t *buffer, uint32_t len);
|
---|
476 | bool sATCIPSENDSingle(String &str);
|
---|
477 | bool sATCIPSENDMultiple(uint8_t mux_id, String &str);
|
---|
478 | bool sATCIPCLOSEMulitple(uint8_t mux_id);
|
---|
479 | bool eATCIPCLOSESingle(void);
|
---|
480 | bool eATCIFSR(String &list);
|
---|
481 | bool sATCIPMUX(uint8_t mode);
|
---|
482 | bool sATCIPSERVER(uint8_t mode, uint32_t port = 333);
|
---|
483 | bool sATCIPSTO(uint32_t timeout);
|
---|
484 |
|
---|
485 | /*
|
---|
486 | * +IPD,len:data
|
---|
487 | * +IPD,id,len:data
|
---|
488 | */
|
---|
489 |
|
---|
490 | #ifdef ESP8266_USE_SOFTWARE_SERIAL
|
---|
491 | SoftwareSerial *m_puart; /* The UART to communicate with ESP8266 */
|
---|
492 | #else
|
---|
493 | HardwareSerial *m_puart; /* The UART to communicate with ESP8266 */
|
---|
494 | #endif
|
---|
495 | uint32_t m_baud;
|
---|
496 | };
|
---|
497 |
|
---|
498 | #endif /* #ifndef __ESP8266_H__ */
|
---|
499 |
|
---|