Created
November 21, 2023 19:09
-
-
Save Niklas9401/86fae76c3fde84ba8115572c5cd5a993 to your computer and use it in GitHub Desktop.
Micropython Wake-On-LAN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| class Waker(): | |
| def send_magic_packet(self, macAddress: str, ip_address: str, port: int = 9) -> None: | |
| macAddress = self.format_macAddress(macAddress) | |
| temp = "" | |
| for i in range(16): | |
| temp += macAddress | |
| packet = bytes.fromhex("FFFFFFFFFFFF" + temp) | |
| soc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
| soc.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1) | |
| soc.connect((ip_address, port)) | |
| soc.send(packet) | |
| def format_macAddress(self, mac: str) -> str: | |
| if len(mac) == 17: | |
| sep = mac[2] | |
| return mac.replace(sep, "") | |
| elif len(mac) == 14: | |
| sep = mac[4] | |
| return mac.replace(sep, "") | |
| if len(mac) != 12: | |
| raise ValueError("Incorrect MAC address format") | |
| return mac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment