pengfei wu
/
ESP8266Program
For test
Diff: ESP8266.cpp
- Revision:
- 10:9d4ec0359a5c
- Child:
- 11:7bd1b2a67b1a
diff -r 95a0ce98e12b -r 9d4ec0359a5c ESP8266.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266.cpp Mon Feb 02 09:46:16 2015 +0000 @@ -0,0 +1,1038 @@ +#include "ESP8266.h" + +#define ESP8266_DEBUG + +#ifdef ESP8266_DEBUG +#define DBG(message) printf(message) +#else +#define DBG(message) do{}while(0) +#endif + + +ESP8266::ESP8266(PinName tx, PinName rx):esp_uart(tx, rx) { + +} + +int ESP8266::get_chlID(void) { + return chlID; +} + +void ESP8266::begin(void) +{ + bool result = false; + esp_uart.begin(9600); //The default baud rate of ESP8266 is 115200 + + esp_uart.flush(); + esp_uart.setTimeout(5000); + esp_uart.println("AT+RST"); + DBG("AT+RST\r\n"); + result = esp_uart.find("eady"); + if(result) + DBG("Module is ready\r\n"); + else + { + DBG("Module have no response\r\n"); + while(1); + } + +} + + +/************************************************************************* +//Initialize port + + mode: setting operation mode + + chl: channel number + ecn: encryption + + return: + true - successfully + false - unsuccessfully + +***************************************************************************/ +bool ESP8266::Initialize(uint8_t mode, String ssid, String pwd, uint8_t chl, uint8_t ecn) +{ + if (mode == ESP8266_MODE_STA) + { + bool b = confMode(mode); + if (!b) + { + return false; + } + Reset(); + confJAP(ssid, pwd); + } + else if (mode == ESP8266_MODE_SAP) + { + bool b = confMode(mode); + if (!b) + { + return false; + } + Reset(); + confSAP(ssid, pwd, chl, ecn); + } + else if (mode == ESP8266_MODE_STA_SAP) + { + bool b = confMode(mode); + if (!b) + { + return false; + } + Reset(); + confJAP(ssid, pwd); + confSAP(ssid, pwd, chl, ecn); + } + + return true; +} + +/************************************************************************* +//Set up tcp or udp connection + + type: tcp or udp + + addr: ip address + + port: port number + + a: set multiple connection + 0 for sigle connection + 1 for multiple connection + + id: id number(0-4) + + return: + true - successfully + false - unsuccessfully + +***************************************************************************/ +bool ESP8266::ipConfig(uint8_t type, String addr, int port, uint8_t a, uint8_t id) +{ + bool result = false; + if (a == 0 ) + { + confMux(a); + + long timeStart = millis(); + while (1) + { + long time0 = millis(); + if (time0 - timeStart > 5000) + { + break; + } + } + result = newMux(type, addr, port); + } + else if (a == 1) + { + confMux(a); + long timeStart = millis(); + while (1) + { + long time0 = millis(); + if (time0 - timeStart > 5000) + { + break; + } + } + result = newMux(id, type, addr, port); + } + return result; +} + +/************************************************************************* +//receive message from wifi + + buf: buffer for receiving data + + chlID: <id>(0-4) + + return: size of the buffer + + +***************************************************************************/ +int ESP8266::ReceiveMessage(char *buf) +{ + //+IPD,<len>:<data> + //+IPD,<id>,<len>:<data> + String data = ""; + if (esp_uart.available()>0) + { + + unsigned long start; + start = millis(); + char c0 = esp_uart.read_char(); + if (c0 == '+') + { + + while (millis()-start<5000) + { + if (esp_uart.available()>0) + { + char c = esp_uart.read_char(); + data += c; + } + if (data.indexOf("\nOK")!=-1) + { + break; + } + } + //Serial.println(data); + int sLen = strlen(data.c_str()); + int i,j; + for (i = 0; i <= sLen; i++) + { + if (data[i] == ':') + { + break; + } + + } + bool found = false; + for (j = 4; j <= i; j++) + { + if (data[j] == ',') + { + found = true; + break; + } + + } + int iSize; + //DBG(data); + //DBG("\r\n"); + if(found ==true) + { + String _id = data.substring(4, j); + chlID = _id.toInt(); + String _size = data.substring(j+1, i); + iSize = _size.toInt(); + //DBG(_size); + String str = data.substring(i+1, i+1+iSize); + strcpy(buf, str.c_str()); + //DBG(str); + + } + else + { + String _size = data.substring(4, i); + iSize = _size.toInt(); + //DBG(iSize); + //DBG("\r\n"); + String str = data.substring(i+1, i+1+iSize); + strcpy(buf, str.c_str()); + //DBG(str); + } + return iSize; + } + } + + return 0; +} + +////////////////////////////////////////////////////////////////////////// + + +/************************************************************************* +//reboot the wifi module + + + +***************************************************************************/ +void ESP8266::Reset(void) +{ + esp_uart.println("AT+RST"); + unsigned long start; + start = millis(); + while (millis()-start<5000) { + if(esp_uart.find("ready")==true) + { + DBG("reboot wifi is OK\r\n"); + break; + } + } +} + +/********************************************* + ********************************************* + ********************************************* + ESP8266 Function Commands + ********************************************* + ********************************************* + ********************************************* + */ + +/************************************************************************* +//inquire the current mode of wifi module + + return: string of current mode + Station + AP + AP+Station + +***************************************************************************/ +String ESP8266::showMode() +{ + String data; + esp_uart.println("AT+CWMODE?"); + unsigned long start; + start = millis(); + while (millis()-start<2000) { + if(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("OK")!=-1) + { + break; + } + } + if(data.indexOf("1")!=-1) + { + return "Station"; + }else if(data.indexOf("2")!=-1) + { + return "AP"; + }else if(data.indexOf("3")!=-1) + { + return "AP+Station"; + }else{ + return "Invalid Mode!"; + } +} + + + +/************************************************************************* +//configure the operation mode + + a: + 1 - Station + 2 - AP + 3 - AP+Station + + return: + true - successfully + false - unsuccessfully + +***************************************************************************/ + +bool ESP8266::confMode(uint8_t a) +{ + String data; + esp_uart.print("AT+CWMODE="); + esp_uart.println(String(a)); + unsigned long start; + start = millis(); + while (millis()-start<2000) { + if(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("OK")!=-1 || data.indexOf("no change")!=-1) + { + return true; + } + if (data.indexOf("ERROR")!=-1 || data.indexOf("busy")!=-1) + { + return false; + } + + } + return false; +} + + +/************************************************************************* +//show the list of wifi hotspot + + return: string of wifi information + encryption,SSID,RSSI + + +***************************************************************************/ + +String ESP8266::showAP(void) +{ + String data; + esp_uart.flush(); + esp_uart.print("AT+CWLAP\r\n"); + delay(5000); + + unsigned long start; + start = millis(); + while (millis()-start<8000) { + if(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 ) + { + break; + } + } + if(data.indexOf("ERROR")!=-1) + { + return "ERROR"; + } + else{ + char head[4] = {0x0D,0x0A}; + char tail[7] = {0x0D,0x0A,0x0D,0x0A}; + data.replace("AT+CWLAP",""); + data.replace("OK",""); + data.replace("+CWLAP","ESP8266"); + data.replace(tail,""); + data.replace(head,""); + + return data; + } + } + + +/************************************************************************* +//show the name of current wifi access port + + return: string of access port name + AP:<SSID> + + +***************************************************************************/ +String ESP8266::showJAP(void) +{ + esp_uart.flush(); + esp_uart.println("AT+CWJAP?"); + String data; + unsigned long start; + start = millis(); + while (millis()-start<3000) { + if(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 ) + { + break; + } + } + //DBG(("data = [%s]\r\n", data.c_str())); + char head[4] = {0x0D,0x0A}; + char tail[7] = {0x0D,0x0A,0x0D,0x0A}; + data.replace("AT+CWJAP?",""); + data.replace("+CWJAP","AP"); + data.replace("OK",""); + data.replace(tail,""); + data.replace(head,""); + + return data; +} + + +/************************************************************************* +//configure the SSID and password of the access port + + return: + true - successfully + false - unsuccessfully + + +***************************************************************************/ +bool ESP8266::confJAP(String ssid , String pwd) +{ + + esp_uart.print("AT+CWJAP="); + esp_uart.print("\""); //"ssid" + esp_uart.print(ssid); + esp_uart.print("\""); + + esp_uart.print(","); + + esp_uart.print("\""); //"pwd" + esp_uart.print(pwd); + esp_uart.println("\""); + + + unsigned long start; + start = millis(); + while (millis()-start<3000) { + if(esp_uart.find("OK")==true) + { + return true; + + } + } + return false; +} +/************************************************************************* +//quite the access port + + return: + true - successfully + false - unsuccessfully + + +***************************************************************************/ + +bool ESP8266::quitAP(void) +{ + esp_uart.println("AT+CWQAP"); + unsigned long start; + start = millis(); + while (millis()-start<3000) { + if(esp_uart.find("OK")==true) + { + return true; + + } + } + return false; + +} + +/************************************************************************* +//show the parameter of ssid, password, channel, encryption in AP mode + + return: + mySAP:<SSID>,<password>,<channel>,<encryption> + +***************************************************************************/ +String ESP8266::showSAP() +{ + esp_uart.println("AT+CWSAP?"); + String data; + unsigned long start; + start = millis(); + while (millis()-start<3000) { + if(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 ) + { + break; + } + } + char head[4] = {0x0D,0x0A}; + char tail[7] = {0x0D,0x0A,0x0D,0x0A}; + data.replace("AT+CWSAP?",""); + data.replace("+CWSAP","mySAP"); + data.replace("OK",""); + data.replace(tail,""); + data.replace(head,""); + + return data; +} + +/************************************************************************* +//configure the parameter of ssid, password, channel, encryption in AP mode + + return: + true - successfully + false - unsuccessfully + +***************************************************************************/ + +bool ESP8266::confSAP(String ssid , String pwd , uint8_t chl , uint8_t ecn) +{ + esp_uart.print("AT+CWSAP="); + esp_uart.print("\""); //"ssid" + esp_uart.print(ssid); + esp_uart.print("\""); + + esp_uart.print(","); + + esp_uart.print("\""); //"pwd" + esp_uart.print(pwd); + esp_uart.print("\""); + + esp_uart.print(","); + esp_uart.print(String(chl)); + + esp_uart.print(","); + esp_uart.println(String(ecn)); + unsigned long start; + start = millis(); + while (millis()-start<3000) { + if(esp_uart.find("OK")==true ) + { + return true; + } + } + + return false; + +} + + +/********************************************* + ********************************************* + ********************************************* + TPC/IP Function Command + ********************************************* + ********************************************* + ********************************************* + */ + +/************************************************************************* +//inquire the connection status + + return: string of connection status + <ID> 0-4 + <type> tcp or udp + <addr> ip + <port> port number + +***************************************************************************/ + +String ESP8266::showStatus(void) +{ + esp_uart.println("AT+CIPSTATUS"); + String data; + unsigned long start; + start = millis(); + while (millis()-start<3000) { + if(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("OK")!=-1) + { + break; + } + } + + char head[4] = {0x0D,0x0A}; + char tail[7] = {0x0D,0x0A,0x0D,0x0A}; + data.replace("AT+CIPSTATUS",""); + data.replace("OK",""); + data.replace(tail,""); + data.replace(head,""); + + return data; +} + +/************************************************************************* +//show the current connection mode(sigle or multiple) + + return: string of connection mode + 0 - sigle + 1 - multiple + +***************************************************************************/ +String ESP8266::showMux(void) +{ + String data; + esp_uart.println("AT+CIPMUX?"); + + unsigned long start; + start = millis(); + while (millis()-start<3000) { + if(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("OK")!=-1) + { + break; + } + } + char head[4] = {0x0D,0x0A}; + char tail[7] = {0x0D,0x0A,0x0D,0x0A}; + data.replace("AT+CIPMUX?",""); + data.replace("+CIPMUX","showMux"); + data.replace("OK",""); + data.replace(tail,""); + data.replace(head,""); + + return data; +} + +/************************************************************************* +//configure the current connection mode(sigle or multiple) + + a: connection mode + 0 - sigle + 1 - multiple + + return: + true - successfully + false - unsuccessfully +***************************************************************************/ +bool ESP8266::confMux(int a) +{ + esp_uart.print("AT+CIPMUX="); + esp_uart.println(a); + unsigned long start; + start = millis(); + while (millis()-start<3000) { + if(esp_uart.find("OK")==true ) + { + return true; + } + } + + return false; +} + + +/************************************************************************* +//Set up tcp or udp connection (signle connection mode) + + type: tcp or udp + + addr: ip address + + port: port number + + + return: + true - successfully + false - unsuccessfully + +***************************************************************************/ +bool ESP8266::newMux(uint8_t type, String addr, int port) + +{ + String data; + esp_uart.flush(); + esp_uart.print("AT+CIPSTART="); + if(type>0) + { + esp_uart.print("\"TCP\""); + }else + { + esp_uart.print("\"UDP\""); + } + esp_uart.print(","); + esp_uart.print("\""); + esp_uart.print(addr); + esp_uart.print("\""); + esp_uart.print(","); +// esp_uart.print("\""); + esp_uart.println(String(port)); +// esp_uart.println("\""); + unsigned long start; + start = millis(); + while (millis()-start<10000) { + if(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 || data.indexOf("ERROR")!=-1) + { + return true; + } + } + return false; +} +/************************************************************************* +//Set up tcp or udp connection (multiple connection mode) + + type: tcp or udp + + addr: ip address + + port: port number + + id: id number(0-4) + + return: + true - successfully + false - unsuccessfully + +***************************************************************************/ +bool ESP8266::newMux( uint8_t id, uint8_t type, String addr, int port) + +{ + + esp_uart.print("AT+CIPSTART="); + esp_uart.print("\""); + esp_uart.print(String(id)); + esp_uart.print("\""); + if(type>0) + { + esp_uart.print("\"TCP\""); + } + else + { + esp_uart.print("\"UDP\""); + } + esp_uart.print(","); + esp_uart.print("\""); + esp_uart.print(addr); + esp_uart.print("\""); + esp_uart.print(","); +// esp_uart.print("\""); + esp_uart.println(String(port)); +// esp_uart.println("\""); + String data; + unsigned long start; + start = millis(); + while (millis()-start<3000) { + if(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 ) + { + return true; + } + } + return false; + + +} +/************************************************************************* +//send data in sigle connection mode + + str: string of message + + return: + true - successfully + false - unsuccessfully + +***************************************************************************/ +bool ESP8266::Send(String str) +{ + esp_uart.flush(); + esp_uart.print("AT+CIPSEND="); +// esp_uart.print("\""); + esp_uart.println(str.length()); +// esp_uart.println("\""); + unsigned long start; + start = millis(); + bool found = false; + while (millis()-start<5000) { + if(esp_uart.find(">")==true ) + { + found = true; + break; + } + } + if(found) + esp_uart.print(str); + else + { + closeMux(); + return false; + } + + + String data; + start = millis(); + while (millis()-start<5000) { + if(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("SEND OK")!=-1) + { + return true; + } + } + return false; +} + +/************************************************************************* +//send data in multiple connection mode + + id: <id>(0-4) + + str: string of message + + return: + true - successfully + false - unsuccessfully + +***************************************************************************/ +bool ESP8266::Send(uint8_t id, String str) +{ + esp_uart.print("AT+CIPSEND="); + + esp_uart.print(String(id)); + esp_uart.print(","); + esp_uart.println(str.length()); + unsigned long start; + start = millis(); + bool found = false; + while (millis()-start<5000) { + if(esp_uart.find(">")==true ) + { + found = true; + break; + } + } + if(found) + esp_uart.print(str); + else + { + closeMux(id); + return false; + } + + + String data; + start = millis(); + while (millis()-start<5000) { + if(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("SEND OK")!=-1) + { + return true; + } + } + return false; +} + +/************************************************************************* +//Close up tcp or udp connection (sigle connection mode) + + +***************************************************************************/ +void ESP8266::closeMux(void) +{ + esp_uart.println("AT+CIPCLOSE"); + + String data; + unsigned long start; + start = millis(); + while (millis()-start<3000) { + if(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("Linked")!=-1 || data.indexOf("ERROR")!=-1 || data.indexOf("we must restart")!=-1) + { + break; + } + } +} + + +/************************************************************************* +//Set up tcp or udp connection (multiple connection mode) + + id: id number(0-4) + +***************************************************************************/ +void ESP8266::closeMux(uint8_t id) +{ + esp_uart.print("AT+CIPCLOSE="); + esp_uart.println(String(id)); + String data; + unsigned long start; + start = millis(); + while (millis()-start<3000) { + if(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("OK")!=-1 || data.indexOf("Link is not")!=-1 || data.indexOf("Cant close")!=-1) + { + break; + } + } + +} + +/************************************************************************* +//show the current ip address + + return: string of ip address + +***************************************************************************/ +String ESP8266::showIP(void) +{ + String data; + unsigned long start; + //DBG("AT+CIFSR\r\n"); + for(int a=0; a<3;a++) + { + esp_uart.println("AT+CIFSR"); + start = millis(); + while (millis()-start<3000) { + while(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("AT+CIFSR")!=-1) + { + break; + } + } + if(data.indexOf(".") != -1) + { + break; + } + data = ""; + } + //DBG(data); + //DBG("\r\n"); + char head[4] = {0x0D,0x0A}; + char tail[7] = {0x0D,0x0D,0x0A}; + data.replace("AT+CIFSR",""); + data.replace(tail,""); + data.replace(head,""); + + return data; +} + +/************************************************************************* +////set the parameter of server + + mode: + 0 - close server mode + 1 - open server mode + + port: <port> + + return: + true - successfully + false - unsuccessfully + +***************************************************************************/ + +bool ESP8266::confServer(uint8_t mode, int port) +{ + esp_uart.print("AT+CIPSERVER="); + esp_uart.print(String(mode)); + esp_uart.print(","); + esp_uart.println(String(port)); + + String data; + unsigned long start; + start = millis(); + bool found = false; + while (millis()-start<3000) { + if(esp_uart.available()>0) + { + char a =esp_uart.read_char(); + data=data+a; + } + if (data.indexOf("OK")!=-1 || data.indexOf("no charge")!=-1) + { + found = true; + break; + } + } + esp_uart.flush(); + return found; +}