For test

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ESP8266.h Source File

ESP8266.h

Go to the documentation of this file.
00001 /**
00002  * @file ESP8266.h
00003  */
00004 #ifndef __ESP8266_H__
00005 #define __ESP8266_H__
00006 
00007 #include "mbed.h"
00008 #include "time.h"
00009 #include "ArduinoSerial.h"
00010 
00011 /**
00012  * The way of SoftAP encrypstion. 
00013  * 
00014  */
00015 typedef enum {
00016     ESP8266_ECN_OPEN          = 0, /**< OPEN */
00017     ESP8266_ECN_WEP           = 1, /**< WEP */
00018     ESP8266_ECN_WAP_PSK       = 2, /**< WAP_PSK */
00019     ESP8266_ECN_WAP2_PSK      = 3, /**< WAP2_PSK */
00020     ESP8266_ECN_WAP_WAP2_PSK  = 4, /**< WAP_WAP2_PSK */
00021 } ESP8266Encrypstion;
00022 
00023 /**
00024  * The ip protocol for communication. 
00025  * 
00026  * Only TCP or UPD supported. 
00027  */
00028 typedef enum {
00029     ESP8266_COMM_UDP              = 0, /**< UPD */
00030     ESP8266_COMM_TCP              = 1, /**< TCP */
00031 } ESP8266CommType; 
00032 
00033 /**
00034  * The work mode. 
00035  * 
00036  * Station | SoftAP | Station + SoftAP. 
00037  */
00038 typedef enum {
00039     ESP8266_MODE_STA         = 1, /**< Station */
00040     ESP8266_MODE_SAP         = 2, /**< SoftAP */
00041     ESP8266_MODE_STA_SAP     = 3, /**< Station + SoftAP */
00042 } ESP8266WorkMode;
00043 
00044 /**
00045  * Mux single or multiple connection mode. 
00046  */
00047 typedef enum {
00048     ESP8266_MUX_SINGLE      = 0,   /**< Single mode */
00049     ESP8266_MUX_MULTIPLE    = 1,   /**< Multiple mode */
00050 } ESP8266Mux;
00051 
00052 /**
00053  * Provide methods to manipulate ESP8266 for internet access via WiFi. 
00054  */
00055 class ESP8266
00056 {
00057 public:
00058 
00059     /**
00060      * Constructor. 
00061      * 
00062      * Need an serial port for communication between MCU and ESP8266. 
00063      * 
00064      * @param tx - the TX of serial port. 
00065      * @param rx - the RX of serial port. 
00066      * @param baud_rate - the baud rate of communication(default 9600).
00067      */
00068     ESP8266(PinName tx, PinName rx, int baud_rate = 9600);
00069     
00070     /**
00071      * Set as station mode and join in ssid with pwd. 
00072      *
00073      * @param ssid - SSID of AP to join in. 
00074      * @param pwd - PASSWORD of AP to join in. 
00075      * @return true if success, false for failure. 
00076      */
00077     bool setStationMode(String ssid, String pwd);
00078     
00079     /**
00080      * Set as softap mode and start softap. 
00081      *
00082      * @param sap_ssid - SSID of its self softap. 
00083      * @param sap_pwd - PASSWORD of its self softap. 
00084      * @param chl - the channel can be 1, 2, 3, ..., 13(default: 7).  
00085      * @param enc - the method of encrypstion(default: ESP8266_ECN_WAP_WAP2_PSK). 
00086      * @return true if success, false for failure. 
00087      */
00088     bool setSoftAPMode(String sap_ssid, String sap_pwd,
00089         int chl = 7, ESP8266Encrypstion ecn = ESP8266_ECN_WAP_WAP2_PSK);
00090     
00091     /**
00092      * Set as softap + station mode and start softap and join in ssid with pwd. 
00093      *
00094      * @param ssid - SSID of AP to join in. 
00095      * @param pwd - PASSWORD of AP to join in. 
00096      * @param sap_ssid - SSID of its self softap. 
00097      * @param sap_pwd - PASSWORD of its self softap. 
00098      * @param chl - the channel can be 1, 2, 3, ..., 13(default: 7).  
00099      * @param enc - the method of encrypstion(default: ESP8266_ECN_WAP_WAP2_PSK). 
00100      * @return true if success, false for failure. 
00101      */
00102     bool setStationSoftAPMode(String ssid, String pwd, String sap_ssid, String sap_pwd ,
00103         int chl = 7, ESP8266Encrypstion ecn = ESP8266_ECN_WAP_WAP2_PSK);
00104     
00105     /**
00106      * Start a TCP connection to server or init UDP communication. 
00107      * 
00108      * @param type - TCP or UDP.
00109      * @param addr - ip address or domain name of server to connect. 
00110      * @param port - the port number of server to connect. 
00111      * @param mux - single or mulitple connection mode(default: ESP8266_MUX_SINGLE). 
00112      * @param id - id number when mux is multiple(default: 0). 
00113      * @return true for success, false for failure.
00114      */
00115     bool ipConfig(ESP8266CommType type, String addr, int port, ESP8266Mux mux = ESP8266_MUX_SINGLE, int id = 0);
00116     
00117     /**
00118      * Set the connection mode(sigle or multiple)
00119      *
00120      * @param mux - single or multiple. 
00121      * @return true for success, false for failure.
00122      */
00123     bool confMux(ESP8266Mux mux);    
00124     
00125     /**
00126      * Start or stop the TCP server.
00127      * 
00128      * @param mode - 1 to start, 0 to stop (restart needed). 
00129      * @param port - the port to listen. 
00130      * @return true for success, false for failure.
00131      */
00132     bool confServer(int mode, int port);  
00133     
00134     /**
00135      * Send data in sigle connection mode(mux = 0). 
00136      * 
00137      * @param str - string to send. 
00138      * @return true for success, false for failure.
00139      */
00140     bool send(String str);  
00141     
00142     /**
00143      * Send data in multiple connection mode(mux = 1). 
00144      *
00145      * @param id - id number. 
00146      * @param str - string to send by id connection. 
00147      * @return true for success, false for failure.
00148      */
00149     bool send(int id, String str);  
00150     
00151     /**
00152      * Start to receive data from network. 
00153      * 
00154      * @param buf - buffer for storing data from network. 
00155      * @return the size of data received.
00156      */
00157     int recvData(char *buf);
00158     
00159     /**
00160      * Get the current id of data received via member method:recvData in multiple connection mode.
00161      *
00162      * @return the id of connection which data received from. 
00163      */
00164     int getMuxID(void);
00165     
00166     /** 
00167      * Reset the module. 
00168      *
00169      * @return true for success, false for failure.
00170      */
00171     bool reset(void);    
00172     
00173     /**
00174      * Get current work mode. 
00175      * 
00176      * @return the mode. 
00177      */
00178     String showMode(void);   
00179     
00180     
00181     /**
00182      * Get the list of Access Points available. 
00183      *
00184      * @return the list of SSID. 
00185      */
00186     String showAP(void);   
00187     
00188     /**
00189      * Get the SSID of AP joined already. 
00190      * 
00191      * @return the SSID joined. 
00192      */
00193     String showJAP(void);  
00194     
00195     /**
00196      * Quit the AP joined before. 
00197      * 
00198      * @return true for success, false for failure.
00199      */
00200     bool quitAP(void);    
00201     
00202     
00203     /**
00204      * Get SoftAP Parameters. 
00205      *
00206      * @return the parameters of SoftAP.
00207      */
00208     String showSAP(void);     
00209 
00210     /**
00211      * Get the status of connection. 
00212      *
00213      * @return the status. 
00214      */
00215     String showStatus(void);  
00216     
00217     /**
00218      * Get the connection mode(sigle or multiple)
00219      * 
00220      * @return the mux string. 
00221      */
00222     String showMux(void);  
00223     
00224     /**
00225      * Show the current ip address. 
00226      * 
00227      * @return the ip address. 
00228      */
00229     String showIP(void);    
00230     
00231     /**
00232      * Create new tcp or udp connection in sigle connection mode. 
00233      *
00234      * @param type - UDP or TCP.
00235      * @param addr - the ip or domain name of server. 
00236      * @param port - the port number. 
00237      */
00238     bool newMux(ESP8266CommType type, String addr, int port);   
00239     
00240     /**
00241      * Create new tcp or udp connection in multiple connection mode)(id:0-4). 
00242      *
00243      * @param id - the id number. 
00244      * @param type - UDP or TCP.
00245      * @param addr - the ip or domain name of server. 
00246      * @param port - the port number.      
00247      */
00248     bool newMux(int id, ESP8266CommType type, String addr, int port);   
00249     
00250     /**
00251      * Close tcp or udp in sigle connection mode.  
00252      */
00253     void closeMux(void);   
00254     
00255     /**
00256      * Close tcp or udp in multiple connection mode. 
00257      *
00258      * @param id - the id number.
00259      */
00260     void closeMux(int id); 
00261 
00262 private:
00263 
00264     /**
00265      * Set work mode. 
00266      * 
00267      */
00268     bool confMode(ESP8266WorkMode mode);   
00269     
00270     
00271     /**
00272      * Join in AP. 
00273      * 
00274      */
00275     bool confJAP(String ssid , String pwd);    
00276     
00277     
00278     /**
00279      * Set SoftAP Parameters. 
00280      *
00281      */
00282     bool confSAP(String ssid , String pwd , int chl , int ecn);      
00283         
00284      
00285     ArduinoSerial m_uart; /* The UART to communicate with ESP8266 */
00286     int m_mux_id;
00287 };
00288 
00289 #endif /* #ifndef __ESP8266_H__ */