Accelerate http client request or make it asynchron?

I have two ESPs that exchnage information - one is the server that is providing a string with sensorvalues and the othe receives that.
Every 10 seconds the client gets the new data from the server and it takes approx 1,5 seconds. Is there a chance to make that faster or make asynchronous? Or any other idea that is faster?

Client:

#include <HTTPClient.h>

//  --------- ---
void getData(char espx[]){   

WiFiClient client;

if(!client.connect(espx, 80)) {
  Serial.println("Connection to Client " + String(espx) + " failed");
  data_esp =("xx|xx");  
  return;
}

Serial.print("Requesting URL: ");
Serial.println (url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + espx + "\r\n" + "Connection: close\r\n\r\n");

unsigned long letzterconnect = millis();
while (!client.available() && ((millis()-letzterconnect)<10000)) {
  delay(1);
  }

while (client.available()) {
  String line = client.readStringUntil('\r');
  if (line != "") data_esp = line;
}

Serial.println("Closing connection to: " + String(espx));
Serial.print("String data_esp: " + data_esp);
Serial.println("");


}  // end void getData

This is the string that is exchanged between the two ESPs: z|40|22.0|37.6|17.0|36.0|37.6|26.5|-52|

and the Server has the following code:

#include <ESPAsyncWebServer.h> 

AsyncWebServer asyncserver(80);

// setup

asyncserver.on("/data", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "text/html", transfer);
  });



// loop:

if (millis() > letztedaten + 5000) { 
  transfer = sendData();
  letztedaten = millis();
}


//  stringerstellung    - some random values for test

String sendData(){            

measuredValue = analogRead(A0)/1024.0 * 3.3;
measuredValue = random(100) / 2.5;
delay(10);
float measuredValue2 = random(100) / 2;
delay(6);
float measuredValue3 = random(100) /1.75;

String transfer2 = "";      
transfer2 += "z|" + String(feuchteEG, 0) + "|" + String(tempEG, 1) + "|" + String(measuredValue, 1) + "|";     
transfer2 +=  String(measuredValue2, 1) + "|" + String(measuredValue3,1) +"|" + String(measuredValue,1) + "|";
transfer2 +=  "26.5|" + String(WiFi.RSSI()) + "|";    

Serial.println(String(transfer2));
return transfer2;

}   

ESP32?
Which model?

You haven't provided full sketches so we can't see what happens on your side.
Allways provide a full sketch.

1.5 sec is for sure to long for a simple HTTP request.

client:
you include the HTTPClient but you are only using the WifiClient.
Follow the example
HTTPClient / BasicHttpClient

and use the instance of HTTPClient

few things you could do here..
first think async on the client..
break this getData into 3 routines..
connect..
send request..
check response..
no waiting, no delays..

might want to use a global client and keep it connected, connecting all by itself eats up time..

or completely rethink things..
instead of querying the server, have the server broadcast the data every 10 seconds..
lose the http,tcp and go udp..
no strings either they will end up fragging your memory..
send the data raw in structures..

UDP sender..
UDP Receiver..

couple of examples for you to look at..

good luck.. ~q

Two ESP, why don't use ESP-NOW protocol?

It's fast, reliable and don't require WiFi Access Point at all (but of course if you need WiFi connectivity it can still be used).

thank you - the full sketch is very long and in more tab in the IDE. Everything is currently running fine, except that topic.

good idea - will try that.
I already put in some Serial print statement to the code and the long waiting time is, when the receiver gets the data with 38 bytes. Need to check that more into detail.

Udp is also a possibility and I wil try it.
What do you think about POST method. lient wil post the data to the server. Server is async, so should be no more timing problem then

make a sketch with just "that topic"

all the relevant code for that topic was posted in my first message of this topic.
In the meantime I found and solved the problem.

In the code of the recieving ESP32 I inserted some Serial.println("...") to find out where exactly is the delay. It was not in the start of the connection nor when exchanging some header information. But it took 1 sec to get the data string from the server.

Then I inserted a CR or /r at the end of the string, that the server is sending out and now the lag when receiving data is around 50-150 ms and that´s OK.

the change in the code on the server side: (on the client side everything stays as it was)

char end = 13;  // ASCII Code for Carriage Return 
String transfer2 = "";      
transfer2 += "z|" + String(feuchteEG, 0) + "|" + String(tempEG, 1) + "|" + String(measuredValue, 1) + "|";     
transfer2 +=  String(measuredValue2, 1) + "|" + String(measuredValue3,1) +"|" + String(measuredValue,1) + "|";
transfer2 +=  "26.5|" + String(WiFi.RSSI()) + "|" + end;      // string transfer has to be terminated with CR

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