-1

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.

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? Commented Jan 9 at 8:58
  • 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. Commented Jan 9 at 9:19
  • setting the mode starts the SoftAP Commented Jan 9 at 9:22
  • 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. Commented Jan 9 at 9:25
  • 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. Commented Jan 9 at 9:58

1 Answer 1

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(){
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.