You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESP32处于WIFI_AP_STA模式时HTTP GET请求无法正常工作的问题

ESP32在WIFI_AP_STA模式下无法向AP模式ESP32发送HTTP请求的解决方法

问题背景

我需要让发送端ESP32工作在WIFI_AP_STA模式,连接到AP模式的接收端ESP32并发送HTTP GET请求。当发送端使用WIFI_STA模式时请求正常,切换到WIFI_AP_STA后,请求超时返回错误码-1,接收端无任何请求记录。

发送端原始代码

#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid     = "ServerSSID";
const char* password = "ServerKey";

void setup(){
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);//完全正常工作
  //WiFi.mode(WIFI_AP_STA);//无法工作
  WiFi.begin(ssid,password);
  Serial.println("Connecting to WiFi:");  
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());  
  delay(1000);
  String Status=httpGETRequest("http://192.168.4.1/send?mess=Test"); 
  Serial.println(Status);
}

void loop(){
}

String httpGETRequest(const char* serverName) {
  WiFiClient client;
  HTTPClient http;
  http.begin(client, serverName);
  
  int httpResponseCode = http.GET();
  String payload = "--"; 
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  http.end();

  return payload;
}

接收端代码

#include <WiFi.h>
#include <ESPAsyncWebServer.h>

const char* ssid     = "ServerSSID";
const char* password = "ServerKey";

AsyncWebServer server(80); 

void setup(){
  Serial.begin(115200);
  delay(1000);
  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid, password);
  IPAddress IP = WiFi.softAPIP();
  Serial.print("IP address: ");
  Serial.println(IP);
  
  server.on("/send", HTTP_GET, [](AsyncWebServerRequest *request){
    Serial.println("Request Received");
    request->send(200, "text/plain", "OK");
    if (request->hasParam("mess")){
      String mess= request->getParam("mess")->value();
      Serial.println(mess);
    }     
  });
  server.begin();
}

void loop(){
}

原因分析

ESP32在WIFI_AP_STA模式下同时运行STA和AP两个接口,系统默认会优先使用AP接口的网络栈处理请求,导致发送的HTTP请求没有走已连接到接收端AP的STA链路,最终请求超时。

解决方案

强制指定WiFiClient使用STA接口,确保请求通过已建立的STA连接发送。

修改后的发送端代码

#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid     = "ServerSSID";
const char* password = "ServerKey";

void setup(){
  Serial.begin(115200);
  WiFi.mode(WIFI_AP_STA); // 现在可正常工作
  WiFi.begin(ssid,password);
  Serial.println("Connecting to WiFi:");  
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(WiFi.localIP());  
  delay(1000);
  String Status=httpGETRequest("http://192.168.4.1/send?mess=Test"); 
  Serial.println(Status);
}

void loop(){
}

String httpGETRequest(const char* serverName) {
  // 绑定WiFiClient到STA接口
  WiFiClient client(WiFiInterface::STA);
  HTTPClient http;
  http.begin(client, serverName);
  
  int httpResponseCode = http.GET();
  String payload = "--"; 
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  http.end();

  return payload;
}

关键修改说明

  • 创建WiFiClient实例时,传入WiFiInterface::STA参数,明确指定使用STA接口的网络栈,确保请求走已连接到接收端AP的链路。

内容的提问来源于stack exchange,提问作者Nils Haverkamp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 01:39:58