ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
mdns_rp2040.cpp
Go to the documentation of this file.
2#if defined(USE_RP2040) && defined(USE_MDNS)
3
7#include "esphome/core/log.h"
8#include "mdns_component.h"
9
10#include <ESP8266mDNS.h>
11
12namespace esphome::mdns {
13
14static void register_rp2040(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 (*proto == '_') {
24 proto++;
25 }
26 auto *service_type = MDNS_STR_ARG(service.service_type);
27 while (*service_type == '_') {
28 service_type++;
29 }
30 uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
31 MDNS.addService(service_type, proto, port);
32 for (const auto &record : service.txt_records) {
33 MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key), MDNS_STR_ARG(record.value));
34 }
35 }
36}
37
39 this->setup_buffers_and_register_(register_rp2040);
40 // Schedule MDNS.update() via set_interval() instead of overriding loop().
41 // This removes the component from the per-iteration loop list entirely,
42 // eliminating virtual dispatch overhead on every main loop cycle.
43 // See MDNS_UPDATE_INTERVAL_MS comment in mdns_component.h for safety analysis.
44 this->set_interval(MDNS_UPDATE_INTERVAL_MS, []() { MDNS.update(); });
45}
46
48 MDNS.close();
49 delay(40);
50}
51
52} // namespace esphome::mdns
53
54#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.