ESPHome  2023.8.3
mdns_esp8266.cpp
Go to the documentation of this file.
1 #if defined(USE_ESP8266) && defined(USE_ARDUINO)
2 
3 #include <ESP8266mDNS.h>
6 #include "esphome/core/hal.h"
7 #include "esphome/core/log.h"
8 #include "mdns_component.h"
9 
10 namespace esphome {
11 namespace mdns {
12 
13 void MDNSComponent::setup() {
14  this->compile_records_();
15 
16  network::IPAddress addr = network::get_ip_address();
17  MDNS.begin(this->hostname_.c_str(), (uint32_t) addr);
18 
19  for (const auto &service : this->services_) {
20  // Strip the leading underscore from the proto and service_type. While it is
21  // part of the wire protocol to have an underscore, and for example ESP-IDF
22  // expects the underscore to be there, the ESP8266 implementation always adds
23  // the underscore itself.
24  auto *proto = service.proto.c_str();
25  while (*proto == '_') {
26  proto++;
27  }
28  auto *service_type = service.service_type.c_str();
29  while (*service_type == '_') {
30  service_type++;
31  }
32  MDNS.addService(service_type, proto, service.port);
33  for (const auto &record : service.txt_records) {
34  MDNS.addServiceTxt(service_type, proto, record.key.c_str(), record.value.c_str());
35  }
36  }
37 }
38 
39 void MDNSComponent::loop() { MDNS.update(); }
40 
42  MDNS.close();
43  delay(10);
44 }
45 
46 } // namespace mdns
47 } // namespace esphome
48 
49 #endif
std::vector< MDNSService > services_
void on_shutdown() override
Definition: mdns_esp32.cpp:51
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:28
network::IPAddress get_ip_address()
Definition: util.cpp:32