[GH-ISSUE #1036] Can't add mDNS to OnDemandNonBlocking example #883

Closed
opened 2026-02-28 01:27:29 +03:00 by kerem · 29 comments
Owner

Originally created by @CoderUni on GitHub (Apr 10, 2020).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1036

Basic Infos

Hardware

WiFimanager Branch/Release:

  • Master
  • Development

Esp8266/Esp32:

  • ESP8266
  • ESP32

Hardware: ESP-12e, esp01, esp25

  • ESP01
  • ESP12 E/F/S (nodemcu, wemos, feather)
  • Other

ESP Core Version: 2.4.0, staging

  • 2.3.0
  • 2.4.0
  • staging (master/dev)

Description

I am trying out the OnDemandNonBlocking example in the development branch and everything works fine. I don't see any code in the example showing that the esp8266 is connected to the internet but instead it is written in the library. I also browsed through the library and pretty much got lost because there is too much code. How would I be able to add mDNS support on your OnDemandNonBlocking example? Thank you.

Originally created by @CoderUni on GitHub (Apr 10, 2020). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1036 ### Basic Infos #### Hardware **WiFimanager Branch/Release:** - [ ] Master - [X] Development **Esp8266/Esp32:** - [X] ESP8266 - [ ] ESP32 **Hardware: ESP-12e, esp01, esp25** - [ ] ESP01 - [X] ESP12 E/F/S (nodemcu, wemos, feather) - [ ] Other **ESP Core Version: 2.4.0, staging** - [ ] 2.3.0 - [X] 2.4.0 - [ ] staging (master/dev) ### Description I am trying out the OnDemandNonBlocking example in the development branch and everything works fine. I don't see any code in the example showing that the esp8266 is connected to the internet but instead it is written in the library. I also browsed through the library and pretty much got lost because there is too much code. How would I be able to add mDNS support on your OnDemandNonBlocking example? Thank you.
kerem 2026-02-28 01:27:29 +03:00
Author
Owner

@tablatronix commented on GitHub (Apr 10, 2020):

Set wm.hostname and use autoconnect, then service mdns in loop as you normally do.

Ill try to add an example, if you check the DEV example it has everything in it if you are interested, still gotta get docs made

<!-- gh-comment-id:612060954 --> @tablatronix commented on GitHub (Apr 10, 2020): Set wm.hostname and use autoconnect, then service mdns in loop as you normally do. Ill try to add an example, if you check the DEV example it has everything in it if you are interested, still gotta get docs made
Author
Owner

@tablatronix commented on GitHub (Apr 10, 2020):

Also check all the public functions in the .h file they all have comments.

<!-- gh-comment-id:612061708 --> @tablatronix commented on GitHub (Apr 10, 2020): Also check all the public functions in the .h file they all have comments.
Author
Owner

@CoderUni commented on GitHub (Apr 11, 2020):

@tablatronix Is there any docs on how to use wm.hostname? Sorry, I've never used it once.

<!-- gh-comment-id:612410920 --> @CoderUni commented on GitHub (Apr 11, 2020): @tablatronix Is there any docs on how to use wm.hostname? Sorry, I've never used it once.
Author
Owner

@tablatronix commented on GitHub (Apr 11, 2020):

    // set a custom hostname, sets sta and ap dhcp client id for esp32, and sta for esp8266

    bool          setHostname(const char * hostname);

DEV example has all options

<!-- gh-comment-id:612435737 --> @tablatronix commented on GitHub (Apr 11, 2020): ```C++ // set a custom hostname, sets sta and ap dhcp client id for esp32, and sta for esp8266 bool setHostname(const char * hostname); ``` DEV example has all options
Author
Owner

@CoderUni commented on GitHub (Apr 12, 2020):

Thanks but isn't auto connect blocking? If I use setHostname and autoconnect, it won't be OnDemandNonBlocking anymore.

<!-- gh-comment-id:612591254 --> @CoderUni commented on GitHub (Apr 12, 2020): Thanks but isn't auto connect blocking? If I use setHostname and autoconnect, it won't be OnDemandNonBlocking anymore.
Author
Owner

@tablatronix commented on GitHub (Apr 12, 2020):

Good point, maybe I can add some mdns helpers instead...

you can still use autoconnect.

    // if true (default) then start the config portal from autoConnect if connection failed
    void          setEnableConfigPortal(boolean enable);


    // if this is set, portal will be blocking and wait until save or exit, 
    // is false user must manually `process()` to handle config portal,
    // setConfigPortalTimeout is ignored in this mode, user is responsible for closing configportal
    void          setConfigPortalBlocking(boolean shouldBlock);
<!-- gh-comment-id:612617034 --> @tablatronix commented on GitHub (Apr 12, 2020): Good point, maybe I can add some mdns helpers instead... you can still use autoconnect. ```C++ // if true (default) then start the config portal from autoConnect if connection failed void setEnableConfigPortal(boolean enable); // if this is set, portal will be blocking and wait until save or exit, // is false user must manually `process()` to handle config portal, // setConfigPortalTimeout is ignored in this mode, user is responsible for closing configportal void setConfigPortalBlocking(boolean shouldBlock); ```
Author
Owner

@CoderUni commented on GitHub (Apr 13, 2020):

How can I make that autoconnect on demand as well? Sorry, I forgot to specifically state that it needs to be on demand as well.

<!-- gh-comment-id:612885658 --> @CoderUni commented on GitHub (Apr 13, 2020): How can I make that autoconnect on demand as well? Sorry, I forgot to specifically state that it needs to be on demand as well.
Author
Owner

@tablatronix commented on GitHub (Apr 13, 2020):

Same thing you were doing before, it doesnt change anything

<!-- gh-comment-id:612911551 --> @tablatronix commented on GitHub (Apr 13, 2020): Same thing you were doing before, it doesnt change anything
Author
Owner

@CoderUni commented on GitHub (Apr 16, 2020):

Sorry for asking again but I'm really stuck. How would I be able to implement OnDemand and NonBlocking at the same time while using mdns? Can you provide a documentation for this or an example code? Thank you.

<!-- gh-comment-id:614603139 --> @CoderUni commented on GitHub (Apr 16, 2020): Sorry for asking again but I'm really stuck. How would I be able to implement OnDemand and NonBlocking at the same time while using mdns? Can you provide a documentation for this or an example code? Thank you.
Author
Owner

@tablatronix commented on GitHub (Apr 16, 2020):

This might work, wm does not include mdnsh files for you, that might have been your problem
(it will if you edit the source files there is a define built in or define WM_MDNS in you build enviroment)

I added around 6 lines of code to the default example

UNTESTED


/**
 * OnDemandNonBlocking.ino WITH MDNS
 * example of running the webportal or configportal manually and non blocking
 * trigger pin will start a webportal for 120 seconds then turn it off.
 * startCP = 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

WiFiManager wm;

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

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.println("\n Starting");
  pinMode(TRIGGER_PIN, INPUT_PULLUP);

   wm.setHostname("MDNS_EXAMPLE_ESP");
   wm.setEnableConfigPortal(false);
   wm.autoConnect();
}

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

void doWiFiManager(){
  // is auto timeout portal running
  if(portalRunning){
    wm.process();
    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 && (!portalRunning)) {
    if(startAP){
      Serial.println("Button Pressed, Starting Config Portal");
      wm.setConfigPortalBlocking(false);
      wm.startConfigPortal();
    }  
    else{
      Serial.println("Button Pressed, Starting Web Portal");
      wm.startWebPortal();
    }  
    portalRunning = true;
    startTime = millis();
  }
}

<!-- gh-comment-id:614743627 --> @tablatronix commented on GitHub (Apr 16, 2020): This might work, wm does not include mdnsh files for you, that might have been your problem (it will if you edit the source files there is a define built in or define `WM_MDNS` in you build enviroment) I added around 6 lines of code to the default example **UNTESTED** ```c++ /** * OnDemandNonBlocking.ino WITH MDNS * example of running the webportal or configportal manually and non blocking * trigger pin will start a webportal for 120 seconds then turn it off. * startCP = 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 WiFiManager wm; unsigned int timeout = 120; // seconds to run for unsigned int startTime = millis(); bool portalRunning = false; bool startAP = false; // start AP and webserver if true, else start only webserver 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.println("\n Starting"); pinMode(TRIGGER_PIN, INPUT_PULLUP); wm.setHostname("MDNS_EXAMPLE_ESP"); wm.setEnableConfigPortal(false); wm.autoConnect(); } void loop() { MDNS.update(); doWiFiManager(); // put your main code here, to run repeatedly: } void doWiFiManager(){ // is auto timeout portal running if(portalRunning){ wm.process(); 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 && (!portalRunning)) { if(startAP){ Serial.println("Button Pressed, Starting Config Portal"); wm.setConfigPortalBlocking(false); wm.startConfigPortal(); } else{ Serial.println("Button Pressed, Starting Web Portal"); wm.startWebPortal(); } portalRunning = true; startTime = millis(); } } ```
Author
Owner

@tablatronix commented on GitHub (Apr 17, 2020):

were you able to test this ?

<!-- gh-comment-id:615337593 --> @tablatronix commented on GitHub (Apr 17, 2020): were you able to test this ?
Author
Owner

@CoderUni commented on GitHub (Apr 18, 2020):

were you able to test this ?

Sorry for late reply, not yet. I will notify you as soon as I test it. Thanks for your solution.

<!-- gh-comment-id:615867041 --> @CoderUni commented on GitHub (Apr 18, 2020): > > > were you able to test this ? Sorry for late reply, not yet. I will notify you as soon as I test it. Thanks for your solution.
Author
Owner

@tablatronix commented on GitHub (Apr 18, 2020):

No prob

<!-- gh-comment-id:615978167 --> @tablatronix commented on GitHub (Apr 18, 2020): No prob
Author
Owner

@CoderUni commented on GitHub (Apr 19, 2020):

It doesn't work for me. It says unable to connect to it.

<!-- gh-comment-id:616122001 --> @CoderUni commented on GitHub (Apr 19, 2020): It doesn't work for me. It says unable to connect to it.
Author
Owner

@tablatronix commented on GitHub (Apr 20, 2020):

so ping hostname.local does nothing ?

<!-- gh-comment-id:616692836 --> @tablatronix commented on GitHub (Apr 20, 2020): so ping hostname.local does nothing ?
Author
Owner

@CoderUni commented on GitHub (May 9, 2020):

@tablatronix Sorry for late reply but it does nothing.

<!-- gh-comment-id:626089736 --> @CoderUni commented on GitHub (May 9, 2020): @tablatronix Sorry for late reply but it does nothing.
Author
Owner

@tablatronix commented on GitHub (May 9, 2020):

I see what I did wrong, let me figure something out, this only would work if there were creds saved

<!-- gh-comment-id:626098223 --> @tablatronix commented on GitHub (May 9, 2020): I see what I did wrong, let me figure something out, this only would work if there were creds saved
Author
Owner

@tablatronix commented on GitHub (May 9, 2020):

hmm this works for me only testing on sta mode, not sure is mdns works in softap

I am having trouble testing because my router does NOT multicast across wired to wireless networks.

/**
 * 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.
 * startCP = 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

WiFiManager wm;

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

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

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

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

void doWiFiManager(){
  // is auto timeout portal running
  if(portalRunning){
    wm.process();
    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 && (!portalRunning)) {
    if(startAP){
      Serial.println("Button Pressed, Starting Config Portal");
      wm.setConfigPortalBlocking(false);
      wm.startConfigPortal();
    }  
    else{
      Serial.println("Button Pressed, Starting Web Portal");
      wm.startWebPortal();
    }  
    portalRunning = true;
    startTime = millis();
  }
}

<!-- gh-comment-id:626104304 --> @tablatronix commented on GitHub (May 9, 2020): hmm this works for me only testing on sta mode, not sure is mdns works in softap I am having trouble testing because my router does NOT multicast across wired to wireless networks. ```C++ /** * 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. * startCP = 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 WiFiManager wm; unsigned int timeout = 120; // seconds to run for unsigned int startTime = millis(); bool portalRunning = false; bool startAP = false; // start AP and webserver if true, else start only webserver 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); // wm.resetSettings(); wm.setHostname("MDNSEXAMPLE"); // wm.setEnableConfigPortal(false); // wm.setConfigPortalBlocking(false); wm.autoConnect(); } void loop() { MDNS.update(); doWiFiManager(); // put your main code here, to run repeatedly: } void doWiFiManager(){ // is auto timeout portal running if(portalRunning){ wm.process(); 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 && (!portalRunning)) { if(startAP){ Serial.println("Button Pressed, Starting Config Portal"); wm.setConfigPortalBlocking(false); wm.startConfigPortal(); } else{ Serial.println("Button Pressed, Starting Web Portal"); wm.startWebPortal(); } portalRunning = true; startTime = millis(); } } ```
Author
Owner

@CoderUni commented on GitHub (May 11, 2020):

it still doesn't work for some reason. I can get in using the local ip while it is in sta mode but then using mdnsexample.local doesn't seem to work

<!-- gh-comment-id:626445827 --> @CoderUni commented on GitHub (May 11, 2020): it still doesn't work for some reason. I can get in using the local ip while it is in sta mode but then using mdnsexample.local doesn't seem to work
Author
Owner

@tablatronix commented on GitHub (May 11, 2020):

Are you sure your network domain is .local ? or you are on the same wifi ?

If you are on wired, mdns may fail across segments, some broadband routers have alt dns domains, mine has attlocal.net for example.

You might need to check a bonjour explorer app to see

<!-- gh-comment-id:626455607 --> @tablatronix commented on GitHub (May 11, 2020): Are you sure your network domain is .local ? or you are on the same wifi ? If you are on wired, mdns may fail across segments, some broadband routers have alt dns domains, mine has attlocal.net for example. You might need to check a bonjour explorer app to see
Author
Owner

@CoderUni commented on GitHub (May 11, 2020):

Ok I will check it. I am on the same wifi as the esp8266.

<!-- gh-comment-id:626752889 --> @CoderUni commented on GitHub (May 11, 2020): Ok I will check it. I am on the same wifi as the esp8266.
Author
Owner

@CoderUni commented on GitHub (May 16, 2020):

I keep on trying and it still doesn't work. Entering the sta ip works but not the mdns.

<!-- gh-comment-id:629596725 --> @CoderUni commented on GitHub (May 16, 2020): I keep on trying and it still doesn't work. Entering the sta ip works but not the mdns.
Author
Owner

@tablatronix commented on GitHub (May 16, 2020):

Ok ill upload this example and start from scratch. You can try that

<!-- gh-comment-id:629696454 --> @tablatronix commented on GitHub (May 16, 2020): Ok ill upload this example and start from scratch. You can try that
Author
Owner

@tablatronix commented on GitHub (May 16, 2020):

What os are you on?

<!-- gh-comment-id:629697961 --> @tablatronix commented on GitHub (May 16, 2020): What os are you on?
Author
Owner

@tablatronix commented on GitHub (May 18, 2020):

I added mdns to that example are you using that?

<!-- gh-comment-id:630453811 --> @tablatronix commented on GitHub (May 18, 2020): I added mdns to that example are you using that?
Author
Owner

@tablatronix commented on GitHub (May 19, 2020):

I have no idea how this used to work but it does not now..

#ifdef ESP8266MDNS_H

Does not work to check if mdns was included..

I have to fix this, precompiler directives are so annoying.

<!-- gh-comment-id:630596120 --> @tablatronix commented on GitHub (May 19, 2020): I have no idea how this used to work but it does not now.. #ifdef ESP8266MDNS_H Does not work to check if mdns was included.. I have to fix this, precompiler directives are so annoying.
Author
Owner

@CoderUni commented on GitHub (May 19, 2020):

Sorry for really late replies. I rarely check my github. I'm using my nodemcu and im using windows 10.

<!-- gh-comment-id:630649860 --> @CoderUni commented on GitHub (May 19, 2020): Sorry for really late replies. I rarely check my github. I'm using my nodemcu and im using windows 10.
Author
Owner

@tablatronix commented on GitHub (May 19, 2020):

Everytime i think I understand precprocessor macros, ill never get it.

Why can I not do

#include <ESP8266mDNS.h>

#ifdef ESP8266MDNS_H
#endif

I dont get it

<!-- gh-comment-id:630894920 --> @tablatronix commented on GitHub (May 19, 2020): Everytime i think I understand precprocessor macros, ill never get it. Why can I not do ``` #include <ESP8266mDNS.h> #ifdef ESP8266MDNS_H #endif ``` I dont get it
Author
Owner

@CoderUni commented on GitHub (May 20, 2020):

I am not good with writing libraries but then did you include it in your .h file? I usually log every action to the console so its easier to debug whats happening.

<!-- gh-comment-id:631181861 --> @CoderUni commented on GitHub (May 20, 2020): I am not good with writing libraries but then did you include it in your .h file? I usually log every action to the console so its easier to debug whats happening.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#883
No description provided.