source: rtos_arduino/trunk/arduino_lib/libraries/ESP8266_Arudino_AT/ESP8266.h@ 276

Last change on this file since 276 was 276, checked in by ertl-honda, 7 years ago

1.3.0.0に対応

File size: 17.9 KB
Line 
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//#define ESP8266_USE_SOFTWARE_SERIAL
29
30#define ESP8266_SUPPORT_VERSION "AT version:0.25 or 0.40 or 1.3.0.0"
31#define ESP8266_SUPPORT_VERSION_025 "AT version:0.25"
32#define ESP8266_SUPPORT_VERSION_040 "AT version:0.40"
33#define ESP8266_SUPPORT_VERSION_130 "AT version:1.3.0.0"
34
35#define ESP8266_NUM_CONNECTION 5
36
37#ifdef ESP8266_USE_SOFTWARE_SERIAL
38#include "SoftwareSerial.h"
39#endif /* ESP8266_USE_SOFTWARE_SERIAL */
40
41#define ESP8266_CONNECTION_OPEN 0x01
42#define ESP8266_CONNECTION_CLOSE 0x02
43
44#define ESP8266_CONNECTION_TCP 0x01
45#define ESP8266_CONNECTION_UDP 0x02
46
47#define ESP8266_CONNECTION_CLIENT 0x01
48#define ESP8266_CONNECTION_SERVER 0x02
49
50#define ESP8266_STATUS_GOTIP 0x02
51#define ESP8266_STATUS_CONNECTED 0x03
52#define ESP8266_STATUS_DISCONNECTED 0x04
53
54#define ESP8266_WMODE_STATION 0x01
55#define ESP8266_WMODE_SOFTAP 0x02
56#define ESP8266_WMODE_AP_STATION 0x03
57
58struct esp8266_connection_status
59{
60 uint8_t connection_type;
61 uint8_t protocol_type;
62};
63
64#define ESP8266_RINGBUFFER_SIZE 1024
65
66class ESP8266_RingBuffer
67{
68 public:
69 ESP8266_RingBuffer(void);
70 void init(void);
71 void free(void) {len = head = tail = 0;};
72 bool write(uint8_t c);
73 uint8_t read(void);
74 uint32_t copy(uint8_t *pdata, uint32_t size);
75 uint8_t peek(void);
76 bool available(void) {return (len > 0);};
77 bool isFull(void) {return (len == ESP8266_RINGBUFFER_SIZE);};
78 uint32_t length(void) {return len;};
79
80 private:
81 uint8_t *buffer;
82 uint32_t head;
83 uint32_t tail;
84 uint32_t len;
85};
86
87/**
88 * Provide an easy-to-use way to manipulate ESP8266.
89 */
90class ESP8266 {
91 public:
92
93 /*
94 * Begin.
95 *
96 * @param uart - an reference of SoftwareSerial object.
97 * @param baud - the buad rate to communicate with ESP8266(default:115200).
98 *
99 * @retval 0 : Success
100 * @retval 1 : Can not communicate ESP8266
101 * @retval 2 : Firmware Version mismatch.
102 *
103 * @warning parameter baud depends on the AT firmware. 115200 is an common value.
104 */
105#ifdef ESP8266_USE_SOFTWARE_SERIAL
106 int begin(SoftwareSerial &uart, uint32_t baud = 115200);
107#else /* HardwareSerial */
108 int begin(HardwareSerial &uart, uint32_t baud = 115200);
109#endif /* ESP8266_USE_SOFTWARE_SERIAL */
110
111 /**
112 * Verify ESP8266 whether live or not.
113 *
114 * Actually, this method will send command "AT" to ESP8266 and waiting for "OK".
115 *
116 * @retval true - alive.
117 * @retval false - dead.
118 */
119 bool kick(void);
120
121 /**
122 * Restart ESP8266 by "AT+RST".
123 *
124 * This method will take 3 seconds or more.
125 *
126 * @retval true - success.
127 * @retval false - failure.
128 */
129 bool restart(void);
130
131 /**
132 * Get the version of AT Command Set.
133 *
134 * @return the string of version.
135 */
136 String getVersion(void);
137
138 /**
139 * Set operation mode to staion.
140 *
141 * @retval true - success.
142 * @retval false - failure.
143 */
144 bool setOprToStation(void);
145
146 /**
147 * Set operation mode to softap.
148 *
149 * @retval true - success.
150 * @retval false - failure.
151 */
152 bool setOprToSoftAP(void);
153
154 /**
155 * Set operation mode to station + softap.
156 *
157 * @retval true - success.
158 * @retval false - failure.
159 */
160 bool setOprToStationSoftAP(void);
161
162 /**
163 * Search available AP list and return it.
164 *
165 * @return the list of available APs.
166 * @note This method will occupy a lot of memeory(hundreds of Bytes to a couple of KBytes).
167 * Do not call this method unless you must and ensure that your board has enough memery left.
168 */
169 String getAPList(void);
170
171 /**
172 * Join in AP.
173 *
174 * @param ssid - SSID of AP to join in.
175 * @param pwd - Password of AP to join in.
176 * @retval true - success.
177 * @retval false - failure.
178 * @note This method will take a couple of seconds.
179 */
180 bool joinAP(String ssid, String pwd);
181
182 /**
183 * Leave AP joined before.
184 *
185 * @retval true - success.
186 * @retval false - failure.
187 */
188 bool leaveAP(void);
189
190 /**
191 * Set SoftAP parameters.
192 *
193 * @param ssid - SSID of SoftAP.
194 * @param pwd - PASSWORD of SoftAP.
195 * @param chl - the channel (1 - 13, default: 7).
196 * @param ecn - the way of encrypstion (0 - OPEN, 1 - WEP,
197 * 2 - WPA_PSK, 3 - WPA2_PSK, 4 - WPA_WPA2_PSK, default: 4).
198 * @note This method should not be called when station mode.
199 */
200 bool setSoftAPParam(String ssid, String pwd, uint8_t chl = 7, uint8_t ecn = 4);
201
202 /**
203 * Get the IP list of devices connected to SoftAP.
204 *
205 * @return the list of IP.
206 * @note This method should not be called when station mode.
207 */
208 String getJoinedDeviceIP(void);
209
210 /**
211 * Get the current status of connection(UDP and TCP).
212 *
213 * @return the status.
214 */
215 String getIPStatus(void);
216
217 /**
218 * Get the IP address of ESP8266.
219 *
220 * @return the IP list.
221 */
222 String getLocalIP(void);
223
224 /**
225 * Enable IP MUX(multiple connection mode).
226 *
227 * In multiple connection mode, a couple of TCP and UDP communication can be builded.
228 * They can be distinguished by the identifier of TCP or UDP named mux_id.
229 *
230 * @retval true - success.
231 * @retval false - failure.
232 */
233 bool enableMUX(void);
234
235 /**
236 * Disable IP MUX(single connection mode).
237 *
238 * In single connection mode, only one TCP or UDP communication can be builded.
239 *
240 * @retval true - success.
241 * @retval false - failure.
242 */
243 bool disableMUX(void);
244
245
246 /**
247 * Create TCP connection in single mode.
248 *
249 * @param addr - the IP or domain name of the target host.
250 * @param port - the port number of the target host.
251 * @retval true - success.
252 * @retval false - failure.
253 */
254 bool createTCP(String addr, uint32_t port);
255
256 /**
257 * Release TCP connection in single mode.
258 *
259 * @retval true - success.
260 * @retval false - failure.
261 */
262 bool releaseTCP(void);
263
264 /**
265 * Register UDP port number in single mode.
266 *
267 * @param addr - the IP or domain name of the target host.
268 * @param port - the port number of the target host.
269 * @retval true - success.
270 * @retval false - failure.
271 */
272 bool registerUDP(String addr, uint32_t port);
273
274 /**
275 * Unregister UDP port number in single mode.
276 *
277 * @retval true - success.
278 * @retval false - failure.
279 */
280 bool unregisterUDP(void);
281
282 /**
283 * Create TCP connection in multiple mode.
284 *
285 * @param mux_id - the identifier of this TCP(available value: 0 - 4).
286 * @param addr - the IP or domain name of the target host.
287 * @param port - the port number of the target host.
288 * @retval true - success.
289 * @retval false - failure.
290 */
291 bool createTCP(uint8_t mux_id, String addr, uint32_t port);
292
293 /**
294 * Release TCP connection in multiple mode.
295 *
296 * @param mux_id - the identifier of this TCP(available value: 0 - 4).
297 * @retval true - success.
298 * @retval false - failure.
299 */
300 bool releaseTCP(uint8_t mux_id);
301
302 /**
303 * Register UDP port number in multiple mode.
304 *
305 * @param mux_id - the identifier of this TCP(available value: 0 - 4).
306 * @param addr - the IP or domain name of the target host.
307 * @param port - the port number of the target host.
308 * @retval true - success.
309 * @retval false - failure.
310 */
311 bool registerUDP(uint8_t mux_id, String addr, uint32_t port);
312
313 /**
314 * Unregister UDP port number in multiple mode.
315 *
316 * @param mux_id - the identifier of this TCP(available value: 0 - 4).
317 * @retval true - success.
318 * @retval false - failure.
319 */
320 bool unregisterUDP(uint8_t mux_id);
321
322
323 /**
324 * Set the timeout of TCP Server.
325 *
326 * @param timeout - the duration for timeout by second(0 ~ 28800, default:180).
327 * @retval true - success.
328 * @retval false - failure.
329 */
330 bool setTCPServerTimeout(uint32_t timeout = 180);
331
332 /**
333 * Start TCP Server(Only in multiple mode).
334 *
335 * After started, user should call method: getIPStatus to know the status of TCP connections.
336 * The methods of receiving data can be called for user's any purpose. After communication,
337 * release the TCP connection is needed by calling method: releaseTCP with mux_id.
338 *
339 * @param port - the port number to listen(default: 333).
340 * @retval true - success.
341 * @retval false - failure.
342 *
343 * @see String getIPStatus(void);
344 * @see uint32_t recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t len, uint32_t timeout);
345 * @see bool releaseTCP(uint8_t mux_id);
346 */
347 bool startTCPServer(uint32_t port = 333);
348
349 /**
350 * Stop TCP Server(Only in multiple mode).
351 *
352 * @retval true - success.
353 * @retval false - failure.
354 */
355 bool stopTCPServer(void);
356
357 /**
358 * Start Server(Only in multiple mode).
359 *
360 * @param port - the port number to listen(default: 333).
361 * @retval true - success.
362 * @retval false - failure.
363 *
364 * @see String getIPStatus(void);
365 * @see uint32_t recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t len, uint32_t timeout);
366 */
367 bool startServer(uint32_t port = 333);
368
369 /**
370 * Stop Server(Only in multiple mode).
371 *
372 * @retval true - success.
373 * @retval false - failure.
374 */
375 bool stopServer(void);
376
377 /**
378 * Send data based on TCP or UDP builded already in single mode.
379 *
380 * @param buffer - the buffer of data to send.
381 * @param len - the length of data to send.
382 * @retval true - success.
383 * @retval false - failure.
384 */
385 bool send(const uint8_t *buffer, uint32_t len);
386
387 /**
388 * Send data based on one of TCP or UDP builded already in multiple mode.
389 *
390 * @param mux_id - the identifier of this TCP(available value: 0 - 4).
391 * @param buffer - the buffer of data to send.
392 * @param len - the length of data to send.
393 * @retval true - success.
394 * @retval false - failure.
395 */
396 bool send(uint8_t mux_id, const uint8_t *buffer, uint32_t len);
397
398 /**
399 * Send data based on TCP or UDP builded already in single mode.
400 *
401 * @param str - String to send.
402 * @retval true - success.
403 * @retval false - failure.
404 */
405 bool send(String &str);
406
407 /**
408 * Send data based on one of TCP or UDP builded already in multiple mode.
409 *
410 * @param mux_id - the identifier of this TCP(available value: 0 - 4).
411 * @param str - String to send.
412 * @retval true - success.
413 * @retval false - failure.
414 */
415 bool send(uint8_t mux_id, String &str);
416
417 /**
418 * Receive data from TCP or UDP builded already in single mode.
419 *
420 * @param buffer - the buffer for storing data.
421 * @param buffer_size - the length of the buffer.
422 * @param timeout - the time waiting data.
423 * @return the length of data received actually.
424 */
425 uint32_t recv(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout = 1000);
426
427 /**
428 * Receive data from one of TCP or UDP builded already in multiple mode.
429 *
430 * @param mux_id - the identifier of this TCP(available value: 0 - 4).
431 * @param buffer - the buffer for storing data.
432 * @param buffer_size - the length of the buffer.
433 * @param timeout - the time waiting data.
434 * @return the length of data received actually.
435 */
436 uint32_t recv(uint8_t mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout = 1000);
437
438 /**
439 * Receive data from all of TCP or UDP builded already in multiple mode.
440 *
441 * After return, coming_mux_id store the id of TCP or UDP from which data coming.
442 * User should read the value of coming_mux_id and decide what next to do.
443 *
444 * @param coming_mux_id - the identifier of TCP or UDP.
445 * @param buffer - the buffer for storing data.
446 * @param buffer_size - the length of the buffer.
447 * @param timeout - the time waiting data.
448 * @return the length of data received actually.
449 */
450 uint32_t recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout = 1000);
451
452 /**
453 * Check data is available in specific connection.
454 *
455 * @param mux_id - the identifier of this TCP(available value: 0 - 4).
456 * @return true - data is available.
457 * @return false - data is not available.
458 */
459 bool isDataAvailable(uint8_t mux_id);
460
461 /**
462 * Check data is available in any connection.
463 *
464 * @return true - data is available.
465 * @return false - data is not available.
466 */
467 bool isDataAvailable(void);
468
469 /**
470 * Check connection is opned in single mode.
471 *
472 * @return true - opend.
473 * @return false - closed.
474 */
475 bool isConnected(void);
476
477 /**
478 * Check specific connection is opned in multiple mode.
479 *
480 * @return true - opend.
481 * @return false - closed.
482 */
483 bool isConnected(uint8_t mux_id);
484
485 /**
486 * Get connection link status
487 *
488 * @return true - success.
489 * @return false - error.
490 */
491 bool getMuxCStatus(uint8_t *mux_id_ptn);
492
493 friend class ESP8266Client;
494
495 private:
496
497 /*
498 * Empty the buffer or UART RX.
499 */
500 void rx_empty(void);
501
502 /*
503 * Read all data in UART RX and store to Rceive Buffer .
504 */
505 void rx_update(void);
506
507 /*
508 * Recvive data from uart. Return all received data if one of target1, target2 and target3 found or timeout.
509 */
510 int recvString(String target1, String target2, String target3, uint32_t timeout = 1000);
511
512 /*
513 * Recvive data from uart and search first target. Return true if target found, false for timeout.
514 */
515 bool recvFind(String target, uint32_t timeout = 1000);
516
517 /*
518 * Recvive data from uart and search first target and cut out the substring between begin and end(excluding begin and end self). a
519 * Return true if target found, false for timeout.
520 */
521 bool recvFindAndFilter(String target, String begin, String end, String &data, uint32_t timeout = 1000);
522
523 /*
524 * Receive a package from uart.
525 *
526 * @param buffer - the buffer storing data.
527 * @param buffer_size - guess what!
528 * @param data_len - the length of data actually received(maybe more than buffer_size, the remained data will be abandoned).
529 * @param timeout - the duration waitting data comming.
530 * @param coming_mux_id - in single connection mode, should be NULL and not NULL in multiple.
531 */
532 uint32_t recvPkg(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout, uint8_t mux_id, uint8_t *coming_mux_id);
533
534 bool eAT(void);
535 bool eATRST(void);
536 bool eATGMR(String &version);
537
538 bool qATCWMODE_CUR(uint8_t *mode);
539 bool sATCWMODE_CUR(uint8_t mode);
540 bool sATCWJAP_CUR(String ssid, String pwd);
541 bool eATCWLAP(String &list);
542 bool eATCWQAP(void);
543 bool sATCWSAP_CUR(String ssid, String pwd, uint8_t chl, uint8_t ecn);
544 bool eATCWLIF(String &list);
545
546 bool eATCIPSTATUS(String &list);
547 bool sATCIPSTARTSingle(String type, String addr, uint32_t port);
548 bool sATCIPSTARTMultiple(uint8_t mux_id, String type, String addr, uint32_t port);
549 bool sATCIPSENDSingle(const uint8_t *buffer, uint32_t len);
550 bool sATCIPSENDMultiple(uint8_t mux_id, const uint8_t *buffer, uint32_t len);
551 bool sATCIPSENDSingle(String &str);
552 bool sATCIPSENDMultiple(uint8_t mux_id, String &str);
553 bool sATCIPCLOSEMulitple(uint8_t mux_id);
554 bool eATCIPCLOSESingle(void);
555 bool eATCIFSR(String &list);
556 bool sATCIPMUX(uint8_t mode);
557 bool sATCIPSERVER(uint8_t mode, uint32_t port = 333);
558 bool sATCIPSTO(uint32_t timeout);
559
560 void initialize_status(void);
561
562 uint32_t recvIPD(uint32_t timeout, uint8_t *buffer = NULL, uint32_t buffer_size = 0,
563 uint8_t mux_id = 0, uint8_t *coming_mux_id = NULL);
564
565 void recvAsyncdata(uint32_t timeout = 0);
566
567 /**
568 * Restart ESP8266 by "AT+RST".
569 *
570 * This method will take 3 seconds or more.
571 *
572 * @retval true - success.
573 * @retval false - failure.
574 */
575 bool _restart(void);
576
577#ifdef ESP8266_USE_SOFTWARE_SERIAL
578 SoftwareSerial *m_puart; /* The UART to communicate with ESP8266 */
579#else
580 HardwareSerial *m_puart; /* The UART to communicate with ESP8266 */
581#endif
582 uint32_t m_baud;
583
584 uint8_t wifi_status;
585
586 bool mux_mode;
587
588 uint8_t connection_bitmap;
589
590 esp8266_connection_status connection_status[ESP8266_NUM_CONNECTION];
591
592 String rx_cmd;
593
594 protected:
595 /**
596 * Rceive Buffer for each link.
597 */
598 ESP8266_RingBuffer rx_buffer[ESP8266_NUM_CONNECTION];
599};
600
601extern ESP8266 WiFi;
602
603#endif /* #ifndef __ESP8266_H__ */
604
Note: See TracBrowser for help on using the repository browser.