For test

Dependencies:   mbed

Revision:
29:75185b05d6a4
Parent:
28:c25aff31dcc5
Child:
30:46a560b71e61
diff -r c25aff31dcc5 -r 75185b05d6a4 ESP8266.cpp
--- a/ESP8266.cpp	Wed Feb 04 13:35:35 2015 +0000
+++ b/ESP8266.cpp	Wed Feb 04 14:27:30 2015 +0000
@@ -1,22 +1,22 @@
 #include "ESP8266.h"
 
 
-ESP8266::ESP8266(PinName tx, PinName rx):m_esp_uart(tx, rx) {
-}
-
-int ESP8266::getChannelID(void) {
-    return m_chl_id;
+ESP8266::ESP8266(PinName tx, PinName rx, int baud_rate):m_uart(tx, rx) {
+    m_uart.begin(baud_rate);
+    m_uart.flush();
+    m_uart.setTimeout(1000);
 }
 
-bool ESP8266::begin(void)
-{
-    m_esp_uart.begin(9600);
-    m_esp_uart.flush();
-    m_esp_uart.setTimeout(5000);
-    m_esp_uart.println("AT+RST");
-    return m_esp_uart.find("eady");
+int ESP8266::getMuxID(void) {
+    return m_mux_id;
 }
 
+bool ESP8266::reset(void)
+{
+    m_uart.setTimeout(5000);
+    m_uart.println("AT+RST");
+    return m_uart.find("eady");
+}
 
 bool ESP8266::init(uint8_t mode, String ssid, String pwd, uint8_t chl, uint8_t ecn)
 {
@@ -95,20 +95,20 @@
 int ESP8266::recvData(char *buf)
 {
     String data = "";
-    if (m_esp_uart.available()>0)
+    if (m_uart.available()>0)
     {
         
         unsigned long start;
         start = millis();
-        char c0 = m_esp_uart.readChr();
+        char c0 = m_uart.readChr();
         if (c0 == '+')
         {
             
             while (millis()-start<5000) 
             {
-                if (m_esp_uart.available()>0)
+                if (m_uart.available()>0)
                 {
-                    char c = m_esp_uart.readChr();
+                    char c = m_uart.readChr();
                     data += c;
                 }
                 if (data.indexOf("\nOK")!=-1)
@@ -141,7 +141,7 @@
             if(found ==true)
             {
             String _id = data.substring(4, j);
-            m_chl_id = _id.toInt();
+            m_mux_id = _id.toInt();
             String _size = data.substring(j+1, i);
             iSize = _size.toInt();
             
@@ -167,31 +167,16 @@
     return 0;
 }
 
-
-void ESP8266::reset(void)
-{
-    m_esp_uart.println("AT+RST");
-    unsigned long start;
-    start = millis();
-    while (millis()-start<5000) {                            
-        if(m_esp_uart.find("eady")==true)
-        {
-           break;
-        }
-    }
-}
-
-
 String ESP8266::showMode()
 {
     String data;
-    m_esp_uart.println("AT+CWMODE?");  
+    m_uart.println("AT+CWMODE?");  
     unsigned long start;
     start = millis();
     while (millis()-start<2000) {
-     if(m_esp_uart.available()>0)
+     if(m_uart.available()>0)
      {
-     char a =m_esp_uart.readChr();
+     char a =m_uart.readChr();
      data=data+a;
      }
      if (data.indexOf("OK")!=-1)
@@ -216,14 +201,14 @@
 bool ESP8266::confMode(uint8_t mode)
 {
     String data;
-     m_esp_uart.print("AT+CWMODE=");  
-     m_esp_uart.println(String(mode));
+     m_uart.print("AT+CWMODE=");  
+     m_uart.println(String(mode));
      unsigned long start;
     start = millis();
     while (millis()-start<2000) {
-      if(m_esp_uart.available()>0)
+      if(m_uart.available()>0)
       {
-      char mode =m_esp_uart.readChr();
+      char mode =m_uart.readChr();
       data=data+mode;
       }
       if (data.indexOf("OK")!=-1 || data.indexOf("no change")!=-1)
@@ -242,16 +227,16 @@
 String ESP8266::showAP(void)
 {
     String data;
-    m_esp_uart.flush();
-    m_esp_uart.print("AT+CWLAP\r\n");  
+    m_uart.flush();
+    m_uart.print("AT+CWLAP\r\n");  
     delay(5000);
     
     unsigned long start;
     start = millis();
     while (millis()-start<8000) {
-   if(m_esp_uart.available()>0)
+   if(m_uart.available()>0)
    {
-     char a =m_esp_uart.readChr();
+     char a =m_uart.readChr();
      data=data+a;
    }
      if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 )
@@ -279,15 +264,15 @@
 String ESP8266::showJAP(void)
 {
     bool ret = false;
-    m_esp_uart.flush();
-    m_esp_uart.println("AT+CWJAP?");  
+    m_uart.flush();
+    m_uart.println("AT+CWJAP?");  
     
     String data;
     unsigned long start;
     start = millis();
     while (millis()-start < 3000) {
-        while(m_esp_uart.available() > 0) {
-            char a = m_esp_uart.readChr();
+        while(m_uart.available() > 0) {
+            char a = m_uart.readChr();
             data += a;
             //printf("%c", a);
         }      
@@ -315,22 +300,22 @@
 bool ESP8266::confJAP(String ssid , String pwd)
 {
     
-    m_esp_uart.print("AT+CWJAP=");
-    m_esp_uart.print("\"");     //"ssid"
-    m_esp_uart.print(ssid);
-    m_esp_uart.print("\"");
+    m_uart.print("AT+CWJAP=");
+    m_uart.print("\"");     //"ssid"
+    m_uart.print(ssid);
+    m_uart.print("\"");
 
-    m_esp_uart.print(",");
+    m_uart.print(",");
 
-    m_esp_uart.print("\"");      //"pwd"
-    m_esp_uart.print(pwd);
-    m_esp_uart.println("\"");
+    m_uart.print("\"");      //"pwd"
+    m_uart.print(pwd);
+    m_uart.println("\"");
 
 
     unsigned long start;
     start = millis();
     while (millis()-start<3000) {                            
-        if(m_esp_uart.find("OK")==true)
+        if(m_uart.find("OK")==true)
         {
            return true;
            
@@ -341,11 +326,11 @@
 
 bool ESP8266::quitAP(void)
 {
-    m_esp_uart.println("AT+CWQAP");
+    m_uart.println("AT+CWQAP");
     unsigned long start;
     start = millis();
     while (millis()-start<3000) {                            
-        if(m_esp_uart.find("OK")==true)
+        if(m_uart.find("OK")==true)
         {
            return true;
            
@@ -357,14 +342,14 @@
 
 String ESP8266::showSAP()
 {
-    m_esp_uart.println("AT+CWSAP?");  
+    m_uart.println("AT+CWSAP?");  
       String data;
       unsigned long start;
     start = millis();
     while (millis()-start<3000) {
-       if(m_esp_uart.available()>0)
+       if(m_uart.available()>0)
        {
-       char a =m_esp_uart.readChr();
+       char a =m_uart.readChr();
        data=data+a;
        }
        if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 )
@@ -385,26 +370,26 @@
 
 bool ESP8266::confSAP(String ssid , String pwd , uint8_t chl , uint8_t ecn)
 {
-    m_esp_uart.print("AT+CWSAP=");  
-    m_esp_uart.print("\"");     //"ssid"
-    m_esp_uart.print(ssid);
-    m_esp_uart.print("\"");
+    m_uart.print("AT+CWSAP=");  
+    m_uart.print("\"");     //"ssid"
+    m_uart.print(ssid);
+    m_uart.print("\"");
 
-    m_esp_uart.print(",");
+    m_uart.print(",");
 
-    m_esp_uart.print("\"");      //"pwd"
-    m_esp_uart.print(pwd);
-    m_esp_uart.print("\"");
+    m_uart.print("\"");      //"pwd"
+    m_uart.print(pwd);
+    m_uart.print("\"");
 
-    m_esp_uart.print(",");
-    m_esp_uart.print(String(chl));
+    m_uart.print(",");
+    m_uart.print(String(chl));
 
-    m_esp_uart.print(",");
-    m_esp_uart.println(String(ecn));
+    m_uart.print(",");
+    m_uart.println(String(ecn));
     unsigned long start;
     start = millis();
     while (millis()-start<3000) {                            
-        if(m_esp_uart.find("OK")==true )
+        if(m_uart.find("OK")==true )
         {
            return true;
         }
@@ -417,14 +402,14 @@
 
 String ESP8266::showStatus(void)
 {
-    m_esp_uart.println("AT+CIPSTATUS");  
+    m_uart.println("AT+CIPSTATUS");  
       String data;
     unsigned long start;
     start = millis();
     while (millis()-start<3000) {
-       if(m_esp_uart.available()>0)
+       if(m_uart.available()>0)
        {
-       char a =m_esp_uart.readChr();
+       char a =m_uart.readChr();
        data=data+a;
        }
        if (data.indexOf("OK")!=-1)
@@ -446,14 +431,14 @@
 String ESP8266::showMux(void)
 {
     String data;
-    m_esp_uart.println("AT+CIPMUX?");  
+    m_uart.println("AT+CIPMUX?");  
 
       unsigned long start;
     start = millis();
     while (millis()-start<3000) {
-       if(m_esp_uart.available()>0)
+       if(m_uart.available()>0)
        {
-       char a =m_esp_uart.readChr();
+       char a =m_uart.readChr();
        data=data+a;
        }
        if (data.indexOf("OK")!=-1)
@@ -474,12 +459,12 @@
 
 bool ESP8266::confMux(int mux)
 {
-    m_esp_uart.print("AT+CIPMUX=");
-    m_esp_uart.println(mux);           
+    m_uart.print("AT+CIPMUX=");
+    m_uart.println(mux);           
     unsigned long start;
     start = millis();
     while (millis()-start<3000) {                            
-        if(m_esp_uart.find("OK")==true )
+        if(m_uart.find("OK")==true )
         {
            return true;
         }
@@ -492,25 +477,25 @@
 
 {
     String data;
-    m_esp_uart.flush();
-    m_esp_uart.print("AT+CIPSTART=");
+    m_uart.flush();
+    m_uart.print("AT+CIPSTART=");
     if(ESP8266_COMM_TCP == type) {
-        m_esp_uart.print("\"TCP\"");
+        m_uart.print("\"TCP\"");
     } else if (ESP8266_COMM_UDP == type) {
-        m_esp_uart.print("\"UDP\"");
+        m_uart.print("\"UDP\"");
     }
-    m_esp_uart.print(",");
-    m_esp_uart.print("\"");
-    m_esp_uart.print(addr);
-    m_esp_uart.print("\"");
-    m_esp_uart.print(",");
-    m_esp_uart.println(String(port));
+    m_uart.print(",");
+    m_uart.print("\"");
+    m_uart.print(addr);
+    m_uart.print("\"");
+    m_uart.print(",");
+    m_uart.println(String(port));
     unsigned long start;
     start = millis();
     while (millis()-start<10000) { 
-     if(m_esp_uart.available()>0)
+     if(m_uart.available()>0)
      {
-     char a =m_esp_uart.readChr();
+     char a =m_uart.readChr();
      data=data+a;
      }
      if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 || data.indexOf("ERROR")!=-1)
@@ -523,28 +508,28 @@
 
 bool ESP8266::newMux( uint8_t id, uint8_t type, String addr, int port)
 {
-    m_esp_uart.print("AT+CIPSTART=");
-    m_esp_uart.print("\"");
-    m_esp_uart.print(String(id));
-    m_esp_uart.print("\"");
+    m_uart.print("AT+CIPSTART=");
+    m_uart.print("\"");
+    m_uart.print(String(id));
+    m_uart.print("\"");
     if(ESP8266_COMM_TCP == type) {
-        m_esp_uart.print("\"TCP\"");
+        m_uart.print("\"TCP\"");
     } else if (ESP8266_COMM_UDP == type) {
-        m_esp_uart.print("\"UDP\"");
+        m_uart.print("\"UDP\"");
     }
-    m_esp_uart.print(",");
-    m_esp_uart.print("\"");
-    m_esp_uart.print(addr);
-    m_esp_uart.print("\"");
-    m_esp_uart.print(",");
-    m_esp_uart.println(String(port));
+    m_uart.print(",");
+    m_uart.print("\"");
+    m_uart.print(addr);
+    m_uart.print("\"");
+    m_uart.print(",");
+    m_uart.println(String(port));
     String data;
     unsigned long start;
     start = millis();
     while (millis()-start<3000) { 
-     if(m_esp_uart.available()>0)
+     if(m_uart.available()>0)
      {
-     char a =m_esp_uart.readChr();
+     char a =m_uart.readChr();
      data=data+a;
      }
      if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 )
@@ -559,21 +544,21 @@
 
 bool ESP8266::send(String str)
 {
-    m_esp_uart.flush();
-    m_esp_uart.print("AT+CIPSEND=");
-    m_esp_uart.println(str.length());
+    m_uart.flush();
+    m_uart.print("AT+CIPSEND=");
+    m_uart.println(str.length());
     unsigned long start;
     start = millis();
     bool found = false;
     while (millis()-start<5000) {                            
-        if(m_esp_uart.find(">")==true )
+        if(m_uart.find(">")==true )
         {
             found = true;
            break;
         }
      }
      if(found)
-        m_esp_uart.print(str);
+        m_uart.print(str);
     else
     {
         closeMux();
@@ -584,9 +569,9 @@
     String data;
     start = millis();
     while (millis()-start<5000) {
-     if(m_esp_uart.available()>0)
+     if(m_uart.available()>0)
      {
-     char a =m_esp_uart.readChr();
+     char a =m_uart.readChr();
      data=data+a;
      }
      if (data.indexOf("SEND OK")!=-1)
@@ -599,23 +584,23 @@
 
 bool ESP8266::send(uint8_t id, String str)
 {
-    m_esp_uart.print("AT+CIPSEND=");
+    m_uart.print("AT+CIPSEND=");
 
-    m_esp_uart.print(String(id));
-    m_esp_uart.print(",");
-    m_esp_uart.println(str.length());
+    m_uart.print(String(id));
+    m_uart.print(",");
+    m_uart.println(str.length());
     unsigned long start;
     start = millis();
     bool found = false;
     while (millis()-start<5000) {                          
-        if(m_esp_uart.find(">")==true )
+        if(m_uart.find(">")==true )
         {
             found = true;
            break;
         }
      }
      if(found)
-        m_esp_uart.print(str);
+        m_uart.print(str);
     else
     {
         closeMux(id);
@@ -626,9 +611,9 @@
     String data;
     start = millis();
     while (millis()-start<5000) {
-     if(m_esp_uart.available()>0)
+     if(m_uart.available()>0)
      {
-     char a =m_esp_uart.readChr();
+     char a =m_uart.readChr();
      data=data+a;
      }
      if (data.indexOf("SEND OK")!=-1)
@@ -642,15 +627,15 @@
 
 void ESP8266::closeMux(void)
 {
-    m_esp_uart.println("AT+CIPCLOSE");
+    m_uart.println("AT+CIPCLOSE");
 
     String data;
     unsigned long start;
     start = millis();
     while (millis()-start<3000) {
-     if(m_esp_uart.available()>0)
+     if(m_uart.available()>0)
      {
-     char a =m_esp_uart.readChr();
+     char a =m_uart.readChr();
      data=data+a;
      }
      if (data.indexOf("Linked")!=-1 || data.indexOf("ERROR")!=-1 || data.indexOf("we must restart")!=-1)
@@ -662,15 +647,15 @@
 
 void ESP8266::closeMux(uint8_t id)
 {
-    m_esp_uart.print("AT+CIPCLOSE=");
-    m_esp_uart.println(String(id));
+    m_uart.print("AT+CIPCLOSE=");
+    m_uart.println(String(id));
     String data;
     unsigned long start;
     start = millis();
     while (millis()-start<3000) {
-     if(m_esp_uart.available()>0)
+     if(m_uart.available()>0)
      {
-     char a =m_esp_uart.readChr();
+     char a =m_uart.readChr();
      data=data+a;
      }
      if (data.indexOf("OK")!=-1 || data.indexOf("Link is not")!=-1 || data.indexOf("Cant close")!=-1)
@@ -684,15 +669,15 @@
 String ESP8266::showIP(void)
 {
     bool ret = false;
-    m_esp_uart.flush();
-    m_esp_uart.println("AT+CIFSR");  
+    m_uart.flush();
+    m_uart.println("AT+CIFSR");  
     
     String data;
     unsigned long start;
     start = millis();
     while (millis()-start < 3000) {
-        while(m_esp_uart.available() > 0) {
-            char a = m_esp_uart.readChr();
+        while(m_uart.available() > 0) {
+            char a = m_uart.readChr();
             data += a;
         }
         if (data.indexOf("OK") != -1) {
@@ -719,19 +704,19 @@
 
 bool ESP8266::confServer(uint8_t mode, int port)
 {
-    m_esp_uart.print("AT+CIPSERVER=");  
-    m_esp_uart.print(String(mode));
-    m_esp_uart.print(",");
-    m_esp_uart.println(String(port));
+    m_uart.print("AT+CIPSERVER=");  
+    m_uart.print(String(mode));
+    m_uart.print(",");
+    m_uart.println(String(port));
 
     String data;
     unsigned long start;
     start = millis();
     bool found = false;
     while (millis()-start<3000) {
-     if(m_esp_uart.available()>0)
+     if(m_uart.available()>0)
      {
-     char a =m_esp_uart.readChr();
+     char a =m_uart.readChr();
      data=data+a;
      }
      if (data.indexOf("OK")!=-1 || data.indexOf("no charge")!=-1)
@@ -740,6 +725,6 @@
          break;
      }
   }
-  m_esp_uart.flush();
+  m_uart.flush();
   return found;
 }