ESP asynchroneous http GET request - which librairy ?

Hello,
On ESP8266 or ESP32 I wish to make asynchroneous http GET request, but I have difficulties to find which librairy to use and how to use.
(For synchroneous I can with ESP8266HTTPClient, but I want asynch)

For instance,
GitHub - hatlabs/ESPAsyncHTTPClient: Asynchronous HTTP client library for ESP8266 and ESP32
states:

Missing functionality

  • ESP8266 support
  • chunked transfer encoding hasn't been tested
  • multiline headers not supported yet
  • output buffering (sending more than 5744 bytes at a time)
  • anything outside of the unit tests, basically...
  • no HTTPS

or
GitHub - judge2005/ESPAsyncHttpClient: An HTTP client that uses ESPAsyncTCP

or ??

Thanks for sharing a simple code example.

http or https ?

Let's start with http, as in my question

just using a WiFiClient.

  • You connect to the server
  • You make your GET request
  • when you see that the WiFiClient available() is not null it means the answer has arrived.

You'll need to parse the headers and then receive the body.

The headers are separated from the body by an empty line (the last 4 bytes should be "\r\n\r\n")

you need to look into the headers:
➜ if the client instead sends a Content-Length, you must use that to know how many bytes to expect in the body section.
➜ If the client sends a header with Transfer-Encoding: Chunked, you will need to parse the answer using the chunked transfer encoding syntax. When the client is using this approach, you can detect the end of the body by a chunk with a length of 0.

(this parsing code probably exists in some of the libraries).

How you make it asynchroneous?

while (client.connected() || client.available())
{
  if (client.available())
  {
    String line = client.readStringUntil('\n');
    Serial.println(line);
  }
}

is not asynchroneous.
https://arduino-esp8266.readthedocs.io/en/2.6.1/esp8266wifi/client-examples.html

  • don't use while() otherwise you are making it synchronous
  • don't mix client.connected() and client.available()

you let the loop spin and it's very much like what's discussed in Serial Input Basics except that your stream is not coming from Serial but from the client and the buffer is much larger.

if after a request client.connected() is false, it means the client is gone. Just handle that.

if client.available() is not null, then it has accumulated (part of) the answer. In many cases it will hold the full HTTP answer, but to know what's coming you'll need to parse the content.

you have a small state machine:

  • read each line that is in the incoming buffer. check for Content-Length or Transfer-Encoding and make a note of the information they provide to adjust your state machine.
  • wait until you get the empty line (the last 4 bytes you received should be "\r\n\r\n" )
  • use the Content-Length and/or Transfer-Encoding to receive the body

just let the loop loops, deal with whatever comes in the buffer.

Thanks I will later take the time to read,
but I think you overlook the fact that there could be miscalleneous requests and also to several servers.
Example

RESIDENTIAL_IP_1 + "\setOn?relay=1
RESIDENTIAL_IP_2 + "\getTemperaturesAsJson
RESIDENTIAL_IP_1 + "\showStatus
...
http://api.open-meteo.com/v1/forecast?latitude=34&longitude=9&current=temperature_2m
...

each request would be a separate client - so if you solve for one, you can solve for many :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.