Init

Fork of ESP8266Interface by ESP8266

Revision:
28:91e65e22e63a
Parent:
26:0d5bcb3903e2
Child:
29:939372104145
--- a/ESP8266/ESP8266.cpp	Mon Apr 20 20:27:11 2015 +0000
+++ b/ESP8266/ESP8266.cpp	Tue Apr 28 18:52:20 2015 +0000
@@ -23,17 +23,17 @@
 #include <algorithm>
 
 //Debug is disabled by default
-#if (defined(DEBUG))
-#define DBG(x, ...) std::printf("[ESP8266 : DBG]"x"\r\n", ##__VA_ARGS__);
-#define WARN(x, ...) std::printf("[ESP8266 : WARN]"x"\r\n", ##__VA_ARGS__);
-#define ERR(x, ...) std::printf("[ESP8266 : ERR]"x"\r\n", ##__VA_ARGS__);
+#ifdef DEBUG
+#define DBG(x, ...)  printf("[ESP8266 : DBG]"x"\r\n", ##__VA_ARGS__);
+#define WARN(x, ...) printf("[ESP8266 : WARN]"x"\r\n", ##__VA_ARGS__);
+#define ERR(x, ...)  printf("[ESP8266 : ERR]"x"\r\n", ##__VA_ARGS__);
 #else
 #define DBG(x, ...)
 #define WARN(x, ...)
 #define ERR(x, ...)
 #endif
 
-#if defined(DEBUG)
+#ifdef DEBUG
 #define INFO(x, ...) printf("[ESP8266 : INFO]"x"\r\n", ##__VA_ARGS__);
 #else
 #define INFO(x, ...)
@@ -45,7 +45,7 @@
 
 ESP8266 * ESP8266::inst;
 
-ESP8266::ESP8266(   PinName tx, PinName rx, PinName _reset, const char * ssid, const char * phrase ):
+ESP8266::ESP8266(   PinName tx, PinName rx, PinName _reset, const char * ssid, const char * phrase, uint32_t baud):
     wifi(tx, rx), reset_pin(_reset), buf_ESP8266(256)
 {
     memset(&state, 0, sizeof(state));
@@ -65,9 +65,9 @@
 
     inst = this;
     attach_rx(false);
-    
-    wifi.baud(115200); // initial baud rate of the ESP8266 
-    
+
+    wifi.baud(baud); // initial baud rate of the ESP8266
+
     state.associated = false;
     state.cmdMode = false;
 }
@@ -76,7 +76,7 @@
 {
     sendCommand( "AT+CWMODE=1", "change", NULL, 1000);
     string cmd="AT+CWJAP=\""+(string)this->ssid+"\",\""+(string)this->phrase+"\"";
-    if( sendCommand( cmd.c_str(), "OK", NULL, 10000) ){
+    if( sendCommand( cmd.c_str(), "OK", NULL, 10000) ) {
         // successfully joined the network
         state.associated = true;
         INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase);
@@ -87,7 +87,8 @@
 
 bool ESP8266::connect()
 {
-    return true;
+    sendCommand("AT+CWDHCP=1,1","OK",NULL,1000); // DHCP Enabled in Station Mode
+    return ESP8266::join();
 }
 
 bool ESP8266::is_connected()
@@ -95,16 +96,17 @@
     return true;
 }
 
-bool ESP8266::startUDP(char* ip, int port){
+bool ESP8266::startUDP(char* ip, int port)
+{
     char portstr[5];
     sprintf(portstr, "%d", port);
     sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000);
-    
-    sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode 
-    sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode 
-    pc.printf("Data Mode\r\n");
+
+    sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode
+    sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode
+    printf("Data Mode\r\n");
     state.cmdMode = false;
-    
+
     return true;
 }
 
@@ -123,14 +125,42 @@
     if (!state.associated)
         return true;
     // send command to quit AP
-    sendCommand("AT+CWQAP", "OK", NULL, 10000); 
+    sendCommand("AT+CWQAP", "OK", NULL, 10000);
     state.associated = false;
     return true;
 }
 
+/*
+    Assuming Returned data looks like this:
+    +CIFSR:STAIP,"192.168.11.2"
+    +CIFSR:STAMAC,"18:fe:34:9f:3a:f5"
+    grabbing IP from first set of quotation marks
+*/
 char* ESP8266::getIPAddress()
 {
-    sendCommand("AT+CWLIF", "OK", NULL, 10000);
+    char result[30] = {0};
+    int check = 0;
+    check = sendCommand("AT+CIFSR", NULL, result, 1000);
+    DBG("\r\nReceivedInfo for IP Command is: %s\r\n",result);
+    if(check){
+        // Success
+        string resultString(result);
+        uint8_t pos1 = 0, pos2 = 0;
+        //uint8_t pos3 = 0, pos4 = 0;
+        pos1 = resultString.find("+CIFSR:STAIP");
+        pos1 = resultString.find('"',pos1);
+        pos2 = resultString.find('"',pos1+1);
+        //pos3 = resultString.find('"',pos2+1); //would find mac address
+        //pos4 = resultString.find('"',pos3+1);
+        strncpy(ipString,resultString.substr(pos1,pos2).c_str(),sizeof(ipString));
+        ipString[pos2 - pos1 +1] = 0; // null terminate string correctly.
+        DBG("IP: %s\r\n",ipString);
+        
+    }else{
+        // Failure
+        DBG("getIPAddress() failed\r\n");
+    }
+    
     return ipString;
 }
 
@@ -149,8 +179,7 @@
     if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) {
         strcpy(ip, host);
         return true;
-    }
-    else {
+    } else {
         // dns needed, not currently available
         return false;
     }
@@ -162,12 +191,12 @@
     wait(0.2);
     reset_pin = 1;
     wait(1);
-    
+
     //send("+++",3);
     //wait(1);
     state.cmdMode = true;
     sendCommand("AT", "OK", NULL, 1000);
-    //sendCommand("AT+RST", "ready", NULL, 10000);
+    sendCommand("AT+RST", "ready", NULL, 10000);
     state.associated = false;
 
 }
@@ -182,7 +211,7 @@
 {
     //read characters
     char c;
-    while (wifi.readable()){
+    while (wifi.readable()) {
         c=wifi.getc();
         buf_ESP8266.queue(c);
         //if (state.cmdMode) pc.printf("%c",c); //debug echo, needs fast serial console to prevent UART overruns
@@ -230,7 +259,7 @@
 {
 
     const char* bufptr=buf;
-    for(int i=0; i<len; i++){
+    for(int i=0; i<len; i++) {
         putc((int)*bufptr++);
     }
     return len;
@@ -244,16 +273,16 @@
     Timer tmr;
     int result = 0;
 
-    //pc.printf("will send: %s\r\n",cmd);
+    DBG("will send: %s\r\n",cmd);
 
     attach_rx(true);
 
     //We flush the buffer
     while (readable())
         getc();
-    
+
     if (!ACK || !strcmp(ACK, "NO")) {
-        for (int i = 0; i < strlen(cmd); i++){
+        for (int i = 0; i < strlen(cmd); i++) {
             result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
             wait(.005); // prevents stuck recieve ready (?) need to let echoed character get read first
         }
@@ -267,7 +296,7 @@
             getc();
 
         tmr.start();
-        for (int i = 0; i < strlen(cmd); i++){
+        for (int i = 0; i < strlen(cmd); i++) {
             result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
             wait(.005); // wait for echo
         }
@@ -287,7 +316,7 @@
                 return -1;
             } else if (readable()) {
                 read = getc();
-                pc.printf("%c",read); //debug echo
+                printf("%c",read); //debug echo
                 if ( read != '\r' && read != '\n') {
                     checking += read;
                     found = checking.find(ACK);
@@ -297,7 +326,7 @@
                         //We flush the buffer
                         while (readable())
                             read = getc();
-                            pc.printf("%c",read); //debug echo
+                        printf("%c",read); //debug echo
                         break;
                     }
                 }