WiFiManager icon indicating copy to clipboard operation
WiFiManager copied to clipboard

Adding the option to start/keep running the portals in the background after connecting

Open axlan opened this issue 3 years ago • 38 comments

This is a really simple feature request. I'd like to use this project both for it's Wifi management, but also as a general configuration page. To do this I run the class in non-blocking mode, and I want the config portal to keep running even after the WiFi connection completes successfully. I've made a simple PR to support this for my own use, but you may be interested in merging it upstream.

axlan avatar Dec 26 '21 05:12 axlan

I actually built this out a bit with a full example of how I'd be using it: https://github.com/axlan/extended-wifimanager

axlan avatar Dec 28 '21 00:12 axlan

This feature is not needed due to non blocking mode already existing, ondemand webportal existing, and this would only work on enableconfigportal and autoconnect failure. So I do not see the utility here, as opposed to manually starting configportal or webportal manually.

I have other requests for leaving cp open after save, which makes a bit more sense to me.

Maybe I am misunderstanding this entirely?

Non blocking should not close, maybe this is a bug?

tablatronix avatar Dec 28 '21 02:12 tablatronix

This feature is not needed due to non blocking mode already existing, ondemand webportal existing, and this would only work on enableconfigportal and autoconnect failure. So I do not see the utility here, as opposed to manually starting configportal or webportal manually.

I have other requests for leaving cp open after save, which makes a bit more sense to me.

Maybe I am misunderstanding this entirely?

I initially hit this before I realized that the settings could be shown on their own page. In that usage mode saving the settings closes the web portal. It was also hard to enter settings in that mode since they would clear the WiFi credentials.

In non-blocking mode with the settings on their own page, I don't think this issue is relevant. I guess you can consider changing the behavior for the case with non-blocking mode, but the settings share the WiFi config page.

axlan avatar Dec 28 '21 03:12 axlan

Both of these should not happen.

  • [x] Empty creds should not wipe anything
  • [x] nonblocking should not stopconfigportal on saves

These might be bugs or badly tested/overlooked scenarios..

tablatronix avatar Dec 28 '21 03:12 tablatronix

do you happen to have logs of this ?

Are you wanting to run the webportal in sta mode or run sta+ap and webportal (thats a bit tough, sometimes it works sometimes not, depends on enviroment and wifi channels)

tablatronix avatar Jan 20 '22 21:01 tablatronix

I could not reproduce saves breaking saved creds

tablatronix avatar Jan 22 '22 00:01 tablatronix

I'm using the master commit: 810f144

It looks like the main issue (clearing the credentials on an empty save) is fixed.

The configuration portal still closes on connection, which is what I originally proposed a setting to disable.

Here are the logs I captured in testing.

Here's an example main.cc I'm using for reference:

#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
WiFiManager wm;
WiFiManagerParameter custom_field; // global param ( for non blocking w params )

void setup() {
    WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP    
    // put your setup code here, to run once:
    Serial.begin(115200);
    
    //reset settings - wipe credentials for testing
    //wm.resetSettings();

    // test custom html(radio)
    const char* custom_radio_str = "<br/><label for='customfieldid'>Custom Field Label</label><input type='radio' name='customfieldid' value='1' checked> One<br><input type='radio' name='customfieldid' value='2'> Two<br><input type='radio' name='customfieldid' value='3'> Three";
    new (&custom_field) WiFiManagerParameter(custom_radio_str); // custom html input
    
    wm.addParameter(&custom_field);

    wm.setConfigPortalBlocking(false);
    //automatically connect using saved credentials if they exist
    //If connection fails it starts an access point with the specified name
    if(wm.autoConnect("AutoConnectAP")){
        Serial.println("connected...yeey :)");
    }
    else {
        Serial.println("Configportal running");
    }

    wm.startConfigPortal();
}

void loop() {
    wm.process();
}

Issue 1: The web configuration closes on initial connection:

*wm:[1] AutoConnect 
*wm:[2] Connecting as wifi client...
*wm:[2] setSTAConfig static ip not set, skipping
*wm:[1] Connecting to SAVED AP: XXXX
*wm:[1] connectTimeout not set, ESP waitForConnectResult... 
*wm:[2] Connection result: WL_NO_SSID_AVAIL
*wm:[1] AutoConnect: FAILED
*wm:[2] Starting Config Portal
*wm:[2] Disabling STA
*wm:[2] Enabling AP
*wm:[1] StartAP with SSID:  AutoConnectAP
*wm:[2] AP has anonymous access! 
*wm:[1] AP IP address: 192.168.4.1
*wm:[1] Starting Web Portal
*wm:[2] HTTP server started
*wm:[2] Config Portal Running, non blocking/processing
Configportal running
*wm:[2] Starting Config Portal
*wm:[2] Disabling STA
*wm:[2] Enabling AP
*wm:[1] StartAP with SSID:  ESP_DE715E
*wm:[2] AP has anonymous access! 
*wm:[1] AP IP address: 192.168.4.1
*wm:[1] Starting Web Portal
*wm:[2] HTTP server started
*wm:[2] Config Portal Running, non blocking/processing 
*wm:[2] <- Request redirected to captive portal 
*wm:[2] <- HTTP Root 
*wm:[2] <- Request redirected to captive portal 
*wm:[2] <- Request redirected to captive portal 
*wm:[2] <- HTTP Wifi 
*wm:[2] WiFi Scan SYNC started
*wm:[2] WiFi Scan completed in 2186 ms
*wm:[1] 6 networks found
*wm:[2] DUP AP: XXXX
*wm:[2] AP:  -64 XXXX
*wm:[2] AP:  -69 ATTj8CKQpi
*wm:[2] AP:  -70 ESP_D3B0F4
*wm:[2] AP:  -81 DU-EERO-LR
*wm:[2] AP:  -84 2 Live
*wm:[2] <- Request redirected to captive portal 
*wm:[2] <- Request redirected to captive portal 
*wm:[2] NUM CLIENTS:  1
*wm:[2] <- HTTP WiFi save  
*wm:[2] Parameters
*wm:[2] --------------------
*wm:[2] :
*wm:[2] --------------------
*wm:[2] processing save 
*wm:[2] Connecting as wifi client... 
*wm:[2] setSTAConfig static ip not set, skipping
*wm:[1] Connecting to NEW AP: XXXX
*wm:[1] connectTimeout not set, ESP waitForConnectResult... 
*wm:[2] Connection result: WL_CONNECTED
*wm:[1] Connect to new AP [SUCCESS]
*wm:[1] Got IP Address:
*wm:[1] 192.168.1.141
*wm:[2] shutdownConfigPortal
*wm:[2] restoring usermode STA
*wm:[2] wifi status: WL_CONNECTED
*wm:[2] wifi mode: STA
*wm:[2] configportal closed

Issue 2: The web configuration is cleared when only setting the user parameters:

This appears to be fixed in this commit!

*wm:[1] AutoConnect 
*wm:[2] Connecting as wifi client...
*wm:[2] setSTAConfig static ip not set, skipping
*wm:[1] Connecting to SAVED AP: XXXX
*wm:[1] connectTimeout not set, ESP waitForConnectResult... 
*wm:[2] Connection result: WL_CONNECTED
*wm:[1] AutoConnect: SUCCESS
*wm:[1] STA IP Address: 192.168.1.141
connected...yeey :)
*wm:[2] Starting Config Portal
*wm:[2] Enabling AP
*wm:[1] StartAP with SSID:  ESP_DE715E
*wm:[2] AP has anonymous access! 
*wm:[1] AP IP address: 192.168.4.1
*wm:[1] Starting Web Portal
*wm:[2] HTTP server started
*wm:[2] Config Portal Running, non blocking/processing
*wm:[2] <- HTTP Root 
*wm:[2] NUM CLIENTS:  0
*wm:[2] <- HTTP Wifi 
*wm:[2] WiFi Scan SYNC started
*wm:[2] WiFi Scan completed in 1586 ms
*wm:[1] 7 networks found
*wm:[2] DUP AP: XXXX
*wm:[2] AP:  -63 XXXX
*wm:[2] AP:  -67 ESP_D3B0F4
*wm:[2] AP:  -67 ATTj8CKQpi
*wm:[2] AP:  -70 XXXX
*wm:[2] AP:  -75 DU-EERO-LR Guest
*wm:[2] AP:  -84 2 Live
*wm:[2] <- HTTP WiFi save  
*wm:[2] Parameters
*wm:[2] --------------------
*wm:[2] :
*wm:[2] -------------------- 
*wm:[2] processing save 
*wm:[2] No ssid, skipping wifi save 
*wm:[2] Portal is non blocking - remaining open

axlan avatar Jan 22 '22 01:01 axlan

Yeah but the first condition only occurs if conx fails so you have to manually do it anyway right. Also how can you keep it open if you are saving new wifi, it has to connect to the ap so it has to close softap.

also if you are on wifi you only want the webportal running you cant have the configportal running and be in sta mode, sta+ap is unstable.

you want the softap running all the time and be connected to your network??

tablatronix avatar Jan 22 '22 01:01 tablatronix

I still cannot understand your pr to test as it has code style changes diffs

tablatronix avatar Jan 22 '22 02:01 tablatronix

Sorry, the PR pulled in further changes I made to that branch for my own application. I created a new issue for the other bug I found, and I'll close it since you already addressed the main issue.

The remaining issue can be summarized as: "When running in the background, I'd like the configuration portal (the web server) to be available regardless of changes to connectivity". Since I'm using the portal for general configuration it avoids needing to restart the board after the initial Wifi configuration. I first access the configuration page through the captive portal, and after the initial configuration it can be accessed at the IP address reservation.

For the PR I had made, it seemed sufficient to just modify the logic not to close the configuration portal. I admit I'm not super familiar with how the web servers are managed, so it's possible to get this to work across different platforms the server might need to be stopped and restarted.

This is admittedly not the main use case of the Wifi manager, so it's understandable if you don't want this behavior.

axlan avatar Jan 22 '22 02:01 axlan

Well you probably want to be using startWebPortal() and have proper control over it and not rely on autoconnect other than actual wifi failure recovery

Just add a check and if wifi is connected startwebportal

    wm.setConfigPortalBlocking(false);
    //automatically connect using saved credentials if they exist
    //If connection fails it starts an access point with the specified name
    if(wm.autoConnect("AutoConnectAP")){
        Serial.println("connected...yeey :)");
        wm.startConfigPortal();
    }
    else {
        Serial.println("Configportal running");
    }

And there is also always setEnableConfigPortal(boolean enable); to use autoconnect without configportal and start it manually if you want, or have no wifi conx

Now all this has not been tested with saving new wifi creds, not sure if things will break after switching wifi modes and all that, I will try to do some tests

tablatronix avatar Jan 22 '22 02:01 tablatronix

Sure, though I don't think autoconnect is the issue here. It's still an issue with this example:

#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager

WiFiManager wm;

void setup() {
    WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP    
    // put your setup code here, to run once:
    Serial.begin(115200);

    wm.setConfigPortalBlocking(false);

    wm.startWebPortal();
    wm.startConfigPortal();
}

void loop() {
    wm.process();
}

I suppose the alternative to the feature I'm requesting would be something like:

#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager

WiFiManager wm;

void setup() {
    WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP    
    // put your setup code here, to run once:
    Serial.begin(115200);

    wm.setConfigPortalBlocking(false);

    wm.startWebPortal();
    wm.startConfigPortal();
}

void loop() {
    wm.process();
    // For a real example would probably want to add a delay between calls to this check
    if (!wm.getConfigPortalActive()) {
      wm.startConfigPortal();
    }
}

axlan avatar Jan 22 '22 03:01 axlan

startConfigPortal includes webportal

wm.startConfigPortal(); = start AP and webportal
wm.startWebPortal(); = only start webportal (presumably on your wifi sta ip)

Like I said you are essentially trying to do WiFi.setMode(WIFI_STA_AP); is this what you want? you want your softap running all the time when connected to wifi ?

Cause this is very unstable

tablatronix avatar Jan 22 '22 03:01 tablatronix

No, I want the webserver running all the time regardless of the Wifi status or changes to the Wifi. That way I can always access the settings and update the settings either though the IP or the AP.

axlan avatar Jan 22 '22 03:01 axlan

My application has a screen that indicates how you should connect to it, so it's obvious to the user which to use.

axlan avatar Jan 22 '22 03:01 axlan

Yeah thats how i use it and why non blocking was created.

If you are connected to wifi just use startWebPortal(), you don't want the ap running

tablatronix avatar Jan 22 '22 03:01 tablatronix

check this example

https://github.com/tzapu/WiFiManager/blob/master/examples/NonBlocking/OnDemandNonBlocking/onDemandNonBlocking.ino

See how the startap option works

https://github.com/tzapu/WiFiManager/blob/master/examples/NonBlocking/OnDemandNonBlocking/onDemandNonBlocking.ino#L24

    if(startAP){
      Serial.println("Button Pressed, Starting Config Portal");
      wm.setConfigPortalBlocking(false);
      wm.startConfigPortal();
    }  
    else{
      Serial.println("Button Pressed, Starting Web Portal");
      wm.startWebPortal();
    }  

you can also add a wifi check and decide that way, or add some kind of checker now and then etc (its usually easier to reboot though)

tablatronix avatar Jan 22 '22 03:01 tablatronix

I do want the AP to run for the initial setup. The transition from AP to STA is the case I'm trying to handle here. I think I had startWebPortal and startConfigPortal confused in my previous example, but it still seems like:

void loop() {
    wm.process();
    // For a real example would probably want to add a delay between calls to this check
    if (!wm.getWebPortalActive()) {
      wm.startWebPortal();
    }
}

would be needed to accomplish this. I could also probably handle similar logic in the callbacks, but that's the basic behavior. I was able to avoid this with an additional setting.

It seems like this is what you're suggesting with your example, except it's triggered by a specific event, and not just running all the time.

axlan avatar Jan 22 '22 03:01 axlan

oh yeah its an ondemand example, but you would just change that to if connected or something else.

How do you plan on doing initial setup ? wifi.ssid=null ?

I think i have a helper function in there

EDIT

/**
 * check if wifi has a saved ap or not
 * @since $dev
 * @access public
 * @return bool true if a saved ap config exists
 */
bool WiFiManager::getWiFiIsSaved(){
  return WiFi_hasAutoConnect();
}

tablatronix avatar Jan 22 '22 03:01 tablatronix

crap, I didn't click comment and lost previous post, feel free to hit up discord ill check it later, I am also working on something atm that will be doing this same behavior with smarter ap starting to deal with power failures and sleep, so I have been thinking about this usage, so I am curious, I will try to test the PR tomorrow

I have no problem with a single toggle , but I need to make sure it handles all failure modes and when saving we can keep the http and dns without having to restart it, i highly doubt this will work ok all the time as its a bit finicky.

tablatronix avatar Jan 22 '22 03:01 tablatronix

Sorry I was working on some other stuff, arduino 2.0 testing etc

tablatronix avatar Feb 03 '22 02:02 tablatronix

@tablatronix I know this is off topic but the discord link in the main readme has expired.

tavdog avatar Aug 24 '22 22:08 tavdog

  • [ ] follow up on this

tablatronix avatar Sep 08 '23 18:09 tablatronix

I am joining late, but have a similar use case I am looking at. I want to be able to easily access saved settings on power up, without overwriting them with the default that is hardcoded in. I really like the captive portal aspect where the user does not need a website, it just opens.

I found that: The setconfigportal loads the default values.
the autoconnect goes straight to the saved network, and I can not update values

I tried the code below from the above post, and it is not saving my parameters when I catch it in the loop. I also would prefer not to have to know the IP address of the device when connected.

void loop() { wm.process(); // For a real example would probably want to add a delay between calls to this check if (!wm.getWebPortalActive()) { wm.startWebPortal(); } }

scudrunner avatar Nov 20 '23 13:11 scudrunner

I updated the code to read the JSON, and change the defaults to previously saved values. I am still lost on how to force a connection later without a button. I would like a period of time on initial boot that I can access the parameters, and after that period of time, the main wifi will be joined. since there is no real trigger, having it in loop seemed very clumsy - but kind of worked. I was re-writing the JSON each loop to make sure it was saved, and this just seemed to sloppy and a waste

scudrunner avatar Nov 20 '23 19:11 scudrunner

Do you want to access via the website or the ap? You can add a configportal timeout and startconfigportal, and when it closes you can autoconnect. Not sure exactly what you are looking for

tablatronix avatar Nov 20 '23 23:11 tablatronix

I am making a controller for a light to change color based on blood sugar for a diabetic, and I want to make it easy for people to configure if someone wants to simply reuse code.

Once initially configured I want to access parameters to change behavior. Ex: change the trigger point for when to turn color or change the color of the leds without the need to reburn the chip, or disconnect the house wifi….

Optimally I would have 1 minute to change settings before it decided to autoconnect. When I tried the forced portal, it cleared the wifi, and I want the wifi to stay.

Second best would be to open a web page on the esp while it is connected to wifi. I am going to explore that route unless there is a good way to get wifimanager to do what I want.

Again, what i would like is a delay on the config page with the esp network before it connects to the saved wifi. I would not need to remember an ip address, or write anything that is not already in this awesome code base.

Thanks

Jeff

On Mon, Nov 20, 2023 at 6:51 PM Shawn A @.***> wrote:

Do you want to access via the website or the ap? You can add a configportal timeout and startconfigportal, and when it closes you can autoconnect. Not sure exactly what you are looking for

— Reply to this email directly, view it on GitHub https://github.com/tzapu/WiFiManager/issues/1327#issuecomment-1819989299, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFQCYWVIQS644IAA6NDITQLYFPUGRAVCNFSM5KYQVSN2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBRHE4TQOJSHE4Q . You are receiving this because you commented.Message ID: @.***>

scudrunner avatar Nov 21 '23 02:11 scudrunner

Yeah thats literally how this works, those things are what it is made for, not sure why its clearing your credentials unless you are doing it in your code..

wm.setConfigportalTimeout(120); wm.startConfigPortal(); autoconnect();

wm.setblocking = false; startWebPortal();

loop process()

This is all in the examples, check the SUPER for every single option

feel free to open a new issue with your actual code etc

tablatronix avatar Nov 21 '23 02:11 tablatronix

I will try a simpler version of my code tomorrow. To be clear - what example should I follow? I did have lots of code snippets running around as got it all working, but I was being careful on this one.

I did find out how to read the json, and overwrite the default values on boot up, and that may be a nice add as an example in case someone else wants it. It ended up being easy enough once I went through the code, but an example may help others.

Thank you for the response, it is definitely appreciated

On Mon, Nov 20, 2023 at 9:47 PM Shawn A @.***> wrote:

Yeah thats literally how this works, not sure why its clearing your credentials unless you are doing it in your code..

— Reply to this email directly, view it on GitHub https://github.com/tzapu/WiFiManager/issues/1327#issuecomment-1820128239, or unsubscribe https://github.com/notifications/unsubscribe-auth/AFQCYWXHBRLQVNAJRLTD473YFQI43AVCNFSM5KYQVSN2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBSGAYTEOBSGM4Q . You are receiving this because you commented.Message ID: @.***>

scudrunner avatar Nov 21 '23 02:11 scudrunner

Yeah there is a spiffs/little fs example for storing params.

But here is the SUPER https://github.com/tzapu/WiFiManager/blob/master/examples/Super/OnDemandConfigPortal/OnDemandConfigPortal.ino

// TEST OPTION FLAGS
bool TEST_CP         = false; // always start the configportal, even if ap found
int  TESP_CP_TIMEOUT = 90; // test cp timeout

tablatronix avatar Nov 21 '23 03:11 tablatronix