HTTP Client Server question

geoffw123
Posts: 4
Joined: Mon Nov 30, 2020 1:32 pm

HTTP Client Server question

Postby geoffw123 » Wed Dec 02, 2020 11:49 pm

I am running esp32 wifi HTTP client to connect to a PC Server

It is basically working but the code I have got now is pretty crummy and does occasionally not get the full HTTP message containing a big JSON string.

The examples I found online just assume the Server replies with a short response packet so it simplifies things. My real world larger response has to loop waiting for more packets to come in. Pseudo code is below
  1.  
  2.   while ((size = client.available()) > 0)
  3.     {
  4.       size = client.read(&msgBig[bufPtr], size);
  5.       bufPtr += size;
  6.  
  7.       int retries = 0;
  8.       while (client.available() == 0 && retries++ < 20)
  9.       {
  10.         delay(2);
  11.       }
  12.     }
Clearly, this is not good for several reasons, firstly 20 * 2 mS = 40 mS delay can sometimes time out in mid packets and therefore lose the end of the msg. Also even if we get all packets, the last packet timeout will be a wasted 40 mS before we decide there is no more to come.

There must be a better way of doing this, can someone with a better knowledge of HTTP protocol and ESP32 libraries comment please ?

Thanks Geoff

ESP_Jan
Posts: 42
Joined: Tue Dec 01, 2020 10:56 am

Re: HTTP Client Server question

Postby ESP_Jan » Tue Dec 08, 2020 2:09 pm

Hi Geoff,
please take a look at this http client example: GitHub link

This example dynamically allocates the buffer so the size of the response is not that important. It also uses an event driven architecture so no delay() commands are required.

Who is online

Users browsing this forum: MicroController and 116 guests