diff --git a/src/modules/ESP8266.cpp b/src/modules/ESP8266.cpp index 54f34b56..0807f580 100644 --- a/src/modules/ESP8266.cpp +++ b/src/modules/ESP8266.cpp @@ -55,10 +55,9 @@ uint8_t ESP8266::join(const char* ssid, const char* password) { return(state); } - // join AP + // build AT command const char* atStr = "AT+CWJAP_CUR=\""; uint8_t cmdLen = strlen(atStr) + strlen(ssid) + strlen(password) + 4; - char* cmd = new char[cmdLen]; strcpy(cmd, atStr); strcat(cmd, ssid); @@ -66,6 +65,7 @@ uint8_t ESP8266::join(const char* ssid, const char* password) { strcat(cmd, password); strcat(cmd, "\""); + // send command bool res = _mod->ATsendCommand(cmd); delete[] cmd; if(!res) { @@ -80,6 +80,48 @@ uint8_t ESP8266::join(const char* ssid, const char* password) { return(ERR_NONE); } +uint8_t ESP8266::openTransportConnection(const char* host, const char* protocol, uint16_t port, uint16_t tcpKeepAlive) { + char portStr[6]; + itoa(port, portStr, 10); + char tcpKeepAliveStr[6]; + itoa(tcpKeepAlive, tcpKeepAliveStr, 10); + + // build AT command + const char* atStr = "AT+CIPSTART=\""; + uint8_t cmdLen = strlen(atStr) + strlen(protocol) + strlen(host) + strlen(portStr) + 5; + if((strcmp(protocol, "TCP") == 0) && (tcpKeepAlive > 0)) { + cmdLen += strlen(tcpKeepAliveStr) + 1; + } + char* cmd = new char[cmdLen]; + strcpy(cmd, atStr); + strcat(cmd, protocol); + strcat(cmd, "\",\""); + strcat(cmd, host); + strcat(cmd, "\","); + strcat(cmd, portStr); + if((strcmp(protocol, "TCP") == 0) && (tcpKeepAlive > 0)) { + strcat(cmd, ","); + strcat(cmd, tcpKeepAliveStr); + } + + // send command + bool res = _mod->ATsendCommand(cmd); + delete[] cmd; + if(!res) { + return(ERR_AT_FAILED); + } + + return(ERR_NONE); +} + +uint8_t ESP8266::closeTransportConnection() { + // send AT command + if(!_mod->ATsendCommand("AT+CIPCLOSE")) { + return(ERR_AT_FAILED); + } + return(ERR_NONE); +} + uint8_t ESP8266::send(const char* data) { // build AT command char lenStr[8]; @@ -89,7 +131,7 @@ uint8_t ESP8266::send(const char* data) { strcpy(cmd, atStr); strcat(cmd, lenStr); - // send data length in bytes + // send command bool res = _mod->ATsendCommand(cmd); delete[] cmd; if(!res) { @@ -113,7 +155,7 @@ uint8_t ESP8266::send(uint8_t* data, uint32_t len) { strcpy(cmd, atStr); strcat(cmd, lenStr); - // send command and data length in bytes + // send command bool res = _mod->ATsendCommand(cmd); delete[] cmd; if(!res) { @@ -131,6 +173,8 @@ uint8_t ESP8266::send(uint8_t* data, uint32_t len) { size_t ESP8266::receive(uint8_t* data, size_t len, uint32_t timeout) { size_t i = 0; uint32_t start = millis(); + + // wait until the required number of bytes is received or until timeout while((millis() - start < timeout) && (i < len)) { while(_mod->ModuleSerial->available() > 0) { uint8_t b = _mod->ModuleSerial->read(); @@ -142,48 +186,6 @@ size_t ESP8266::receive(uint8_t* data, size_t len, uint32_t timeout) { return(i); } -uint8_t ESP8266::openTransportConnection(const char* host, const char* protocol, uint16_t port, uint16_t tcpKeepAlive) { - char portStr[6]; - itoa(port, portStr, 10); - char tcpKeepAliveStr[6]; - itoa(tcpKeepAlive, tcpKeepAliveStr, 10); - - const char* atStr = "AT+CIPSTART=\""; - uint8_t cmdLen = strlen(atStr) + strlen(protocol) + strlen(host) + strlen(portStr) + 5; - - if((strcmp(protocol, "TCP") == 0) && (tcpKeepAlive > 0)) { - cmdLen += strlen(tcpKeepAliveStr) + 1; - } - - char* cmd = new char[cmdLen]; - strcpy(cmd, atStr); - strcat(cmd, protocol); - strcat(cmd, "\",\""); - strcat(cmd, host); - strcat(cmd, "\","); - strcat(cmd, portStr); - - if((strcmp(protocol, "TCP") == 0) && (tcpKeepAlive > 0)) { - strcat(cmd, ","); - strcat(cmd, tcpKeepAliveStr); - } - - bool res = _mod->ATsendCommand(cmd); - delete[] cmd; - if(!res) { - return(ERR_AT_FAILED); - } - - return(ERR_NONE); -} - -uint8_t ESP8266::closeTransportConnection() { - if(!_mod->ATsendCommand("AT+CIPCLOSE")) { - return(ERR_AT_FAILED); - } - return(ERR_NONE); -} - uint16_t ESP8266::getNumBytes(uint32_t timeout, size_t minBytes) { // wait for available data uint32_t start = millis(); diff --git a/src/modules/ESP8266.h b/src/modules/ESP8266.h index 473406f6..c26af300 100644 --- a/src/modules/ESP8266.h +++ b/src/modules/ESP8266.h @@ -7,14 +7,15 @@ class ESP8266: public TransportLayer { public: + // constructor ESP8266(Module* module); - // Basic methods + // basic methods uint8_t begin(long speed); uint8_t reset(); uint8_t join(const char* ssid, const char* password); - // Transport layer methods + // transport layer methods (implementations of purely virtual methods in TransportMethod class) uint8_t openTransportConnection(const char* host, const char* protocol, uint16_t port, uint16_t tcpKeepAlive = 0); uint8_t closeTransportConnection(); uint8_t send(const char* data); diff --git a/src/protocols/TransportLayer.h b/src/protocols/TransportLayer.h index 70e5ae71..4020c455 100644 --- a/src/protocols/TransportLayer.h +++ b/src/protocols/TransportLayer.h @@ -5,6 +5,10 @@ class TransportLayer { public: + // constructor + // this class is purely virtual and does not require explicit constructor + + // basic methods virtual uint8_t openTransportConnection(const char* host, const char* protocol, uint16_t port, uint16_t tcpKeepAlive = 0) = 0; virtual uint8_t closeTransportConnection() = 0; virtual uint8_t send(const char* data) = 0;