diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/examples/WebClient/WebClient.ino b/examples/WebClient/WebClient.ino index 7e8c802..07be74f 100644 --- a/examples/WebClient/WebClient.ino +++ b/examples/WebClient/WebClient.ino @@ -19,7 +19,7 @@ char ssid[] = "Twim"; // your network SSID (name) char pass[] = "12345678"; // your network password int status = WL_IDLE_STATUS; // the Wifi radio's status -char server[] = "arduino.cc"; +char server[] = "arduino.tips"; // Initialize the Ethernet client object WiFiEspClient client; @@ -60,7 +60,7 @@ void setup() Serial.println("Connected to server"); // Make a HTTP request client.println("GET /asciilogo.txt HTTP/1.1"); - client.println("Host: arduino.cc"); + client.println("Host: "+String(server)); client.println("Connection: close"); client.println(); } diff --git a/src/WiFiEspClient.cpp b/src/WiFiEspClient.cpp index 5a242a1..49849fc 100644 --- a/src/WiFiEspClient.cpp +++ b/src/WiFiEspClient.cpp @@ -26,13 +26,9 @@ along with The Arduino WiFiEsp library. If not, see #include "utility/debug.h" -WiFiEspClient::WiFiEspClient() : _sock(255) -{ -} +WiFiEspClient::WiFiEspClient() : _sock(255){STMillis=0;} -WiFiEspClient::WiFiEspClient(uint8_t sock) : _sock(sock) -{ -} +WiFiEspClient::WiFiEspClient(uint8_t sock) : _sock(sock){STMillis=0;} //////////////////////////////////////////////////////////////////////////////// @@ -137,15 +133,13 @@ size_t WiFiEspClient::write(const uint8_t *buf, size_t size) int WiFiEspClient::available() { - if (_sock != 255) - { + STMillis=millis(); + while(millis()-STMillis < getTimeout()){ + if (_sock != 255){ int bytes = EspDrv::availData(_sock); - if (bytes>0) - { - return bytes; - } - } - + if(bytes>0){return bytes;} + } + } return 0; } diff --git a/src/WiFiEspClient.h b/src/WiFiEspClient.h index 1e3823c..61db8c8 100644 --- a/src/WiFiEspClient.h +++ b/src/WiFiEspClient.h @@ -29,6 +29,7 @@ along with The Arduino WiFiEsp library. If not, see class WiFiEspClient : public Client { +protected: unsigned long STMillis; //variable used to do the subtraction and see how much time has passed, for the timeout public: WiFiEspClient(); WiFiEspClient(uint8_t sock); diff --git a/src/utility/EspDrv.cpp b/src/utility/EspDrv.cpp index cb577fe..9ded72b 100644 --- a/src/utility/EspDrv.cpp +++ b/src/utility/EspDrv.cpp @@ -75,14 +75,14 @@ void EspDrv::wifiDriverInit(Stream *espSerial) bool initOK = false; - for(int i=0; i<5; i++) + for(int i=0; i<10; i++) { if (sendCmd(F("AT")) == TAG_OK) { initOK=true; break; } - delay(1000); + delay(500); } if (!initOK) @@ -97,17 +97,15 @@ void EspDrv::wifiDriverInit(Stream *espSerial) // check firmware version getFwVersion(); - // prints a warning message if the firmware is not 1.X or 2.X - if ((fwVersion[0] != '1' and fwVersion[0] != '2') or - fwVersion[1] != '.') - { + // prints a warning message if the firmware is not 1.X or 2.X or 3.X + if ((fwVersion[0] == '1' or fwVersion[0] == '2' or fwVersion[0] == '3')){ + LOGINFO1(F("Initilization successful -"), fwVersion); + } + else{ LOGWARN1(F("Warning: Unsupported firmware"), fwVersion); delay(4000); } - else - { - LOGINFO1(F("Initilization successful -"), fwVersion); - } + } @@ -232,7 +230,7 @@ void EspDrv::config(IPAddress ip) char buf[16]; sprintf_P(buf, PSTR("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]); - int ret = sendCmd(F("AT+CIPSTA_CUR=\"%s\""), 2000, buf); + int ret = sendCmd(F("AT+CIPSTA_CUR=\"%s\""), 5000, buf); delay(500); if (ret==TAG_OK) @@ -256,7 +254,7 @@ void EspDrv::configAP(IPAddress ip) char buf[16]; sprintf_P(buf, PSTR("%d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]); - int ret = sendCmd(F("AT+CIPAP_CUR=\"%s\""), 2000, buf); + int ret = sendCmd(F("AT+CIPAP_CUR=\"%s\""), 5000, buf); delay(500); if (ret==TAG_OK) @@ -468,16 +466,16 @@ uint8_t EspDrv::getScanNetworks() espSerial->println(F("AT+CWLAP")); - idx = readUntil(10000, "+CWLAP:("); + idx = readUntil(30000, "+CWLAP:("); while (idx == NUMESPTAGS) { _networkEncr[ssidListNum] = espSerial->parseInt(); // discard , and " characters - readUntil(1000, "\""); + readUntil(5000, "\""); - idx = readUntil(1000, "\"", false); + idx = readUntil(5000, "\"", false); if(idx==NUMESPTAGS) { memset(_networkSsid[ssidListNum], 0, WL_SSID_MAX_LENGTH ); @@ -485,11 +483,11 @@ uint8_t EspDrv::getScanNetworks() } // discard , character - readUntil(1000, ","); + readUntil(5000, ","); _networkRssi[ssidListNum] = espSerial->parseInt(); - idx = readUntil(1000, "+CWLAP:("); + idx = readUntil(5000, "+CWLAP:("); if(ssidListNum==WL_NETWORKS_LIST_MAXNUM-1) break; @@ -588,7 +586,7 @@ bool EspDrv::startServer(uint16_t port, uint8_t sock) { LOGDEBUG1(F("> startServer"), port); - int ret = sendCmd(F("AT+CIPSERVER=%d,%d"), 1000, sock, port); + int ret = sendCmd(F("AT+CIPSERVER=%d,%d"), 5000, sock, port); return ret==TAG_OK; } @@ -629,7 +627,7 @@ void EspDrv::stopClient(uint8_t sock) { LOGDEBUG1(F("> stopClient"), sock); - sendCmd(F("AT+CIPCLOSE=%d"), 4000, sock); + sendCmd(F("AT+CIPCLOSE=%d"), 5000, sock); } @@ -661,7 +659,7 @@ uint16_t EspDrv::availData(uint8_t connId) int bytes = espSerial->available(); - + if (bytes) { //LOGDEBUG1(F("Bytes in the serial buffer: "), bytes); @@ -737,7 +735,7 @@ bool EspDrv::getData(uint8_t connId, uint8_t *data, bool peek, bool* connClose) // 48 = '0' if (espSerial->peek()==48+connId) { - int idx = readUntil(500, ",CLOSED\r\n", false); + int idx = readUntil(5000, ",CLOSED\r\n", false); if(idx!=NUMESPTAGS) { LOGERROR(F("Tag CLOSED not found")); @@ -801,7 +799,7 @@ bool EspDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) sprintf_P(cmdBuf, PSTR("AT+CIPSEND=%d,%u"), sock, len); espSerial->println(cmdBuf); - int idx = readUntil(1000, (char *)">", false); + int idx = readUntil(5000, (char *)">", false); if(idx!=NUMESPTAGS) { LOGERROR(F("Data packet send error (1)")); @@ -810,7 +808,7 @@ bool EspDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) espSerial->write(data, len); - idx = readUntil(2000); + idx = readUntil(5000); if(idx!=TAG_SENDOK) { LOGERROR(F("Data packet send error (2)")); @@ -830,7 +828,7 @@ bool EspDrv::sendData(uint8_t sock, const __FlashStringHelper *data, uint16_t le sprintf_P(cmdBuf, PSTR("AT+CIPSEND=%d,%u"), sock, len2); espSerial->println(cmdBuf); - int idx = readUntil(1000, (char *)">", false); + int idx = readUntil(5000, (char *)">", false); if(idx!=NUMESPTAGS) { LOGERROR(F("Data packet send error (1)")); @@ -850,7 +848,7 @@ bool EspDrv::sendData(uint8_t sock, const __FlashStringHelper *data, uint16_t le espSerial->write('\n'); } - idx = readUntil(2000); + idx = readUntil(5000); if(idx!=TAG_SENDOK) { LOGERROR(F("Data packet send error (2)")); @@ -870,7 +868,7 @@ bool EspDrv::sendDataUdp(uint8_t sock, const char* host, uint16_t port, const ui //LOGDEBUG1(F("> sendDataUdp:"), cmdBuf); espSerial->println(cmdBuf); - int idx = readUntil(1000, (char *)">", false); + int idx = readUntil(5000, (char *)">", false); if(idx!=NUMESPTAGS) { LOGERROR(F("Data packet send error (1)")); @@ -879,7 +877,7 @@ bool EspDrv::sendDataUdp(uint8_t sock, const char* host, uint16_t port, const ui espSerial->write(data, len); - idx = readUntil(2000); + idx = readUntil(5000); if(idx!=TAG_SENDOK) { LOGERROR(F("Data packet send error (2)")); @@ -929,7 +927,7 @@ bool EspDrv::sendCmdGet(const __FlashStringHelper* cmd, const char* startTag, co espSerial->println(cmd); // read result until the startTag is found - idx = readUntil(1000, startTag); + idx = readUntil(5000, startTag); if(idx==NUMESPTAGS) { @@ -937,7 +935,7 @@ bool EspDrv::sendCmdGet(const __FlashStringHelper* cmd, const char* startTag, co ringBuf.init(); // start tag found, search the endTag - idx = readUntil(500, endTag); + idx = readUntil(5000, endTag); if(idx==NUMESPTAGS) { @@ -946,7 +944,7 @@ bool EspDrv::sendCmdGet(const __FlashStringHelper* cmd, const char* startTag, co ringBuf.getStrN(outStr, strlen(endTag), outStrLen-1); // read the remaining part of the response - readUntil(2000); + readUntil(5000); ret = true; } @@ -1052,6 +1050,7 @@ int EspDrv::readUntil(unsigned int timeout, const char* tag, bool findTags) { if(espSerial->available()) { + c = (char)espSerial->read(); LOGDEBUG0(c); ringBuf.push(c); @@ -1077,6 +1076,7 @@ int EspDrv::readUntil(unsigned int timeout, const char* tag, bool findTags) } } } + if (millis() - start >= timeout) { @@ -1109,7 +1109,7 @@ void EspDrv::espEmptyBuf(bool warn) // copied from Serial::timedRead int EspDrv::timedRead() { - unsigned int _timeout = 1000; + unsigned int _timeout = 5000; int c; long _startMillis = millis(); do diff --git a/src/utility/debug.h b/src/utility/debug.h index 37c266f..cae5ad0 100644 --- a/src/utility/debug.h +++ b/src/utility/debug.h @@ -29,7 +29,7 @@ along with The Arduino WiFiEsp library. If not, see // 4: DEBUG: errors, warnings, informational and debug #ifndef _ESPLOGLEVEL_ -#define _ESPLOGLEVEL_ 3 +#define _ESPLOGLEVEL_ 4 #endif