I am creating a project with an ESP8266 (Arduino IDE) which requires it to serve a webpage to a client. The module will be connected to a WiFi network by default, but when that WiFi network is not available, I need it to turn on its Soft Access Point, so that the user can still access that webpage by connecting to the AP. When the WiFi network becomes available, I want the module to reconnect to the WiFi network and disable the SoftAP.
Here is the chunk of code in my sketch that deals with all the WiFi stuff:
void wifi() {
if(WiFi.status() != WL_CONNECTED){
WiFi.mode(WIFI_AP_STA);
if((unsigned long)(millis() - waitUntil) >= interval){ //this section I used as an alternative to the delay() function, the delay is 3 sec
WiFi.begin(ssid, password);
waitUntil = waitUntil + interval;
if(WiFi.status() == WL_CONNECTED){
Serial.println("Connection successful! ");
Serial.print("IP: ");
Serial.print(WiFi.localIP());
WiFi.softAPdisconnect(true);
}
}
}
if(WiFi.status() != WL_CONNECTED && softAPEnabled == false){
Serial.println("WiFi not connected, enabling SoftAP...");
WiFi.softAP(softAP_ssid, softAP_password);
softAPEnabled = true;
Serial.println("SoftAP ON");
}
else if(WiFi.status() == WL_CONNECTED && softAPEnabled == true){
WiFi.softAPdisconnect(true);
softAPEnabled = false;
Serial.println("SoftAP OFF");
}
}
Here are the problems:
The webpage works fine when the module is connected to WiFi and I go to the IP address from a browser, but when SoftAP mode is enabled, and I connect to the AP and go to 192.168.1.1, it does not work. The next problem may be related to this.
I don't think I'm using WiFi.mode() correctly. I currently have it set to WIFI_AP_STA, but after problem 1 occurred, I tried to change it to WIFI_AP and WIFI_STA individually as I needed each mode, but this still did not work.
1 Answer 1
The following may not solve of your problems, but just in case it helps... I was using the ESP8266 both in Station mode and AP. Both worked. In AP mode, it worked but the ESP8266 ignored my instructions to set name and password for the AP , until I realised the following . The password must at least be 8 characters long, otherwise the instruction is ignored . in the IDE, you should erase flash AND WiFI settings, otherwise it will be "lazy" and use previous settings
WiFi.softAPIP();
to print the IP