1

I am facing problem with ESP32. I use basic example for WiFiClient and making request to my server. The problem is that the response should be 15000 bytes long, but very often I got only 2750 or less.

char buffer[15000];
while (client.available()) {
 char c = client.read();
 buffer[i++] = c;
}
Serial.println(i);
asked Sep 19, 2019 at 19:19

1 Answer 1

1

You are assuming that all 15000 bytes are available all the time.

They won't be.

Instead of "while available read something" you need to "While not enough has been read, then if available read something".

When reading from (assuming) HTTP you first need to read and interpret the headers (the lines before a single blank line). One of these will be the "Content-Length:" header, which will tell you exactly how many bytes are in the body of the response. Then you use that to read the right number of bytes from the body.

So the sequence is:

  1. Read line by line for the headers interpreting the ones you need to, until:
  2. You receive a single blank line
  3. Read the data byte-wise until you have read the number of bytes indicated in the Content-Length header.

If you aren't reading from an HTTP server and you are certain that the data is exactly 15000 bytes in length then you need to skip to point 3 above. Never assume that all the data is there. If there is data then read it, and repeat until you have totalled 15000 bytes.

answered Sep 19, 2019 at 19:38
1
  • Thanks, the problem was probably caused by client.available(), while reading response, it found a gap when client has no data available and so ends the reading. When I havent got enought data, I wait (with timeout) to get some data available. Commented Sep 20, 2019 at 8:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.