Project Arduino - Wake On Lan your PC over internet (WoL)!

Hi there,
I recently bought an Ethernet Arduino Rev3 board and I'm trying to write a sketch so I can Wake up my PC over the internet.
I have the server code ready but I can't find out how to send a magic packet via arduino.

I don't know how to program anything else and I'm frustrated with the Udp protocol.
I've found lots of useful links after lots of hours of searching and testing but none of the codes can be compiled successfully.

I always get a message "Error compiling" when I try to compile the codes. I've tried the sketches on Arduino 023, Arduino 1.0.1 & Arduino 1.0.2 with no luck.
I've added any necessary libraries but still no progress.

Can someone help me by sharing his/her code for sending WoL packets over LAN?
Have in mind that I'll be sharing my final code here, so its not just for me, if you feel like spending some time posting.
Any help would be grateful!

Some of the links I've already tried and are not compiling:
http://www.blueleafsoftware.com/Resources/EmbeddedSand/Wake_on_LAN

http://www.arduino.cc/playground/Main/ArduinoWaker
http://arduino.cc/forum/index.php/topic,93848.0.html
http://code.google.com/p/arduino/source/browse/trunk/libraries/Ethernet/Udp.h?r=1094

Can any of you compile any of the sketches?

Can't anyone help me please? I thought it'sbeen an issue for lots of people?

Can anybody please tell me if you can compile the codes given in the links?

The download on the first link compiled ok in IDE 1.0.1.

dxw00d:
The download on the first link compiled ok in IDE 1.0.1.

Dear dxw00d,
can you please tell me what libraries are you using?
Please copy paste all the code from the arduino IDE 1.0.1 sketch and let me know about the libraries you are using.
Are libraries the original ones?

All I can think of is that my Ethernet board is not working properly if you can easily upload the program.

Please send the code that you compiled so I know I don't have any errors when I copy paste or something.

I can see that the <utility/socket.h> library is not highlighted in blue like the others. So I don't think its been recognised.

The code I've trying to upload:

#include <SPI.h>
#include <Ethernet.h>
#include <Utility\Socket.h>
 
void SendWOLMagicPacket(byte * pMacAddress)
{
  // The magic packet data sent to wake the remote machine. Target machine's
  // MAC address will be composited in here.
  const int nMagicPacketLength = 102;
  byte abyMagicPacket[nMagicPacketLength] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  byte abyTargetIPAddress[] = { 255, 255, 255, 255 }; // don't seem to need a real ip address.
  const int nWOLPort = 7;
  const int nLocalPort = 8888; // to "listen" on (only needed to initialize udp)
 
 
  // Compose magic packet to wake remote machine. 
  for (int ix=6; ix<102; ix++)
    abyMagicPacket[ix]=pMacAddress[ix%6];
 
  if (UDP_RawSendto(abyMagicPacket, nMagicPacketLength, nLocalPort, 
  abyTargetIPAddress, nWOLPort) != nMagicPacketLength)
    Serial.println("Error sending WOL packet");
}
 
int UDP_RawSendto(byte* pDataPacket, int nPacketLength, int nLocalPort, byte* pRemoteIP, int nRemotePort)
{
  int nResult;
  int nSocketId; // Socket ID for Wiz5100
 
  // Find a free socket id.
  nSocketId = MAX_SOCK_NUM;
  for (int i = 0; i < MAX_SOCK_NUM; i++) 
  {
    uint8_t s = W5100.readSnSR(i);
    if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) 
	  {
      nSocketId = i;
      break;
    }
  }
 
  if (nSocketId == MAX_SOCK_NUM)
    return 0; // couldn't find one. 
 
  if (socket(nSocketId, SnMR::UDP, nLocalPort, 0))
  {
    nResult = sendto(nSocketId,(unsigned char*)pDataPacket,nPacketLength,(unsigned char*)pRemoteIP,nRemotePort);
    close(nSocketId);
  } else
    nResult = 0;
 
  return nResult;
}

And the errors I get:

C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:1: error: stray '\377' in program
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:1: error: stray '\376' in program
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:51: error: stray '@' in program
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:59:4: error: invalid preprocessing directive #i
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:61:4: error: invalid preprocessing directive #i
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:63:4: error: invalid preprocessing directive #i
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:69:4: error: invalid preprocessing directive #i
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:71:4: error: invalid preprocessing directive #i
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:135: error: stray '#' in program
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:187: error: missing terminating ' character
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:213: error: missing terminating ' character
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:229: error: stray '#' in program
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:229: error: missing terminating ' character
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:1: error: expected unqualified-id before '/' token
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:75: error: expected unqualified-id before '/' token
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:99: error: expected unqualified-id before '/' token

I clicked on the link at the end of "Here's the source for...", to download the sketch. Unzipped it and opened it in the IDE. The link points to http://www.blueleafsoftware.com/Products/MegunoLink/Downloads/WOLTest.zip.

Colour coding in the IDE is really rather hit and miss. It relies on library developers providing the appropriate information, in the form of a keywords.txt file, in the library directory.

Just did the same thing and its not compiling. I get "Error compiling"
Can you please copy,paste & upload the libraries somewhere so I can check them out?
I don't understand the problem!

I'm sure you get more information than just 'Error compiling".

Here is the sketch, directly from the zip file. I didn't have to download any additional libraries.

#include <SPI.h>
#include <Ethernet.h>
#include <Utility\Socket.h>

// Make up a mac Address & an IP address. Both should be globally unique or
// at least unique on the local network. 
static byte g_abyMyMacAddress[] = {0x00,0x1A,0x4B,0x38,0x0C,0x5C};
static IPAddress g_MyIPAddress(192,168,15,44);

// The machine to wake up. WOL should already be configured for the target machine. 
// The free windows program "Wake On LAN Ex 2" by Joseph Cox can be useful for testing the remote
// machine is properly configured. Download it here: http://software.bootblock.co.uk/?id=wakeonlanex2
static byte g_TargetMacAddress[] = {0x00,0x1A,0x4D,0x59,0x98,0xbc};

void setup () 
{
  Ethernet.begin(g_abyMyMacAddress, g_MyIPAddress);
  
  Serial.begin(9600);
  Serial.println("Wake on Lan (WOL) Demo");
  Serial.println("Send 'w' to initiate WOL"); 
}
 
void loop() 
{
 
  // When 'w' is received, send a magic packet to wake the remote machine. 
  if(Serial.available() > 0) 
    if(Serial.read() == 'w') 
    {
      SendWOLMagicPacket(g_TargetMacAddress);
      Serial.println("Magic packet sent");
    }
}

void SendWOLMagicPacket(byte * pMacAddress)
{
  // The magic packet data sent to wake the remote machine. Target machine's
  // MAC address will be composited in here.
  const int nMagicPacketLength = 102;
  byte abyMagicPacket[nMagicPacketLength] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  byte abyTargetIPAddress[] = { 255, 255, 255, 255 }; // don't seem to need a real ip address.
  const int nWOLPort = 7;
  const int nLocalPort = 8888; // to "listen" on (only needed to initialize udp)

  
  // Compose magic packet to wake remote machine. 
  for (int ix=6; ix<102; ix++)
    abyMagicPacket[ix]=pMacAddress[ix%6];
  
  if (UDP_RawSendto(abyMagicPacket, nMagicPacketLength, nLocalPort, 
  abyTargetIPAddress, nWOLPort) != nMagicPacketLength)
    Serial.println("Error sending WOL packet");
}

int UDP_RawSendto(byte* pDataPacket, int nPacketLength, int nLocalPort, byte* pRemoteIP, int nRemotePort)
{
  int nResult;
  int nSocketId; // Socket ID for Wiz5100

  // Find a free socket id.
  nSocketId = MAX_SOCK_NUM;
  for (int i = 0; i < MAX_SOCK_NUM; i++) 
  {
    uint8_t s = W5100.readSnSR(i);
    if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) 
	  {
      nSocketId = i;
      break;
    }
  }

  if (nSocketId == MAX_SOCK_NUM)
    return 0; // couldn't find one. 

  if (socket(nSocketId, SnMR::UDP, nLocalPort, 0))
  {
    nResult = sendto(nSocketId,(unsigned char*)pDataPacket,nPacketLength,(unsigned char*)pRemoteIP,nRemotePort);
    close(nSocketId);
  } else
    nResult = 0;

  return nResult;
}

dxw00d:
I'm sure you get more information than just 'Error compiling".

Here is the error information:

C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:1: error: stray '\377' in program
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:1: error: stray '\376' in program
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:51: error: stray '@' in program
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:59:4: error: invalid preprocessing directive #i
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:61:4: error: invalid preprocessing directive #i
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:63:4: error: invalid preprocessing directive #i
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:69:4: error: invalid preprocessing directive #i
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:71:4: error: invalid preprocessing directive #i
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:135: error: stray '#' in program
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:187: error: missing terminating ' character
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:213: error: missing terminating ' character
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:229: error: stray '#' in program
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:229: error: missing terminating ' character
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:1: error: expected unqualified-id before '/' token
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:75: error: expected unqualified-id before '/' token
C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp:99: error: expected unqualified-id before '/' token

You need to post this file:

C:\Program Files\arduino-1.0.2\libraries\Ethernet\UdpBytewise.cpp

It appears that is has gotten corrupt.

My library is attached here in this post.
Can you please send me yours? UdpBytewise is not originally included with arduino and I added it there manually. In ethernet library folder.
Please send your library as a file so I can test it.

I hope there is nothing wrong with my arduino.

UdpBytewise.cpp (9.65 KB)

UdpBytewise.h (6.81 KB)

I don't have any UdpBytewise files on my machine, and the sketch compiles ok without them.

Dear friend dxw00d,
you helped get passed a very simple problem, which cost me over 5-6 hours of searching.
After I reinstalled arduino interface, then all worked fine.

Thanks again for your great help!

You're most welcome.