Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
axlan opened this issue Dec 26, 2021 · 38 comments
Labels
bug Validated BUG

Comments

@axlan
Copy link

axlan commented Dec 26, 2021

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
Copy link
Author

axlan commented Dec 28, 2021

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

@tablatronix
Copy link
Collaborator

tablatronix commented Dec 28, 2021

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?

@axlan
Copy link
Author

axlan commented Dec 28, 2021

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.

@tablatronix
Copy link
Collaborator

tablatronix commented Dec 28, 2021

Both of these should not happen.

  • Empty creds should not wipe anything
  • nonblocking should not stopconfigportal on saves

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

@tablatronix tablatronix added the bug Validated BUG label Dec 28, 2021
@tablatronix tablatronix reopened this Dec 28, 2021
@tablatronix
Copy link
Collaborator

tablatronix commented Jan 20, 2022

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 pushed a commit that referenced this issue Jan 21, 2022
@tablatronix
Copy link
Collaborator

I could not reproduce saves breaking saved creds

@axlan
Copy link
Author

axlan commented Jan 22, 2022

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

@tablatronix
Copy link
Collaborator

tablatronix commented Jan 22, 2022

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
Copy link
Collaborator

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

@axlan
Copy link
Author

axlan commented Jan 22, 2022

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.

@tablatronix
Copy link
Collaborator

tablatronix commented Jan 22, 2022

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

@axlan
Copy link
Author

axlan commented Jan 22, 2022

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();
    }
}

@tablatronix
Copy link
Collaborator

tablatronix commented Jan 22, 2022

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

@axlan
Copy link
Author

axlan commented Jan 22, 2022

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
Copy link
Author

axlan commented Jan 22, 2022

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

@tablatronix
Copy link
Collaborator

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
Copy link
Collaborator

tablatronix commented Jan 22, 2022

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)

@axlan
Copy link
Author

axlan commented Jan 22, 2022

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.

@tablatronix
Copy link
Collaborator

tablatronix commented Jan 22, 2022

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
Copy link
Collaborator

tablatronix commented Jan 22, 2022

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
Copy link
Collaborator

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

@tavdog
Copy link

tavdog commented Aug 24, 2022

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

@tablatronix
Copy link
Collaborator

  • follow up on this

@scudrunner
Copy link

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
Copy link

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

@tablatronix
Copy link
Collaborator

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

@scudrunner
Copy link

scudrunner commented Nov 21, 2023 via email

@tablatronix
Copy link
Collaborator

tablatronix commented Nov 21, 2023

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

@scudrunner
Copy link

scudrunner commented Nov 21, 2023 via email

@tablatronix
Copy link
Collaborator

tablatronix commented Nov 21, 2023

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

@scudrunner
Copy link

scudrunner commented Nov 21, 2023 via email

@tablatronix
Copy link
Collaborator

Ah you do not have the latest git version, are you using arduino or platformio?
This was updated

@scudrunner
Copy link

scudrunner commented Nov 21, 2023 via email

@scudrunner
Copy link

scudrunner commented Nov 21, 2023 via email

@scudrunner
Copy link

scudrunner commented Nov 21, 2023 via email

@tablatronix
Copy link
Collaborator

tablatronix commented Nov 21, 2023

Yeah you would need the git dev version, you can just delete those errors for now, they are not important lines

@scudrunner
Copy link

scudrunner commented Nov 21, 2023 via email

@tablatronix
Copy link
Collaborator

I think there is a guide here somewhere to load dev version git version of arduino libraries manually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Validated BUG
Projects
None yet
Development

No branches or pull requests

4 participants