Index: rtos_arduino/trunk/arduino_lib/libraries/ESP8266_Arudino_AT/ESP8266.cpp
===================================================================
--- rtos_arduino/trunk/arduino_lib/libraries/ESP8266_Arudino_AT/ESP8266.cpp	(revision 177)
+++ rtos_arduino/trunk/arduino_lib/libraries/ESP8266_Arudino_AT/ESP8266.cpp	(revision 189)
@@ -31,4 +31,15 @@
 #ifdef TOPPERS_WITH_ARDUINO
 #define WAIT_TIMEOUT delay(1);
+#define ENTER_CRITICAL
+#define LEAVE_CRITICAL
+#else /* !TOPPERS_WITH_ARDUINO */
+#define WAIT_TIMEOUT
+#define ENTER_CRITICAL
+#define LEAVE_CRITICAL
+#endif /* TOPPERS_WITH_ARDUINO */
+
+
+#ifdef TOPPERS_WITH_ARDUINO
+#define WAIT_TIMEOUT delay(1);
 #else /* !TOPPERS_WITH_ARDUINO */
 #define WAIT_TIMEOUT
@@ -98,5 +109,4 @@
 }
 
-
 uint32_t ESP8266_RingBuffer::copy(uint8_t *pdata, uint32_t size)
 {
@@ -143,5 +153,7 @@
 {
     String version;
-    
+    int ret = 0;
+    
+    ENTER_CRITICAL;
     m_puart = &uart;
     m_baud = baud;
@@ -150,24 +162,34 @@
     rx_empty();
     
-    if (!kick()) {
-        return 1;
-    }
-    
-    eATGMR(version);
-    if(version.indexOf(ESP8266_SUPPORT_VERSION) == -1) {
-        return 2;
-    }
-    
-    return 0;
+    if (eAT()) {
+        eATGMR(version);
+        if(version.indexOf(ESP8266_SUPPORT_VERSION) == -1) {
+            ret = 2;
+        }
+    }
+    else {        
+        ret = 1;
+    }
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::kick(void)
 {
-    return eAT();
-}
-
-bool ESP8266::restart(void)
+    bool ret;
+    
+    ENTER_CRITICAL;
+    ret = eAT();
+    LEAVE_CRITICAL;
+    
+    return ret;
+}
+
+bool ESP8266::_restart(void)
 {
     unsigned long start;
+    bool ret = false;
+    
     initialize_status();
     if (eATRST()) {
@@ -179,10 +201,22 @@
             if (eAT()) {
                 delay(1500); /* Waiting for stable */
-                return true;
+                ret = true;
             }
             delay(100);
         }
     }
-    return false;
+    
+    return ret;
+}
+
+bool ESP8266::restart(void)
+{
+    bool ret;
+    
+    ENTER_CRITICAL;
+    ret = _restart();
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
@@ -190,5 +224,9 @@
 {
     String version;
+    
+    ENTER_CRITICAL;
     eATGMR(version);
+    LEAVE_CRITICAL;
+    
     return version;
 }
@@ -197,16 +235,19 @@
 {
     uint8_t mode;
-    if (!qATCWMODE_CUR(&mode)) {
-        return false;
-    }
-    if (mode == ESP8266_WMODE_STATION) {
-        return true;
-    } else {
-        if (sATCWMODE_CUR(ESP8266_WMODE_STATION) && restart()) {
-            return true;
+    bool ret = false;
+
+    ENTER_CRITICAL;
+    if (qATCWMODE_CUR(&mode)) {
+        if (mode == ESP8266_WMODE_STATION) {
+            ret = true;
         } else {
-            return false;
-        }
-    }
+            if (sATCWMODE_CUR(ESP8266_WMODE_STATION) && _restart()) {
+                ret = true;
+            }
+        }
+    }
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
@@ -214,16 +255,19 @@
 {
     uint8_t mode;
-    if (!qATCWMODE_CUR(&mode)) {
-        return false;
-    }
-    if (mode == ESP8266_WMODE_SOFTAP) {
-        return true;
-    } else {
-        if (sATCWMODE_CUR(ESP8266_WMODE_SOFTAP) && restart()) {
-            return true;
+    bool ret = false;
+
+    ENTER_CRITICAL;
+    if (qATCWMODE_CUR(&mode)) {
+        if (mode == ESP8266_WMODE_SOFTAP) {
+            ret = true;
         } else {
-            return false;
-        }
-    }
+            if (sATCWMODE_CUR(ESP8266_WMODE_SOFTAP) && _restart()) {
+                ret = true;
+            }
+        }
+    }
+    LEAVE_CRITICAL;
+
+    return ret;
 }
 
@@ -231,16 +275,19 @@
 {
     uint8_t mode;
-    if (!qATCWMODE_CUR(&mode)) {
-        return false;
-    }
-    if (mode == ESP8266_WMODE_AP_STATION) {
-        return true;
-    } else {
-        if (sATCWMODE_CUR(ESP8266_WMODE_AP_STATION) && restart()) {
-            return true;
+    bool ret = false;
+
+    ENTER_CRITICAL;
+    if (qATCWMODE_CUR(&mode)) {
+        if (mode == ESP8266_WMODE_AP_STATION) {
+            ret = true;
         } else {
-            return false;
-        }
-    }
+            if (sATCWMODE_CUR(ESP8266_WMODE_AP_STATION) && restart()) {
+                ret = true;
+            }
+        }
+    }
+    LEAVE_CRITICAL;
+
+    return ret;    
 }
 
@@ -248,5 +295,9 @@
 {
     String list;
+    
+    ENTER_CRITICAL;
     eATCWLAP(list);
+    LEAVE_CRITICAL;
+    
     return list;
 }
@@ -254,15 +305,33 @@
 bool ESP8266::joinAP(String ssid, String pwd)
 {
-    return sATCWJAP_CUR(ssid, pwd);
+    bool ret;
+
+    ENTER_CRITICAL;
+    ret = sATCWJAP_CUR(ssid, pwd);
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::leaveAP(void)
 {
-    return eATCWQAP();
+    bool ret;
+
+    ENTER_CRITICAL;
+    ret = eATCWQAP();
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::setSoftAPParam(String ssid, String pwd, uint8_t chl, uint8_t ecn)
 {
-    return sATCWSAP_CUR(ssid, pwd, chl, ecn);
+    bool ret;
+
+    ENTER_CRITICAL;
+    ret = sATCWSAP_CUR(ssid, pwd, chl, ecn);
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
@@ -270,5 +339,9 @@
 {
     String list;
+
+    ENTER_CRITICAL;
     eATCWLIF(list);
+    LEAVE_CRITICAL;
+    
     return list;
 }
@@ -277,5 +350,9 @@
 {
     String list;
+
+    ENTER_CRITICAL;
     eATCIPSTATUS(list);
+    LEAVE_CRITICAL;
+    
     return list;
 }
@@ -283,43 +360,59 @@
 String ESP8266::getLocalIP(void)
 {
-    String list;
+    String list = "";
     int32_t index_start = -1;
     int32_t index_end = -1;
-    
+
+    ENTER_CRITICAL;
     eATCIFSR(list);
     index_start = list.indexOf('\"');
     index_end   = list.indexOf('\"', list.indexOf('\"')+1);
     if ((index_start !=  -1) && (index_end != -1)) {
-        return list.substring(index_start+1, index_end);
-    }
-    return "";
+        list = list.substring(index_start+1, index_end);
+    }
+    LEAVE_CRITICAL;
+    
+    return list;
 }
 
 bool ESP8266::enableMUX(void)
 {
+    bool ret = false;
+
+    ENTER_CRITICAL;
     if (sATCIPMUX(1)) {
         mux_mode = true;
-        return true;
-      }
-    return false;
+        ret = true;
+    }
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::disableMUX(void)
 {
+    bool ret = false;
+
+    ENTER_CRITICAL;
     if (sATCIPMUX(0)){
         mux_mode = false;
-        return true;
-    }
-    return false;
+        ret = true;
+    }
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::createTCP(String addr, uint32_t port)
 {
-    bool ret;
+    bool ret = false;
     if (mux_mode) return false;
 
+    ENTER_CRITICAL;
     if((ret = sATCIPSTARTSingle("TCP", addr, port))) {
         connection_bitmap = 1;
     }
+    LEAVE_CRITICAL;
+    
     return ret;
 }
@@ -327,11 +420,14 @@
 bool ESP8266::releaseTCP(void)
 {
-    bool ret;
-    
-    if (mux_mode) return false;    
+    bool ret = false;    
+    if (mux_mode) return false;
+
+    ENTER_CRITICAL;
     if((ret = eATCIPCLOSESingle())) {
         connection_bitmap = 0;
         rx_buffer[MUXID_SINGLE].free();
     }
+    LEAVE_CRITICAL;
+    
     return ret;    
 }
@@ -339,9 +435,13 @@
 bool ESP8266::registerUDP(String addr, uint32_t port)
 {
-    bool ret;
+    bool ret = false;
     if (mux_mode) return false;
+
+    ENTER_CRITICAL;
     if((ret = sATCIPSTARTSingle("UDP", addr, port))) {
         connection_bitmap = 1;
-    }    
+    }
+    LEAVE_CRITICAL;
+    
     return ret;
 }
@@ -349,10 +449,14 @@
 bool ESP8266::unregisterUDP(void)
 {
-    bool ret;
+    bool ret = false;
     if (mux_mode) return false;
+
+    ENTER_CRITICAL;
     if((ret = eATCIPCLOSESingle())) {
         connection_bitmap = 0;
         rx_buffer[MUXID_SINGLE].free();
-    }    
+    }
+    LEAVE_CRITICAL;
+    
     return ret;    
 }
@@ -360,11 +464,14 @@
 bool ESP8266::createTCP(uint8_t mux_id, String addr, uint32_t port)
 {
-    bool ret;
+    bool ret = false;
     if (!(mux_id < ESP8266_NUM_CONNECTION)) return false;
     if (!mux_mode) return false;
-    
+
+    ENTER_CRITICAL;    
     if((ret = sATCIPSTARTMultiple(mux_id, "TCP", addr, port))) {
         connection_bitmap |= 1 << mux_id;
     }
+    LEAVE_CRITICAL;
+    
     return ret;
 }
@@ -375,9 +482,12 @@
     if (!(mux_id < ESP8266_NUM_CONNECTION)) return false;
     if (!mux_mode) return false;
-    
+
+    ENTER_CRITICAL;
     if ((ret = sATCIPCLOSEMulitple(mux_id))) {
         connection_bitmap &= ~(1 << mux_id);
         rx_buffer[mux_id].free();
     }
+    LEAVE_CRITICAL;
+    
     return ret;
 }
@@ -388,8 +498,11 @@
     if (!(mux_id < ESP8266_NUM_CONNECTION)) return false;
     if (!mux_mode) return false;
-    
+
+    ENTER_CRITICAL;
     if ((ret = sATCIPSTARTMultiple(mux_id, "UDP", addr, port))) {
         connection_bitmap |= 1 << mux_id;
     }
+    LEAVE_CRITICAL;
+    
     return ret;
 }
@@ -400,9 +513,12 @@
     if (!(mux_id < ESP8266_NUM_CONNECTION)) return false;
     if (!mux_mode) return false;
-    
+
+    ENTER_CRITICAL;
     if ((ret = sATCIPCLOSEMulitple(mux_id))) {
         connection_bitmap &= ~(1 << mux_id);
         rx_buffer[mux_id].free();
     }
+    LEAVE_CRITICAL;
+    
     return ret;    
 }
@@ -410,131 +526,228 @@
 bool ESP8266::setTCPServerTimeout(uint32_t timeout)
 {
+    bool ret;
     if (!mux_mode) return false;
-    return sATCIPSTO(timeout);
+
+    ENTER_CRITICAL;
+    ret = sATCIPSTO(timeout);
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::startTCPServer(uint32_t port)
 {
+    bool ret = false;
     if (!mux_mode) return false;
-    if (sATCIPSERVER(1, port)) {
-        return true;
-    }
-    return false;
+
+    ENTER_CRITICAL;
+    ret = sATCIPSERVER(1, port);
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::stopTCPServer(void)
 {
-    sATCIPSERVER(0);
-    return false;
+    bool ret = false;
+    
+    ENTER_CRITICAL;
+    ret = sATCIPSERVER(0);
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::startServer(uint32_t port)
 {
-    if (!mux_mode) return false;    
-    return startTCPServer(port);
+    bool ret = false;    
+    if (!mux_mode) return false;
+    
+    ENTER_CRITICAL;
+    ret = startTCPServer(port);
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::stopServer(void)
 {
-    return stopTCPServer();
+    bool ret;
+
+    ENTER_CRITICAL;
+    ret = stopTCPServer();
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::send(const uint8_t *buffer, uint32_t len)
 {
+    bool ret;
+    if (mux_mode) return false;
+
+    ENTER_CRITICAL;
+    ret = sATCIPSENDSingle(buffer, len);
+    LEAVE_CRITICAL;
+    
+    return ret;
+}
+
+bool ESP8266::send(uint8_t mux_id, const uint8_t *buffer, uint32_t len)
+{
+    bool ret;
+    if (!mux_mode) return false;
+
+    ENTER_CRITICAL;
+    ret = sATCIPSENDMultiple(mux_id, buffer, len);
+    LEAVE_CRITICAL;
+    
+    return ret;
+}
+
+bool ESP8266::send(String &str)
+{
+    bool ret;
     if (mux_mode) return 0;
-    return sATCIPSENDSingle(buffer, len);
-}
-
-bool ESP8266::send(uint8_t mux_id, const uint8_t *buffer, uint32_t len)
-{
-    if (!mux_mode) return false;
-    return sATCIPSENDMultiple(mux_id, buffer, len);
-}
-
-bool ESP8266::send(String &str)
-{
-    if (mux_mode) return 0;
-    return sATCIPSENDSingle(str);
+
+    ENTER_CRITICAL;
+    ret = sATCIPSENDSingle(str);
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::send(uint8_t mux_id, String &str)
 {
+    bool ret;
     if (!mux_mode) return false;
     if (!(mux_id < ESP8266_NUM_CONNECTION)) return false;
-    
-    return sATCIPSENDMultiple(mux_id, str);
+
+    ENTER_CRITICAL;
+    ret = sATCIPSENDMultiple(mux_id, str);
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 uint32_t ESP8266::recv(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
 {
+    uint32_t ret;
     if (mux_mode) return 0;
-    return recvPkg(buffer, buffer_size, timeout, 0, NULL);
+
+    ENTER_CRITICAL;
+    ret = recvPkg(buffer, buffer_size, timeout, 0, NULL);
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 uint32_t ESP8266::recv(uint8_t mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
 {
+    uint32_t ret;
     if (!mux_mode) return false;
     if (!(mux_id < ESP8266_NUM_CONNECTION)) return false;
-    
-    return recvPkg(buffer, buffer_size, timeout, mux_id, NULL);
+
+    ENTER_CRITICAL;
+    ret = recvPkg(buffer, buffer_size, timeout, mux_id, NULL);
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 uint32_t ESP8266::recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
 {
+    uint32_t ret;
     if (!mux_mode) return false;
-    return recvPkg(buffer, buffer_size, timeout, MUXID_ANY, coming_mux_id);
+
+    ENTER_CRITICAL;
+    ret = recvPkg(buffer, buffer_size, timeout, MUXID_ANY, coming_mux_id);
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::isDataAvailable(uint8_t mux_id)
 {
+    bool ret;
     if (!mux_mode) return false;
     if (!(mux_id < ESP8266_NUM_CONNECTION)) return false;
 
-    if(rx_buffer[mux_id].length() > 0) return true;
-    rx_update();
-    if(rx_buffer[mux_id].length() > 0) return true;
-    return 0;
+    ENTER_CRITICAL;
+    if(rx_buffer[mux_id].length() > 0) ret = true;
+    if (ret == false) {
+        rx_update();
+        if(rx_buffer[mux_id].length() > 0) ret = true;
+    }    
+    LEAVE_CRITICAL;
+        
+    return ret;
 }
 
 bool ESP8266::isDataAvailable(void)
 {
+    bool ret = false;
+
+    ENTER_CRITICAL;
     if (mux_mode) {
         for(int i = 0; i < ESP8266_NUM_CONNECTION; i++){
-            if(rx_buffer[i].length() > 0) return true;
-        }
-        rx_update();
-        for(int i = 0; i < ESP8266_NUM_CONNECTION; i++){
-            if(rx_buffer[i].length() > 0) return true;
+            if(rx_buffer[i].length() > 0) ret = true;
+        }
+        if (ret == false) {
+            rx_update();
+            for(int i = 0; i < ESP8266_NUM_CONNECTION; i++){
+                if(rx_buffer[i].length() > 0) ret = true;
+            }
         }
     }
     else {
         /* single mode */
-        if(rx_buffer[0].length() > 0) return true;
-        rx_update();
-        if(rx_buffer[0].length() > 0) return true;        
-    }
-    return false;
+        if(rx_buffer[0].length() > 0) ret = true;
+        if (ret == false) {
+            rx_update();
+            if(rx_buffer[0].length() > 0) ret = true;
+        }
+    }
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::isConnected(void)
 {
-    if (mux_mode) return false;    
-    rx_update();
-    return (connection_bitmap == 1);
+    bool ret;
+    if (mux_mode) return false;
+    
+    ENTER_CRITICAL;
+    rx_update();
+    ret = (connection_bitmap == 1);
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::isConnected(uint8_t mux_id)
 {
+    bool ret;
     if (!mux_mode) return false;    
     if (!(mux_id < ESP8266_NUM_CONNECTION)) return false;
-    rx_update();
-    return ((connection_bitmap & (1<<mux_id)) != 0);
+
+    ENTER_CRITICAL;
+    rx_update();
+    ret = ((connection_bitmap & (1<<mux_id)) != 0);
+    LEAVE_CRITICAL;
+    
+    return ret;
 }
 
 bool ESP8266::getMuxCStatus(uint8_t *mux_id_ptn)
 {
-    if (!mux_mode) return false;    
+    if (!mux_mode) return false;
+
+    ENTER_CRITICAL;
     rx_update();
     *mux_id_ptn = connection_bitmap;
+    LEAVE_CRITICAL;
+    
     return true;
 }
@@ -582,5 +795,5 @@
             if (rx_cmd.indexOf("+IPD,") != -1) {
                 if((ret = recvIPD(timeout - (millis() - start), buffer, buffer_size,
-                                  mux_id, coming_mux_id)) > 0) {
+                                  mux_id, coming_mux_id)) != 0) {
                     rx_cmd = "";
                     return ret;
@@ -614,5 +827,5 @@
     uint32_t i;
     ESP8266_RingBuffer *p_rx_buffer;
-      
+
     start = millis();
     while (millis() - start < timeout) {        
@@ -665,6 +878,6 @@
                 }
                 WAIT_TIMEOUT
-            }            
-            return 0;
+            }
+            return i;
         }
         else {
@@ -691,5 +904,6 @@
                 WAIT_TIMEOUT;
             }
-        }        
+        }
+        return i;
     }
     
@@ -710,5 +924,5 @@
 
     if (rx_cmd.indexOf("+IPD,") != -1) {
-        if(recvIPD(timeout) > 0) {
+        if(recvIPD(timeout) != 0) {
             rx_cmd = "";
         }
@@ -755,5 +969,5 @@
     unsigned long start = millis();
     while (millis() - start < timeout) {
-        while (m_puart->available() > 0) {            
+        while (m_puart->available() > 0) {
             a = m_puart->read();
             rx_cmd += a;
@@ -800,4 +1014,5 @@
 bool ESP8266::eAT(void)
 {
+    rx_update();
     m_puart->println("AT");
     return recvFind("OK");
@@ -806,4 +1021,5 @@
 bool ESP8266::eATRST(void) 
 {
+    rx_update();
     m_puart->println("AT+RST");
     return recvFind("OK");
@@ -812,4 +1028,5 @@
 bool ESP8266::eATGMR(String &version)
 {
+    rx_update();
     m_puart->println("AT+GMR");
     return recvFindAndFilter("OK", "\r\r\n", "\r\nOK", version); 
@@ -823,4 +1040,5 @@
         return false;
     }
+    rx_update();
     m_puart->println("AT+CWMODE_CUR?");
     ret = recvFindAndFilter("OK", "+CWMODE_CUR:", "\r\n\r\nOK", str_mode); 
@@ -836,5 +1054,6 @@
 {
     int ret;
-    
+
+    rx_update();
     m_puart->print("AT+CWMODE_CUR=");
     m_puart->println(mode);
@@ -849,5 +1068,6 @@
     int ret;
     
-    m_puart->print("AT+CWJAP=\"");
+    rx_update();
+    m_puart->print("AT+CWJAP_CUR=\"");
     m_puart->print(ssid);
     m_puart->print("\",\"");
@@ -862,4 +1082,5 @@
 bool ESP8266::eATCWLAP(String &list)
 {
+    rx_update();
     m_puart->println("AT+CWLAP");
     return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list, 10000);
@@ -868,4 +1089,5 @@
 bool ESP8266::eATCWQAP(void)
 {
+    rx_update();
     m_puart->println("AT+CWQAP");
     return recvFind("OK");
@@ -876,4 +1098,5 @@
     int ret;
     
+    rx_update();
     m_puart->print("AT+CWSAP_CUR=\"");
     m_puart->print(ssid);
@@ -892,4 +1115,5 @@
 bool ESP8266::eATCWLIF(String &list)
 {
+    rx_update();
     m_puart->println("AT+CWLIF");
     return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
@@ -898,4 +1122,5 @@
 bool ESP8266::eATCIPSTATUS(String &list)
 {
+    rx_update();
     m_puart->println("AT+CIPSTATUS");
     return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
@@ -905,5 +1130,6 @@
 {
     int ret;
-    
+
+    rx_update();
     m_puart->print("AT+CIPSTART=\"");
     m_puart->print(type);
@@ -921,5 +1147,6 @@
 {
     int ret;
-    
+
+    rx_update();
     m_puart->print("AT+CIPSTART=");
     m_puart->print(mux_id);
@@ -938,4 +1165,6 @@
 bool ESP8266::sATCIPSENDSingle(const uint8_t *buffer, uint32_t len)
 {
+    bool ret;
+    rx_update();
     m_puart->print("AT+CIPSEND=");
     m_puart->println(len);
@@ -944,6 +1173,8 @@
             m_puart->write(buffer[i]);
         }
-        return recvFind("SEND OK", 10000);
-    }
+        ret = recvFind("SEND OK", 10000);
+        return ret;
+    }
+    
     return false;
 }
@@ -951,4 +1182,5 @@
 bool ESP8266::sATCIPSENDMultiple(uint8_t mux_id, const uint8_t *buffer, uint32_t len)
 {
+    rx_update();
     m_puart->print("AT+CIPSEND=");
     m_puart->print(mux_id);
@@ -966,4 +1198,5 @@
 bool ESP8266::sATCIPSENDSingle(String &str)
 {
+    rx_update();
     m_puart->print("AT+CIPSEND=");
     m_puart->println(str.length());
@@ -979,4 +1212,5 @@
 bool ESP8266::sATCIPSENDMultiple(uint8_t mux_id, String &str)
 {
+    rx_update();
     m_puart->print("AT+CIPSEND=");
     m_puart->print(mux_id);
@@ -996,4 +1230,5 @@
     int ret;
     
+    rx_update();
     m_puart->print("AT+CIPCLOSE=");
     m_puart->println(mux_id);
@@ -1006,10 +1241,17 @@
 bool ESP8266::eATCIPCLOSESingle(void)
 {
+    int ret;
+    
+    Serial.println(rx_cmd);
+    rx_update();
     m_puart->println("AT+CIPCLOSE");
-    return recvFind("OK", 5000);
+    ret = recvString("OK", "ERROR", "", 5000);
+    if (ret != -1) rx_cmd = "";
+    return ((ret == 1) || (ret == 2));
 }
 
 bool ESP8266::eATCIFSR(String &list)
 {
+    rx_update();
     m_puart->println("AT+CIFSR");
     return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
@@ -1019,5 +1261,6 @@
 {
     int ret;
-    
+
+    rx_update();
     m_puart->print("AT+CIPMUX=");
     m_puart->println(mode);
@@ -1033,4 +1276,5 @@
     
     if (mode) {
+        rx_update();
         m_puart->print("AT+CIPSERVER=1,");
         m_puart->println(port);        
@@ -1039,4 +1283,5 @@
         return (ret == 1);
     } else {
+        rx_update();
         m_puart->println("AT+CIPSERVER=0");
         ret = recvString("OK", "ERROR", "");
@@ -1047,4 +1292,5 @@
 bool ESP8266::sATCIPSTO(uint32_t timeout)
 {
+    rx_update();
     m_puart->print("AT+CIPSTO=");
     m_puart->println(timeout);
