tzapu / WiFiManager

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

Can't add mDNS to OnDemandNonBlocking example #1036

Closed CoderUni closed 3 years ago

CoderUni commented 4 years ago

Basic Infos

Hardware

WiFimanager Branch/Release:

Esp8266/Esp32:

Hardware: ESP-12e, esp01, esp25

ESP Core Version: 2.4.0, staging

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

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

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

CoderUni commented 4 years ago

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

tablatronix commented 4 years ago
    // 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 commented 4 years ago

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

tablatronix commented 4 years ago

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

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

Same thing you were doing before, it doesnt change anything

CoderUni commented 4 years ago

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

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

were you able to test this ?

CoderUni commented 4 years ago

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

No prob

CoderUni commented 4 years ago

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

tablatronix commented 4 years ago

so ping hostname.local does nothing ?

CoderUni commented 4 years ago

@tablatronix Sorry for late reply but it does nothing.

tablatronix commented 4 years ago

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

tablatronix commented 4 years ago

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

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

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

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

CoderUni commented 4 years ago

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

tablatronix commented 4 years ago

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

tablatronix commented 4 years ago

What os are you on?

tablatronix commented 4 years ago

I added mdns to that example are you using that?

tablatronix commented 4 years ago

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

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

tablatronix commented 4 years ago

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

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.