modified by ohneta

Dependencies:   ATParser

Dependents:   ESP8266Interface

Fork of ESP8266 by NetworkSocketAPI

Revision:
9:dcf3aa250bc1
Parent:
8:80048194de79
Child:
12:2c5afb36bc8c
--- a/ESP8266.cpp	Thu Jul 23 21:25:06 2015 +0000
+++ b/ESP8266.cpp	Sun Jul 26 21:53:38 2015 +0000
@@ -13,49 +13,52 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
- 
+
 #include "ESP8266.h"
 
-ESP8266::ESP8266(PinName tx, PinName rx)
-: serial(tx, rx), atParser(&serial)
+ESP8266::ESP8266(PinName tx, PinName rx) : serial(tx, rx), atParser(serial)
 {
     serial.baud(115200);
 }
 
-
 bool ESP8266::startup(void)
 {
     return (atParser.send("AT") && atParser.recv("OK"));
 }
+
 bool ESP8266::reset(void)
 {
     return (atParser.send("AT+RST") && atParser.recv("OK\r\nready"));
 }
+
 bool ESP8266::wifiMode(int mode)
 {
     //only 3 valid modes
-    if(mode < 1 || mode > 3)
+    if(mode < 1 || mode > 3) {
         return false;
-    
+    }
+
     char modestr[1];
     sprintf(modestr,"%d",mode);
     string mode_command = "AT+CWMODE="+string(modestr);
-    return (atParser.send(mode_command.c_str()) && atParser.recv("OK"));   
+    return (atParser.send(mode_command.c_str()) && atParser.recv("OK"));
 }
+
 bool ESP8266::multipleConnections(bool enabled)
 {
     int on = (int)enabled;
     char enable[1];
     sprintf(enable,"%d",on);
     string mux_command = "AT+CIPMUX="+string(enable);
-    return (atParser.send(mux_command.c_str()) && atParser.recv("OK"));  
+    return (atParser.send(mux_command.c_str()) && atParser.recv("OK"));
 }
+
 bool ESP8266::dhcp(int mode, bool enabled)
 {
     //only 3 valid modes
-    if(mode < 0 || mode > 2)
+    if(mode < 0 || mode > 2) {
         return false;
-        
+    }
     int on = (int)enabled;
     char enable[1];
     sprintf(enable,"%d",on);
@@ -68,7 +71,7 @@
 bool ESP8266::connect(const char *ap, const char *passPhrase)
 {
     string connect_command = "AT+CWJAP=\""+(string)ap+"\",\""+(string)passPhrase+"\"";
-    return (atParser.send(connect_command.c_str()) && atParser.recv("OK")); 
+    return (atParser.send(connect_command.c_str()) && atParser.recv("OK"));
 }
 
 bool ESP8266::disconnect(void)
@@ -80,28 +83,29 @@
 {
     return (atParser.send("AT+CIPSTA?") && atParser.recv("+CIPSTA:\"%[^\"]\"", ip));
 }
-bool ESP8266::isConnected(void) 
+
+bool ESP8266::isConnected(void)
 {
-    char* ip;
+    char* ip = "";
     return getIPAddress(ip);
 }
-bool ESP8266::openSocket(string sockType, int id, int port, char* addr)
+
+bool ESP8266::openSocket(string sockType, int id, int port, const char* addr)
 {
     //IDs only 0-4
-    if(id > 4)
+    if(id > 4) {
         return false;
-    
+    }
     char portstr[5];
     char idstr[2];
     sprintf(idstr,"%d",id);
     sprintf(portstr, "%d", port);
 
     string start_command = "AT+CIPSTART="+(string)idstr+",\""+sockType+"\",\""+(string)addr+"\","+(string)portstr;
-    if (!(atParser.send(start_command.c_str()) && atParser.recv("OK"))){
+    if (!(atParser.send(start_command.c_str()) && atParser.recv("OK"))) {
         return false;//opening socket not succesful
     }
     return true;
-    
 }
 
 bool ESP8266::sendData(int id, const void *data, uint32_t amount)
@@ -110,11 +114,11 @@
     sprintf(idstr,"%d",id);
     char lenstr[5];
     sprintf(lenstr,"%d",(int)amount);
-    
+
     string send_command = "AT+CIPSEND="+(string)idstr+","+(string)lenstr;
-    if(!atParser.send(send_command.c_str(), "SEND OK")){ 
+    if(!atParser.send(send_command.c_str(), "SEND OK")) {
         return false;
-    } 
+    }
     atParser.write((char*)data, (int)amount);
     return true;
 }
@@ -123,26 +127,26 @@
 {
     int length;
     int id;
-    if (!(atParser.recv("+IPD,%d,%d:", &id, &length) && 
-      atParser.read((char*)data, length) &&
-      atParser.recv("OK"))){  
+    if (!(atParser.recv("+IPD,%d,%d:", &id, &length) && atParser.read((char*)data, length) && atParser.recv("OK"))) {
         return 0;
     }
-    return length;   
+    return length;
 }
-bool ESP8266::close(int id) 
+
+bool ESP8266::close(int id)
 {
     //IDs only 0-4
-    if(id > 4)
+    if(id > 4) {
         return false;
-    
+    }
     char idstr[2];
     sprintf(idstr,"%d",id);
-    string close_command = "AT+CIPCLOSE="+(string)idstr;  
-    
+    string close_command = "AT+CIPCLOSE="+(string)idstr;
+
     return (atParser.send(close_command.c_str()) && atParser.recv("OK"));
 }
+
 void ESP8266::setTimeout(uint32_t timeout_ms)
 {
     atParser.setTimeout(timeout_ms);
-}   
\ No newline at end of file
+}
\ No newline at end of file