Client.available() sometimes returns false when downloading a file

I am doing a HTTP GET to read a file. The file is 500 KB long. I am reading byte by byte in a loop and it printing on the screen. Sometimes client.available() returns false during download. Sometimes the download is successful. Code looks like:

while(len > 0){ //len is the size of the file, say 500000 bytes
if(client.available() > 0){
char c =;
Serial.println(“should not be here”);

Any ideas to fix this?


It is possible to read faster than it is transmitted hence seeing a zero. One improvement is to add a read timeout. I.E.: keep reading although a zero is returned until the timeout, then exit the loop.

Another improvement would be to know the exact size of the download so you don’t have to always use the timeout. You can exit when the entire file is read.

You can also utilize the connected() function if the connection dies and there are no bytes to read.

Thanks for replying. Adding a timeout helped. :slight_smile: