I am currently using ESP32 which First Act as an AP and We can connect to it and connect to Available WIFI . I am using server end point as API for this. I am using two API one in get and other in post . First get api scan wifi and send details in json and other one receives data of the wifi network which it is needed to connect and it connect to it .and the response of this api is the ip and ssid name . but for some reason the response is not always sending back to the client who is connected at the time of AP .the response not getting is started after attempting to WIFI. Is there a way that i can make sure API response is received Everytime the API calls.
_server.on("/WiFi", HTTP_POST, [](AsyncWebServerRequest* request) {
// To clear any WIFi Stack
// WiFi.disconnect(true);
// delay(50);
if (request->hasArg("ssid") && request->hasArg("password")) {
String ssid = request->arg("ssid");
String password = request->arg("password");
// Initialize optional static IP configuration variables
String ip = "", gateway = "", subnet = "", primaryDNS = "", secondaryDNS = "";
// Check for optional static IP and DNS parameters
if (request->hasArg("ip") && request->hasArg("gateway") && request->hasArg("subnet")) {
ip = request->arg("ip");
gateway = request->arg("gateway");
subnet = request->arg("subnet");
if (request->hasArg("dns1")) primaryDNS = request->arg("dns1");
if (request->hasArg("dns2")) secondaryDNS = request->arg("dns2");
} else {
ip = "";
gateway = "";
subnet = "";
primaryDNS = "";
secondaryDNS = "";
}
// Create a WiFiConfig structure with the received data
currentConfig = { ssid, password, ip, gateway, subnet, primaryDNS, secondaryDNS };
// Save WiFi configuration to LittleFS
// saveWiFiConfig(currentConfig);
if (currentConfig.ssid != "" && currentConfig.password != "") {
// Optionally set static IP, gateway, and DNS if provided
if (currentConfig.ip != "" && currentConfig.gateway != "" && currentConfig.subnet != "") {
IPAddress ip, gateway, subnet, dns1, dns2;
ip.fromString(currentConfig.ip);
gateway.fromString(currentConfig.gateway);
subnet.fromString(currentConfig.subnet);
dns1.fromString(currentConfig.dns1);
dns2.fromString(currentConfig.dns2);
// Serial.println("NOt be this line");
WiFi.config(ip, gateway, subnet, dns1, dns2);
} else {
// Serial.println("fien by this");
// WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
}
WiFi.begin(currentConfig.ssid.c_str(), currentConfig.password.c_str());
// Wait for connection
int retries = 0;
while (WiFi.status() != WL_CONNECTED && retries < 50) {
delay(200);
esp_task_wdt_reset();
Serial.print(".");
retries++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected to WiFi");
Serial.println("IP Address: " + WiFi.localIP().toString());
saveWiFiConfig(currentConfig);
request->send(200, "text/plain", "Connected to : " + WiFi.SSID() + ", IP Address: " + WiFi.localIP().toString());
} else {
currentConfig = { "", "", "", "", "", "", "" };
if (WiFi.status() != WL_CONNECTED && WiFi.status() != WL_NO_SSID_AVAIL) {
Serial.println("Password is not correct in Server");
request->send(200, "text/plain", "Password is not correct");
} else if (WiFi.status() != WL_CONNECTED && WiFi.status() == WL_NO_SSID_AVAIL) {
Serial.println("Wifi network is not avaliable");
request->send(200, "text/plain", WiFi.SSID() + " is not avaliable");
}
}
}
// Send response to client
// // request->send(200, "text/plain", "WiFi setup data received and saved to Device");
// // delay(100);
// Serial.println("SSID: " + ssid);
// Serial.println("Password: " + password);
// if (ip != "") Serial.println("Static IP: " + ip);
// if (gateway != "") Serial.println("Gateway: " + gateway);
// if (subnet != "") Serial.println("Subnet: " + subnet);
// if (primaryDNS != "") Serial.println("Primary DNS: " + primaryDNS);
// if (secondaryDNS != "") Serial.println("Secondary DNS: " + secondaryDNS);
// delay(100);
// Serial.println("Device Restarting..");
// ESP.restart();
} else {
request->send(400, "text/plain", "Missing parameters (ssid, password)");
}
});
this is the server code