ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
mdns_esp8266.cpp
Go to the documentation of this file.
2#if defined(USE_ESP8266) && defined(USE_ARDUINO) && defined(USE_MDNS)
3
4#include <ESP8266mDNS.h>
8#include "esphome/core/hal.h"
9#include "esphome/core/log.h"
10#include "mdns_component.h"
11
12namespace esphome::mdns {
13
14static void register_esp8266(MDNSComponent *, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
15 MDNS.begin(App.get_name().c_str());
16
17 for (const auto &service : services) {
18 // Strip the leading underscore from the proto and service_type. While it is
19 // part of the wire protocol to have an underscore, and for example ESP-IDF
20 // expects the underscore to be there, the ESP8266 implementation always adds
21 // the underscore itself.
22 auto *proto = MDNS_STR_ARG(service.proto);
23 while (progmem_read_byte((const uint8_t *) proto) == '_') {
24 proto++;
25 }
26 auto *service_type = MDNS_STR_ARG(service.service_type);
27 while (progmem_read_byte((const uint8_t *) service_type) == '_') {
28 service_type++;
29 }
30 uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
31 MDNS.addService(FPSTR(service_type), FPSTR(proto), port);
32 for (const auto &record : service.txt_records) {
33 MDNS.addServiceTxt(FPSTR(service_type), FPSTR(proto), FPSTR(MDNS_STR_ARG(record.key)),
34 FPSTR(MDNS_STR_ARG(record.value)));
35 }
36 }
37}
38
40 this->setup_buffers_and_register_(register_esp8266);
41 // Schedule MDNS.update() via set_interval() instead of overriding loop().
42 // This removes the component from the per-iteration loop list entirely,
43 // eliminating virtual dispatch overhead on every main loop cycle.
44 // See MDNS_UPDATE_INTERVAL_MS comment in mdns_component.h for safety analysis.
45 this->set_interval(MDNS_UPDATE_INTERVAL_MS, []() { MDNS.update(); });
46}
47
49 MDNS.close();
50 delay(10);
51}
52
53} // namespace esphome::mdns
54
55#endif
const std::string & get_name() const
Get the name of this Application set by pre_setup().
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_interval(const std voi set_interval)(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
Definition component.h:350
void setup_buffers_and_register_(PlatformRegisterFn platform_register)
static constexpr uint32_t MDNS_UPDATE_INTERVAL_MS
void HOT delay(uint32_t ms)
Definition core.cpp:27
Application App
Global storage of Application pointer - only one Application can exist.
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:50