ESP8266 Socket Library. AT Thinker firmware.

Dependents:   ESP8266_MQTT_HelloWorld ESP8266_IFTTT_Test ECE_4180_Lab_4 websocketmbed ... more

Fork of ESP8266Interface by ESP8266

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP8266.h Source File

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 #define ESP_MBUFFE_MAX 256
00037 
00038 /**
00039  * The ESP8266 class
00040  */
00041 class ESP8266
00042 {
00043 
00044 public:
00045     /**
00046     * Constructor
00047     *
00048     * @param tx mbed pin to use for tx line of Serial interface
00049     * @param rx mbed pin to use for rx line of Serial interface
00050     * @param reset reset pin of the wifi module ()
00051     * @param ssid ssid of the network
00052     * @param phrase WEP, WPA or WPA2 key
00053     * @param baud the baudrate of the serial connection
00054     */
00055     ESP8266( PinName tx, PinName rx, PinName reset, const char * ssid, const char * phrase, uint32_t baud );
00056 
00057     /**
00058     * Connect the wifi module to the ssid contained in the constructor.
00059     *
00060     * @return true if connected, false otherwise
00061     */
00062     bool join();
00063 
00064     /**
00065     * Same as Join: connect to the ssid and get DHCP settings
00066     * @return true if successful
00067     */
00068     bool connect();
00069     
00070     /**
00071     * Check connection to the access point
00072     * @return true if successful
00073     */
00074     bool is_connected();
00075 
00076     /**
00077     * Disconnect the ESP8266 module from the access point
00078     *
00079     * @return true if successful
00080     */
00081     bool disconnect();
00082     
00083     /*
00084     * Start up a UDP or TCP Connection
00085     * @param type 0 for UDP, 1 for TCP
00086     * @param ip A string that contains the IP, no quotes
00087     * @param port Numerical port number to connect to
00088     * @param id number between 0-4, if defined it denotes ID to use in multimode (Default to Single connection mode with -1)
00089     * @return true if sucessful, 0 if fail
00090     */
00091     bool start(bool type, char* ip, int port, int id = -1);
00092 
00093     /*
00094     * Legacy Start for UDP only connection in transparent mode
00095     * @param ip A string that contains the IP, no quotes
00096     * @param id number between 0-4
00097     * @param port Numerical port number to connect to
00098     * @param length number of characters in the message being sent
00099     */
00100     bool startUDP(char* ip, int port, int id, int length);
00101 
00102     /*
00103     *Starts the ESP chip as a TCP Server
00104     *@param port Numerical port of the server, default is 333
00105     */
00106     bool startTCPServer(int port = 333);
00107 
00108     /**
00109     * Close a connection
00110     *
00111     * @return true if successful
00112     */
00113     bool close();
00114     
00115     /**
00116     * Return the IP address 
00117     * @return IP address as a string
00118     */
00119     char* getIPAddress();
00120 
00121     /**
00122     * Return the IP address from host name
00123     * @return true on success, false on failure
00124     */    
00125     bool gethostbyname(const char * host, char * ip);
00126 
00127     /**
00128     * Reset the wifi module
00129     */
00130     void reset();
00131     
00132     /**
00133     * Reboot the wifi module
00134     */
00135     bool reboot();
00136 
00137     /**
00138     * Check if characters are available
00139     *
00140     * @return number of available characters
00141     */
00142     int readable();
00143 
00144     /**
00145     * Check if characters are available
00146     *
00147     * @return number of available characters
00148     */
00149     int writeable();
00150 
00151     /**
00152     * Read a character
00153     *
00154     * @return the character read
00155     */
00156     char getc();
00157 
00158     /**
00159     * Write a character
00160     *
00161     * @param the character which will be written
00162     */
00163     int putc(char c);
00164 
00165     /**
00166     * Flush the buffer
00167     */
00168     void flush();
00169 
00170     /**
00171     * Send a command to the wifi module. Check if the module is in command mode. If not enter in command mode
00172     *
00173     * @param str string to be sent
00174     * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknowledged. (default: "NO")
00175     * @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)
00176     *
00177     * @return true if successful
00178     */
00179     bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
00180 
00181     /**
00182     * 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.
00183     * Useful to send a command to the module and wait a response.
00184     *
00185     *
00186     * @param str string to be sent
00187     * @param len string length
00188     * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
00189     * @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)
00190     *
00191     * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
00192     */
00193     int send(const char * buf, int len);
00194 
00195     static ESP8266 * getInstance() {
00196         return inst;
00197     };
00198 
00199 protected:
00200     RawSerial wifi;
00201     DigitalOut reset_pin;
00202     char phrase[30];
00203     char ssid[30];
00204     char ipString[20];
00205     CircBuffer<char> buf_ESP8266;
00206 
00207     static ESP8266 * inst;
00208 
00209     void attach_rx(bool null);
00210     void handler_rx(void);
00211 
00212 
00213     typedef struct STATE {
00214         bool associated;
00215         bool cmdMode;
00216     } State;
00217 
00218     State state;
00219 };
00220 
00221 #endif