0

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:

  1. 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.

  2. 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.

  • 192.168.0.1 and 192.168.1.1 are commonly used by commercial routers as their own local address. Could your chosen station-address be colliding with that? – JRobert Aug 3 at 12:09
  • the default SoftAP IP with Arduino core is not 192.168.1.1. Use WiFi.softAPIP(); to print the IP – Juraj Aug 3 at 12:22
  • @Juraj Thanks for this. I will have a look. – skillz21 Aug 3 at 12:52
  • @Juraj Also, I have spent way too much time searching for functions of WiFi.h. Do you happen to have a link to a website with the list of all the functions of WiFi.h? – skillz21 Aug 3 at 12:57
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.