ESPHome  2024.4.1
wake_on_lan.cpp
Go to the documentation of this file.
1 #ifdef USE_ARDUINO
2 
3 #include "wake_on_lan.h"
4 #include "esphome/core/log.h"
7 
8 namespace esphome {
9 namespace wake_on_lan {
10 
11 static const char *const TAG = "wake_on_lan.button";
12 static const uint8_t PREFIX[6] = {255, 255, 255, 255, 255, 255};
13 
14 void WakeOnLanButton::set_macaddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f) {
15  macaddr_[0] = a;
16  macaddr_[1] = b;
17  macaddr_[2] = c;
18  macaddr_[3] = d;
19  macaddr_[4] = e;
20  macaddr_[5] = f;
21 }
22 
24  LOG_BUTTON("", "Wake-on-LAN Button", this);
25  ESP_LOGCONFIG(TAG, " Target MAC address: %02X:%02X:%02X:%02X:%02X:%02X", macaddr_[0], macaddr_[1], macaddr_[2],
26  macaddr_[3], macaddr_[4], macaddr_[5]);
27 }
28 
30  ESP_LOGI(TAG, "Sending Wake-on-LAN Packet...");
31  bool begin_status = false;
32  bool end_status = false;
33  IPAddress broadcast = IPAddress(255, 255, 255, 255);
34 #ifdef USE_ESP8266
35  for (auto ip : esphome::network::get_ip_addresses()) {
36  if (ip.is_ip4()) {
37  begin_status = this->udp_client_.beginPacketMulticast(broadcast, 9, ip, 128);
38  break;
39  }
40  }
41 #endif
42 #ifdef USE_ESP32
43  begin_status = this->udp_client_.beginPacket(broadcast, 9);
44 #endif
45 
46  if (begin_status) {
47  this->udp_client_.write(PREFIX, 6);
48  for (size_t i = 0; i < 16; i++) {
49  this->udp_client_.write(macaddr_, 6);
50  }
51  end_status = this->udp_client_.endPacket();
52  }
53  if (!begin_status || end_status) {
54  ESP_LOGE(TAG, "Sending Wake-on-LAN Packet Failed!");
55  }
56 }
57 
58 } // namespace wake_on_lan
59 } // namespace esphome
60 
61 #endif
void set_macaddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t e, uint8_t f)
Definition: wake_on_lan.cpp:14
network::IPAddresses get_ip_addresses()
Definition: util.cpp:40
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7