大家好,
我无法在 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();
}