Remote Power Button for Your PC
I love having all my computers off when I don’t need them. This saves plenty of power, makes me reboot regularly (good for Windows), and probably even prolongs my hardware lifetime. But you never know when you may need to connect to your home or office PC via RDP or TeamViewer to complete some urgent and critical task. If that PC is off, you can’t log into it, right? In fact, you can turn it on remotely from the Internet. If you haven’t heard about WoL or tried to make it work without luck, read on, let’s make it work together!
Meet Wake on LAN
Wake on LAN (WoL) is a protocol that allows you to turn a PC on by broadcasting a specially formed packet into your local network.
If your router’s configuration is flexible enough, you can even catch that packet from the Internet and broadcast it as required.
The protocol is radically simple. All you need to do is to send a so-called Magic Packet to your network’s broadcast IP (e.g., 192.168.1.255 or even 255.255.255.255) and port 9. The magic packet is a UDP datagram that should begin with six 0xFF bytes, then contain the MAC-address of your target PC repeated 16 times. That’s all. 144 bytes with magic power. If your LAN-card receives a magic packet with its MAC while waiting for it, the LAN-card tries to turn the computer on via ACPI. And yes, it waits for the magic packet when the entire system is completely turned off (just don’t remove the plug from the socket).
The WoL protocol is simple in theory, but in practice, it turns up to be a challenging thing to set up. Here are some possible issues when we are trying to deal with magic packets from the Internet (the most convenient case):
- You can be behind your provider’s NAT and unable to accept incoming connections.
- Your provider can block incoming connections on port 9 (or even all ports, for safety).
- Your router can be not capable of forwarding a port to a broadcast address.
- Your LAN-card can be off. It happens when something is not configured, or in case of a failure.
- Your ACPI can be configured to ignore the PCI wake requests.
The issues in first three items are not solved smoothly: you need to change either your provider or your router or both. So, there’s no way to turn on your PC from the Internet if your provider and/or router are junk, right? In fact, you can use an always-on single-board PC like Raspberry Pi which listens for a command sent from the Internet and broadcasts a magic packet. That’s what we are going to set up in this article! But first, I’ll try to summarize everything you should do with your Windows PC to make it accept WoL packets.
Configure Wake on LAN
There are several things you should do on Windows 10 to make your PC turn on when its LAN-card receives the magic packet:
1. Configure LAN-card
- Right-click the Start button and open Network Connections.
- Proceed to Change Adapter Options.
- Right-click your primary Ethernet adapter and choose Properties.
- Click Configure to open the device properties.
- Proceed to the Advanced tab.
- Set Wake Up Capabilities to Magic Packet or Magic Packet & Pattern Match.
- Set Shutdown Wake Up to Enabled.
Uncheck everything on Power Management tab (you can try to check everything, but this configuration may not work for some reason):
The MAC-address of this adapter will also be required in the future, you can find it by double-clicking your primary Ethernet adapter and choosing Details. The Physical Address is what you need there, save this string on your smartphone (or paper, if you still use it).
2. Enable Shutdown
By default, when you shut down your PC, Windows 8+ performs a sort of hibernation. The disadvantages of this are more significant than a modest startup acceleration: your system drive remains in an unsafe state (you may notice this on Linux), and this mode somehow affects Wake on LAN. Let’s bring the real shutdown back.
- Right-click the Start button and open Power Options.
- Open the old interface using the Additional Power Options on the right side.
- Click the Choose what the power buttons do link on the left side.
- Unlock the checkboxes using the Change settings that are currently unavailable link.
- Uncheck the Turn on fast startup option.
3. Configure ACPI
Unfortunately, I can’t give the precise instructions here. The SETUP interface is very different on different motherboards. You need to enter SETUP (also called BIOS) in the first few seconds after turning the PC on. Hold a button to do this: it can be Del, Esc, or one of the F-keys. Refer to your motherboard’s manual for detailed instructions.
In SETUP, enable everything related to Wake on LAN and also allow PCI devices to power the PC on.
These three steps should solve the issues 4 and 5 in the list of possible WoL problems. Try to wake your PC using your smartphone. I recommend this Android app, but you can use any WoL app, they are very simple. The Android app I use can detect the target MAC address automatically, try to search for your PC while the Windows is running.
If you were able to wake your PC using your smartphone connected to your Wi-Fi, it’s time to configure Wake on LAN over the Internet.
Wake on Cloud
The remaining part of the article is for those who can’t receive a packet from the Internet or can’t broadcast it after receiving. The additional hardware is a little bit overkill, but it’s good to have an always-on Linux in your network. You can use it for many different tasks apart from turning on your PC. The plan is simple:
- Getting a single-board PC (Omega2, C.H.I.P., Raspberry Pi, etc.) or taking the one you already have.
- Configuring a Cloud4RPi service, which allows you to send a Power On command from anywhere.
I use Next Thing Co, C.H.I.P., because I have one. You can use any Linux-based device (from an OpenWRT-enabled router to a powerful server). There’s no C.H.I.P. specifics involved in this task, the instructions below should work anywhere.
- Follow the Getting Started sections in the Cloud4RPi documentation to get the sample code working. If you face any problems, feel free to contact the developers, this team is very responsive.
- Install the wakeonlan Python package using the
pip install wakeonlancommand
- Implement the function that broadcasts a magic packet and the one that pings your PC (to ensure that the WoL request worked). I did it in a separate file wol.py.
- Change the variables section in the control.py sample file and add two new variables.
The full code is presented in my cloud4rpi-wol repository, feel free to clone it and use my code.
Note that I use a NetBIOS name instead of an IP address to ping my PC. This will work if you install the libnss-winbind package and add the wins option to the hosts line in the /etc/nsswitch.conf file:
sudo apt install libnss-winbind
sed "s/^hosts:.*/& wins/" /etc/nsswitch.conf | sudo tee /etc/nsswitch.conf
Create a Control Panel and bind a Switch widget to the PC WoL variable.
Unfortunately, Cloud4RPi has no Big Red Button widget, although the Switch widget does the great job in sending True into the wake_pc function. The function always makes it switch off right after switching on, but it doesn’t matter.
You can now turn on your PC from anywhere. Isn’t that great?
If you have cloned a Cloud4RPi example, you should have a service_install.sh script. Use it to install your main script as a system service.
sudo service_install.sh ~/cloud4rpi-wol/control.py
Now it starts with the system and sends logs to journald. You can control the service as follows:
sudo service cloud4rpi start|stop|restart|status
Don’t waste electric power! Now, you don’t have to.