Created
January 17, 2025 17:39
-
-
Save alessaba/da0e8048f26ef33f603b2876e4beddd1 to your computer and use it in GitHub Desktop.
Wi-Fi Wake on WAN using ESP32. Useful to make a inexpensive intermediary WoL device that can send WoL packets to problematic devices like Synology NASs who apparently get their ARP table flushed very quickly and lose the possibility of being woken up remotely using WoL packets
This file contains 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
#include <WiFi.h> | |
#include <WiFiUdp.h> | |
// Configurazione Wi-Fi | |
const char* ssid = "INSERT-WIFI-SSID"; // Nome della rete Wi-Fi | |
const char* password = "INSERT-WIFI-PASSWORD"; // Password della rete Wi-Fi | |
// Configrurazione IP | |
const unsigned int listenPort = 36; // Porta in ascolto | |
const unsigned int targetPort = 9; // Porta per il Magic Packet | |
const uint8_t targetMAC[6] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}; // MAC del dispositivo da svegliare | |
// Configurazione UDP | |
WiFiUDP udp; | |
IPAddress broadcastIP; // IP di broadcast | |
void setup() { | |
Serial.begin(115200); | |
// Connessione al Wi-Fi | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.println("Connessione al Wi-Fi..."); | |
} | |
Serial.println("Connesso!"); | |
Serial.print("IP: "); | |
Serial.println(WiFi.localIP()); | |
// Configura IP di broadcast | |
broadcastIP = ~WiFi.subnetMask() | WiFi.gatewayIP(); | |
Serial.print("Broadcast IP: "); | |
Serial.println(broadcastIP); | |
// Inizializza UDP | |
udp.begin(listenPort); | |
Serial.print("In ascolto sulla porta: "); | |
Serial.println(listenPort); | |
// Configura Light Sleep | |
WiFi.setSleep(true); | |
Serial.println("Wi-Fi configurato in Light Sleep"); | |
} | |
void loop() { | |
// Controlla se è arrivato un pacchetto UDP | |
int packetSize = udp.parsePacket(); | |
if (packetSize > 0) { | |
Serial.println("Pacchetto UDP ricevuto!"); | |
// Invio del Magic Packet | |
sendMagicPacket(); | |
} | |
udp.clear(); // Pulisci pacchetto UDP in modo da poterne ricevere multipli | |
//esp_light_sleep_start(); // Entra in Light Sleep per risparmiare energia | |
} | |
void sendMagicPacket() { | |
// Crea il Magic Packet con il MAC conosciuto | |
uint8_t magicPacket[102] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};// 6 bit 0xFF + 16 ripetizioni del MAC (6 * 17 = 102 byte) | |
for (int i = 1; i <= 16; i++) { | |
memcpy(magicPacket + i * 6, targetMAC, 6); // Copia il MAC ripetuto | |
} | |
// Invia il Magic Packet via UDP | |
udp.beginPacket(broadcastIP, targetPort); | |
udp.write(magicPacket, sizeof(magicPacket)); | |
udp.endPacket(); | |
Serial.println("Magic Packet inviato!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment