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

Can't add mDNS to OnDemandNonBlocking example #1036

Closed
4 of 10 tasks
CoderUni opened this issue Apr 10, 2020 · 29 comments
Closed
4 of 10 tasks

Can't add mDNS to OnDemandNonBlocking example #1036

CoderUni opened this issue Apr 10, 2020 · 29 comments
Labels
bug Validated BUG Documentation Flagged for docs Question User Question member to member support

Comments

@CoderUni
Copy link

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.

@tablatronix tablatronix added Documentation Flagged for docs Question User Question member to member support labels Apr 10, 2020
@tablatronix
Copy link
Collaborator

tablatronix commented 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

@tablatronix
Copy link
Collaborator

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

@CoderUni
Copy link
Author

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

@tablatronix
Copy link
Collaborator

tablatronix commented 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

@CoderUni
Copy link
Author

CoderUni commented Apr 12, 2020

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

@tablatronix
Copy link
Collaborator

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

@CoderUni
Copy link
Author

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.

@tablatronix
Copy link
Collaborator

Same thing you were doing before, it doesnt change anything

@CoderUni
Copy link
Author

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.

@tablatronix
Copy link
Collaborator

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

@tablatronix
Copy link
Collaborator

were you able to test this ?

@CoderUni
Copy link
Author

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.

@tablatronix
Copy link
Collaborator

No prob

@CoderUni
Copy link
Author

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

@tablatronix
Copy link
Collaborator

so ping hostname.local does nothing ?

@CoderUni
Copy link
Author

CoderUni commented May 9, 2020

@tablatronix Sorry for late reply but it does nothing.

@tablatronix
Copy link
Collaborator

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

@tablatronix
Copy link
Collaborator

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

@CoderUni
Copy link
Author

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

@tablatronix
Copy link
Collaborator

tablatronix commented 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

@CoderUni
Copy link
Author

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

@CoderUni
Copy link
Author

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

@tablatronix
Copy link
Collaborator

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

@tablatronix
Copy link
Collaborator

What os are you on?

@tablatronix
Copy link
Collaborator

I added mdns to that example are you using that?

@tablatronix
Copy link
Collaborator

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.

@CoderUni
Copy link
Author

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

@tablatronix
Copy link
Collaborator

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

@CoderUni
Copy link
Author

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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Validated BUG Documentation Flagged for docs Question User Question member to member support
Projects
None yet
Development

No branches or pull requests

2 participants