Timing issue causes alternate HTTP POST request to fail while using ESP32

I am using an ESP32 to make a http POST request. The problem is if the requests are made within less than 4 seconds, every post request is a success. But if the delay between requests in greater than 4 seconds, then every alternate request succeeds with a code of 200. What could be the problem? I have posted the code below, along with a screenshot of the serial monitor.

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

HTTPClient http;
const char *ssid = "DIGISOL";
const char *password = "Praj@Nsc#123";
//int lcount = 0;

void setup() {
  Serial.begin(115200);
  delay(500);

  WiFi.begin(ssid,password);
  while(WiFi.status() != WL_CONNECTED){
    delay(1000);
    Serial.println("Connecting to WiFi");
  }
  Serial.println("Connected to WiFi");
}

void loop() {
  //lcount++;
  String httpr = "https://support.nscpl.dev/testgalv3/php/esp32.php" ; 
  String data = "ABCD";
  http.begin(httpr);
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  http.setTimeout(15000);
  String postData = "key1=" + data; 

  int httpResponseCode = http.POST(postData);

  if (httpResponseCode > 0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    String response = http.getString();
    Serial.println(response);
  } else {
    Serial.print("HTTP Request failed. Error code: ");
    Serial.println(httpResponseCode);
  }
  http.end();
  delay(5000);
}

The serial monitor output is
Capture

Is it a timeout issue? Cause even though the timeout is set to 15 seconds, when the request fails it returns -2/-5 immediately.

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