Micropython WoL

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Re: Micropython WoL

Post by Jibun no kage » Fri Aug 05, 2022 1:00 am

@Strimis10,

Mind sharing how you got MicroPython on an ESP01? I am trying to do it, but after flashing, I can seem to connect to the REPL? Any help would be appreciated! Thanks.

Strimis10
Posts: 7
Joined: Sun Jul 10, 2022 4:13 pm

Re: Micropython WoL

Post by Strimis10 » Fri Aug 05, 2022 5:43 am

Jibun no kage wrote:
Fri Aug 05, 2022 1:00 am
@Strimis10,

Mind sharing how you got MicroPython on an ESP01? I am trying to do it, but after flashing, I can seem to connect to the REPL? Any help would be appreciated! Thanks.
@Jibun no kage

ah yes, I also had a lot of trouble trying to get the firmware to work, first make sure your esp01 has 1mb of flash memory (if it has less it won't work) and then you need a specific firmware version (the one for 1mb boards). Here's the download: https://micropython.org/download/esp8266-1m/ I hope it works out for you!

DeaD_EyE
Posts: 19
Joined: Sun Jul 17, 2022 12:57 pm
Contact:

Re: Micropython WoL

Post by DeaD_EyE » Fri Aug 05, 2022 10:46 am

I can send and receive broadcasts without setting the sockopt. I get an Exception if I try to set SO_BROADCAST, which should be 0x20.

The code I used for testing on an ESP32 (4 MiB flash).

Code: Select all

from ubinascii import unhexlify
from socket import AF_INET, SOCK_DGRAM, socket
from micropython import const

BROADCAST_IP = const(b"255.255.255.255")
DEFAULT_PORT = const(9)
HEADER = "F" * 12


def create_magic_packet(macaddress: str) -> bytes:
    for to_replace in ":- ":
        macaddress = macaddress.replace(to_replace, "")

    if len(macaddress) != 12:
        raise ValueError("Incorrect MAC address format")

    return unhexlify(HEADER + macaddress * 16)


class Closing:
    """
    Contextmanager to call close method before leaving context
    """
    def __init__(self, obj):
        self.obj = obj
    def __enter__(self):
        return self.obj
    def __exit__(self, exc_type, exc_obj, exc_tb):
        self.obj.close()


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]

    with Closing(socket(AF_INET, SOCK_DGRAM)) as sock:
        if interface is not None:
            sock.bind((interface, 0))
        for packet in packets: 
            sock.sendto(packet, (ip_address, port))



# test with wireshark and use your MAC
import time
while True:
    send_magic_packet("74 e5 f9 4e 73 70")
    send_magic_packet("74:e5:f9:4e:73:70")
    send_magic_packet("74-e5-f9-4e-73-70")
    time.sleep_ms(500)
I also added a Contextmanager. I don't know if it's a guarantee in Micropython, that the __exit__ method is called, if an Exception occurs in the with-block.

Jibun no kage
Posts: 144
Joined: Mon Jul 25, 2022 9:45 pm

Re: Micropython WoL

Post by Jibun no kage » Mon Aug 08, 2022 3:09 am

What if you have more than one device with WOL enabled? If you use the broadcast, are you qualify a specific MAC address as well? I was working with Node Red and WOL, and at one point I send WOL packet to every WOL enabled device on my network, that was interesting. If you only have one, single device, enabled for WOL, ok. Otherwise, surprise.

DeaD_EyE
Posts: 19
Joined: Sun Jul 17, 2022 12:57 pm
Contact:

Re: Micropython WoL

Post by DeaD_EyE » Mon Aug 08, 2022 12:11 pm

What if you have more than one device with WOL enabled?
Nothing would happen because each NIC checks if his MAC-Address is in the broadcast. If not, no action.

https://en.wikipedia.org/wiki/Wake-on-LAN#Magic_packet
The Wake-on-LAN implementation is designed to be very simple and to be quickly processed by the circuitry present on the network interface card with minimal power requirement. Because Wake-on-LAN operates below the IP protocol layer, IP addresses and DNS names are meaningless and so the MAC address is required.

Post Reply