WiFiClient fails when called very frequently
Posted: Thu Nov 24, 2016 11:21 am
Hi,
In order to test the software and hardware stability, I've reduced the time between calls to a function that uses WiFiClient to make an HTTP GET and read the server response. The function is the following:
This is the call in the main loop:
I'm usually calling this function in the loop function, only once per minute. In order to stress test the system I started calling it once per second, and it worked well (more than 8 hours running without any failure). But if I reduce the time in the loop to 500ms, I started to have failures, the node reports that doesn't have received the answer from the server, and some times it even crashes (with exception 28 and 29). So my doubt is what could be the cause? Should I guarantee some time between connections in order to have some 'hosekeeping' done in the background? I don't need to transmit every 500ms but I want to understand the failure, because in the field this node is blocking after some days working, and the problem could be related to this one - perhaps if two connections are made very close in time could trigger this situation?
Best regards
Fernando
In order to test the software and hardware stability, I've reduced the time between calls to a function that uses WiFiClient to make an HTTP GET and read the server response. The function is the following:
Code: Select all
bool sendGatewayInfo()
{
WiFiClient client;
if (client.connect(myHost, myPort)) {
printLog("Send gateway: connected...");
sprintf(myUrl, "/input/post.json?node=%d&apikey=%s&json={millis:%ul,heap:%d,rssi:%d}", NODEID, apikey, millis(), ESP.getFreeHeap(), WiFi.RSSI());
client.print(String("GET ") + myUrl + " HTTP/1.1\r\n" + "Host: " + myHost + ":" + myPort + "\r\n" + "User-Agent: VLC/2.2.1 LibVLC/2.2.1\r\nConnection: close\r\n\r\n");
if(client.connected()){
unsigned long next = millis() + 5000;
while(!client.available()) {
yield();
if(!client.connected()) {
client.stop();
serverFail++;
printLog("Server failed 2.0\r\n");
return false;
}
if ((long)(millis()-next) > 0) {
Serial.println(">5s waiting for server reply");
Serial.println(client.available());
client.stop();
serverFail++;
printLog("Server failed 2.1\r\n");
return false;
}
}
if(client.connected() && client.available()) {
client.read(buf, client.available());
printLog((char*)buf);
client.stop();
serverFail = 0;
return true;
}
} else {
serverFail++;
printLog("Server failed 2.2\r\n");
}
} else {
serverFail++;
printLog("Server failed 2.3\r\n");
}
client.stop();
return false;
}
This is the call in the main loop:
Code: Select all
if((long)( millis()-lNext) >= 0) {
lNext += 500;
if(!sendGatewayInfo()) {
printLog("Send gateway info failed!");
}
}
I'm usually calling this function in the loop function, only once per minute. In order to stress test the system I started calling it once per second, and it worked well (more than 8 hours running without any failure). But if I reduce the time in the loop to 500ms, I started to have failures, the node reports that doesn't have received the answer from the server, and some times it even crashes (with exception 28 and 29). So my doubt is what could be the cause? Should I guarantee some time between connections in order to have some 'hosekeeping' done in the background? I don't need to transmit every 500ms but I want to understand the failure, because in the field this node is blocking after some days working, and the problem could be related to this one - perhaps if two connections are made very close in time could trigger this situation?
Best regards
Fernando