Wake On Lan using an ESP32 Telegram bot
I wanted a simple and reliable way to turn on my PC from anywhere without having to choose one of the following:
-
Port Forwarding: As most consumer routers don’t support forwarding broadcast packets, this would imply having to make a DHCP reservation for the device that needs waking up so that the local IP will not change. Also, in case of internet connections with dynamic IP, a DDNS service would be required. -
Local Server: This would require having to maintain a small local server like a Raspberry Pi and having it powered on 24/7. They don’t consume much power but it’s overkill and if you run them from an SD card it often gets corrupted.
I wanted something reliable that I could quickly setup and then mostly forget about. The solution I came up with was to host a simple Telegram Bot on an ESP32 board, it will:
- Connect to the local network via WiFI
- Wait for the
/wol
command on Telegram - Broadcast the Wake-on-Lan packet on the local network
💵 The power consumption is very low, less than half a watt, so you can keep it running for about 1$ in electricity per year without any optimization.
It keeps working if I buy a new router, it does not depend on my public or local IP staying the same and I don’t have to keep a linux server running for such a simple task.
The sketch
I am using an ESP32 board called M5Atom, it is pretty small and can be powered by USB type-c. It’s a pretty simple project, so I am using the Arduino IDE for ease of use.
The code should be pretty simple to understand and can be found on my Github at https://github.com/daniele-salvagni/wol-bot-esp32
Configuration
In orfer to compile, the following extra libraries can be installed from the Library Manager:
- M5Atom: needed for the M5Atom board
- WakeOnLan: for sending the magic packet
- UniversalTelegramBot: for using the Telegram API
The following configuration needs to be updated in the sketch code:
// WiFi configuration
#define WIFI_SSID "<wifi-network-name>"
#define WIFI_PASS "<wifi-password>"
// MAC address of the target device
#define MAC_ADDR "00:00:00:00:00:00"
// Telegram Bot Token
#define BOT_TOKEN "<telegram-bot-token-id>"
#define ALLOWED_ID "<id-for-the-allowed-user>"
You just need to fill your WiFi configuration and MAC Address, your Telegram bot token and your Telegram User ID so no one else can control the bot. You can use @Botfather to create a new bot and @userinfobot to get your ID.
Usage
- Use
/start
to get a list of the available commands - Use the
/wol
command or press the physical button to turn on your PC - Use the
/ping
command to check if the bot is online
Enabling Wake on Lan
Most motherboards support the “Wake-on-Lan” (WOL) function. This works by sending a packet of data called a “Magic Packet”. When this packet is received by the target macine, its network interface wakes up the rest of the computer.
The magic packet consists of the following parts:
- Header: 6 Bytes which is nothing but 6 bytes of 0xff.
- Data: 16*6 Bytes, the MAC Address of the target device repeated 16 times.
- Password: Some clients require 6 extra Bytes containing a password known as SecureOn.
This feature usually needs to be enabled from both the BIOS and the operating system of the PC/server you want to wake up.
Here is a screenshot of the setting in my BIOS:
To enable Wake-on-Lan on Windows you can go to Device Manager => Network Adapters => Properties. Then enable “Wake on Magic Packet” in the Advanced tab and check “Only allow a magic packet to wake the computer” in the Power Management tab.
“Fast Startup” also needs to be disabled (Power Options => “Choose what the power buttons do”) as it will conflict with this feature.
Debugging with Wireshark
If you encounter any issues you can use Wireshark and filter for WOL packets to check what’s going wrong.
TODO List
- Refactor code
- Store network configuration in EEPROM
- Add configuration commands to Telegram bot