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