{"id":40,"date":"2011-08-02T11:32:50","date_gmt":"2011-08-02T09:32:50","guid":{"rendered":"http:\/\/localhost\/~johan\/wordpress\/?p=40"},"modified":"2014-07-20T14:45:25","modified_gmt":"2014-07-20T12:45:25","slug":"wake-on-lan","status":"publish","type":"post","link":"https:\/\/www.chaosgeordend.nl\/wordpress\/2011\/08\/02\/wake-on-lan\/","title":{"rendered":"Wake on LAN"},"content":{"rendered":"<p>To enable wake-on-LAN over the Internet, the open hardware platform <a href=\"https:\/\/www.arduino.cc\/\">Arduino<\/a> is used.<\/p>\n<p>Not all routers support wake-on-LAN from the Internet to LAN. This depends on whether you can configure the <a href=\"https:\/\/www.petri.co.il\/csc_arp_cache.htm\">ARP cache<\/a> and send or forward a WOL broadcast package.<\/p>\n<p>To support WOL, in this case on an Apple TimeCapsule, an <a href=\"https:\/\/www.sparkfun.com\/products\/10536\">Arduino Ethernet Pro board<\/a> is used. Thus having a low-power device (Arduino) listening for WOL.<\/p>\n<p>Both the Arduino board and machine to wake-up are connected via Ethernet to the TimeCapsule. The Arduino is powered using the TimeCapsule USB port. <\/p>\n<p><!--more--><\/p>\n<p>The Arduino is programmed to listen for wake-up packets and, if any received, send a broadcast wake-up packet.<\/p>\n<p>Here a Perl CGI script is used to first send a WOL packet to the Arduino and then redirect to the domain of the server just woken up. Of course, this assumes the script to be on an other, on-line, server. For other applications, e.g. remote desktop, you can just send the WOL packet from a terminal session.<\/p>\n<p>The following code is run on the <strong>Arduino<\/strong>:<\/p>\n<pre lang=\"shell\">\r\n\/*\r\n* WakeUpCube\r\n*\r\n* 2011-07-28 JvO, New sketch\r\n*\r\n* Reference links:\r\n* https:\/\/arduino.cc\/en\/Reference\/ServerConstructor\r\n* https:\/\/arduino.cc\/forum\/index.php?topic=62185.0\r\n*\r\n* Derived from Arduino WakeMyPc\r\n*   Ricardo Dias\r\n*   https:\/\/ricardo-dias.com\/\r\n*\r\n* This sketch sends the \"magic packet\" to wake up\r\n* a PC on Local Area Network [when a push-button\r\n* is pressed.]\r\n*\/\r\n#include <SPI.h>  \/\/ needed for Arduino versions later than 0018\r\n#include <Ethernet.h>\r\n#include <Udp.h>  \/\/ UDP library from: bjoern@cs.stanford.edu 12\/30\/2008\r\n\/\/ Arduino configuration\r\nbyte arduinoMAC[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; \/\/ Ethernet shield MAC address\r\nbyte arduinoIP[] = { 10, 0, 1, 203 };\r\nbyte gatewayIP[] = { 10, 0, 1, 1 };\r\nbyte subnetMask[] = { 255, 255, 255, 0 };\r\nunsigned int localPort = 9; \/\/ local port to listen on\r\n\/\/ the IP broadcast and wake-up target MAC address\r\nbyte targetIp[] = { 10, 0, 1, 255 }; \/\/ LAN broadcast address 10.0.1.255\r\nint targetPort = 9;\r\nbyte targetMAC[] = { 0x00,0x12,0x34,0x56,0x78,0x90 }; \/\/ MAC address of the machine to wake-up\r\n\/\/ these two variables are set when a packet is received\r\nbyte remoteIp[4];        \/\/ holds received packet's originating IP\r\nunsigned int remotePort; \/\/ holds received packet's originating port\r\n\/\/ buffers for receiving and sending data\r\nchar packetBuffer[UDP_TX_PACKET_MAX_SIZE]; \/\/ buffer to hold incoming packet,\r\nchar replyBuffer[] = \"acknowledged\";       \/\/ a string to send back\r\n\/\/ serial connection speed\r\nconst int serialBaudRate = 9600;\r\n\/\/ Initialisation\r\nvoid setup() {\r\n\/\/ initialize the ethernet device\r\nEthernet.begin( arduinoMAC, arduinoIP, gatewayIP, subnetMask );\r\n\/\/ initialize server\r\nUdp.begin( localPort );\r\n}\r\n\/\/ Main LOOP\r\nvoid loop() {\r\ndelay(1000);\r\n\/\/ if there's data available, read a packet\r\nint packetSize = Udp.available(); \/\/ note that this includes the UDP header\r\nif(packetSize) {\r\npacketSize = packetSize - 8; \/\/ subtract the 8 byte header\r\n\/\/ read the packet into packetBufffer and get the senders IP address and Port number\r\nUdp.readPacket( packetBuffer, UDP_TX_PACKET_MAX_SIZE, remoteIp, remotePort );\r\n\/\/ send the wake-up packet\r\nsendPkt();\r\n}\r\n}\r\n\/\/ Send the wake-up on LAN message (aka magic packet)\r\nvoid sendPkt() {\r\nbyte all[102];\r\nint i, c1, j=0;\r\n\/\/ the 'magic packet' consists of 6 times 0xFF followed\r\n\/\/ by 16 times the hardware address (MAC)\r\nfor( i = 0; i < 6; i++, j++ ) {\r\nall[j] = 0xFF;\r\n}\r\nfor( i = 0; i < 16; i++ ) {\r\nfor( c1 = 0; c1 < 6; c1++, j++ ) {\r\nall[j] = targetMAC[c1];\r\n}\r\n}\r\nUdp.sendPacket( all, 102, targetIp, targetPort );\r\n}\r\n<\/pre>\n<p>The <strong>Perl CGI script<\/strong> to send the WOL package and redirect to the server:<\/p>\n<pre lang=\"Perl\">\r\n#!\/usr\/bin\/perl\r\n###################################################\r\n# Send Wakeup On LAN broadcast message and redirect\r\n#\r\n# usage: https:\/\/yourURL\/cgi\/wakeup.cgi\r\n# test: https:\/\/yourURL\/cgi\/wakeup.cgi?test=1\r\n#\r\n# 25 july 2011, JvO, New script version 0.1\r\n###################################################\r\nuse CGI;\r\nuse CGI::Carp qw( fatalsToBrowser ); # pass errors to the browser\r\nuse Socket;\r\nuse POSIX;\r\n$query = CGI::new();\r\n&getParms;   # Get parameters\r\n&sendWOL;    # Send Wakeup on LAN\r\n&returnHtml; # Return result page\r\n#\r\n# Set params\r\n#\r\nsub getParms {\r\n$destAddr = \"192.168.10.20\";    # Destination IP Adres\r\n$destMAC  = \"de:ad:be:ef:fe:ed\"; # Ethernet Pro MAC address\r\n$hostURL = \"https:\/\/your.url.here\"; # the URL to redirect to\r\n$port     = 9;\r\n# Remove colons\r\n$destMAC =~ tr\/:\/\/d;\r\n$test = $query-&gt;param(\"test\");\r\n}\r\n#\r\n# Return HTML page\r\n#\r\nsub returnHtml {\r\n# redirect to the Cube\r\nprint \"Content-type: text\/htmlnn\";\r\nif ($test) {\r\nprint \"&lt;html&gt;&lt;head&gt;&lt;title&gt;Wake on LAN&lt;\/title&gt;&lt;\/head&gt;n\";\r\nprint \"&lt;body bgcolor=\"#CCCCCC\"&gt;n\";\r\nprint \"&lt;hr size=5 width=75%&gt;&lt;p&gt;n\";\r\nprint \"&lt;center&gt;&lt;h1&gt;Server at IP adres \"$destAddr\" has been sent a wakeup msg&lt;\/h1&gt;&lt;\/center&gt;n\";\r\nprint \"&lt;hr size=5 width=75%&gt;n\";\r\nprint \"&lt;\/body&gt;&lt;\/html&gt;n\";\r\n}\r\nelse {\r\nprint \"&lt;meta HTTP-EQUIV=\"REFRESH\" content=\"2; url=$hostURL\/gallery3\"&gt;n\";\r\nprint \"&lt;html&gt;&lt;body&gt;You are being redirected to $hostURL...&lt;\/body&gt;&lt;\/html&gt;n\";\r\n}\r\n}\r\nsub sendWOL {\r\n# Magic packet is 6 bytes of FF followed by the MAC address 16 times\r\nmy $magic = (\"xff\" x 6) . (pack('H12', $destMAC) x 16);\r\n# create socket handle and connect\r\nsocket($sh, PF_INET, SOCK_DGRAM, getprotobyname('udp')) or die \"Socket could not be created. Reason: $!n\";\r\nconnect($sh, sockaddr_in($port, inet_aton($destAddr))) or die print $runlog \"Socket connect failed: $! host: $destAddr port: $portn\";\r\n# send the wakeup packet\r\nprint $sh $magic;\r\nclose($sh);\r\n}\r\n<\/pre>\n<p>The TimeCapsule is set to forward WOL packages, using the <strong>Airport configuration<\/strong> program \"Advanced\" option  (sorry for the screenprint being in Dutch):<\/p>\n<div id=\"entry_img\">\n<img decoding=\"async\" alt=\"Airport_forward.png\" src=\"https:\/\/www.chaosgeordend.nl\/mt-blog-cg\/images\/Airport_forward.png\" \/><\/div>\n","protected":false},"excerpt":{"rendered":"<p>To enable wake-on-LAN over the Internet, the open hardware platform Arduino is used. Not all routers support wake-on-LAN from the Internet to LAN. This depends on whether you can configure the ARP cache and send or forward a WOL broadcast package. To support WOL, in this case on an Apple TimeCapsule, an Arduino Ethernet Pro &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.chaosgeordend.nl\/wordpress\/2011\/08\/02\/wake-on-lan\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Wake on LAN&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43,31],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-arduino","category-electronicaelectronics"],"_links":{"self":[{"href":"https:\/\/www.chaosgeordend.nl\/wordpress\/wp-json\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chaosgeordend.nl\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chaosgeordend.nl\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chaosgeordend.nl\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chaosgeordend.nl\/wordpress\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":2,"href":"https:\/\/www.chaosgeordend.nl\/wordpress\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":91,"href":"https:\/\/www.chaosgeordend.nl\/wordpress\/wp-json\/wp\/v2\/posts\/40\/revisions\/91"}],"wp:attachment":[{"href":"https:\/\/www.chaosgeordend.nl\/wordpress\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chaosgeordend.nl\/wordpress\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chaosgeordend.nl\/wordpress\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}