This Library provides an easy way to generate/send magic packets from an ESP8266 or ESP32 to any MAC Address. Additionally, it supports the "SecureOn" feature from some motherboard manufacturers. Finally, it also supports using any port number (instead of the default port 9.)
This library can be used in any environment that the IPAddress, WiFiUDP & delay classes are available.
You can install this library in the Arduino IDE by navigating to Tools -> Library Manager and search for this library's name. Alternatively, download this repository as zip file, then in the IDE navigate to Sketch -> Include library -> Add .ZIP library
To install the library in the PlatformIO IDE, use the library name like so:
lib_deps = WakeOnLan
OR copy the repository link and add to the 'lib_deps' variable:
lib_deps = https://github.com/a7md0/WakeOnLan.git
#include <WiFiUdp.h>
WiFiUDP UDP;
#include <WakeOnLan.h>
WakeOnLan WOL(UDP); // Pass WiFiUDP class
WOL.setRepeat(3, 100); // Repeat the packet three times with 100ms delay between
WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask());
WOL.setBroadcastAddress("192.168.1.255");
const char *MACAddress = "01:23:45:67:89:AB";
WOL.sendMagicPacket(MACAddress);
WOL.sendMagicPacket(MACAddress, 7);
const char *MACAddress = "01:23:45:67:89:AB";
const char *secureOn = "FE:DC:BA:98:76:54";
WOL.sendSecureMagicPacket(MACAddress, secureOn);
WOL.sendSecureMagicPacket(MACAddress, secureOn, 7);
uint8_t MAC[6] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB}; // 01:23:45:67:89:AB
WOL.sendMagicPacket(MAC, sizeof(MAC));
WOL.sendMagicPacket(MAC, sizeof(MAC), 7);
uint8_t MAC[6] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB}; // 01:23:45:67:89:AB
uint8_t SECURE_ON[6] = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54}; // FE:DC:BA:98:76:54
WOL.sendSecureMagicPacket(MAC, sizeof(MAC), SECURE_ON, sizeof(SECURE_ON));
WOL.sendSecureMagicPacket(MAC, sizeof(MAC), SECURE_ON, sizeof(SECURE_ON), 7);
size_t magicPacketSize = 6 + (6 * 16); // FF*6 + MAC*16
uint8_t* magicPacket = new uint8_t[magicPacketSize]; // Magic packet will be stored in this variable
uint8_t MAC[6] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB}; // 01:23:45:67:89:AB
WOL.generateMagicPacket(magicPacket, magicPacketSize, pMacAddress, sizeof(MAC));
size_t magicPacketSize = 6 + (6 * 16) + 6; // FF*6 + MAC*16 + SecureOn
uint8_t* magicPacket = new uint8_t[magicPacketSize]; // Magic packet will be stored in this variable
uint8_t MAC[6] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB}; // MAC Address = 01:23:45:67:89:AB
uint8_t SECURE_ON[6] = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54}; // SecureOn = FE:DC:BA:98:76:54
WOL.generateMagicPacket(magicPacket, magicPacketSize, MAC, sizeof(MAC), SECURE_ON, sizeof(SECURE_ON));