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

mDNS problem #7210

Closed
BbIKTOP opened this issue Apr 12, 2020 · 5 comments
Closed

mDNS problem #7210

BbIKTOP opened this issue Apr 12, 2020 · 5 comments
Assignees
Milestone

Comments

@BbIKTOP
Copy link
Contributor

BbIKTOP commented Apr 12, 2020

I've been told to open an issue about it, so here it is: (edited by maintainer)

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>


char *wifiNet = "mywifi";
char *wifiPassword = "mywifipasswd";

char *apName = "box";
char *apPassword = "box";
char *apIp = "10.1.1.1";
char *apMask = "255.255.255.0";


void setup()
{
  int res;

  Serial.begin(115200);
  Serial.println("\nStarting");

  WiFi.persistent(false);
  WiFi.disconnect();

  WiFi.mode(WIFI_AP_STA);

  IPAddress localIp;
  IPAddress localMask;
  IPAddress localGw;

  localIp.fromString(apIp);
  localMask.fromString(apMask);
  localGw.fromString(apIp);

  res = WiFi.softAPConfig(localIp, localGw, localMask);
  Serial.printf("After softAPConfig: %d\n", res);

  res = WiFi.softAP(apName, apPassword, 2, false, 20);
  Serial.printf("After softAP: %d\n", res);

  Serial.printf("After softAP IP: %s\n", WiFi.softAPIP().toString().c_str());

  WiFi.hostname(apName);
  WiFi.setAutoReconnect(true);
  WiFi.begin();

  if (!MDNS.begin(apName))
  {
    Serial.printf("Cannot start mDNS responder\n");
  }
  else
  {
    Serial.printf("mDNS responder started\n");
  }
  MDNS.addService("http", "tcp", 80);

  Serial.println("Started");
}

void loop()
{
  static int counter = 0;
  static int counter2 = 0;

  MDNS.update();

  if (counter++ > 10)
  {
    static int lastStatus = WiFi.status();

    if (WiFi.status() == WL_IDLE_STATUS)
    {
      Serial.println("Reconnecting...");
      WiFi.begin(wifiNet, wifiPassword);
      WiFi.reconnect();
    }

    if (WiFi.status() == WL_CONNECTED && lastStatus != WL_CONNECTED)
    {
      Serial.printf("Connected, IP is: %s\n", WiFi.localIP().toString().c_str());
      counter = 0;

      MDNS.end();
      if (!MDNS.begin(apName))
      {
        Serial.printf("Cannot restart mDNS responder\n");
      }
      else
      {
        Serial.printf("mDNS responder restarted\n");
      }
      MDNS.addService("http", "tcp", 80);
    }
    lastStatus = WiFi.status();
  }

  if (counter2++ > 10)
  {
    int sc = MDNS.queryService("http", "tcp");
    Serial.printf("MDNS: %d services found\n", sc);
    counter2 = 0;
  }

  delay(1000);
}

Produces

Starting
After softAPConfig: 1
After softAP: 0
After softAP IP: 10.1.1.1
mDNS responder started
Started
Reconnecting...
MDNS: 0 services found
Connected, IP is: 172.16.5.79
mDNS responder restarted
MDNS: 0 services found
MDNS: 0 services found
MDNS: 0 services found
MDNS: 0 services found
MDNS: 0 services found
MDNS: 0 services found
MDNS: 0 services found
MDNS: 0 services found
MDNS: 0 services found
MDNS: 0 services found

And no services are visible in service discovery like dns-sd -B

viktor@Work:~ $ telnet box.local 80
box.local: nodename nor servname provided, or not known
viktor@Work:~ $

Versions:

$ platformio update
Updating tool-unity                      @ 1.20500.200322 [Up-to-date]
Updating contrib-piohome                 @ 3.1.1          [Up-to-date]
Updating tool-scons                      @ 2.20501.191222 [Up-to-date]
Updating contrib-pysite                  @ 2.27.191017    [Up-to-date]

Platform Manager
================
Platform Espressif 8266
--------
Updating espressif8266                   @ 2.4.0          [Up-to-date]
Updating tool-esptoolpy                  @ 1.20800.0      [Up-to-date]
Updating tool-mkspiffs                   @ 1.200.0        [Up-to-date]
Updating tool-esptool                    @ 1.413.0        [Up-to-date]
Updating framework-arduinoespressif8266  @ 3.20603.200130 [Up-to-date]
Updating toolchain-xtensa                @ 2.40802.191122 [Up-to-date]


Library Manager
===============
Library Storage: /Users/viktor/.platformio/lib
Updating ESPAsyncTCP                     @ 1.2.2          [Up-to-date]

A quick update:

  1. Downloaded latest arduino 1.8.12
  2. Made a clean install
  3. Added https://arduino.esp8266.com/stable/package_esp8266com_index.json to the "Additional Boards manager URLs"
  4. Installed esp8266 2.6.3
  5. Tested this sketch and observed the same result

image

viktor@Work:~ $ telnet box.local 80
box.local: nodename nor servname provided, or not known
viktor@Work:~ $
@d-a-v
Copy link
Collaborator

d-a-v commented Apr 12, 2020

Well now I read it through, the issue is the following:

Starting from 2.6.x, mDNS runs on only one interface.
When both AP+STA are enabled,
it is preferred to give mDNS which interface it is to run on:

  • In 2.6.3, default interface was AP.
  • In current master and future 2.7.0, STA is used.

In both case, giving the interface you want works:

if (!MDNS.begin(apName, WiFi.localIP())) // start on STA

A second mDNS instance must be created to have it run (independently) on both interfaces.

@BbIKTOP
Copy link
Contributor Author

BbIKTOP commented Apr 14, 2020

Yes, thank you. I have checked this lwip implementation and found that it cannot be done automatically. So, second instance.

To be able to create second instance you'd probably need to replace
lib/ESP8266mDNS/src/LEAmDNS_Helpers.cpp

if (m_pUDPContext->listen(IP4_ADDR_ANY, DNS_MQUERY_PORT))

to something like

if (m_pUDPContext->listen(ip_2_ip4(&m_netif->ip_addr), DNS_MQUERY_PORT))

To prevent err -8 ERR_USE

Also, is there a way to determine is the instance "began" or not? In case begin has not been called and MDNSResponder::queryService called for that instance, it produces Exception (28)

@devyte devyte modified the milestones: 2.7.1, 2.7.2 May 4, 2020
@d-a-v d-a-v self-assigned this May 11, 2020
@d-a-v
Copy link
Collaborator

d-a-v commented Jun 21, 2020

@BbIKTOP This issue is still opened.
@LaborEtArs provided us with a new version of mDNS, which is currently in an adaptation process, with @hreintke, to make it interface-agnostic. It is not yet a PR but you can try it there.
(This version is likely to replace #7335)

edit: try it:

$ git checkout master
$ git pull   upstream-or-origin-or-whatever-remote-name-is   master
$ git checkout -b LEAmDNS2x
$ git remote add mdns https://github.com/Labor-Et-Ars/ESP8266-Arduino.git
$ git pull mdns LEAmDNS2x

then (edited)

  • declare and use clsLEAMDNSHost MDNS; in a sketch that is using AP and STA,
  • enable IPv6 if you feel brave
  • try to ping your "hostname".local from both AP and STA network
  • use avahi-browse on linux, or an mdns app like Service Browser on android (tbd: macos/ios and windows) to check for published services

@devyte
Copy link
Collaborator

devyte commented Jul 6, 2020

The updated MDNS code is still showing issues and requires more work. Per internal discussions, pushing back to v3.

@devyte devyte modified the milestones: 2.7.2, 3.0.0 Jul 6, 2020
@d-a-v
Copy link
Collaborator

d-a-v commented Nov 9, 2020

mDNS in current master should solve this issue. Please open a new one if that's not the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants