Skip to content
Go back

ESP32 Wake-on-LAN via MQTT

2 min read

Remote access to my desktop is essential for my workflow, but keeping a high-power PC running 24/7 is inefficient. Standard Wake-on-LAN (WOL) is usually limited to the local network or requires setting up complex and vulnerable port forwarding with a public IP. I also wanted to avoid sophisticated mechanical triggers that some people use.

I built this bridge using an ESP32. It is a cheap, affordable, and low-power microcontroller that can be left on indefinitely without worry. It just stays plugged in with a low-power micro USB charger.

The ESP32 maintains a secure connection to a HiveMQ broker using TLS. When it receives a JSON command via MQTT, it parses the target MAC address and broadcasts the Magic Packet. I made the MAC address parsing robust enough to handle both colon and dash formats.

bool parseMacAddress(const char *macStr, uint8_t *macBytes) {
  int values[6];
  if (sscanf(macStr, "%x:%x:%x:%x:%x:%x", &values[0], &values[1], &values[2],
             &values[3], &values[4], &values[5]) == 6) {
    for (int i = 0; i < 6; i++) macBytes[i] = (uint8_t)values[i];
    return true;
  }
  // Fallback for dash format
  if (sscanf(macStr, "%x-%x-%x-%x-%x-%x", &values[0], &values[1], &values[2],
             &values[3], &values[4], &values[5]) == 6) {
    for (int i = 0; i < 6; i++) macBytes[i] = (uint8_t)values[i];
    return true;
  }
  return false;
}

The device also features a non-blocking reconnection logic to ensure it recovers from router reboots or ISP hiccups automatically. It is now a permanent part of my home lab, allowing me to wake my PC from a terminal command or mobile app from anywhere in the world.


Share this post on:

Previous Post
Ateneo-utils
Next Post
PandaBrew