Added monitoring feature of ESP8266's UART

Dependents:   ESP8266_W7500_Example DualNetworkInterface-Basic

Fork of ESP8266Interface by ESP8266

Committer:
SteveKim
Date:
Tue Jul 14 09:09:55 2015 +0000
Revision:
49:2c05d747bb91
Parent:
45:c180905b5b79
Added monitoring-feature of ESP8266's UART.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:fb4494783863 1 /* Copyright (C) 2012 mbed.org, MIT License
samux 1:fb4494783863 2 *
samux 1:fb4494783863 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 1:fb4494783863 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
samux 1:fb4494783863 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
samux 1:fb4494783863 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
samux 1:fb4494783863 7 * furnished to do so, subject to the following conditions:
samux 1:fb4494783863 8 *
samux 1:fb4494783863 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 1:fb4494783863 10 * substantial portions of the Software.
samux 1:fb4494783863 11 *
samux 1:fb4494783863 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 1:fb4494783863 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 1:fb4494783863 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 1:fb4494783863 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 1:fb4494783863 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 1:fb4494783863 17 *
samux 1:fb4494783863 18 * @section DESCRIPTION
samux 1:fb4494783863 19 *
michaeljkoster 16:3f0efaa57a12 20 * ESP8266 serial wifi module
samux 1:fb4494783863 21 *
samux 1:fb4494783863 22 * Datasheet:
samux 1:fb4494783863 23 *
michaeljkoster 16:3f0efaa57a12 24 * http://www.electrodragon.com/w/Wi07c
samux 1:fb4494783863 25 */
samux 1:fb4494783863 26
michaeljkoster 13:41098c907200 27 #ifndef ESP8266_H
michaeljkoster 13:41098c907200 28 #define ESP8266_H
samux 1:fb4494783863 29
samux 1:fb4494783863 30 #include "mbed.h"
samux 1:fb4494783863 31 #include "CBuffer.h"
samux 1:fb4494783863 32
samux 1:fb4494783863 33 #define DEFAULT_WAIT_RESP_TIMEOUT 500
mbedAustin 30:c035696b9397 34 #define ESP_TCP_TYPE 1
mbedAustin 30:c035696b9397 35 #define ESP_UDP_TYPE 0
mbedAustin 36:e1545c6c2cb3 36 #define ESP_MBUFFE_MAX 256
samux 1:fb4494783863 37
screamer 5:81e264dbbf03 38 /**
michaeljkoster 13:41098c907200 39 * The ESP8266 class
screamer 5:81e264dbbf03 40 */
michaeljkoster 13:41098c907200 41 class ESP8266
samux 1:fb4494783863 42 {
samux 1:fb4494783863 43
samux 1:fb4494783863 44 public:
screamer 5:81e264dbbf03 45 /**
samux 1:fb4494783863 46 * Constructor
samux 1:fb4494783863 47 *
samux 1:fb4494783863 48 * @param tx mbed pin to use for tx line of Serial interface
samux 1:fb4494783863 49 * @param rx mbed pin to use for rx line of Serial interface
samux 1:fb4494783863 50 * @param reset reset pin of the wifi module ()
samux 1:fb4494783863 51 * @param ssid ssid of the network
screamer 9:f2439377c172 52 * @param phrase WEP, WPA or WPA2 key
mbedAustin 28:91e65e22e63a 53 * @param baud the baudrate of the serial connection
samux 1:fb4494783863 54 */
mbedAustin 28:91e65e22e63a 55 ESP8266( PinName tx, PinName rx, PinName reset, const char * ssid, const char * phrase, uint32_t baud );
samux 1:fb4494783863 56
screamer 5:81e264dbbf03 57 /**
samux 1:fb4494783863 58 * Connect the wifi module to the ssid contained in the constructor.
samux 1:fb4494783863 59 *
samux 1:fb4494783863 60 * @return true if connected, false otherwise
samux 1:fb4494783863 61 */
samux 1:fb4494783863 62 bool join();
samux 1:fb4494783863 63
screamer 5:81e264dbbf03 64 /**
michaeljkoster 15:37a7a56a424f 65 * Same as Join: connect to the ssid and get DHCP settings
screamer 6:5176e0864078 66 * @return true if successful
samux 1:fb4494783863 67 */
michaeljkoster 13:41098c907200 68 bool connect();
michaeljkoster 13:41098c907200 69
screamer 5:81e264dbbf03 70 /**
michaeljkoster 13:41098c907200 71 * Check connection to the access point
screamer 6:5176e0864078 72 * @return true if successful
samux 1:fb4494783863 73 */
michaeljkoster 13:41098c907200 74 bool is_connected();
samux 1:fb4494783863 75
screamer 5:81e264dbbf03 76 /**
michaeljkoster 15:37a7a56a424f 77 * Disconnect the ESP8266 module from the access point
michaeljkoster 15:37a7a56a424f 78 *
michaeljkoster 15:37a7a56a424f 79 * @return true if successful
michaeljkoster 15:37a7a56a424f 80 */
michaeljkoster 15:37a7a56a424f 81 bool disconnect();
michaeljkoster 16:3f0efaa57a12 82
michaeljkoster 16:3f0efaa57a12 83 /*
mbedAustin 30:c035696b9397 84 * Start up a UDP or TCP Connection
mbedAustin 30:c035696b9397 85 * @param type 0 for UDP, 1 for TCP
mbedAustin 30:c035696b9397 86 * @param ip A string that contains the IP, no quotes
mbedAustin 30:c035696b9397 87 * @param port Numerical port number to connect to
mbedAustin 30:c035696b9397 88 * @param id number between 0-4, if defined it denotes ID to use in multimode (Default to Single connection mode with -1)
mbedAustin 30:c035696b9397 89 * @return true if sucessful, 0 if fail
michaeljkoster 16:3f0efaa57a12 90 */
mbedAustin 30:c035696b9397 91 bool start(bool type, char* ip, int port, int id = -1);
mbedAustin 30:c035696b9397 92
mbedAustin 30:c035696b9397 93 /*
mbedAustin 30:c035696b9397 94 * Legacy Start for UDP only connection in transparent mode
mbedAustin 30:c035696b9397 95 * @param ip A string that contains the IP, no quotes
sarahmarshy 44:3a7b6083210b 96 * @param id number between 0-4
mbedAustin 30:c035696b9397 97 * @param port Numerical port number to connect to
sarahmarshy 44:3a7b6083210b 98 * @param length number of characters in the message being sent
mbedAustin 30:c035696b9397 99 */
sarahmarshy 44:3a7b6083210b 100 bool startUDP(char* ip, int port, int id, int length);
sarahmarshy 44:3a7b6083210b 101
sarahmarshy 44:3a7b6083210b 102 /*
sarahmarshy 44:3a7b6083210b 103 *Starts the ESP chip as a TCP Server
sarahmarshy 44:3a7b6083210b 104 *@param port Numerical port of the server, default is 333
sarahmarshy 44:3a7b6083210b 105 */
sarahmarshy 44:3a7b6083210b 106 bool startTCPServer(int port = 333);
michaeljkoster 15:37a7a56a424f 107
michaeljkoster 15:37a7a56a424f 108 /**
michaeljkoster 16:3f0efaa57a12 109 * Close a connection
michaeljkoster 15:37a7a56a424f 110 *
michaeljkoster 15:37a7a56a424f 111 * @return true if successful
michaeljkoster 15:37a7a56a424f 112 */
michaeljkoster 15:37a7a56a424f 113 bool close();
michaeljkoster 15:37a7a56a424f 114
michaeljkoster 15:37a7a56a424f 115 /**
michaeljkoster 14:4d1128f72e00 116 * Return the IP address
michaeljkoster 14:4d1128f72e00 117 * @return IP address as a string
michaeljkoster 14:4d1128f72e00 118 */
michaeljkoster 14:4d1128f72e00 119 char* getIPAddress();
michaeljkoster 15:37a7a56a424f 120
michaeljkoster 15:37a7a56a424f 121 /**
michaeljkoster 15:37a7a56a424f 122 * Return the IP address from host name
mbedAustin 31:fd0eaf273b11 123 * @return true on success, false on failure
mbedAustin 32:cf071dc33972 124 */
mbedAustin 32:cf071dc33972 125 bool gethostbyname(const char * host, char * ip);
michaeljkoster 15:37a7a56a424f 126
michaeljkoster 14:4d1128f72e00 127 /**
samux 1:fb4494783863 128 * Reset the wifi module
samux 1:fb4494783863 129 */
samux 1:fb4494783863 130 void reset();
samux 3:9aa05e19c62e 131
screamer 5:81e264dbbf03 132 /**
samux 3:9aa05e19c62e 133 * Reboot the wifi module
samux 3:9aa05e19c62e 134 */
samux 3:9aa05e19c62e 135 bool reboot();
samux 1:fb4494783863 136
screamer 5:81e264dbbf03 137 /**
samux 1:fb4494783863 138 * Check if characters are available
samux 1:fb4494783863 139 *
samux 1:fb4494783863 140 * @return number of available characters
samux 1:fb4494783863 141 */
samux 1:fb4494783863 142 int readable();
samux 1:fb4494783863 143
screamer 5:81e264dbbf03 144 /**
samux 1:fb4494783863 145 * Check if characters are available
samux 1:fb4494783863 146 *
samux 1:fb4494783863 147 * @return number of available characters
samux 1:fb4494783863 148 */
samux 1:fb4494783863 149 int writeable();
samux 1:fb4494783863 150
screamer 5:81e264dbbf03 151 /**
samux 1:fb4494783863 152 * Read a character
samux 1:fb4494783863 153 *
samux 1:fb4494783863 154 * @return the character read
samux 1:fb4494783863 155 */
samux 1:fb4494783863 156 char getc();
samux 1:fb4494783863 157
screamer 5:81e264dbbf03 158 /**
samux 1:fb4494783863 159 * Write a character
samux 1:fb4494783863 160 *
samux 1:fb4494783863 161 * @param the character which will be written
samux 1:fb4494783863 162 */
samux 1:fb4494783863 163 int putc(char c);
samux 1:fb4494783863 164
screamer 5:81e264dbbf03 165 /**
michaeljkoster 15:37a7a56a424f 166 * Flush the buffer
michaeljkoster 15:37a7a56a424f 167 */
michaeljkoster 15:37a7a56a424f 168 void flush();
michaeljkoster 15:37a7a56a424f 169
michaeljkoster 15:37a7a56a424f 170 /**
mbedAustin 28:91e65e22e63a 171 * Send a command to the wifi module. Check if the module is in command mode. If not enter in command mode
michaeljkoster 15:37a7a56a424f 172 *
michaeljkoster 15:37a7a56a424f 173 * @param str string to be sent
sarahmarshy 44:3a7b6083210b 174 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknowledged. (default: "NO")
michaeljkoster 15:37a7a56a424f 175 * @param res this field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL)
samux 1:fb4494783863 176 *
screamer 6:5176e0864078 177 * @return true if successful
samux 1:fb4494783863 178 */
michaeljkoster 15:37a7a56a424f 179 bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
michaeljkoster 15:37a7a56a424f 180
screamer 5:81e264dbbf03 181 /**
samux 1:fb4494783863 182 * Send a string to the wifi module by serial port. This function desactivates the user interrupt handler when a character is received to analyze the response from the wifi module.
samux 1:fb4494783863 183 * Useful to send a command to the module and wait a response.
samux 1:fb4494783863 184 *
samux 1:fb4494783863 185 *
samux 1:fb4494783863 186 * @param str string to be sent
samux 1:fb4494783863 187 * @param len string length
samux 1:fb4494783863 188 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
samux 1:fb4494783863 189 * @param res this field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL)
samux 1:fb4494783863 190 *
samux 1:fb4494783863 191 * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
samux 1:fb4494783863 192 */
michaeljkoster 16:3f0efaa57a12 193 int send(const char * buf, int len);
samux 1:fb4494783863 194
michaeljkoster 13:41098c907200 195 static ESP8266 * getInstance() {
samux 1:fb4494783863 196 return inst;
samux 1:fb4494783863 197 };
samux 1:fb4494783863 198
samux 1:fb4494783863 199 protected:
mbedAustin 28:91e65e22e63a 200 RawSerial wifi;
samux 1:fb4494783863 201 DigitalOut reset_pin;
samux 1:fb4494783863 202 char phrase[30];
samux 1:fb4494783863 203 char ssid[30];
michaeljkoster 14:4d1128f72e00 204 char ipString[20];
michaeljkoster 13:41098c907200 205 CircBuffer<char> buf_ESP8266;
samux 1:fb4494783863 206
michaeljkoster 13:41098c907200 207 static ESP8266 * inst;
samux 1:fb4494783863 208
samux 1:fb4494783863 209 void attach_rx(bool null);
samux 1:fb4494783863 210 void handler_rx(void);
samux 1:fb4494783863 211
samux 1:fb4494783863 212
samux 1:fb4494783863 213 typedef struct STATE {
samux 1:fb4494783863 214 bool associated;
michaeljkoster 22:c4360e61486a 215 bool cmdMode;
samux 1:fb4494783863 216 } State;
samux 1:fb4494783863 217
samux 1:fb4494783863 218 State state;
samux 1:fb4494783863 219 };
samux 1:fb4494783863 220
samux 1:fb4494783863 221 #endif