-->
Page 1 of 2

How to disable wifiMulti if unsucessful attempt to connect?

PostPosted: Mon Apr 29, 2019 7:30 am
by mrlightsman
I am using the standard example of wifiMulti in a sketch in an ArduinoIDE environment on an ESP8266-01. What I have found is if the SSID credentials are entered wrong, or the device is taken to a new environment (ie, work), the sketch just hangs up in the while loop searching for a viable network.

I want to be able to break the loop and start a softAP in event of an unsuccessful connect to WAN. I can break the while loop, and start a softAP. However, while the softAP starts and I can see its SSID on my laptop, it is very slow to connect and does not support the webserver which is running on the ESP. (In other words, the laptop connects, but Chrome says I am not connected to the internet and shows file not found errors.)


As long as wifiMulti connects to a viable network, everything runs smoothly using the ESP in _AP_STA mode. Or, if I comment out setupWifi() and use only apWifi(), everything runs smoothly using the ESP in _AP mode. It is only when wifiMulti fails to connect that I get hung up.

What do I need to do to disable wifiMulti when I break the loop? It seems to be negatively interfering with the softAP ability to establish a connection for the webserver. Thank you.

Here are some of the code snippets:
In my globals:
Code: Select all#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
ESP8266WiFiMulti wifiMulti;
ESP8266WebServer server(80);
//set up all of the necessary variables

This function is called from setup. It sets up wifiMulti.
Code: Select allvoid setupWifi() {
//  WiFi.mode(WIFI_AP_STA);
  aptime = millis();
  wifiMulti.addAP("SSID1","PW1");
  wifiMulti.addAP("SSID2", "PW2");
  wifiMulti.addAP("SSID3", "PW3");
  wifiMulti.addAP("SSID4", PW4");

  Serial.println("Connecting Wifi...");
  while (wifiMulti.run() != WL_CONNECTED) {
    if (millis()-aptime >= apdelay) {
        Serial.println("Network Failed");
        break;
      }
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("Connected to SSID: ");
  Serial.println(WiFi.SSID());
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}



This function is called from setup to set up the softAP. After wifiMulti is called.

Code: Select allvoid apWifi () {
  //Setup softAP to run befor WiFiMulti
  Serial.print("Configuring access point...");
  WiFi.softAP("Box", "a");

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
}



My void Setup has these bits, among many others to start up the wifi and server.

Code: Select allVoid Setup(){
//… lots of variables, etc.

setupWifi();
apWifi():
  server.on("/setup", handleConfigureDevice);
  server.on("/update",handleUpdate);
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP Server Started");


Code: Select allvoid loop() {
  server.handleClient();
}

Re: How to disable wifiMulti if unsucessful attempt to conne

PostPosted: Mon Apr 29, 2019 9:50 am
by QuickFix
Have you tried to implement WiFiManager?

Re: How to disable wifiMulti if unsucessful attempt to conne

PostPosted: Mon Apr 29, 2019 9:52 am
by mrlightsman
Can I just insert a wiFi.mode(WIFI_AP_STA) at the head of my function and a WiFi.mode(WIFI_OFF) line here?
Code: Select allvoid setupWifi() {
  WiFi.mode(WIFI_AP_STA);  //  <<<<<<<<<<<<<<---------------
  aptime = millis();
  wifiMulti.addAP(SSID1, PW1);
  wifiMulti.addAP(SSID2, PW2);
  wifiMulti.addAP(SSID3, PW3);
  wifiMulti.addAP(SSID4, PW4);

  Serial.println("Connecting Wifi...");
  while (wifiMulti.run() != WL_CONNECTED) {
    if (millis()-aptime >= apdelay) {
      Serial.println("Network Failed");
      WiFi.mode(WIFI_OFF);  //  <<<<<<<---------------
      delay(3000);
      apWifi();
      break;
    }
   
  Serial.print(".");
  delay(500);
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("Connected to SSID: ");
  Serial.println(WiFi.SSID());
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


Then add a WiFi.mode(WIFI_AP) here?
Code: Select allvoid apWifi () {
  //Setup softAP to run before WiFiMulti
  WiFi.mode(WIFI_AP); //              <<<<<<<<<<<<<<<<<<----------------------
  Serial.print("Configuring access point...");
  WiFi.softAP(ap_SSID, ap_Password);

  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
}



I'm not near my board so I can't test right now. Will this turn off the wifiMulti functions so I can set up the softAP uninhibited? Thanks.

Re: How to disable wifiMulti if unsucessful attempt to conne

PostPosted: Mon Apr 29, 2019 10:01 am
by mrlightsman
QuickFix wrote:Have you tried to implement WiFiManager?


Yes. I have been using wiFiManager for a couple of years. I very much like it, but find it has some limitations I cannot workaround. I was using the-real-orca branch for a while also. It was closest to my needs.

What I am hoping to achieve is essentially the same functionality, except:
1. Use multiWifi (with multiple SSID & PW configured as parameters on the served up webpage)
2. Have the webserver constantly serve up the configuration page in _AP_STA mode

Similarities:
1. First, try to connect to known Wifi networks in _AP_STA mode and, if unsuccessful, start up AP mode only and serve up the configuration page so I can edit credentials. While in AP mode, the rest of the sketch maintains functionality (ie non-blocking).
2. Once configured, restart the ESP and try it all again.

Thanks.