Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ESP8266Interface by
ESP8266.h
00001 /* Copyright (C) 2012 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 * 00018 * @section DESCRIPTION 00019 * 00020 * ESP8266 serial wifi module 00021 * 00022 * Datasheet: 00023 * 00024 * http://www.electrodragon.com/w/Wi07c 00025 */ 00026 00027 #ifndef ESP8266_H 00028 #define ESP8266_H 00029 00030 #include "mbed.h" 00031 #include "CBuffer.h" 00032 00033 #define DEFAULT_WAIT_RESP_TIMEOUT 500 00034 #define ESP_TCP_TYPE 1 00035 #define ESP_UDP_TYPE 0 00036 00037 /** 00038 * The ESP8266 class 00039 */ 00040 class ESP8266 00041 { 00042 00043 public: 00044 /** 00045 * Constructor 00046 * 00047 * @param tx mbed pin to use for tx line of Serial interface 00048 * @param rx mbed pin to use for rx line of Serial interface 00049 * @param reset reset pin of the wifi module () 00050 * @param ssid ssid of the network 00051 * @param phrase WEP, WPA or WPA2 key 00052 * @param baud the baudrate of the serial connection 00053 */ 00054 ESP8266( PinName tx, PinName rx, PinName reset, const char * ssid, const char * phrase, uint32_t baud ); 00055 00056 /** 00057 * Connect the wifi module to the ssid contained in the constructor. 00058 * 00059 * @return true if connected, false otherwise 00060 */ 00061 bool join(); 00062 00063 /** 00064 * Same as Join: connect to the ssid and get DHCP settings 00065 * @return true if successful 00066 */ 00067 bool connect(); 00068 00069 /** 00070 * Check connection to the access point 00071 * @return true if successful 00072 */ 00073 bool is_connected(); 00074 00075 /** 00076 * Disconnect the ESP8266 module from the access point 00077 * 00078 * @return true if successful 00079 */ 00080 bool disconnect(); 00081 00082 /* 00083 * Start up a UDP or TCP Connection 00084 * @param type 0 for UDP, 1 for TCP 00085 * @param ip A string that contains the IP, no quotes 00086 * @param port Numerical port number to connect to 00087 * @param id number between 0-4, if defined it denotes ID to use in multimode (Default to Single connection mode with -1) 00088 * @return true if sucessful, 0 if fail 00089 */ 00090 bool start(bool type, char* ip, int port, int id = -1); 00091 00092 /* 00093 * Legacy Start for UDP only connection in transparent mode 00094 * @param ip A string that contains the IP, no quotes 00095 * @param id number between 0-4 00096 * @param port Numerical port number to connect to 00097 * @param length number of characters in the message being sent 00098 */ 00099 bool startUDP(char* ip, int port, int id, int length); 00100 00101 /* 00102 *Starts the ESP chip as a TCP Server 00103 *@param port Numerical port of the server, default is 333 00104 */ 00105 bool startTCPServer(int port = 333); 00106 00107 /** 00108 * Close a connection 00109 * 00110 * @return true if successful 00111 */ 00112 bool close(); 00113 00114 /** 00115 * Return the IP address 00116 * @return IP address as a string 00117 */ 00118 char* getIPAddress(); 00119 00120 /** 00121 * Return the IP address from host name 00122 * @return true on success, false on failure 00123 */ 00124 bool gethostbyname(const char * host, char * ip); 00125 00126 /** 00127 * Reset the wifi module 00128 */ 00129 void reset(); 00130 00131 /** 00132 * Reboot the wifi module 00133 */ 00134 bool reboot(); 00135 00136 /** 00137 * Check if characters are available 00138 * 00139 * @return number of available characters 00140 */ 00141 int readable(); 00142 00143 /** 00144 * Check if characters are available 00145 * 00146 * @return number of available characters 00147 */ 00148 int writeable(); 00149 00150 /** 00151 * Read a character 00152 * 00153 * @return the character read 00154 */ 00155 char getc(); 00156 00157 /** 00158 * Write a character 00159 * 00160 * @param the character which will be written 00161 */ 00162 int putc(char c); 00163 00164 /** 00165 * Flush the buffer 00166 */ 00167 void flush(); 00168 00169 /** 00170 * Send a command to the wifi module. Check if the module is in command mode. If not enter in command mode 00171 * 00172 * @param str string to be sent 00173 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknowledged. (default: "NO") 00174 * @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) 00175 * 00176 * @return true if successful 00177 */ 00178 bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT); 00179 00180 /** 00181 * 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. 00182 * Useful to send a command to the module and wait a response. 00183 * 00184 * 00185 * @param str string to be sent 00186 * @param len string length 00187 * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO") 00188 * @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) 00189 * 00190 * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s. 00191 */ 00192 int send(const char * buf, int len); 00193 00194 static ESP8266 * getInstance() { 00195 return inst; 00196 }; 00197 00198 protected: 00199 RawSerial wifi; 00200 DigitalOut reset_pin; 00201 char phrase[30]; 00202 char ssid[30]; 00203 char ipString[20]; 00204 CircBuffer<char> buf_ESP8266; 00205 00206 static ESP8266 * inst; 00207 00208 void attach_rx(bool null); 00209 void handler_rx(void); 00210 00211 00212 typedef struct STATE { 00213 bool associated; 00214 bool cmdMode; 00215 } State; 00216 00217 State state; 00218 }; 00219 00220 #endif
Generated on Wed Jul 13 2022 19:32:39 by
1.7.2
