TCPClient.available() does not return length

It’s because the client.available() has no data yet, i.e. your request is taking longer than 1 second, but less than 2 seconds. When that happens your “bytes” == 0 so your for() loop gets skipped and your client.stop() is run.

On the surface, better code would be instead of the delay(2000);

// wait 5 seconds or less for the server to respond
uint32_t startTime = millis();
while(!client.available() && (millis() - startTime) < 5000);

It might be possible to put this functionality into the client.available() method… but would you really want it in there?

EDIT: I tried this in your code and it works great!

1 Like