So because I was too lazy to figure out how to build a magic packet I just yoinked the code from https://pypi.org/project/wakeonlan/
and modified it a little (mostly removed what I didn't need) and just ran that and it worked!, but now I copy pasted the code to my esp01, I had to modify some stuff and now I have an error where it throws an error;
"AttributeError: 'module' object has no attribute 'SO_BROADCAST'" it uses socket btw...
Here is the code I run in micropython:
Code: Select all
import socket
import ubinascii
BROADCAST_IP = "192.168.0.61"
DEFAULT_PORT = 9
def create_magic_packet(macaddress: str) -> bytes:
if len(macaddress) == 17:
sep = macaddress[2]
macaddress = macaddress.replace(sep, "")
elif len(macaddress) != 12:
raise ValueError("Incorrect MAC address format")
return ubinascii.unhexlify("F" * 12 + macaddress * 16)
def send_magic_packet(
*macs: str,
ip_address: str = BROADCAST_IP,
port: int = DEFAULT_PORT,
interface: str = None
) -> None:
packets = [create_magic_packet(mac) for mac in macs]
print("aaa")
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if interface is not None:
sock.bind((interface, 0))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.connect((ip_address, port))
for packet in packets:
sock.send(packet)
sock.close()
print("sent")
And here's the code I run on my pc:
import socket
BROADCAST_IP = "192.168.0.61"
DEFAULT_PORT = 9
def create_magic_packet(macaddress: str) -> bytes:
"""
Create a magic packet.
A magic packet is a packet that can be used with the for wake on lan
protocol to wake up a computer. The packet is constructed from the
mac address given as a parameter.
Args:
macaddress: the mac address that should be parsed into a magic packet.
"""
if len(macaddress) == 17:
sep = macaddress[2]
macaddress = macaddress.replace(sep, "")
elif len(macaddress) != 12:
raise ValueError("Incorrect MAC address format")
return bytes.fromhex("F" * 12 + macaddress * 16)
def send_magic_packet(
*macs: str,
ip_address: str = BROADCAST_IP,
port: int = DEFAULT_PORT,
interface: str = None
) -> None:
"""
Wake up computers having any of the given mac addresses.
Wake on lan must be enabled on the host device.
Args:
macs: One or more macaddresses of machines to wake.
Keyword Args:
ip_address: the ip address of the host to send the magic packet to.
port: the port of the host to send the magic packet to.
interface: the ip address of the network adapter to route the magic packet through.
"""
packets = [create_magic_packet(mac) for mac in macs]
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
if interface is not None:
sock.bind((interface, 0))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.connect((ip_address, port))
for packet in packets:
sock.send(packet)
send_magic_packet("bc:5f:f4:91:ed:14")
As you might see I don't use the broadcast ip, that's because I can't find it... :[
any help is greatly appreciated :)
I had a hard time figuring out what my broadcast ip was but I found this really good video on YouTube explaining how to get it: https://www.youtube.com/watch?v=1pZNjRZLNqI