乐鑫技术交流
直播中

李桂兰

7年用户 1432经验值
私信 关注
[问答]

为什么无法在ESPAsyncWebServer回调中使用HTTP客户端?

大家好,
我无法在 ESPAsyncWebServer 回调中使用 HTTP 客户端(“连接被拒绝”错误)。
从回调之外的任何地方调用的相同函数按预期工作。
我的测试代码:
代码:全选#include
#include
#include
#include

const char *ssid = "********";
const char *password = "********";
const char *hostname = "esptest";
const char *url = "http://httpbin.org/ip";

HTTPClient http;
AsyncWebServer server(80);

void cltTest(const char *context) {

    Serial.printf("nn>>>> cltTest called from %snHeap: %dn", context, ESP.getFreeHeap());

    http.begin(url);
    if (http.GET() > 0) {
        http.writeToStream(&Serial);
    }
    http.end();
}

void setup() {

  Serial.begin(115200);
  Serial.setDebugOutput(true);

  Serial.printf("Attempting to connect to SSID: %s", ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000);
  }
  Serial.println("nConnected!");

  MDNS.begin(hostname);
  MDNS.addService("http", "tcp", 80);

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    cltTest("Server callback");
    request->send(200, "text/plain", "Done");
  });
  server.begin();

  cltTest("Setup");
}

void loop() {
  MDNS.update();
}

更多回帖

发帖
×
20
完善资料,
赚取积分