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

Custom configuration, on demand server, and invoking web server for future config updates #1743

Open maneeshblr opened 1 month ago

maneeshblr commented 1 month ago

Basic Infos

I have the autoConnect() working. Then, I am running the config portal for configuration of custom parameters. However, I cannot figure how to get the local IP hyperlink so that I can click to get to the local IP to run the configuration in a browser. Additionally, I would like to see the status of the MQTT connection, which is configured using custom parameters. Lastly, once the MQTT configuration is done, would like to have a way to update the parameters. So, how to invoke the web server? If it must be run continuously, then I should protect with HTTP auth.

Hardware

WiFimanager Branch/Release: 2.0.17

Esp8266/Esp32: ESP8266

Hardware: esp01

Core Version: 3.1.2

Description

Here is what I am trying to do:

  1. Use WiFiManager to start a WiFi AP and configure the SSID/password for WiFi router
  2. MQTT parameters are in a page of their own with a save button. However, it is possible that the MQTT parameters are configured only later, and not at the same time when the SSID/password is configured.
  3. To allow the custom configuration, the config portal is started manually for MQTT configuration.

The above is working - the configuration pages come up and MQTT config can be saved. At the bottom of the page, it says "connected to AP with IP 192.168.1.25

Limitations

  1. The MQTT parameters may need to be changed if there is a mistake or a typo or server info changes. In such case, there is no clear way to restart the configuration portal, if the device is installed in a hard to reach place and no button can be pressed.
  2. The MQTT parameters are on the "captive portal" (as in the page that comes up after connecting to SoftAP of the ESP device. Ideally, I would like to load the "web portal" at 192.168.1.25 and configure MQTT parameters there. For that, I think I can use "wm.startWebPortal();" after the autoConnect call. But I cannot easily know the IP address "192.168.1.25", without relying on mDNS.

So, here is what I request help with:

  1. How to see the local IP address in captive portal pages, and ideally, add a hyperlink that can be clicked to get to it.
  2. How to see that MQTT has connected. It would be good to add a "connected" type status in the page itself.
  3. How to start the web server without pressing any button for any subsequent custom parameter changes (and not the WiFi credentials)
  4. How to add HTTP auth on the web portal pages. I guess this can be done by changing false to true in "bool testauth = false;" line. Correct?

Please guide. Thank you!

Settings in IDE

Module: ESP8266 Generic Module

Additional libraries: WiFiManager, LittleFS, PubSubClient, NTPClient

Sketch

#BEGIN
bool saveConfig = false;
char mqttServer[40] = "mqttserver.com";
char mqttTopic[40] = "sensors/mysensor";
bool portalRunning = false;
WiFiManager wm;

void saveConfigCallback () {
  saveConfig = true;
}

void configModeCallback (WiFiManager *wm) {
    Serial.println("Entered Configuration Mode"); 
    Serial.print("SSID: ");
    Serial.println(wm->getConfigPortalSSID());
}

void setup(){
  WiFi.mode(WIFI_STA);
  wm.setHostname(WIFI_SSID);
  wm.setSaveParamsCallback(saveConfigCallback);  // callback for save on custom parameter page
  wm.setAPCallback(configModeCallback);
  wm.setParamsPage(true);
  loadSettings(); // loads custom parameters from a JSON file in LittleFS 
  WiFiManagerParameter customMqttServer("server", "MQTT Server", mqttServer, 40);
  WiFiManagerParameter customMqttTopic("topic", "MQTT Topic", mqttTopic, 40);   
  wm.addParameter(&customMqttServer); 
  wm.addParameter(&customMqttTopic);
  wifiStatus = wm.autoConnect(WIFI_SSID, WIFI_PASSWD);
  if (!wifiStatus){
    delay(5000);
    ESP.restart();
  }
  wm.setHostname(WIFI_SSID);
  wm.startConfigPortal(WIFI_SSID, WIFI_PASSWD);
  portalRunning = true;
  if (saveConfig){
    strcpy(mqttServer, customMqttServer.getValue());
    strcpy(mqttTopic, customMqttTopic.getValue());
    saveSettings();
 }

void loop()
{
  if (portalRunning){
    wm.process();
  }
 // other code... 
 }
#END

Debug Messages

{"mqttServer":"mqttserver.com","mqttTopic":"sensors/mysensor2"}
Parsed config json
*wm:[3] Added Parameter: server
*wm:[3] Added Parameter: topic
*wm:[2] AutoConnect 
*wm:[3] Setting Hostnames:  ESP
*wm:[3] Setting WiFi hostname 
*wm:[3] Connecting as wifi client... 
*wm:[3] setSTAConfig static ip not set, skipping 
*wm:[2] Connecting to SAVED AP: Skywalker
*wm:[2] connectTimeout not set, ESP waitForConnectResult... 
*wm:[3] Connection result: WL_CONNECTED
*wm:[2] AutoConnect: SUCCESS 
*wm:[3] Connected in 2446 ms
*wm:[2] STA IP Address: 192.168.1.25
*wm:[3] Starting Config Portal 
*wm:[3] AccessPoint set password is VALID 
*wm:[3] Enabling AP 
*wm:[2] StartAP with SSID:  ESP
*wm:[2] AP IP address: 192.168.4.1
*wm:[3] [CB] _apcallback calling 
Entered Configuration Mode
SSID: ESP
*wm:[2] Starting Web Portal 
*wm:[3] HTTP server started 
*wm:[3] Config Portal Running, blocking, waiting for clients... 
*wm:[3] Portal Timeout In 120 seconds