Physical WakeOnLan button with Raspberry Pi and Python

This is a fun little demo I threw together in an evening. I’m running Raspbian “wheezy”.

Wire up a switch (or switches) to your GPIO port. I have switches connected to GPIO pins 22, 23, and 24.

Install RPIO.

Physical WakeOnLan button with Raspberry Pi and Pythonsudo apt-get install python-setuptools sudo easy_install -U RPIO

Install wakeonlan.

sudo apt-get install wakeonlan

Check that you wired your switch up correctly by running ‘rpio-curses’ from the terminal. You should see the state of the GPIO pin change when you press the button.

Save the following code to ‘gpio-wol.py’:

# using the RPIO library and a physical switch to perform WakeOnLan 

import RPIO
from subprocess import call

# this dictionary maps gpio pins to MAC addresses, substitute your machine's MAC
gpio_to_mac = { 24: '00:00:00:00:00:00',
                23: '11:11:11:11:11:11',
                22: 'ff:ff:ff:ff:ff:ff'  }
                
def gpio_callback(gpio_id, val):
    ''' wakes up the appropriate MAC '''
    call(['wakeonlan', gpio_to_mac[gpio_id]])
    
# setup the GPIO pins.
for gpio, mac in gpio_to_mac.items():
    RPIO.add_interrupt.callback(gpio, gpio_callback, edge='falling')

print('Waiting for GPIO input. Press CTRL+C to quit.')
RPIO.wait_for_interrupts()

Run gpio-wol.py and try it out. Note: the RPIO library requires root access.

sudo python gpio-wol.py

When you press a button, this is the output you should see:

Sending magic packet to 255.255.255.255:9 with 00:00:00:00:00:00

Physical WakeOnLan button with Raspberry Pi and Python schematicIf you have any trouble, you can open up Wireshark and scan for UDP packets. Since UDP is connectionless, it is broadcast to all machines in the subnet. If you can see the WOL UDP packets and your machine isn’t waking up, double-check the MAC address. Also, make sure that the machine is setup to accept WOL packets, which might mean a trip to the BIOS to change some settings.

I would like to point out that you could bind any terminal command to a physical button using this technique by simply changing the gpio_callback() function. How cool is that?

 

For more detail: Physical WakeOnLan button with Raspberry Pi and Python


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top