Components and supplies
1
Wemos D1 Mini
Apps and platforms
1
Arduino IDE
Project description
Code
WOL-Gateway.ino
c_cpp
The whole WOL gateway
1/* 2 Wake On Lan Gateway for any ESP8266 based board. 3 Compiled on Arduine IDE version 1.8.3 and a Wemos D1 & Mini board 4 Copyright: the GNU General Public License version 3 (GPL-3.0) by Eric Kreuwels, 2019 5 6 Directly forwarding WOL UDP packets to a particular device on your network is in general not a good idea. 7 It potentially causes floading of data on your local LAN induced by external sources 8 It is also limited as it only allows wakeup one device per forwarded port in your router; this because routers typically don't allow to forward UPD packets as a broadcast!!! 9 10 The WOL gateway solves both issues: 11 When the WOL gateway receives a WOL packet (forwarded by the router), it evaluates: 12 - If it is a Secure WOL packet (Prefix should be FF:FF:FF:FF:FF:FF) 13 - If the SecureOn password matches the SecureOn of your choice. 14 Only matching secure WOL packets are broadcasted on your local LAN. 15 - This prevents floading of your LAN due random external UDP packets comming from your router 16 - Only a WOL wakeble device on your local LAN that has a matching MAC address will respond (even if it doesn't supportthe SecureOn option) 17 Router configuration is simple: 18 Give this WOL Gateway device a fixed IP address (assign it via the reserved DHCP list in your router) 19 Forward a port of your linking on your router (advice is from 49152 to 65 535 ) and forward only the UDP packets to this device (port 50000) 20 See: https://www.utilizewindows.com/list-of-common-network-port-numbers/ 21 22*/ 23 24#include <ESP8266WiFi.h> // istall board manager sw via http://arduino.esp8266.com/stable/package_esp8266com_index.json 25#include <ESP8266WebServer.h> 26#include <WiFiUdp.h> 27 28//////////////////////////////////////////////// 29// Class to Send/Receive UDP's: 30//////////////////////////////////////////////// 31class BroadCastDeamon: public WiFiUDP { 32 public: 33 void PostUDP( const char *Packet, IPAddress ip, unsigned int destPort) { 34 PostUDP(Packet, strlen(Packet), ip, destPort); 35 } 36 void PostUDP( const char *Packet, int len, IPAddress ip, unsigned int destPort) { 37 WiFiUDP::beginPacket( ip, destPort); 38 WiFiUDP::write(Packet, len); 39 WiFiUDP::endPacket(); 40 } 41 void BroadcastUDP( const char *Packet, unsigned int destPort) { 42 BroadcastUDP(Packet, strlen(Packet), destPort); 43 } 44 void BroadcastUDP( const char *Packet, int len, unsigned int destPort ) { 45 IPAddress broadcastIP = WiFi.localIP() | (~WiFi.subnetMask()); 46 PostUDP(Packet, len, broadcastIP, destPort); 47 } 48 49 bool CheckReceived() { 50 // if there's data available, read a packet and return length 51 packetSize = parsePacket(); 52 if (packetSize) 53 { 54 //Serial.print("Received UDP packet from ["); 55 //Serial.print(broadcastUdp.remoteIP()); 56 //Serial.print("] : "); 57 // read the packet into packetBufffer 58 int len = read(packetBuffer, 255); 59 packetBuffer[len] = 0; 60 //Serial.println(packetBuffer); 61 return true; 62 } 63 return false; 64 } 65 char *GetPacketData() { 66 return packetBuffer; 67 } 68 int GetPacketSize() { 69 return packetSize; 70 } 71 private: 72 char packetBuffer[256]; //buffer to hold last packet retreived 73 int packetSize; 74}; 75 76BroadCastDeamon WOLDeamon; 77 78//////////////////////////////// 79// the configuration Part of the code: 80 81// SecureOn for WOL has to be 6 hex bytes; Advice change the default. 82byte SecureOn[7] = "123456"; // In most WOL apps that support SecureOn you have to enter the bytes in HEX format like this: "31:32:33:34:35:36" for "123456" 83// Receiver Port for WOL packets forwarded by the router 84unsigned int WOLPort=50000; // just some random choice in the free range. But make sure you forward UDP packets to this port in your router 85 86// Enter your own WLAN credentials 87char wlanSSID[]="YourWiFiSSID"; 88char wlanPWD[]="YourWiFIPWD"; 89 90 91void setup() { 92 delay(1000); 93 Serial.begin(115200); 94 delay(1000); 95 Serial.println("Starting the WOL Gateway "); 96 while (!ConnectToNetwork(wlanSSID, wlanPWD)) { 97 delay(2000); 98 } 99 WOLDeamon.begin(WOLPort); 100} 101 102 103 104void loop() { 105 HandleWOLReceived(SecureOn); 106 delay(10); 107} 108 109 110 111bool ConnectToNetwork(char *ssid, char *pass ) { 112 int timeout=0; 113 bool succes=false; 114 WiFi.mode(WIFI_STA); 115 Serial.print("Connecting SSID: "); 116 Serial.println(ssid); 117 WiFi.begin(ssid, pass); 118 while (WiFi.status() != WL_CONNECTED && timeout < 10) 119 { 120 delay(1000); 121 timeout++; 122 } 123 if (WiFi.status() == WL_CONNECTED) { 124 Serial.println(); 125 Serial.print("Connected, IP address: "); 126 Serial.println(WiFi.localIP()); 127 succes=true; 128 } 129 else { 130 Serial.println("Connecting to SSID Failed "); 131 } 132 // printWifiMode(); 133 return succes; 134} 135 136 137 138#define WOL_SECURE_PACKET_SIZE 108 139bool HandleWOLReceived (byte *pwd) { 140 if (WOLDeamon.CheckReceived() && WOLDeamon.GetPacketSize() == WOL_SECURE_PACKET_SIZE) { 141 byte *pPrefix = (byte *)WOLDeamon.GetPacketData(); 142 byte *pPWD = pPrefix + 102; // start position of the SecureOn 143 // check if it is has both a valid WOL prefix (6 times FF) and that it contains the defined SecureOn password 144 for (int i=0; i<6; i++) { 145 if (pPrefix[i] != 255 || pPWD[i] != pwd[i]) { 146 Serial.println("INVALID WOL packet received"); 147 return false; 148 } 149 } 150 // Broadcast WOL packet on local LAN 151 Serial.println("Valid WOL packet received"); 152 WOLDeamon.BroadcastUDP(WOLDeamon.GetPacketData(), WOL_SECURE_PACKET_SIZE, 7); // accidently used for WOL (official for echo) 153 WOLDeamon.BroadcastUDP(WOLDeamon.GetPacketData(), WOL_SECURE_PACKET_SIZE, 9); // (un)official WOL port 154 } 155 return true; 156} 157 158
Comments
Only logged in users can leave comments