Very often it can be useful to be able to turn your PC remotely, whether it's local network at home or office whether to use the Internet to do so. We know that it is basically easy to do the reverse operation off once we have control of your PC ... but how to turn it on?

To this end, we find solace in a feature of which today are equipped with all modern PCs on your network card that is Wake on LAN (also known as WOL). Enabling properly in the BIOS settings and also in activating the network card from the operating system, it causes the shutdown of the network card remains powered (PC ATX power supply) and then listening to the traffic that's on the network. Obviously, in the ISO-OSI level we are in the Data Link Layer (layer 2), then we must think in terms of MAC address, since all TCP / IP is a software layer that is managed by the operating system, which obviously we have a running PC.

The "magic" that allows switching on the PC is just due to a packet of bytes that is transmitted over the network and which takes its name "Magic Packet".

 

magicpacket

It is characterized by a total of 102 bytes of which the first 6 are all 0xFF followed by the MAC address of the PC you want to turn on and repeated 16 times. Why do so many repetitions of the MAC address? Suffice to say that a network card listens for all packets that pass but discards them when it is not the recipient. If the MAC address was entered only once in the package, surely we would have the possibility that a normal packet traffic (downloading a file, email, ...) has a sequence of bytes in it very similar to it and at that point the PC mistakenly interpret it as a "Magic Packet" and hit. Repeating 16 times the MAC address is drastically reduced (essentially zero) this possibility. The best way to transfer the package is to use UDP broadcast, because we do not need the TCP handshake, and we could not even use it properly whereas the PC is off target. The moment, the PC "hears" this sequence of 102 bytes and recognize in it its MAC address ... that's the magic come true ... the PC turned on!

Inspired by this article by Marco Lai, "Wake on Lan power on the computer with Arduino ethernet", it obviously could not miss my implementation for. Net Micro Framework within the library uPLibrary (arrived at version 1.7.0.0). Inside the namespace for the Networking, uPLibrary.Networking, I added the static class Wol that exposes the only static Wake() method , which provides as its only parameter an array of bytes that represents the MAC address of network card PC to boot.

Using a Netduino board, we can build a simple application that starts the PC to the pressure switch present on the same board.

 

   1: public class Program
   2: {
   3:     private static InterruptPort sw;
   4:  
   5:     public static void Main()
   6:     {
   7:         // write your code here
   8:         sw = new InterruptPort(Pins.ONBOARD_SW1, true, 
   9:                                 Port.ResistorMode.Disabled, 
  10:                                 Port.InterruptMode.InterruptEdgeHigh);
  11:         sw.OnInterrupt += new NativeEventHandler(sw_OnInterrupt);
  12:  
  13:         Thread.Sleep(Timeout.Infinite);
  14:     }
  15:  
  16:     static void sw_OnInterrupt(uint data1, uint data2, DateTime time)
  17:     {
  18:         Wol.Wake(new byte { 0x00, 0x26, 0x22, 0xA2, 0xF2, 0xD3 });
  19:     }
  20:  
  21: }

Obviously, the whole is available on Codeplex and has also been updated in the Nuget Gallery !

I remember that for the correct operation, your PC network card must support Wake On LAN from the  and on a you have to do a proper setup in the BIOS (sometimes also some settings in the driver's operating system).