Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ESP8266Interface by
Revision 28:91e65e22e63a, committed 2015-04-28
- Comitter:
- mbedAustin
- Date:
- Tue Apr 28 18:52:20 2015 +0000
- Parent:
- 27:9c6cefc12f0c
- Child:
- 29:939372104145
- Commit message:
- aquiring IP Address correctly, but not returning it from inner call level. Safety commit before mucking around
Changed in this revision
--- 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; } }
--- a/ESP8266/ESP8266.h Mon Apr 20 20:27:11 2015 +0000 +++ b/ESP8266/ESP8266.h Tue Apr 28 18:52:20 2015 +0000 @@ -47,8 +47,9 @@ * @param reset reset pin of the wifi module () * @param ssid ssid of the network * @param phrase WEP, WPA or WPA2 key + * @param baud the baudrate of the serial connection */ - ESP8266( PinName tx, PinName rx, PinName reset, const char * ssid, const char * phrase ); + ESP8266( PinName tx, PinName rx, PinName reset, const char * ssid, const char * phrase, uint32_t baud ); /** * Connect the wifi module to the ssid contained in the constructor. @@ -143,7 +144,7 @@ void flush(); /** - * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode + * Send a command to the wifi module. Check if the module is in command mode. If not enter in command mode * * @param str string to be sent * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO") @@ -172,7 +173,7 @@ }; protected: - Serial wifi; + RawSerial wifi; DigitalOut reset_pin; char phrase[30]; char ssid[30];
--- a/ESP8266Interface.cpp Mon Apr 20 20:27:11 2015 +0000 +++ b/ESP8266Interface.cpp Tue Apr 28 18:52:20 2015 +0000 @@ -1,8 +1,8 @@ #include "ESP8266Interface.h" ESP8266Interface::ESP8266Interface( PinName tx, PinName rx, PinName reset, - const char * ssid, const char * phrase ) : - ESP8266(tx, rx, reset, ssid, phrase ) + const char * ssid, const char * phrase, uint32_t baud ) : + ESP8266(tx, rx, reset, ssid, phrase, baud ) { }
--- a/ESP8266Interface.h Mon Apr 20 20:27:11 2015 +0000 +++ b/ESP8266Interface.h Tue Apr 28 18:52:20 2015 +0000 @@ -37,8 +37,9 @@ * \param reset reset pin of the wifi module () * \param ssid ssid of the network * \param phrase WEP or WPA key + * \param baud the baudrate of the serial connection (defaults to 115200, diff revs of the firmware use diff baud rates */ - ESP8266Interface(PinName tx, PinName rx, PinName reset, const char * ssid, const char * phrase ); + ESP8266Interface(PinName tx, PinName rx, PinName reset, const char * ssid, const char * phrase, uint32_t baud = 115200 ); /** Initialize the interface with DHCP. * Initialize the interface and configure it to use DHCP (no connection at this point).
--- a/Socket/Socket.cpp Mon Apr 20 20:27:11 2015 +0000 +++ b/Socket/Socket.cpp Tue Apr 28 18:52:20 2015 +0000 @@ -28,7 +28,7 @@ } void Socket::set_blocking(bool blocking, unsigned int timeout) { - pc.printf("set blocking: %d %d\r\n", blocking, timeout); + printf("set blocking: %d %d\r\n", blocking, timeout); _blocking = blocking; _timeout = timeout; }