esp_iot_framework  v0.1.0-alpha
© 2026 AmakeSasha, distributed under a license Apache-2.0
mDNS Configuration

Network discovery and mDNS management. More...

Macros

#define EIF_MDNS_TXT_RECORDS_MAX_COUNT   32
 Maximum number of mDNS txt-records.
 

Functions

esp_err_t eif_set_mdns (const char *const hostname, const char *const instance_name)
 Configure the mDNS hostname and instance name. More...
 
esp_err_t eif_set_mdns_records (const mdns_txt_item_t txt_records[EIF_MDNS_TXT_RECORDS_MAX_COUNT], size_t txt_records_count)
 Set custom TXT records for mDNS service discovery. More...
 

Detailed Description

Note
This module is available when Kconfig option CONFIG_EIF_ENABLE_MDNS is enabled and you include this line at the beginning of the file.:
#include <esp_iot_framework_core_mdns.h>

This module allows you to configure mDNS. mDNS enables the device to be discovered via a human-readable name (e.g., my-device.local) instead of an IP address.

The framework automatically appends a unique identifier based on the MAC address to prevent network collisions (e.g., common hostname set: esp32.local, the framework turns this into esp32-1a2b3c.local on a device with MAC XX:XX:XX:1A:2B:3C).

Note
In case the mDNS hostname is not set, device will be used as the common hostname. To disable mDNS, turn off Kconfig option CONFIG_EIF_ENABLE_MDNS.

If you frequently need to enable and disable mDNS during development, you can use the following construct:

#include <mdns.h>
#include <esp_err.h>
#include <esp_iot_framework_core.h>
#ifdef CONFIG_EIF_ENABLE_MDNS
#include <esp_iot_framework_core_mdns.h>
#endif
// Not necessary, but nice :)
#ifdef CONFIG_EIF_ENABLE_MDNS
static const mdns_txt_item_t my_txt_records[] = {
{"friendly_name", "Kitchen Main Light"},
{"room", "Kitchen"},
{"hardware", "ESP32-S3-DevKit"},
{"v", "1.0.4"}
};
#endif
void app_main(void) {
ESP_ERROR_CHECK(eif_core_initialize());
ESP_ERROR_CHECK(eif_nvs_initialize());
#ifdef CONFIG_EIF_ENABLE_MDNS
ESP_ERROR_CHECK(eif_set_mdns("my-esp", "Smart Controller"));
ESP_ERROR_CHECK(eif_set_mdns_records(my_txt_records, 4));
#endif
// Further code...
}
esp_err_t eif_nvs_initialize(void)
Initialize Non-Volatile Storage (NVS) and framework data.
esp_err_t eif_core_initialize(void)
Initializes the CORE of the framework.
esp_err_t eif_set_mdns(const char *const hostname, const char *const instance_name)
Configure the mDNS hostname and instance name.
esp_err_t eif_set_mdns_records(const mdns_txt_item_t txt_records[EIF_MDNS_TXT_RECORDS_MAX_COUNT], size_t txt_records_count)
Set custom TXT records for mDNS service discovery.

Function Documentation

◆ eif_set_mdns()

esp_err_t eif_set_mdns ( const char *const  hostname,
const char *const  instance_name 
)

Sets the base identity of the device on the local network.

Note
This function must be called after eif_core_initialize() but before eif_wifi_initialize().
Parameters
hostnameBase name prefix (e.g., "my-sensor"). Max length: 32.
instance_nameFriendly name for discovery tools (e.g., "Main Hall Sensor"). If NULL or empty, the formatted mdns_hostname(with MAC) will be used as the instance name by default.
Returns
  • ESP_OK: Configuration stored.
  • ESP_ERR_INVALID_ARG: The pointer hostname or instance_name is NULL.
  • ESP_ERR_INVALID_SIZE: The length of the hostname or instance_name is outside the acceptable range.

Example of use:

#include <esp_err.h>
#include <esp_iot_framework_core.h>
#include <esp_iot_framework_core_mdns.h>
void app_main(void) {
ESP_ERROR_CHECK(eif_core_initialize());
ESP_ERROR_CHECK(eif_nvs_initialize());
ESP_ERROR_CHECK(eif_set_mdns("my-esp", "Smart Controller"));
// Further code...
}

◆ eif_set_mdns_records()

esp_err_t eif_set_mdns_records ( const mdns_txt_item_t  txt_records[EIF_MDNS_TXT_RECORDS_MAX_COUNT],
size_t  txt_records_count 
)

TXT records allow you to broadcast additional metadata about the device (e.g., firmware version, model, or status). The framework creates an internal copy of the structure, so the original data's lifecycle no longer matters after the call.

Note
This function must be called after eif_core_initialize() but before eif_wifi_initialize().
Warning
The handler array length must be equal to txt_records_count. If the count exceeds the actual array length, a Buffer Overread will occur. If the count is less than the actual length, error ESP_ERR_INVALID_ARG will return.
If the array is accessible by value at the function call site, you can use the following code to automatically calculate the length:
sizeof(array) / sizeof(array[0])
Parameters
txt_recordsArray of mDNS TXT items. The length should be EIF_MDNS_TXT_RECORDS_MAX_COUNT.
txt_records_countNumber of elements in the array.
Returns
  • ESP_OK: Records applied.
  • ESP_ERR_INVALID_ARG: The pointer key or value from txt_records is NULL.
  • ESP_ERR_INVALID_SIZE: The txt_records length is less than txt_records_count.

Example of use:

#include <mdns.h>
#include <esp_err.h>
#include <esp_iot_framework_core.h>
#include <esp_iot_framework_core_mdns.h>
static const mdns_txt_item_t my_txt_records[] = {
{"friendly_name", "Kitchen Main Light"},
{"room", "Kitchen"},
{"hardware", "ESP32-S3-DevKit"},
{"v", "1.0.4"}
};
void app_main(void) {
ESP_ERROR_CHECK(eif_core_initialize());
ESP_ERROR_CHECK(eif_nvs_initialize());
ESP_ERROR_CHECK(eif_set_mdns_records(my_txt_records, 4));
// Further code...
}