For test

Dependencies:   mbed

Revision:
33:f721a46ef2f2
Parent:
30:46a560b71e61
Child:
35:90be2bc2a492
diff -r 91b3f3162c6a -r f721a46ef2f2 ESP8266.cpp
--- a/ESP8266.cpp	Wed Feb 04 15:40:07 2015 +0000
+++ b/ESP8266.cpp	Thu Feb 05 02:28:09 2015 +0000
@@ -13,9 +13,27 @@
 
 bool ESP8266::reset(void)
 {
-    m_uart.setTimeout(5000);
+    unsigned long start;
+    m_uart.setTimeout(100);
+    m_uart.flush();
     m_uart.println("AT+RST");
-    return m_uart.find("eady");
+    if (!m_uart.find("OK")) {
+        return false;
+    }
+    
+    delay(2000);
+    
+    start = millis();
+    while (millis() - start < 3000) {
+        m_uart.flush();
+        m_uart.println("AT");
+        if (m_uart.find("OK")) {
+            delay(500);
+            return true;
+        }
+        delay(100);
+    }
+    return false;
 }
 
 bool ESP8266::setStationMode(String ssid, String pwd)
@@ -24,7 +42,6 @@
     {
         return false;
     }
-    reset();
     return confJAP(ssid, pwd);
 }
 
@@ -201,27 +218,25 @@
 bool ESP8266::confMode(uint8_t mode)
 {
     String data;
-     m_uart.print("AT+CWMODE=");  
-     m_uart.println(String(mode));
-     unsigned long start;
+    m_uart.flush();
+    m_uart.print("AT+CWMODE=");  
+    m_uart.println(String(mode));
+    unsigned long start;
     start = millis();
     while (millis()-start<2000) {
-      if(m_uart.available()>0)
-      {
-      char mode =m_uart.readChr();
-      data=data+mode;
-      }
-      if (data.indexOf("OK")!=-1 || data.indexOf("no change")!=-1)
-      {
-          return true;
-      }
-      if (data.indexOf("ERROR")!=-1 || data.indexOf("busy")!=-1)
-      {
-          return false;
-      }
-      
-   }
-     return false;     
+        while(m_uart.available()>0) {
+            char c = m_uart.readChr();
+            data += c;
+        }
+        if (data.indexOf("OK")!=-1 || data.indexOf("no change")!=-1)
+        {
+            return true;
+        } else if (data.indexOf("ERROR")!=-1 || data.indexOf("busy")!=-1)
+        {
+            return false;
+        }
+    }
+    return false;     
 }
 
 String ESP8266::showAP(void)
@@ -299,7 +314,8 @@
 
 bool ESP8266::confJAP(String ssid , String pwd)
 {
-    
+    bool ret = false;
+    m_uart.flush();
     m_uart.print("AT+CWJAP=");
     m_uart.print("\"");     //"ssid"
     m_uart.print(ssid);
@@ -311,17 +327,28 @@
     m_uart.print(pwd);
     m_uart.println("\"");
 
-
+    String data;
     unsigned long start;
     start = millis();
-    while (millis()-start<3000) {                            
-        if(m_uart.find("OK")==true)
-        {
-           return true;
-           
+    while (millis() - start < 10000) {        
+        while (m_uart.available() > 0) {
+            char c = m_uart.readChr();
+            data += c;
+        }
+        if(data.indexOf("OK") != -1) {
+            ret = true;
+            break;
+        } else if (data.indexOf("ERROR") != -1) {
+            ret = false;
+            break;
         }
     }
-    return false;
+    
+    if (ret) {
+        return true;
+    } else {
+        return false;
+    }
 }
 
 bool ESP8266::quitAP(void)
@@ -688,12 +715,12 @@
             break;
         }
     }
-
+    //printf("data = [%s]\r\n", data.c_str());
     if (ret) {
-        int index1 = data.indexOf("\r\n");
+        int index1 = data.indexOf("AT+CIFSR\r\r\n");
         int index2 = data.indexOf("\r\n\r\nOK");
         if (index1 != -1 && index2 != -1) {
-            return data.substring(index1 + strlen("\r\n"), index2);
+            return data.substring(index1 + strlen("AT+CIFSR\r\r\n"), index2);
         } else {
             return "not found";
         }