使用 ESP32 时,时序问题导致备用 HTTP POST 请求失败

问题描述 投票:0回答:1

我正在使用 ESP32 发出 http POST 请求。问题是,如果请求在 4 秒内发出,则每个 post 请求都会成功。但是,如果连续请求之间的延迟超过 4 秒,则每个alternate请求都会成功,代码为 200。这可能是什么问题,我已发布下面的代码以及串行监视器的屏幕截图。

#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);
}

串行监视器的输出是

enter image description here `

我尝试更改超时和延迟来尝试各种组合。但如果延迟超过 4 秒,每个备用请求都会失败。是超时的问题吗?因为即使超时设置为15秒,当请求失败时,也会立即返回-2/-5,而不等待响应。

http arduino timeout http-post esp32
1个回答
0
投票

这些是来自 HTTPClient.h 的错误代码

#define HTTPC_ERROR_CONNECTION_REFUSED  (-1)
#define HTTPC_ERROR_SEND_HEADER_FAILED  (-2)
#define HTTPC_ERROR_SEND_PAYLOAD_FAILED (-3)
#define HTTPC_ERROR_NOT_CONNECTED       (-4)
#define HTTPC_ERROR_CONNECTION_LOST     (-5)
#define HTTPC_ERROR_NO_STREAM           (-6)
#define HTTPC_ERROR_NO_HTTP_SERVER      (-7)
#define HTTPC_ERROR_TOO_LESS_RAM        (-8)
#define HTTPC_ERROR_ENCODING            (-9)
#define HTTPC_ERROR_STREAM_WRITE        (-10)
#define HTTPC_ERROR_READ_TIMEOUT        (-11)

我不完全确定 15 秒超时和延迟(5000),也许尝试删除延迟(5000)并使用像这样的非阻塞方法

我只会发布相关部分,在第一篇文章之前会有一个最初的 5 秒延迟,但如果之后它继续正常工作,那么你可以尝试并调整它

unsigned long delay_timer = 0;
int delay_interval = 5000; //<-- change this to whatever delay you want

void setup() {
  // your code as it is
}

void loop() {

  if(millis() - delay_timer >= delay_interval){
  
     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); //<-- remove this

     delay_timer = millis(); 
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.