tzapu / WiFiManager

ESP8266 WiFi Connection manager with web captive portal
http://tzapu.com/esp8266-wifi-connection-manager-library-arduino-ide/
MIT License
6.55k stars 1.97k forks source link

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

Open axlan opened 2 years ago

axlan commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

Both of these should not happen.

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

tablatronix commented 2 years ago

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 commented 2 years ago

I could not reproduce saves breaking saved creds

axlan commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

axlan commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

tablatronix commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

tavdog commented 2 years ago

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

tablatronix commented 1 year ago
scudrunner commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

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: @.***>

tablatronix commented 10 months ago

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 commented 10 months ago

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: @.***>

tablatronix commented 10 months ago

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 commented 10 months ago

I tried with the CPTEST set to true and false, and did not get the access point to come up either time.

I tried running the example you pointed to and get the following errors. Running on a 8266 wemos D1 clone

C:\Users...sketch_nov21a\sketch_nov21a.ino:92:21: error: 'void WiFiManager::handleNotFound()' is private within this context 92 | wm.handleNotFound(); | ^ In file included from C:\Users....sketch_nov21a\sketch_nov21a.ino:6: c:\Users....Arduino\libraries\WiFiManager/WiFiManager.h:655:19: note: declared private here 655 | void handleNotFound(); | ^~~~~~ C:\Users..sketch_nov21a\sketch_nov21a.ino: In function 'void setup()': C:\Users..Temp.arduinoIDE-unsaved20231021-19320-7kuv5w.zvso9\sketch_nov21a\sketch_nov21a.ino:125:27: error: 'WM_DEBUG_DEV' was not declared in this scope; did you mean 'WM_DEBUG_LEVEL'? 125 | wm.setDebugOutput(true, WM_DEBUG_DEV); | ^~~~ | WM_DEBUG_LEVEL

exit status 1

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

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

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

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

tablatronix commented 10 months ago

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

scudrunner commented 10 months ago

Arduino, and I just loaded it last week. I gave up on platform io, it had many repositories i was trying to use that would not load properly.

On Tue, Nov 21, 2023 at 11:22 AM Shawn A @.***> wrote:

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

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

scudrunner commented 10 months ago

the library manager in arduino is showing 2.0.16-rc.2

On Tue, Nov 21, 2023 at 11:22 AM Shawn A @.***> wrote:

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

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

scudrunner commented 10 months ago

in my library folder, the readme has the link to 2.0.16-rc.2, and the JSON file also has the same version.

On Tue, Nov 21, 2023 at 11:22 AM Shawn A @.***> wrote:

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

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

tablatronix commented 10 months ago

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

scudrunner commented 10 months ago

I see you posted in 2018 with a link for the dev branch at that time, but I dont know how to get the current Dev Branch....

On Tue, Nov 21, 2023 at 3:31 PM Shawn A @.***> wrote:

Yeah you would need the git dev version

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

tablatronix commented 10 months ago

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