tzapu / WiFiManager

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

If anyone wants to change the name (SSID) of the Access Point (AP) that appears (with the IP address 192.168.4.1) #1721

Closed rtek1000 closed 3 months ago

rtek1000 commented 3 months ago

If anyone wants to change the name (SSID) of the Access Point (AP) that appears (with the IP address 192.168.4.1), go to line 685. There seems to be a lack of alternatives in this part.

boolean WiFiManager::startConfigPortal() {
  String ssid = getDefaultAPName(); // <----------- SSID here: ESP32_XXXX
  return startConfigPortal(ssid.c_str(), NULL);
}

Example:

boolean WiFiManager::startConfigPortal() {
  String ssid = "My_AP"; //getDefaultAPName(); // <----------- SSID here: My_AP
  return startConfigPortal(ssid.c_str(), NULL);
}

https://github.com/tzapu/WiFiManager/blob/d82d0a1b9fca741b9ec44accdf553606a6576dda/WiFiManager.cpp#L686


On line 538 something about HostName seems to be implemented, but I don't know if this should also appear when you press the button, for on-demand mode.

  // set ap hostname
  #ifdef ESP32
    if(ret && _hostname != ""){
      bool res =  WiFi.softAPsetHostname(_hostname.c_str());
      #ifdef WM_DEBUG_LEVEL
      DEBUG_WM(WM_DEBUG_VERBOSE,F("setting softAP Hostname:"),_hostname);
      if(!res)DEBUG_WM(WM_DEBUG_ERROR,F("[ERROR] hostname: AP set failed!"));
      DEBUG_WM(WM_DEBUG_DEV,F("hostname: AP: "),WiFi.softAPgetHostname());
      #endif
   }

https://github.com/tzapu/WiFiManager/blob/d82d0a1b9fca741b9ec44accdf553606a6576dda/WiFiManager.cpp#L538

tablatronix commented 3 months ago

Its the first argument in the function.. what do you mean?

startConfigPortal(ssid,..

rtek1000 commented 3 months ago

The initial post about the 686 line was with ESP32.

It also occurs with the ESP8266, in the line 388

  // not connected start configportal
  bool res = startConfigPortal(apName, apPassword);

At the beginning of Main there is wm.setHostname("MDNSEXAMPLE"), line 37, onDemandNonBlocking.ino.

Shouldn't the library have used this same "MDNSEXAMPLE" as the SSID?

rtek1000 commented 3 months ago

It appears there is a way to read the SSID, but there is no way to Set it

/**
 * get config portal AP SSID
 * @since 0.0.1
 * @access public
 * @return String the configportal ap name
 */
String WiFiManager::getConfigPortalSSID() {
  return _apName;
}
rtek1000 commented 3 months ago

I managed to solve it, I was missing using:

autoConnect(apName, apPassword)

startConfigPortal(apName, apPassword)

char const * portal_SSID = {"DEVICE1"};
char const * portal_PASSWORD = {"12345678"}; // Need >= 8 chars

wm.autoConnect(portal_SSID, portal_PASSWORD);
wm.startConfigPortal(portal_SSID, portal_PASSWORD);

wm.autoConnect(portal_SSID, portal_PASSWORD);

wm.startConfigPortal(portal_SSID, portal_PASSWORD);

Maybe it would be easier if there was a function to set this, instead of the user declaring the SSID and password on each line they use when the portal starts.

Thank you.

tablatronix commented 3 months ago

Are you confusing mdns hostname and ssid, they are not the same thing.

I suppose there could be a global for setting apname, shrug seems trivial

rtek1000 commented 3 months ago

Should the onDemandNonBlocking.ino example always be non-blocking?

If the ESP is not connected, how long will the portal wait to go to the loop() routine?

When I comment out the wm.autoConnect() line, then the led blinks, but if I leave the line, the portal is activated and remains active indefinitely.

I used wm.setConfigPortalTimeout(timeout), but it still takes the timeout time to go to loop().

rtek1000 commented 3 months ago

Should the onDemandNonBlocking.ino example always be non-blocking?

If the ESP is not connected, how long will the portal wait to go to the loop() routine?

When I comment out the wm.autoConnect() line, then the led blinks, but if I leave the line, the portal is activated and remains active indefinitely.

I used wm.setConfigPortalTimeout(timeout), but it still takes the timeout time to go to loop().

The line needed to be uncommented:

// wm.setConfigPortalBlocking(false);

rtek1000 commented 3 months ago

Should the onDemandNonBlocking.ino example always be non-blocking? If the ESP is not connected, how long will the portal wait to go to the loop() routine? When I comment out the wm.autoConnect() line, then the led blinks, but if I leave the line, the portal is activated and remains active indefinitely. I used wm.setConfigPortalTimeout(timeout), but it still takes the timeout time to go to loop().

The line needed to be uncommented:

// wm.setConfigPortalBlocking(false);

If I use the portal Timeout, how can I know if the portal has already been stopped?

Note: On line 599, a form of time control that is not recommended appears to be being implemented, in the case of recycling the variable returned to millis().

    // handle timed out
    if(millis() > _configPortalStart + _configPortalTimeout){
      #ifdef WM_DEBUG_LEVEL
      DEBUG_WM(F("config portal has timed out"));
      #endif
      return true; // timeout bail, else do debug logging
    } 

But on line 607, A recommended form of time control is being implemented

      // log timeout time remaining every 30s
      if((millis() - timer) > logintvl){
        timer = millis();
        #ifdef WM_DEBUG_LEVEL
        DEBUG_WM(WM_DEBUG_VERBOSE,F("Portal Timeout In"),(String)((_configPortalStart + _configPortalTimeout-millis())/1000) + (String)F(" seconds"));
        #endif
      }
rtek1000 commented 3 months ago

If I use the portal Timeout, how can I know if the portal has already been stopped?

I found this: getConfigPortalActive()

My sketch now (ESP8266):

Sketch:

#include <Arduino.h>

/**
 * OnDemandNonBlocking.ino
 * example of running the webportal or configportal manually and non blocking
 * trigger pin will start a webportal for 120 seconds then turn it off.
 * startAP = true will start both the configportal AP and webportal
 */
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager

// include MDNS
#ifdef ESP8266
#include <ESP8266mDNS.h>
#elif defined(ESP32)
#include <ESPmDNS.h>
#endif

// select which pin will trigger the configuration portal when set to LOW
#define TRIGGER_PIN 0 // ESP8266 GPIO0 (NodeMCU D3)

#define LED_BUILTIN_D4 2 // ESP8266 GPIO2 (NodeMCU D4)

char const *portal_SSID = {"DEVICE1"};
char const *portal_PASSWORD = {"12345678"}; // Need >= 8 chars

WiFiManager wm;

unsigned int timeout = 120; // seconds to run for
unsigned int startTime = millis();
//bool portalRunning = false;
bool startAP = true; // start AP and webserver if true, else start only webserver

uint32_t millis1 = 0;
uint32_t millis2 = 0;

void doWiFiManager();

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);
  Serial.setDebugOutput(true);
  delay(1000);
  Serial.println("\n Starting");

  pinMode(TRIGGER_PIN, INPUT_PULLUP);
  pinMode(LED_BUILTIN_D4, OUTPUT);
  digitalWrite(LED_BUILTIN_D4, HIGH);

  wm.setConfigPortalTimeout(timeout);

  // wm.resetSettings();
  wm.setHostname("MDNSEXAMPLE");
  // wm.setEnableConfigPortal(false);
  wm.setConfigPortalBlocking(false);
  // wm.autoConnect();

  wm.autoConnect(portal_SSID, portal_PASSWORD);
}

void loop()
{
#ifdef ESP8266
  MDNS.update();
#endif
  doWiFiManager();
  // put your main code here, to run repeatedly:

  if (wm.getConfigPortalActive() == true)
  {
    if ((millis() - millis2) > 500)
    {
      millis2 = millis();

      digitalWrite(LED_BUILTIN_D4, !digitalRead(LED_BUILTIN_D4));
    }
  } else {
    if ((millis() - millis2) > 5000)
    {
      millis2 = millis();
    }

    if (((millis() - millis2) < 500))
    {
      digitalWrite(LED_BUILTIN_D4, LOW);
    } else if (((millis() - millis2) > 500) && ((millis() - millis2) < 1000))
    {
      digitalWrite(LED_BUILTIN_D4, HIGH);
    }
  }
}

void doWiFiManager()
{
  // is auto timeout portal running
  if (wm.getConfigPortalActive()) //portalRunning
  {
    wm.process(); // do processing

    // check for timeout
    if ((millis() - startTime) > (timeout * 1000))
    {
      Serial.println("portaltimeout");
      //portalRunning = false;
      if (startAP)
      {
        wm.stopConfigPortal();
      }
      else
      {
        wm.stopWebPortal();
      }
    }
  }

  // is configuration portal requested?
  if (digitalRead(TRIGGER_PIN) == LOW && (!wm.getConfigPortalActive())) // !portalRunning
  {
    // Minimum button press time to activate the portal
    if ((millis() - millis1) > 15000)
    {
      digitalWrite(LED_BUILTIN_D4, HIGH);

      // Wait for user to release button
      while(digitalRead(TRIGGER_PIN) == LOW) {
        yield();
      }

      if (startAP)
      {
        Serial.println("Button Pressed, Starting Config Portal");
        wm.resetSettings();
        wm.setConfigPortalBlocking(false);
        wm.startConfigPortal(portal_SSID, portal_PASSWORD);
      }
      else
      {
        Serial.println("Button Pressed, Starting Web Portal");
        wm.startWebPortal();
      }
      //portalRunning = true;
      startTime = millis();
    }
    else
    {
      digitalWrite(LED_BUILTIN_D4, LOW);
    }
  }
  else
  {
    millis1 = millis();
  }
}

(Now I think I can go to the part "auto update via email")

Thank you.

tablatronix commented 3 months ago

The ondemand part is non blocking but not autoconnect is not in that example, // wm.setConfigPortalBlocking(false);

I see the millis rollover issue, thanks

Looks like you figured the rest out. I suggest you read the .h file the super example and the wiki https://github.com/tzapu/WiFiManager/wiki/Methods

The actual readme still needs work as do general docs

rtek1000 commented 3 months ago

Hello, I would like to suggest that an example be made available (if possible) with a login to access the portal, it is the standard for almost all routers I have seen, I found a project that has this, perhaps it could serve as an example of how to implement it :

https://github.com/rtek1000/esp32-asyncwebserver-fileupload-example/tree/master/example-02

Login to access the portal: page_login

Additional page that a commercial router shows if the user is unable to log in: page_login2

info.zip

tablatronix commented 3 months ago

1669