### Initialize WakeOnLan Instance (Arduino C++) Source: https://context7.com/a7md0/wakeonlan/llms.txt This snippet demonstrates how to initialize the WakeOnLan library by creating a WakeOnLan object and associating it with a WiFiUDP socket. It includes connecting to a WiFi network, which is a prerequisite for sending network packets. The setup ensures the microcontroller is connected before any Wake-on-LAN operations can be performed. ```cpp #include #include #include WiFiUDP UDP; WakeOnLan WOL(UDP); const char* ssid = "MyNetwork"; const char* password = "MyPassword123"; void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nConnected to WiFi"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); } void loop() { // Ready to send WOL packets } ``` -------------------------------- ### Manually Set Broadcast Address - C++ Source: https://context7.com/a7md0/wakeonlan/llms.txt Allows manual configuration of the broadcast address for sending magic packets. This is useful for specific network setups or when dealing with multi-subnet environments where the default broadcast address might not be suitable. ```cpp #include #include #include WiFiUDP UDP; WakeOnLan WOL(UDP); void setup() { Serial.begin(115200); WiFi.begin("MyNetwork", "MyPassword123"); while (WiFi.status() != WL_CONNECTED) { delay(500); } // Option 1: Set specific broadcast address for your subnet IPAddress customBroadcast(192, 168, 1, 255); WOL.setBroadcastAddress(customBroadcast); Serial.print("Using broadcast address: "); Serial.println(customBroadcast); // Send magic packet to device on 192.168.1.x network const char *mac = "AA:BB:CC:DD:EE:FF"; WOL.sendMagicPacket(mac); delay(2000); // Option 2: Switch to different subnet broadcast IPAddress otherSubnet(192, 168, 2, 255); WOL.setBroadcastAddress(otherSubnet); Serial.print("Switched to broadcast address: "); Serial.println(otherSubnet); // Send to device on 192.168.2.x network const char *otherMac = "11:22:33:44:55:66"; WOL.sendMagicPacket(otherMac); } void loop() { // Main loop } ``` -------------------------------- ### Send Basic Wake-on-LAN Packet (Arduino C++) Source: https://context7.com/a7md0/wakeonlan/llms.txt This example shows how to send a basic Wake-on-LAN magic packet. It supports both string and byte array representations of MAC addresses and allows for specifying a custom UDP port. The code first establishes a WiFi connection and calculates the broadcast address for better network compatibility before sending the packet. ```cpp #include #include #include WiFiUDP UDP; WakeOnLan WOL(UDP); void setup() { Serial.begin(115200); WiFi.begin("MyNetwork", "MyPassword123"); while (WiFi.status() != WL_CONNECTED) { delay(500); } // Calculate broadcast address for better network compatibility WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask()); } void loop() { // Example 1: Using string MAC address (colon-separated) const char *macString = "00:1A:2B:3C:4D:5E"; bool result = WOL.sendMagicPacket(macString); if (result) { Serial.println("Magic packet sent successfully"); } else { Serial.println("Failed to send magic packet"); } delay(5000); // Example 2: Using byte array MAC address with custom port uint8_t macBytes[6] = {0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E}; result = WOL.sendMagicPacket(macBytes, sizeof(macBytes), 7); // Port 7 instead of default 9 if (result) { Serial.println("Magic packet sent to port 7"); } delay(60000); // Wait 1 minute before next attempt } ``` -------------------------------- ### setBroadcastAddress - Manually Set Broadcast Address Source: https://context7.com/a7md0/wakeonlan/llms.txt Allows manual configuration of the broadcast address. This is useful for specific network setups or when the automatic calculation might not be suitable. ```APIDOC ## setBroadcastAddress - Manually Set Broadcast Address ### Description Manually configures the broadcast address for sending magic packets, useful for specific network configurations or multi-subnet environments. ### Method `void setBroadcastAddress(IPAddress broadcast)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **broadcast** (IPAddress) - Required - The IP address to set as the broadcast address. ### Request Example ```cpp // Option 1: Set specific broadcast address for your subnet IPAddress customBroadcast(192, 168, 1, 255); WOL.setBroadcastAddress(customBroadcast); ``` ### Response #### Success Response (200) N/A (This is a configuration method, not an endpoint returning data). #### Response Example N/A ``` -------------------------------- ### Initialize WiFiUDP and WakeOnLan Source: https://github.com/a7md0/wakeonlan/blob/main/README.md Initializes the WiFiUDP class and the WakeOnLan class, passing the WiFiUDP instance to the WakeOnLan constructor. This is a prerequisite for sending network packets. ```cpp #include WiFiUDP UDP; #include WakeOnLan WOL(UDP); // Pass WiFiUDP class ``` -------------------------------- ### Send Secure Wake-on-LAN Packet (Arduino C++) Source: https://context7.com/a7md0/wakeonlan/llms.txt This code demonstrates sending a Wake-on-LAN packet with the SecureOn password feature for enhanced security. It shows how to use both string and byte array formats for the MAC address and the SecureOn password, and allows for specifying a custom UDP port. The function `wakeSecurePC` sends to the default port 9, while `wakeSecurePCCustomPort` sends to port 7. ```cpp #include #include #include WiFiUDP UDP; WakeOnLan WOL(UDP); void wakeSecurePC() { const char *macAddress = "AA:BB:CC:DD:EE:FF"; const char *securePassword = "11:22:33:44:55:66"; // Send secure magic packet to default port 9 bool result = WOL.sendSecureMagicPacket(macAddress, securePassword); if (result) { Serial.println("Secure magic packet sent successfully"); } else { Serial.println("Failed to send secure magic packet"); } } void wakeSecurePCCustomPort() { // Using byte arrays with custom port uint8_t mac[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; uint8_t password[6] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; // Send to port 7 bool result = WOL.sendSecureMagicPacket(mac, sizeof(mac), password, sizeof(password), 7); if (result) { Serial.println("Secure packet sent to port 7"); } } void setup() { Serial.begin(115200); WiFi.begin("MyNetwork", "MyPassword123"); while (WiFi.status() != WL_CONNECTED) { delay(500); } WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask()); wakeSecurePC(); delay(1000); wakeSecurePCCustomPort(); } void loop() { // Main loop } ``` -------------------------------- ### Send WakeOnLan Packet with SecureOn using Byte Array MAC Source: https://github.com/a7md0/wakeonlan/blob/main/README.md Sends a Wake-on-LAN magic packet with SecureOn authentication using MAC and SecureOn addresses as byte arrays. Allows specifying a custom UDP port. ```cpp 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); ``` -------------------------------- ### Create Raw Magic Packet - C++ Source: https://context7.com/a7md0/wakeonlan/llms.txt Generates a raw magic packet byte array, offering low-level control over packet handling. This is useful for custom transmission methods or when inspecting the packet structure for debugging purposes. ```cpp #include #include #include WiFiUDP UDP; WakeOnLan WOL(UDP); void setup() { Serial.begin(115200); WiFi.begin("MyNetwork", "MyPassword123"); while (WiFi.status() != WL_CONNECTED) { delay(500); } // Prepare MAC address uint8_t mac[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; // Calculate packet size: 6 bytes (0xFF) + 16 repetitions of MAC (6 bytes each) size_t packetSize = 6 + (6 * 16); // = 102 bytes uint8_t *packet = new uint8_t[packetSize]; // Generate the magic packet WOL.generateMagicPacket(packet, packetSize, mac, sizeof(mac)); // Display packet contents Serial.println("Magic Packet Contents:"); for (size_t i = 0; i < packetSize; i++) { if (i > 0 && i % 16 == 0) { Serial.println(); } Serial.printf("%02X ", packet[i]); } Serial.println(); // You can now send this packet using your own UDP implementation // or inspect it for debugging purposes // Clean up delete[] packet; } void loop() { // Main loop } ``` -------------------------------- ### setRepeat - Configure Packet Repetition Source: https://context7.com/a7md0/wakeonlan/llms.txt Configures the number of times a magic packet is repeated and the delay between repetitions. This enhances reliability over unstable networks. ```APIDOC ## setRepeat - Configure Packet Repetition ### Description Sets the number of times to repeat the magic packet transmission and the delay between each transmission, improving reliability on lossy networks. ### Method `setRepeat(uint8_t count, uint16_t delayMs)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **count** (uint8_t) - Required - The number of times to repeat the magic packet. - **delayMs** (uint16_t) - Required - The delay in milliseconds between each packet transmission. ### Request Example ```cpp // Configure repetition: 5 packets with 150ms delay between each WOL.setRepeat(5, 150); ``` ### Response #### Success Response (200) N/A (This is a configuration method, not an endpoint returning data). #### Response Example N/A ``` -------------------------------- ### Send WakeOnLan Packet with SecureOn using Char MAC Source: https://github.com/a7md0/wakeonlan/blob/main/README.md Sends a Wake-on-LAN magic packet with SecureOn authentication using MAC and SecureOn addresses as character arrays. Allows specifying a custom UDP port. ```cpp 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); ``` -------------------------------- ### Configure Packet Repetition - C++ Source: https://context7.com/a7md0/wakeonlan/llms.txt Sets the number of times to repeat the magic packet transmission and the delay between each transmission. This improves reliability on lossy networks by re-sending the packet multiple times. ```cpp #include #include #include WiFiUDP UDP; WakeOnLan WOL(UDP); void setup() { Serial.begin(115200); // Configure repetition: 5 packets with 150ms delay between each WOL.setRepeat(5, 150); WiFi.begin("MyNetwork", "MyPassword123"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask()); Serial.println("\nSending magic packet with 5x repetition..."); const char *mac = "00:11:22:33:44:55"; if (WOL.sendMagicPacket(mac)) { Serial.println("All 5 packets sent successfully"); } else { Serial.println("Some packets failed to send"); } } void loop() { // Main loop } ``` -------------------------------- ### Generate WakeOnLan Magic Packet with SecureOn Source: https://github.com/a7md0/wakeonlan/blob/main/README.md Generates the raw byte array for a Wake-on-LAN magic packet including SecureOn authentication. This function takes both the destination MAC address and the SecureOn key. ```cpp 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)); ``` -------------------------------- ### Generate Secure Magic Packet - ESP32/ESP8266 Source: https://context7.com/a7md0/wakeonlan/llms.txt Generates a raw SecureOn magic packet with a specified password appended. This is useful for custom transmission or debugging secure wake packets. It requires WiFi connectivity and the WakeOnLan library. ```cpp #include #include #include WiFiUDP UDP; WakeOnLan WOL(UDP); void setup() { Serial.begin(115200); WiFi.begin("MyNetwork", "MyPassword123"); while (WiFi.status() != WL_CONNECTED) { delay(500); } // Prepare MAC address and SecureOn password uint8_t mac[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; uint8_t password[6] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; // Calculate packet size: 6 bytes (0xFF) + 16 repetitions of MAC + 6 bytes password size_t packetSize = 6 + (6 * 16) + 6; // = 108 bytes uint8_t *securePacket = new uint8_t[packetSize]; // Generate the secure magic packet WOL.generateSecureMagicPacket(securePacket, packetSize, mac, sizeof(mac), password, sizeof(password)); // Display packet structure Serial.println("Secure Magic Packet Structure:"); Serial.println("First 6 bytes (0xFF header):"); for (int i = 0; i < 6; i++) { Serial.printf("%02X ", securePacket[i]); } Serial.println("\n\nMAC repeated 16 times:"); for (int i = 6; i < 102; i++) { if ((i - 6) % 6 == 0) { Serial.println(); } Serial.printf("%02X ", securePacket[i]); } Serial.println("\n\nSecureOn password (last 6 bytes):"); for (int i = 102; i < 108; i++) { Serial.printf("%02X ", securePacket[i]); } Serial.println(); // Clean up delete[] securePacket; } void loop() { // Main loop } ``` -------------------------------- ### Convert MAC String to Byte Array - ESP32/ESP8266 Source: https://context7.com/a7md0/wakeonlan/llms.txt Converts a MAC address string from various formats (colon-separated, hyphen-separated, continuous hex) into a 6-byte array. This function is essential for parsing MAC addresses before they can be used in WakeOnLan packets. It handles different string formats and returns a boolean indicating success or failure. ```cpp #include #include #include WiFiUDP UDP; WakeOnLan WOL(UDP); void testMACConversion(const char* macString) { uint8_t macArray[6]; Serial.print("Converting: "); Serial.print(macString); bool success = WOL.stringToArray(macArray, macString); if (success) { Serial.print(" -> "); for (int i = 0; i < 6; i++) { Serial.printf("%02X", macArray[i]); if (i < 5) Serial.print(":"); } Serial.println(" (Success)"); } else { Serial.println(" -> Conversion failed!"); } } void setup() { Serial.begin(115200); Serial.println("MAC Address String Conversion Tests:\n"); // Supported format 1: Colon-separated (17 chars) testMACConversion("AA:BB:CC:DD:EE:FF"); // Supported format 2: Hyphen-separated in pairs (14 chars) testMACConversion("AABB-CCDD-EEFF"); // Supported format 3: Continuous hex string (12 chars) testMACConversion("AABBCCDDEEFF"); // Invalid format (wrong length) testMACConversion("AA:BB:CC"); // Invalid format (wrong separators) testMACConversion("AA.BB.CC.DD.EE.FF"); } void loop() { // Main loop } ``` -------------------------------- ### Configure WakeOnLan Packet Repetition Source: https://github.com/a7md0/wakeonlan/blob/main/README.md Sets the number of times the magic packet should be repeated and the delay between repetitions. This can be useful for ensuring packet delivery. ```cpp WOL.setRepeat(3, 100); // Repeat the packet three times with 100ms delay between ``` -------------------------------- ### generateMagicPacket - Create Raw Magic Packet Source: https://context7.com/a7md0/wakeonlan/llms.txt Generates the raw byte array for a magic packet. This function provides low-level control and is useful for custom packet transmission or inspection. ```APIDOC ## generateMagicPacket - Create Raw Magic Packet ### Description Generates a raw magic packet byte array for custom transmission or inspection, providing low-level control over packet handling. ### Method `void generateMagicPacket(uint8_t *packet, size_t packetSize, const uint8_t *mac, size_t macSize)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **packet** (uint8_t *) - Required - Pointer to a buffer where the magic packet will be stored. - **packetSize** (size_t) - Required - The size of the buffer provided for the packet. - **mac** (const uint8_t *) - Required - Pointer to the MAC address (6 bytes) of the target device. - **macSize** (size_t) - Required - The size of the MAC address buffer (should be 6). ### Request Example ```cpp // Prepare MAC address uint8_t mac[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; // Calculate packet size: 6 bytes (0xFF) + 16 repetitions of MAC (6 bytes each) size_t packetSize = 6 + (6 * 16); // = 102 bytes uint8_t *packet = new uint8_t[packetSize]; // Generate the magic packet WOL.generateMagicPacket(packet, packetSize, mac, sizeof(mac)); ``` ### Response #### Success Response (200) N/A (This function populates a buffer with the packet data). #### Response Example N/A ``` -------------------------------- ### Send WakeOnLan Packet with Byte Array MAC Source: https://github.com/a7md0/wakeonlan/blob/main/README.md Sends a Wake-on-LAN magic packet using a MAC address provided as a byte array. Supports specifying a custom UDP port. ```cpp 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); ``` -------------------------------- ### Compute Network Broadcast Address - C++ Source: https://context7.com/a7md0/wakeonlan/llms.txt Calculates the broadcast address for the current network based on the device's IP address and subnet mask. This ensures that Wake-on-LAN packets reach all devices on the local subnet. ```cpp #include #include #include WiFiUDP UDP; WakeOnLan WOL(UDP); void setup() { Serial.begin(115200); WiFi.begin("MyNetwork", "MyPassword123"); while (WiFi.status() != WL_CONNECTED) { delay(500); } Serial.println("\nConnected to WiFi"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); Serial.print("Subnet Mask: "); Serial.println(WiFi.subnetMask()); // Calculate and store broadcast address IPAddress broadcast = WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask()); Serial.print("Broadcast Address: "); Serial.println(broadcast); // Now WOL packets will be sent to the calculated broadcast address // instead of the default 255.255.255.255 const char *mac = "00:11:22:33:44:55"; WOL.sendMagicPacket(mac); } void loop() { // Main loop } ``` -------------------------------- ### Generate WakeOnLan Magic Packet Source: https://github.com/a7md0/wakeonlan/blob/main/README.md Generates the raw byte array for a Wake-on-LAN magic packet, which can then be sent manually. This function takes the destination MAC address. ```cpp 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, MAC, sizeof(MAC)); ``` -------------------------------- ### Send WakeOnLan Packet with Char MAC Address Source: https://github.com/a7md0/wakeonlan/blob/main/README.md Sends a Wake-on-LAN magic packet using a MAC address provided as a character array. Supports specifying a custom UDP port. ```cpp const char *MACAddress = "01:23:45:67:89:AB"; WOL.sendMagicPacket(MACAddress); WOL.sendMagicPacket(MACAddress, 7); ``` -------------------------------- ### calculateBroadcastAddress - Compute Network Broadcast Address Source: https://context7.com/a7md0/wakeonlan/llms.txt Calculates the network's broadcast address based on the current IP address and subnet mask. This ensures packets are sent to the correct broadcast address for the local network. ```APIDOC ## calculateBroadcastAddress - Compute Network Broadcast Address ### Description Calculates the broadcast address for the current network based on IP address and subnet mask, ensuring packets reach all devices on the subnet. ### Method `IPAddress calculateBroadcastAddress(IPAddress ip, IPAddress subnetMask)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **ip** (IPAddress) - Required - The current IP address of the device. - **subnetMask** (IPAddress) - Required - The subnet mask of the network. ### Request Example ```cpp // Calculate and store broadcast address IPAddress broadcast = WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask()); ``` ### Response #### Success Response (200) - **broadcastAddress** (IPAddress) - The calculated broadcast address for the network. #### Response Example ```json { "broadcastAddress": "192.168.1.255" } ``` ``` -------------------------------- ### Set WakeOnLan Broadcast Address Source: https://github.com/a7md0/wakeonlan/blob/main/README.md Configures the broadcast address for sending Wake-on-LAN packets. This can be automatically calculated based on the device's local IP and subnet mask, or set manually. ```cpp WOL.calculateBroadcastAddress(WiFi.localIP(), WiFi.subnetMask()); WOL.setBroadcastAddress("192.168.1.255"); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.