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

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

サポートバージョンの追加

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