For test

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
shennongmin
Date:
Thu Feb 05 06:48:45 2015 +0000
Parent:
34:65cd9c6eedb8
Commit message:
compile okay and test okay

Changed in this revision

ESP8266.cpp Show annotated file Show diff for this revision Revisions of this file
ESP8266.h Show annotated file Show diff for this revision Revisions of this file
diff -r 65cd9c6eedb8 -r 90be2bc2a492 ESP8266.cpp
--- a/ESP8266.cpp	Thu Feb 05 03:38:10 2015 +0000
+++ b/ESP8266.cpp	Thu Feb 05 06:48:45 2015 +0000
@@ -45,26 +45,22 @@
     return confJAP(ssid, pwd);
 }
 
-bool ESP8266::setSoftAPMode(String sap_ssid, String sap_pwd,
-    ESP8266Channel chl, ESP8266Encrypstion ecn)
+bool ESP8266::setSoftAPMode(String sap_ssid, String sap_pwd, int chl, ESP8266Encrypstion ecn)
 {
     if (!confMode(ESP8266_MODE_SAP))
     {
         return false;
     }
-    reset();
     return confSAP(sap_ssid, sap_pwd, chl, ecn);
 }
 
-bool ESP8266::setStationSoftAPMode(String ssid, String pwd, 
-        String sap_ssid, String sap_pwd ,
-        ESP8266Channel chl, ESP8266Encrypstion ecn)
+bool ESP8266::setStationSoftAPMode(String ssid, String pwd, String sap_ssid, String sap_pwd ,
+        int chl, ESP8266Encrypstion ecn)
 {
     if (!confMode(ESP8266_MODE_STA_SAP))
     {
         return false;
     }
-    reset();
     if (confJAP(ssid, pwd) && confSAP(sap_ssid, sap_pwd, chl, ecn)) {
         return true;
     } else {
@@ -72,43 +68,24 @@
     }
 }
 
-
-bool ESP8266::ipConfig(uint8_t type, String addr, int port, uint8_t mux, uint8_t id)
+bool ESP8266::ipConfig(ESP8266CommType type, String addr, int port, ESP8266Mux mux, int id)
 {
     bool result = false;
-    if (mux == 0 )
+    if (mux == ESP8266_MUX_SINGLE)
     {
         confMux(mux);
-        
-        long timeStart = millis();
-        while (1)
-        {
-            long time0 = millis();
-            if (time0 - timeStart > 5000)
-            {
-                break;
-            }
-        }
+        delay(5000);
         result = newMux(type, addr, port);
     }
-    else if (mux == 1)
+    else if (mux == ESP8266_MUX_MULTIPLE)
     {
         confMux(mux);
-        long timeStart = millis();
-        while (1)
-        {
-            long time0 = millis();
-            if (time0 - timeStart > 5000)
-            {
-                break;
-            }
-        }
+        delay(5000);
         result = newMux(id, type, addr, port);
     }
     return result;
 }
 
-
 int ESP8266::recvData(char *buf)
 {
     String data = "";
@@ -215,7 +192,7 @@
     }
 }
 
-bool ESP8266::confMode(uint8_t mode)
+bool ESP8266::confMode(ESP8266WorkMode mode)
 {
     String data;
     m_uart.flush();
@@ -395,7 +372,7 @@
           return data;
 }
 
-bool ESP8266::confSAP(String ssid , String pwd , uint8_t chl , uint8_t ecn)
+bool ESP8266::confSAP(String ssid , String pwd , int chl , int ecn)
 {
     m_uart.print("AT+CWSAP=");  
     m_uart.print("\"");     //"ssid"
@@ -484,7 +461,7 @@
           return data;
 }
 
-bool ESP8266::confMux(int mux)
+bool ESP8266::confMux(ESP8266Mux mux)
 {
     m_uart.print("AT+CIPMUX=");
     m_uart.println(mux);           
@@ -500,7 +477,7 @@
      return false;
 }
 
-bool ESP8266::newMux(uint8_t type, String addr, int port)
+bool ESP8266::newMux(ESP8266CommType type, String addr, int port)
 
 {
     String data;
@@ -533,7 +510,7 @@
   return false;
 }
 
-bool ESP8266::newMux( uint8_t id, uint8_t type, String addr, int port)
+bool ESP8266::newMux( int id, ESP8266CommType type, String addr, int port)
 {
     m_uart.print("AT+CIPSTART=");
     m_uart.print("\"");
@@ -609,7 +586,7 @@
   return false;
 }
 
-bool ESP8266::send(uint8_t id, String str)
+bool ESP8266::send(int id, String str)
 {
     m_uart.print("AT+CIPSEND=");
 
@@ -672,7 +649,7 @@
   }
 }
 
-void ESP8266::closeMux(uint8_t id)
+void ESP8266::closeMux(int id)
 {
     m_uart.print("AT+CIPCLOSE=");
     m_uart.println(String(id));
@@ -729,7 +706,7 @@
     }  
 }
 
-bool ESP8266::confServer(uint8_t mode, int port)
+bool ESP8266::confServer(int mode, int port)
 {
     m_uart.print("AT+CIPSERVER=");  
     m_uart.print(String(mode));
diff -r 65cd9c6eedb8 -r 90be2bc2a492 ESP8266.h
--- a/ESP8266.h	Thu Feb 05 03:38:10 2015 +0000
+++ b/ESP8266.h	Thu Feb 05 06:48:45 2015 +0000
@@ -28,7 +28,7 @@
 typedef enum {
     ESP8266_COMM_UDP              = 0, /**< UPD */
     ESP8266_COMM_TCP              = 1, /**< TCP */
-} ESP8266CommMode; 
+} ESP8266CommType; 
 
 /**
  * The work mode. 
@@ -42,26 +42,12 @@
 } ESP8266WorkMode;
 
 /**
- * The channel for SoftAP. 
- * 
- * Only 1 - 13 acceptable. 
+ * Mux single or multiple connection mode. 
  */
 typedef enum {
-    ESP8266_CHANNEL_1 = 1,
-    ESP8266_CHANNEL_2,
-    ESP8266_CHANNEL_3,
-    ESP8266_CHANNEL_4,
-    ESP8266_CHANNEL_5,
-    ESP8266_CHANNEL_6,
-    ESP8266_CHANNEL_7,
-    ESP8266_CHANNEL_8,
-    ESP8266_CHANNEL_9,
-    ESP8266_CHANNEL_10,
-    ESP8266_CHANNEL_11,
-    ESP8266_CHANNEL_12,
-    ESP8266_CHANNEL_13,
-} ESP8266Channel;
-
+    ESP8266_MUX_SINGLE      = 0,   /**< Single mode */
+    ESP8266_MUX_MULTIPLE    = 1,   /**< Multiple mode */
+} ESP8266Mux;
 
 /**
  * Provide methods to manipulate ESP8266 for internet access via WiFi. 
@@ -95,12 +81,12 @@
      *
      * @param sap_ssid - SSID of its self softap. 
      * @param sap_pwd - PASSWORD of its self softap. 
-     * @param chl - the channel can be 1, 2, 3, ..., 13. 
-     * @param enc - the method of encrypstion. 
+     * @param chl - the channel can be 1, 2, 3, ..., 13(default: 7).  
+     * @param enc - the method of encrypstion(default: ESP8266_ECN_WAP_WAP2_PSK). 
      * @return true if success, false for failure. 
      */
     bool setSoftAPMode(String sap_ssid, String sap_pwd,
-        ESP8266Channel chl = ESP8266_CHANNEL_7, ESP8266Encrypstion ecn = ESP8266_ECN_WAP_WAP2_PSK);
+        int chl = 7, ESP8266Encrypstion ecn = ESP8266_ECN_WAP_WAP2_PSK);
     
     /**
      * Set as softap + station mode and start softap and join in ssid with pwd. 
@@ -109,52 +95,177 @@
      * @param pwd - PASSWORD of AP to join in. 
      * @param sap_ssid - SSID of its self softap. 
      * @param sap_pwd - PASSWORD of its self softap. 
-     * @param chl - the channel can be 1, 2, 3, ..., 13. 
-     * @param enc - the method of encrypstion. 
+     * @param chl - the channel can be 1, 2, 3, ..., 13(default: 7).  
+     * @param enc - the method of encrypstion(default: ESP8266_ECN_WAP_WAP2_PSK). 
      * @return true if success, false for failure. 
      */
     bool setStationSoftAPMode(String ssid, String pwd, String sap_ssid, String sap_pwd ,
-        ESP8266Channel chl = ESP8266_CHANNEL_7, ESP8266Encrypstion ecn = ESP8266_ECN_WAP_WAP2_PSK);
+        int chl = 7, ESP8266Encrypstion ecn = ESP8266_ECN_WAP_WAP2_PSK);
     
     /**
      * Start a TCP connection to server or init UDP communication. 
+     * 
+     * @param type - TCP or UDP.
+     * @param addr - ip address or domain name of server to connect. 
+     * @param port - the port number of server to connect. 
+     * @param mux - single or mulitple connection mode(default: ESP8266_MUX_SINGLE). 
+     * @param id - id number when mux is multiple(default: 0). 
+     * @return true for success, false for failure.
      */
-    bool ipConfig(uint8_t type, String addr, int port, uint8_t mux = 0, uint8_t id = 0);
+    bool ipConfig(ESP8266CommType type, String addr, int port, ESP8266Mux mux = ESP8266_MUX_SINGLE, int id = 0);
     
+    /**
+     * Set the connection mode(sigle or multiple)
+     *
+     * @param mux - single or multiple. 
+     * @return true for success, false for failure.
+     */
+    bool confMux(ESP8266Mux mux);    
+    
+    /**
+     * Start or stop the TCP server.
+     * 
+     * @param mode - 1 to start, 0 to stop (restart needed). 
+     * @param port - the port to listen. 
+     * @return true for success, false for failure.
+     */
+    bool confServer(int mode, int port);  
     
     /**
      * Send data in sigle connection mode(mux = 0). 
      * 
+     * @param str - string to send. 
+     * @return true for success, false for failure.
      */
     bool send(String str);  
     
     /**
      * Send data in multiple connection mode(mux = 1). 
      *
+     * @param id - id number. 
+     * @param str - string to send by id connection. 
+     * @return true for success, false for failure.
      */
-    bool send(uint8_t id, String str);  
+    bool send(int id, String str);  
     
     /**
      * Start to receive data from network. 
+     * 
+     * @param buf - buffer for storing data from network. 
+     * @return the size of data received.
      */
     int recvData(char *buf);
     
     /**
      * Get the current id of data received via member method:recvData in multiple connection mode.
+     *
+     * @return the id of connection which data received from. 
      */
     int getMuxID(void);
     
     /** 
      * Reset the module. 
+     *
+     * @return true for success, false for failure.
      */
     bool reset(void);    
     
+    /**
+     * Get current work mode. 
+     * 
+     * @return the mode. 
+     */
+    String showMode(void);   
     
+    
+    /**
+     * Get the list of Access Points available. 
+     *
+     * @return the list of SSID. 
+     */
+    String showAP(void);   
+    
+    /**
+     * Get the SSID of AP joined already. 
+     * 
+     * @return the SSID joined. 
+     */
+    String showJAP(void);  
+    
+    /**
+     * Quit the AP joined before. 
+     * 
+     * @return true for success, false for failure.
+     */
+    bool quitAP(void);    
+    
+    
+    /**
+     * Get SoftAP Parameters. 
+     *
+     * @return the parameters of SoftAP.
+     */
+    String showSAP(void);     
+
+    /**
+     * Get the status of connection. 
+     *
+     * @return the status. 
+     */
+    String showStatus(void);  
+    
+    /**
+     * Get the connection mode(sigle or multiple)
+     * 
+     * @return the mux string. 
+     */
+    String showMux(void);  
+    
+    /**
+     * Show the current ip address. 
+     * 
+     * @return the ip address. 
+     */
+    String showIP(void);    
+    
+    /**
+     * Create new tcp or udp connection in sigle connection mode. 
+     *
+     * @param type - UDP or TCP.
+     * @param addr - the ip or domain name of server. 
+     * @param port - the port number. 
+     */
+    bool newMux(ESP8266CommType type, String addr, int port);   
+    
+    /**
+     * Create new tcp or udp connection in multiple connection mode)(id:0-4). 
+     *
+     * @param id - the id number. 
+     * @param type - UDP or TCP.
+     * @param addr - the ip or domain name of server. 
+     * @param port - the port number.      
+     */
+    bool newMux(int id, ESP8266CommType type, String addr, int port);   
+    
+    /**
+     * Close tcp or udp in sigle connection mode.  
+     */
+    void closeMux(void);   
+    
+    /**
+     * Close tcp or udp in multiple connection mode. 
+     *
+     * @param id - the id number.
+     */
+    void closeMux(int id); 
+
+private:
+
     /**
      * Set work mode. 
      * 
      */
-    bool confMode(uint8_t mode);   
+    bool confMode(ESP8266WorkMode mode);   
     
     
     /**
@@ -168,86 +279,9 @@
      * Set SoftAP Parameters. 
      *
      */
-    bool confSAP(String ssid , String pwd , uint8_t chl , uint8_t ecn);       
-    
-    
-    /**
-     * Get current work mode. 
-     *
-     */
-    String showMode(void);   
-    
-    
-    /**
-     * Get the list of Access Points available. 
-     *
-     */
-    String showAP(void);   
-    
-    /**
-     * Get the SSID of AP joined already. 
-     */
-    String showJAP(void);  
-    
-    /**
-     * Quit the AP joined before. 
-     */
-    bool quitAP(void);    
-    
-    
-    /**
-     * Get SoftAP Parameters. 
-     *
-     */
-    String showSAP(void);     
-
-    /**
-     * Get the status of connection. 
-     *
-     */
-    String showStatus(void);  
-    
-    /**
-     * Get the connection mode(sigle or multiple)
-     */
-    String showMux(void);  
-    
-    /**
-     * Set the connection mode(sigle or multiple)
-     */
-    bool confMux(int mux);    
-    
-    /**
-     * Create new tcp or udp connection in sigle connection mode. 
-     */
-    bool newMux(uint8_t type, String addr, int port);   
-    
-    /**
-     * Create new tcp or udp connection in multiple connection mode)(id:0-4). 
-     */
-    bool newMux(uint8_t id, uint8_t type, String addr, int port);   
-    
-    /**
-     * Close tcp or udp in sigle connection mode.  
-     */
-    void closeMux(void);   
-    
-    /**
-     * Close tcp or udp in multiple connection mode. 
-     */
-    void closeMux(uint8_t id); 
-    
-    /**
-     * Show the current ip address. 
-     */
-    String showIP(void);    
-    
-    /**
-     * Start or stop the TCP server.
-     */
-    bool confServer(uint8_t mode, int port);  
-
-private:
+    bool confSAP(String ssid , String pwd , int chl , int ecn);      
+        
+     
     ArduinoSerial m_uart; /* The UART to communicate with ESP8266 */
     int m_mux_id;
 };