Power on Other Devices with Python

Tom Clarke
CodeX
Published in
3 min readSep 2, 2021

This is a pretty neat little function that not a lot of people know about, called Wake-On-Lan (WoL). It’s built into a lot of desktops and laptops, and as far as I know, some more modern smart TV’s. But what actually is it?

WoL is a functionality that keeps the networking card of a device on, with some reserve power, which allows us to send it a message. This specific message is called a ‘magic packet’, and when a magic packet is received by a device, it will power on.

Before starting to write the very (and I mean very) short python code for this, there are a few things you need to check. First of all, make sure the devices you want to play around with have WoL capability, and I recommend trying it first with a PC/laptop. To do this you will need to enter the devices BIOS on start-up. In the BIOS you will need to navigate to the power management section, and look for a Wake-On-Lan setting and enable it. It can also be called PCIe/PCI, so be on the lookout for that also, and can sometimes come under network or CPU power management settings, all depends on which brand of motherboard you have. If you’re struggling to find it, search for your motherboard online, and it may be easier to find where to go.

Next there are a couple of things to check on the device.

→Fast boot/start-up needs to be disabled

→In device manager navigate to your network card, double click it and go to power management, and make sure to enable ‘allow this device to wake the computer’ and ‘only allow a magic packet to wake the computer’

→Then under advanced enable ‘wake on magic packet’ and ‘wake on pattern match’

While your other device is on, you may also want to note down the mac address, and the local IP address. The easiest way to do this is open a command prompt terminal and type ‘ipconfig/all’ + enter. You will be looking for the section that matches the network card name you were just looking at before. From there make a note of the physical address, and the IPv4 address, these will be needed next. The physical address will come separated with a few ‘-’ signs, you should replace these with ‘:’ when you write it down.

Once this is all complete you can power off the device, and move back to the machine you want to run the python script on.

Okay so now time for the code! You will need to pip install a library called ‘wakeonlan’.

pip install wakeonlan

From this we are only interested in one method called ‘send_magic_packet’:

from wakeonlan import send_magic_packet

Next we want to create a dictionary for the devices we want to control. This is actually going to contain another dictionary as well to store the mac address and IP address noted down before.

devices = {
'my_pc':{'mac':'YOUR MAC ADDRESS','ip_address':'YOUR IP'}
}

You can add as many devices in this dictionary as you like, just follow the same format.

Now we will write a small function that will send our magic packet!

def wake_device(device_name):
if device_name in devices:
mac,ip = devices[device_name].values()
send_magic_packet(mac,ip_address=ip)
print('Magic Packet Sent')
else:
print('Device Not Found')

It’s as simple as that! Just call your function like so:

wake_device('my_pc')

And your device should turn on!

So that’s the background on waking up the devices. The interesting part is really putting it into use, and making it a helpful or convenient part of your day. For example you may want to add a cool way to activate the function, maybe check out my articles on gesture control of the fire tv, and you could tweak the outputs to turn on different devices instead! Perhaps instead you have servers in the house that you want on when you leave the house, or devices you want to come on when you get home. To do this check out my article on auto-locking pc when you leave the house, this can very easily be changed to turn on a device when you leave or return!

I hope you found this helpful and interesting! I think it’s a pretty cool capability!

--

--

Tom Clarke
CodeX
Writer for

I enjoy coming up with fun and interesting python concepts, as well as useful projects. You can also view my articles on Blogger: https://azcoding.blogspot.com/