Send WOL (Wake on LAN) magic packets from ESP8266

I want to have a simple ESP device that sends out WOL packages every 5 minute as long as it have power. So I started here:

Just as the example, but with my omw MAC adress.

button:
  - platform: wake_on_lan
    name: "Start the Server"
    target_mac_address: E9:48:B8:CA:58:A1

Sent it to the ESP8266 and hit the button in Home Assistant.

The output log tells me:

[D][button:010]: 'Start the Server' Pressed.
[I][wake_on_lan.button:030]: Sending Wake-on-LAN Packet...
[E][wake_on_lan.button:050]: Sending Wake-on-LAN Packet Failed!

The ESP are connected to the WiFi and the status sensors are working correctly. Where to start finding the issue?

I wouldn’t know,
but maybe increase your loglevel (temporary)

if you run the service from HA using the same mac address does it work?

service: wake_on_lan.send_magic_packet
data:
  mac: E9:48:B8:CA:58:A1
1 Like

How can an UDP packet of a few bytes fail to send?

Setting the log level to VERY_VERBOSE doesn’t reveal any additional noteworthy information. I had already enabled DEBUG, but further verbosity doesn’t provide any valuable insights.

I also find it strange that the UDP packet can fail immediately.

Some Issue here,
how can we go further and troubleshoot? I really need this feature.

Marco

Short of tcpdump udp port 9 on the same wifi iface in your router where your chip is connected to… No, no idea.

Just to add my findings. I am also trying to send WOL packets from an ESP8266 with ESPHome and I get the same message that the packet has failed. However it seems to actually work regardless of the failed message and the servers do actually boot!

I can confirm that the ESPHome Wake-on-LAN Button component in fact indeed is working fine: the only problem seems to be a bug in the logging output, generating incorrect error logs.

To test it I first added the current Wake-on-LAN Button component to an ESP8266 (Wemos D1 Mini v4), and although there always was an error logged in the ESPHome output the WoL function did indeed work fine.

So then I had a brief look at the Wake-on-LAN component code on Github (specifically the wake_on_lan.cpp code), and, although I am not completely sure about the complete functioning of this code, it looks like it is a bug in the logging function.
This is a snippet of the current code where the magic packet is sent and where the error log is generated:

  if (begin_status) {
    this->udp_client_.write(PREFIX, 6);
    for (size_t i = 0; i < 16; i++) {
      this->udp_client_.write(macaddr_, 6);
    }
    end_status = this->udp_client_.endPacket();
  }
  if (!begin_status || end_status) {
    ESP_LOGE(TAG, "Sending Wake-on-LAN Packet Failed!");
  }

The line if (!begin_status || end_status) means:
IF ( NOT begin_status OR end_status ) THEN ERROR
But I think this should be:
IF ( NOT begin_status OR NOT end_status ) THEN ERROR
It should only generate an error log when either the begin_status or the end_status is false (not true), and like it is now it will generate an error log when either the begin_status is false or the end_status is true?
So I think the code probably should be:

  if (begin_status) {
    this->udp_client_.write(PREFIX, 6);
    for (size_t i = 0; i < 16; i++) {
      this->udp_client_.write(macaddr_, 6);
    }
    end_status = this->udp_client_.endPacket();
  }
  if (!begin_status || !end_status) {
    ESP_LOGE(TAG, "Sending Wake-on-LAN Packet Failed!");
  }

For clarity: the difference here is the added exclamation mark (meaning NOT) before end_status.

I did test this in a local component, and indeed the WoL function is still working fine like that but the error is not generated anymore.

Can someone with a better understanding of this code please confirm this to be correct?

2 Likes

Your assessment appears correct. Please open a PR with the fix!

1 Like

Thanks!
For now I reported this as an Issue, but when it is still required I will open a PR as well later when I have some more time.