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.