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 problems (IDFGH-14160) #14962

Open
3 tasks done
pavel808 opened this issue Dec 1, 2024 · 7 comments
Open
3 tasks done

MDNS problems (IDFGH-14160) #14962

pavel808 opened this issue Dec 1, 2024 · 7 comments
Labels
Status: Opened Issue is new

Comments

@pavel808
Copy link

pavel808 commented Dec 1, 2024

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

General issue report

I am trying to get mdns working for my project. I am referencing the following documentation :

https://docs.espressif.com/projects/esp-protocols/mdns/docs/latest/en/index.html

About my project. It is an application which runs on an ESP32-S3 on an ESP Thread Borer Router board.
The application has a HTTP server running which allows clients like CURL to communicate with it over Wi-Fi.

Obviously having a hostname assigned would be the best so that the HTTP client doesn't have to remember the local IP address every time. I am trying to set the hostname as "server".

Here is my below code in my app_main.cppand also some of the terminal output when my application starts.

I start the mdns service before the HTTP server.
From the logs it appears to set the hostname ok. Regardless, it is not possible to ping the hostname from anywhere on the same local network.

I also notice that on my router, I can see that hostname "espressif" gets assigned, via the sdkconfig file : CONFIG_LWIP_LOCAL_HOSTNAME="espressif"

Any help with this would be greatly appreciated. Thanks.

#include <esp_err.h>
#include <esp_log.h>
#include <nvs_flash.h>

#include <esp_matter.h>
#include <esp_matter_console.h>
#include <esp_matter_controller_client.h>
#include <esp_matter_controller_console.h>
#include <esp_matter_controller_utils.h>
#include <esp_matter_ota.h>
#include <esp_http_server.h>
#if CONFIG_OPENTHREAD_BORDER_ROUTER
#include <esp_openthread_border_router.h>
#include <esp_openthread_lock.h>
#include <esp_ot_config.h>
#include <esp_spiffs.h>
#include <platform/ESP32/OpenthreadLauncher.h>
#endif // CONFIG_OPENTHREAD_BORDER_ROUTER
#include <app_reset.h>
#include <common_macros.h>

#include <app/server/Server.h>
#include <credentials/FabricTable.h>

#include "protocol_examples_common.h"

#include "lwip/err.h"
#include "lwip/sys.h"

#include "mdns.h"

static const char *TAG = "app_main";
uint16_t switch_endpoint_id = 0;

using namespace esp_matter;
using namespace esp_matter::attribute;
using namespace esp_matter::endpoint;

extern void initialize_device_controller();

httpd_handle_t start_webserver();

static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg)
{
    switch (event->Type)
    {
    case chip::DeviceLayer::DeviceEventType::PublicEventTypes::kInterfaceIpAddressChanged:
        ESP_LOGI(TAG, "Interface IP Address changed");
        break;
    case chip::DeviceLayer::DeviceEventType::kESPSystemEvent:
        if (event->Platform.ESPSystemEvent.Base == IP_EVENT &&
            event->Platform.ESPSystemEvent.Id == IP_EVENT_STA_GOT_IP)
        {
#if CONFIG_OPENTHREAD_BORDER_ROUTER
            static bool sThreadBRInitialized = false;
            if (!sThreadBRInitialized)
            {
                esp_openthread_set_backbone_netif(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
                esp_openthread_lock_acquire(portMAX_DELAY);
                esp_openthread_border_router_init();
                esp_openthread_lock_release();
                sThreadBRInitialized = true;
            }
#endif
        }
        break;
    default:
        break;
    }
}

void start_mdns_service()
{
    //initialize mDNS service
    esp_err_t err = mdns_init();
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "MDNS Init failed: %s", esp_err_to_name(err));
        return;
    }

    ESP_LOGI(TAG, "MDNS Init OK");
    //set hostname
    err = mdns_hostname_set("server");
    if (err != ESP_OK)
    {
    	 ESP_LOGE(TAG, "MDNS Hostname Set fail : %s", esp_err_to_name(err));
    	 return;
    }

    ESP_LOGI(TAG, "MDNS mdns_hostname_set ok");
    //set default instance
    mdns_instance_name_set("Audio Room Edge Server");
    if (err != ESP_OK)
	{
		 ESP_LOGE(TAG, "MDNS instance name set fail : %s", esp_err_to_name(err));
		 return;
	}

    ESP_LOGI(TAG, "MDNS instance name set ok");
}

extern "C" void app_main()
{
	 esp_err_t err = ESP_OK;

	    /* Initialize the ESP NVS layer */
	    nvs_flash_init();
	#if CONFIG_ENABLE_CHIP_SHELL
	    esp_matter::console::diagnostics_register_commands();
	    esp_matter::console::wifi_register_commands();
	    esp_matter::console::init();
	#if CONFIG_ESP_MATTER_CONTROLLER_ENABLE
	    esp_matter::console::controller_register_commands();
	#endif // CONFIG_ESP_MATTER_CONTROLLER_ENABLE
	#ifdef CONFIG_OPENTHREAD_BORDER_ROUTER
	    esp_matter::console::otcli_register_commands();
	#endif // CONFIG_OPENTHREAD_BORDER_ROUTER
	#endif // CONFIG_ENABLE_CHIP_SHELL
	#ifdef CONFIG_OPENTHREAD_BORDER_ROUTER
	#ifdef CONFIG_AUTO_UPDATE_RCP
	    esp_vfs_spiffs_conf_t rcp_fw_conf = {
	        .base_path = "/rcp_fw", .partition_label = "rcp_fw", .max_files = 10, .format_if_mount_failed = false};
	    if (ESP_OK != esp_vfs_spiffs_register(&rcp_fw_conf)) {
	        ESP_LOGE(TAG, "Failed to mount rcp firmware storage");
	        return;
	    }
	    esp_rcp_update_config_t rcp_update_config = ESP_OPENTHREAD_RCP_UPDATE_CONFIG();
	    openthread_init_br_rcp(&rcp_update_config);
	#endif
	    /* Set OpenThread platform config */
	    esp_openthread_platform_config_t config = {
	        .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
	        .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
	        .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
	    };
	    set_openthread_platform_config(&config);
	#endif // CONFIG_OPENTHREAD_BORDER_ROUTER
	    /* Matter start */
	    err = esp_matter::start(app_event_cb);
	    ABORT_APP_ON_FAILURE(err == ESP_OK, ESP_LOGE(TAG, "Failed to start Matter, err:%d", err));

	#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE
	    esp_matter::lock::chip_stack_lock(portMAX_DELAY);
	    esp_matter::controller::matter_controller_client::get_instance().init(112233, 1, 5580);
	    esp_matter::controller::matter_controller_client::get_instance().setup_commissioner();
	    esp_matter::lock::chip_stack_unlock();
	#endif // CONFIG_ESP_MATTER_COMMISSIONER_ENABLE

    static httpd_handle_t server = NULL;

    initialize_device_controller();

    start_mdns_service();

    ESP_LOGI(TAG, "Calling start_webserver()");
    server = start_webserver();

    while (server)
    {
	sleep(5);
    }
}

Here is some of the terminal output from the application on startup :

I (2290) chip[TS]: Committing Last Known Good Time to storage: 2023-10-14T01:16:48
I (2290) chip[CTL]: Joined the fabric at index 1. Fabric ID is 0x0000000000000001 (Compressed Fabric ID: AFB55A8B484802DB)
I (2290) chip[DIS]: Updating services using commissioning mode 0
E (2290) mdns: mdns_service_remove_for_host(6377): Invalid state or arguments
E (2290) mdns: mdns_service_remove_for_host(6377): Invalid state or arguments
E (2290) mdns: mdns_service_remove_for_host(6377): Invalid state or arguments
I (2290) chip[DIS]: Advertise operational node AFB55A8B484802DB-000000000001B669
I (2300) chip[DIS]: Advertise commission parameter vendorID=65521 productID=32768 discriminator=3840/15 cm=0 cp=0
I (2300) chip[DIS]: Advertise commission parameter vendorID=65521 productID=32768 discriminator=0000/00 cm=0 cp=0
I (2390) device_controller: initialize_device_controller()
I (2390) chip[DL]: HandlePlatformSpecificBLEEvent 32786
I (2390) chip[DIS]: Updating services using commissioning mode 0
I (2390) chip[DIS]: Advertise operational node AFB55A8B484802DB-000000000001B669
I (2400) chip[DIS]: Advertise commission parameter vendorID=65521 productID=32768 discriminator=3840/15 cm=0 cp=0
I (2400) chip[DIS]: Advertise commission parameter vendorID=65521 productID=32768 discriminator=0000/00 cm=0 cp=0
I (2400) app_main: MDNS Init OK
I (2400) app_main: MDNS mdns_hostname_set ok
I (2400) app_main: MDNS instance name set ok
I (2400) app_main: Calling start_webserver()
I (2400) web_server: Starting server on port: '80'
I (2410) web_server: Registering URI handlers
W (2410) httpd_uri: httpd_register_uri_handler: no slots left for registering handler
W (2410) httpd_uri: httpd_register_uri_handler: no slots left for registering handler
W (2410) httpd_uri: httpd_register_uri_handler: no slots left for registering handler
W (2410) httpd_uri: httpd_register_uri_handler: no slots left for registering handler
I (3300) chip[DL]: Posting ESPSystemEvent: Wifi Event with eventId : 5
I (3310) web_server: Stopping webserver
E (3310) httpd: Failed to send shutdown signal err=-1
E (3310) web_server: Failed to stop http server
I(27270) OPENTHREAD:[N] Mle-----------: RLOC16 2c00 -> fffe
I(27630) OPENTHREAD:[N] Mle-----------: Attach attempt 1, AnyPartition reattaching with Active Dataset
I(34220) OPENTHREAD:[N] RouterTable---: Allocate router id 11
I(34240) OPENTHREAD:[N] Mle-----------: RLOC16 fffe -> 2c00
I(34250) OPENTHREAD:[N] Mle-----------: Role detached -> leader
I(34260) OPENTHREAD:[N] Mle-----------: Partition ID 0x262e9573
I (34300) OPENTHREAD: Platform UDP bound to port 49153
I (121610) chip[DL]: Posting ESPSystemEvent: IP Event with eventId : 1
@espressif-bot espressif-bot added the Status: Opened Issue is new label Dec 1, 2024
@github-actions github-actions bot changed the title MDNS problems MDNS problems (IDFGH-14160) Dec 1, 2024
@nopnop2002
Copy link

You can check the mDNS status with the command below.

$ avahi-browse --all
+ enp2s0 IPv6 VOYAGER                                       Microsoft Windows Network local
+ enp2s0 IPv4 VOYAGER                                       Microsoft Windows Network local
+ enp2s0 IPv4 landisk-HDL-AA1(smb)                          Microsoft Windows Network local
+ enp2s0 IPv6 VOYAGER                                       _device-info._tcp    local
+ enp2s0 IPv4 VOYAGER                                       _device-info._tcp    local
+ enp2s0 IPv4 esp32-server                                  Web Site             local
+ enp2s0 IPv4 landisk-HDL-AA1                               Web Site             local
+ enp2s0 IPv4 landisk-HDL-AA1 [34:76:c5:71:e0:6d]           Workstation          local


$ avahi-resolve --name esp32-server.local
esp32-server.local      192.168.10.155


@pavel808
Copy link
Author

@nopnop2002 Thanks for your response. Unfortunately with that command I still don't see my ESP device. Only local stuff from the Ubuntu computer where the command is issued from.

avahi-browse --all
+ wlp0s20f3 IPv6 Latitude-3420                                 SSH Remote Terminal  ubuntu-server.local
+ wlp0s20f3 IPv4 Latitude-3420                                 SSH Remote Terminal  ubuntu-server.local
+ wlp0s20f3 IPv6 Pi-hole DNS on Latitude-3420                  DNS Server           ubuntu-server.local
+ wlp0s20f3 IPv4 Pi-hole DNS on Latitude-3420                  DNS Server           ubuntu-server.local
+ wlp0s20f3 IPv6 Pi-hole DNS on Latitude-3420                  _domain._tcp         ubuntu-server.local
+ wlp0s20f3 IPv4 Pi-hole DNS on Latitude-3420                  _domain._tcp         ubuntu-server.local
+ wlp0s20f3 IPv6 Pi-hole DNS on Latitude-3420                  Web Site             ubuntu-server.local
+ wlp0s20f3 IPv4 Pi-hole DNS on Latitude-3420                  Web Site             ubuntu-server.local
+ wlp0s20f3 IPv6 Nextcloud on Latitude-3420                    Secure Web Site      ubuntu-server.local
+ wlp0s20f3 IPv4 Nextcloud on Latitude-3420                    Secure Web Site      ubuntu-server.local
+ wlp0s20f3 IPv6 Location of Latitude-3420                     Apple Home Sharing   ubuntu-server.local
+ wlp0s20f3 IPv4 Location of Latitude-3420                     Apple Home Sharing   ubuntu-server.local

@nopnop2002
Copy link

Can you stop this local DNS server?

+ wlp0s20f3 IPv6 Pi-hole DNS on Latitude-3420                  DNS Server           ubuntu-server.local
+ wlp0s20f3 IPv4 Pi-hole DNS on Latitude-3420                  DNS Server           ubuntu-server.local

@pavel808
Copy link
Author

@nopnop2002 Ok. I stopped the local server. Now things are looking a bit more promising :

avahi-browse --all
+ wlp0s20f3 IPv6 Latitude-3420                                 SSH Remote Terminal  local
+ wlp0s20f3 IPv4 Latitude-3420                                 SSH Remote Terminal  local
+     lo IPv4 Latitude-3420                                 SSH Remote Terminal  local
+ wlp0s20f3 IPv6 Pi-hole DNS on Latitude-3420                  DNS Server           local
+ wlp0s20f3 IPv4 Pi-hole DNS on Latitude-3420                  DNS Server           local
+     lo IPv4 Pi-hole DNS on Latitude-3420                  DNS Server           local
+ wlp0s20f3 IPv6 Pi-hole DNS on Latitude-3420                  _domain._tcp         local
+ wlp0s20f3 IPv4 Pi-hole DNS on Latitude-3420                  _domain._tcp         local
+     lo IPv4 Pi-hole DNS on Latitude-3420                  _domain._tcp         local
+ wlp0s20f3 IPv6 Pi-hole DNS on Latitude-3420                  Web Site             local
+ wlp0s20f3 IPv4 Pi-hole DNS on Latitude-3420                  Web Site             local
+     lo IPv4 Pi-hole DNS on Latitude-3420                  Web Site             local
+ wlp0s20f3 IPv6 Nextcloud on Latitude-3420                    Secure Web Site      local
+ wlp0s20f3 IPv4 Nextcloud on Latitude-3420                    Secure Web Site      local
+     lo IPv4 Nextcloud on Latitude-3420                    Secure Web Site      local
+ wlp0s20f3 IPv6 Location of Latitude-3420                     Apple Home Sharing   local
+ wlp0s20f3 IPv4 Location of Latitude-3420                     Apple Home Sharing   local
+     lo IPv4 Location of Latitude-3420                     Apple Home Sharing   local
+ wlp0s20f3 IPv6 192-168-178-1                                 Microsoft Windows Network local
+ wlp0s20f3 IPv6 fritz-box                                     Microsoft Windows Network local
+ wlp0s20f3 IPv4 192-168-178-1                                 Microsoft Windows Network local
+ wlp0s20f3 IPv4 fritz-box                                     Microsoft Windows Network local
+ wlp0s20f3 IPv6 AFB55A8B484802DB-000000000001B669             _matter._tcp         local
+ wlp0s20f3 IPv6 17DE56DE42501718                              _matterc._udp        local
+ wlp0s20f3 IPv6 17DE56DE42501718                              _matterd._udp        local
+ wlp0s20f3 IPv6 esp-server                                    _meshcop._udp        local
+ wlp0s20f3 IPv4 AFB55A8B484802DB-000000000001B669             _matter._tcp         local
+ wlp0s20f3 IPv4 17DE56DE42501718                              _matterc._udp        local
+ wlp0s20f3 IPv4 17DE56DE42501718                              _matterd._udp        local
+ wlp0s20f3 IPv4 esp-server                                    _meshcop._udp        local

However I can't resolve esp-server :

avahi-resolve --name esp-server
Failed to create host name resolver: Invalid host name

or esp-server.local :

avahi-resolve --name esp-server.local
Failed to resolve host name 'esp-server.local': Timeout reached

Here is my function on the esp device which starts the mdns server. I have also attached a copy of my /etc/avahi/avahi-daemon.conf file. If you have some idea of the issue? Thank you.

void start_mdns_service()
{
    //initialize mDNS service
    esp_err_t err = mdns_init();
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "MDNS Init failed: %s", esp_err_to_name(err));
        return;
    }

    ESP_LOGI(TAG, "MDNS Init OK");
    //set hostname
    err = mdns_hostname_set("server");
    if (err != ESP_OK)
    {
    	 ESP_LOGE(TAG, "MDNS Hostname Set fail : %s", esp_err_to_name(err));
    	 return;
    }

    ESP_LOGI(TAG, "MDNS mdns_hostname_set ok");
    //set default instance
    mdns_instance_name_set("esp-server");
    if (err != ESP_OK)
    {
	 ESP_LOGE(TAG, "MDNS instance name set fail : %s", esp_err_to_name(err));
	 return;
    }

    ESP_LOGI(TAG, "MDNS instance name set ok");
}

avahi-daemon.conf-copy.txt

@nopnop2002
Copy link

nopnop2002 commented Dec 12, 2024

The following is not required.

    //set default instance
    mdns_instance_name_set("esp-server");
    if (err != ESP_OK)
    {
	 ESP_LOGE(TAG, "MDNS instance name set fail : %s", esp_err_to_name(err));
	 return;
    }

    ESP_LOGI(TAG, "MDNS instance name set ok");

Now I think you can ping server.local.

This is the code I often use.

static void initialize_mdns(void)
{
    // initialize mDNS
    ESP_ERROR_CHECK( mdns_init() );

    // set mDNS hostname
    ESP_ERROR_CHECK( mdns_hostname_set("esp32-server") );

    // add service
    ESP_ERROR_CHECK( mdns_service_add(NULL, "_device-info", "_tcp", 80, NULL, 0) );
}

@pavel808
Copy link
Author

@nopnop2002 I have tried both of these but unfortunately I still get Name or service not known when trying to ping it.

@nopnop2002
Copy link

nopnop2002 commented Dec 13, 2024

A simple sample is here.
https://github.com/espressif/esp-protocols/tree/master/components/mdns/examples/query_advertise

When you run it, it will respond to ping.

$ LANG=C ping esp32-mdns.local
PING esp32-mdns.local (192.168.10.111) 56(84) bytes of data.
64 bytes from 192.168.10.111 (192.168.10.111): icmp_seq=1 ttl=64 time=93.3 ms
64 bytes from 192.168.10.111 (192.168.10.111): icmp_seq=2 ttl=64 time=93.5 ms
64 bytes from 192.168.10.111 (192.168.10.111): icmp_seq=3 ttl=64 time=14.8 ms
64 bytes from 192.168.10.111 (192.168.10.111): icmp_seq=4 ttl=64 time=36.1 ms
64 bytes from 192.168.10.111 (192.168.10.111): icmp_seq=5 ttl=64 time=58.2 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Opened Issue is new
Projects
None yet
Development

No branches or pull requests

3 participants