admin管理员组

文章数量:1125964

I am trying to get an ESP32 (sender) to send a http get Request to a second esp32(receiver) via WiFi. For that i want the sender to be in WIFI_AP_STA mode and connect to the receiver (AP-Mode) and than make an HTTP Request.

I wrote the following sketches (minimum working examples; obviously i want to do some more things). Transmitter:

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

void setup(){
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);//Works totally fine.
  //WiFi.mode(WIFI_AP_STA);//Doesn't work. 
  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;
  // Your Domain name with URL path or IP address with path
  http.begin(client, serverName);
  
  // Send HTTP POST request
  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);
  }
  // Free resources
  http.end();

  return payload;
}

Receiver:


#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(){
}

It works totally fine as long as i use WIFI_STA-mode for the transmitter, but stops working when i use WIFI_AP_STA mode. Error code is "-1" meaning, that it takes to long and stops trying/no answer. The receiver also doesn't receive anything.

I am trying to get an ESP32 (sender) to send a http get Request to a second esp32(receiver) via WiFi. For that i want the sender to be in WIFI_AP_STA mode and connect to the receiver (AP-Mode) and than make an HTTP Request.

I wrote the following sketches (minimum working examples; obviously i want to do some more things). Transmitter:

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

void setup(){
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);//Works totally fine.
  //WiFi.mode(WIFI_AP_STA);//Doesn't work. 
  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;
  // Your Domain name with URL path or IP address with path
  http.begin(client, serverName);
  
  // Send HTTP POST request
  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);
  }
  // Free resources
  http.end();

  return payload;
}

Receiver:


#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(){
}

It works totally fine as long as i use WIFI_STA-mode for the transmitter, but stops working when i use WIFI_AP_STA mode. Error code is "-1" meaning, that it takes to long and stops trying/no answer. The receiver also doesn't receive anything.

Share Improve this question asked Jan 9 at 5:28 Nils HaverkampNils Haverkamp 1 New contributor Nils Haverkamp is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 5
  • What is the IP address of the SoftAP on the GET request origin esp32? yes it is the same so how should the TCP stack know where to send the request? – Juraj Commented 2 days ago
  • Does it even have an IP-Adress, if it doesn't start the SoftAP? Right now it only sets the mode to enable using it. It is not even started. – Nils Haverkamp Commented 2 days ago
  • setting the mode starts the SoftAP – Juraj Commented 2 days ago
  • I can't reproduce what you claims, it works just fine for the soft_AP sketch. Change your 192.168.4.1/send?mess=Test to a test server like httpbin.org/get and test it yourself. – hcheung Commented 2 days ago
  • Big thanks to both of you! Setting a different IP-Address for the receiver seems to solve the problem. I just changed the IP-adress via Wifi.softAPConfig() before calling Wifi.mode. – Nils Haverkamp Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 0

Changing the ip Adress of the Receiver works:

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

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

IPAddress AP_LOCAL_IP(192, 168, 3, 1);
IPAddress AP_GATEWAY_IP(192, 168, 3, 254);
IPAddress AP_NETWORK_MASK(255, 255, 255, 0);

AsyncWebServer server(80); 

void setup(){
  Serial.begin(115200);
  delay(1000);    
  WiFi.softAPConfig(AP_LOCAL_IP, AP_GATEWAY_IP, AP_NETWORK_MASK);
  WiFi.softAPsetHostname(ssid);
  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid, password);
  IPAddress IP = WiFi.softAPIP();  

  Serial.print("IP address: ");
  Serial.println(IP);
  Serial.println(WiFi.softAPmacAddress());
  Serial.println(WiFi.softAPSubnetCIDR());
  
  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(){
}

本文标签: webserverHTTP Get request does not work when ESP32 is in WIFIAPSTA ModeStack Overflow