ESP32 post to web server slower than expected

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "mySSID";
const char* password = "myPASSword";

//Your Domain name with URL path or IP address with path
String serverName = "https://post.mywebserver.repl.co";

String httpRequestData = "{\"yes\": 1, \"no\": 2}"; 


void setup() {
   Serial.begin(115200);
  pinMode(26, OUTPUT);
  pinMode(23, OUTPUT);
  digitalWrite(26, HIGH);
  digitalWrite(23, LOW);
  
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverName + "/test");

      // Specify content-type header
      http.addHeader("Content-Type", "application/json");
      // Data to send with HTTP POST
                
      // Send HTTP POST request
      Serial.println(http.POST(httpRequestData));
      
      String response = http.getString();
      
      //Serial.println(http.getString());
      Serial.println(response);
      Serial.println(digitalRead(26));
      if (response == "\"red\"") {
        digitalWrite(26, HIGH);
        digitalWrite(23, LOW);
        httpRequestData = "{\"yes\": 2}";
      }
      else {
        digitalWrite(23, HIGH);
        digitalWrite(26, LOW);
        httpRequestData = "{\"yes\": 1}";
      }
      
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
}

(the above code is the code on the esp32)
So i want to get my esp32 to constantly post with my repl.it server, (eventually i wanna hookup a camera and send the video feed to the web server) right now im just testing with sending some info, and having the server return some data, and then switching between some leds. But each POST request takes like 2-3 seconds, which for my purpose of having a video feed on the site, is wayyy too slow, what can i do to speed this up?
(the below code is the code on the repl.it server)

import os
import cherrypy

class Camera(object):
  
  @cherrypy.expose
  @cherrypy.tools.json_in()
  @cherrypy.tools.json_out()
  def test(self):
        data = cherrypy.request.json 
        print(data)
        if data["yes"] == 1:
          return "red"
        else:
          return "blue"
      
  @cherrypy.expose
  @cherrypy.tools.json_in()
  @cherrypy.tools.json_out()
  def index(self):
    return "hello world" 


if __name__ == "__main__":
    server = Camera()
    cherrypy.config.update({"server.socket_host": "0.0.0.0"})
    cherrypy.config.update(
        {"server.socket_port": int(os.environ.get("PORT", "8080"))}
    )
    print("Starting Server...")
    cherrypy.quickstart(server)

HTTP is slow and a few seconds of time it OK. When I did the HTTP post thingy I gave 4 seconds per transaction.

You may want to look into using web sockets for quicker/faster Inet data comm thingies.

Your code uses SSL/TLS and that will slow down you code because it uses encryption and because there is a handshake every time you connect with client.begin(..). You should be able to significantly speed up your code by re-using the http client and by keeping the connection alive (often referred to as "http persistence").

How can i keep the connection alive?

You CANNOT send video with POST requests....

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