Hello all,
I'm wondering anyone else has seen strange behavior from the client.available() function when using the Wifi library? According to the documentation, it is supposed to return the number of bytes to be read in the buffer, much like Serial.available(). However, in practice, it seems to behaving more like a boolean - 0 if there is no client connection, 1 if there is (and therefore available in a different sense of the term?). In testing, I have never seen it return any value higher than 1, even when I don't immediately read incoming data, which should cause it to back up in the buffer.
In a more specific case, I am wondering if this strange behavior is causing one of my programs to lock up. I check client.available() and, if it is true, I proceed to peek() and/or read() the data as necessary. However, I'm thinking that available() is returning a 1 (since the connection is still there) when it should (according to the documentation) be returning a 0 since there is no more data to be read. Would calling peek/read cause the program to lock if no data was available in the buffer?
Below is the snippet of code that is locking:
Serial.println("Begin response:");
startTime = millis();
errorFlag = false;
complete = false;
while (!complete && !errorFlag) {
Serial.println(millis());
Serial.println(client.available());
if (client.available()) {
Serial.println(char(client.peek()));
complete = client.read() == '#'; //wait to recieve the opening symbol
}
else {
delay(300);
}
if (WiFi.status() != WL_CONNECTED) { //bail out if wifi connection is lost
Serial.println("Lost connection");
errorFlag = true;
state = 2;
count++;
}
if (millis() > startTime + 30000UL) { //bail out if no response within 30 seconds
Serial.println("Response timeout");
errorFlag = true;
state = 2;
count++;
}
}
And the log file showing where it locked:
Begin response:
213518584
0
213518889
0
213519191
0
213519493
0
213519795
0
213520096
1
H
213520098
1
T
213520100
1
T
213520103
1
P
213520105
1
/
213520107
1
1
213520109
1
.
213520111
1
1
213520113
1
.
.
.
.
.
.
.
213520581
1
/
213520583
1
/
213520585
1
D
213520587
1
T
213520590
1
Since the code hung between printing the results of client.available() and client.read(), I'm assuming that this is the issue. If you see anything else, please share.
Thanks,
Scott