Init
Fork of ESP8266Interface by
Diff: ESP8266/ESP8266.cpp
- Revision:
- 16:3f0efaa57a12
- Parent:
- 15:37a7a56a424f
- Child:
- 17:d11fa4c3ac65
--- a/ESP8266/ESP8266.cpp Sun Nov 30 21:56:03 2014 +0000 +++ b/ESP8266/ESP8266.cpp Mon Dec 01 06:22:00 2014 +0000 @@ -18,6 +18,7 @@ #include "mbed.h" #include "ESP8266.h" +#include "Endpoint.h" #include <string> #include <algorithm> @@ -63,14 +64,16 @@ inst = this; attach_rx(false); + + wifi.baud(9600); // initial baud rate of the ESP8266 + pc.printf("ESP8266 test\r\n"); } bool ESP8266::join() { - - for (int i= 0; i < MAX_TRY_JOIN; i++) { - - //join the network + string cmd="AT+CWJAP=\""+(string)this->ssid+"\",\""+(string)this->phrase+"\""; + 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); return true; @@ -80,7 +83,7 @@ bool ESP8266::connect() { - return join(); + return true; } bool ESP8266::is_connected() @@ -88,8 +91,20 @@ return true; } +bool ESP8266::startUDP(char* ip, int port){ + char* portstr = ""; + sprintf(portstr, "%d", port); + + sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"" + (string) portstr + "\"").c_str(), "OK", NULL, 10000); + + sendCommand("AT+CIPSEND", "OK", NULL, 1000);// go into transparent mode + + return true; +} + bool ESP8266::close() { + sendCommand("AT+CIPCLOSE","OK", NULL, 10000); return true; } @@ -99,13 +114,14 @@ if (!state.associated) return true; // send command to quit AP - // + sendCommand("AT+CWQAP", "OK", NULL, 10000); state.associated = false; return true; } char* ESP8266::getIPAddress() { + sendCommand("AT+CWLIF", "OK", NULL, 10000); return ipString; } @@ -133,22 +149,30 @@ void ESP8266::reset() { + sendCommand("AT+RST", "ready", NULL, 10000); + /* reset_pin = 0; wait(0.2); reset_pin = 1; wait(0.2); + */ } bool ESP8266::reboot() { + reset(); return true; } void ESP8266::handler_rx(void) { //read characters - while (wifi.readable()) - buf_ESP8266.queue(wifi.getc()); + char c; + while (wifi.readable()){ + c=wifi.getc(); + buf_ESP8266.queue(c); + //pc.printf("%c",c); + } } void ESP8266::attach_rx(bool callback) @@ -188,12 +212,17 @@ buf_ESP8266.flush(); } -bool ESP8266::sendCommand(const char * cmd, const char * ack, char * res, int timeout) +int ESP8266::send(const char * buf, int len) { - return true; + const char* bufptr=buf; + while(!wifi.writeable()){} + for(int i=0; i<len; i++){ + wifi.putc((int)*bufptr++); + while(!wifi.writeable()){} + }return true; } -int ESP8266::send(const char * str, int len, const char * ACK, char * res, int timeout) +bool ESP8266::sendCommand(const char * cmd, const char * ACK, char * res, int timeout) { char read; size_t found = string::npos; @@ -201,25 +230,38 @@ Timer tmr; int result = 0; - DBG("will send: %s\r\n",str); + DBG("will send: %s\r\n",cmd); attach_rx(false); //We flush the buffer while (wifi.readable()) wifi.getc(); + + while(!wifi.writeable()){}; if (!ACK || !strcmp(ACK, "NO")) { - for (int i = 0; i < len; i++) - result = (putc(str[i]) == str[i]) ? result + 1 : result; + for (int i = 0; i < sizeof(cmd); i++){ + result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result; + while(!wifi.writeable()){}; + } + putc(13); //CR + while(!wifi.writeable()){}; + putc(10); //LF + } else { //We flush the buffer while (wifi.readable()) wifi.getc(); tmr.start(); - for (int i = 0; i < len; i++) - result = (putc(str[i]) == str[i]) ? result + 1 : result; + for (int i = 0; i < sizeof(cmd); i++){ + result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result; + while(!wifi.writeable()){}; + } + putc(13); //CR + while(!wifi.writeable()){}; + putc(10); //LF while (1) { if (tmr.read_ms() > timeout) { @@ -233,6 +275,7 @@ return -1; } else if (wifi.readable()) { read = wifi.getc(); + pc.putc(read);//debug if ( read != '\r' && read != '\n') { checking += read; found = checking.find(ACK); @@ -299,3 +342,16 @@ DBG("result: %d\r\n", result) return result; } + +void ESP8266::ATcommand(char* command){ + char* cmd=command; + while(!wifi.writeable() || wifi.readable()){} + while(*cmd){ + wifi.putc((int)*cmd++); + wait(.005); // wait for the echo + while(!wifi.writeable() || wifi.readable()){} + } + wifi.putc(13); //CR + while(!wifi.writeable() || wifi.readable()){} + wifi.putc(10); //LF +}