Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the timeout didn't work and some changes not updated on github #228

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
4 changes: 2 additions & 2 deletions examples/WebClient/WebClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
22 changes: 8 additions & 14 deletions src/WiFiEspClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;}


////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/WiFiEspClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
62 changes: 31 additions & 31 deletions src/utility/EspDrv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}

}


Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -468,28 +466,28 @@ 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 );
ringBuf.getStrN(_networkSsid[ssidListNum], 1, WL_SSID_MAX_LENGTH-1);
}

// 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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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)"));
Expand All @@ -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)"));
Expand All @@ -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)"));
Expand All @@ -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)"));
Expand All @@ -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)"));
Expand All @@ -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)"));
Expand Down Expand Up @@ -929,15 +927,15 @@ 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)
{
// clean the buffer to get a clean string
ringBuf.init();

// start tag found, search the endTag
idx = readUntil(500, endTag);
idx = readUntil(5000, endTag);

if(idx==NUMESPTAGS)
{
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -1077,6 +1076,7 @@ int EspDrv::readUntil(unsigned int timeout, const char* tag, bool findTags)
}
}
}


if (millis() - start >= timeout)
{
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/utility/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down