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

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

まとめたVersin表記も追加

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