Created
June 14, 2025 10:06
-
-
Save 0xAungkon/6afdc2fab696d21ae0246748f0b8ce37 to your computer and use it in GitHub Desktop.
Ping device if counld't ping sends wol magic packets
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
| #include <ESP8266WiFi.h> | |
| #include <ESP8266Ping.h> | |
| #include <WiFiUdp.h> | |
| const char* ssid = "SSID"; | |
| const char* password = "PWD"; | |
| const IPAddress targetIP(192, 168, 0, 239); | |
| const IPAddress broadcastIP(192, 168, 0, 255); | |
| const byte mac[] = {0x54, 0xE1, 0xAD, 0x51, 0x27, 0x4B}; | |
| WiFiUDP udp; | |
| void sendMagicPacket() { | |
| byte magicPacket[102]; | |
| for (int i = 0; i < 6; i++) { | |
| magicPacket[i] = 0xFF; | |
| } | |
| for (int i = 1; i <= 16; i++) { | |
| memcpy(&magicPacket[i * 6], mac, 6); | |
| } | |
| udp.beginPacket(broadcastIP, 9); | |
| udp.write(magicPacket, sizeof(magicPacket)); | |
| udp.endPacket(); | |
| } | |
| void setup() { | |
| Serial.begin(115200); | |
| WiFi.mode(WIFI_STA); | |
| WiFi.begin(ssid, password); | |
| while (WiFi.status() != WL_CONNECTED) { | |
| delay(500); | |
| Serial.print("."); | |
| } | |
| Serial.println(""); | |
| Serial.println("WiFi connected"); | |
| Serial.print("IP address: "); | |
| Serial.println(WiFi.localIP()); | |
| udp.begin(9); | |
| } | |
| void loop() { | |
| Serial.println("Pinging 192.168.0.239..."); | |
| if (!Ping.ping(targetIP)) { | |
| Serial.println("Ping failed, sending magic packet..."); | |
| sendMagicPacket(); | |
| } else { | |
| Serial.println("Ping successful."); | |
| } | |
| delay(300000); // wait 5 minutes | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment