Arduino Wake on LAN ENC28J60

In order to power on your PC, using network conectivity, you need to activate “Wake on LAN” functionality and to connect it to an wired(Ethernet) network. Some computers have the “Wake on WAN” function built in too.

With “Wake on LAN” enabled, while in sleep mode, your computer will continuously waiting (listen on the network) for a special message called “magic packet” who contains its MAC address.
The magic packet can be sent from another device on the network (Smartphone, PC , Arduino, Raspberry PI, Etc. ).

ENC28J60 ehernet module

ENC28J60 ehernet module is a very cheap way to add ethernet conectivity to Arduino.
The official Arduino Ethernet Library is based on W5100 chip, so we will have to use another library called EtherCard witch is especially written for the ENC28J60 chip.
You can install EtherCard Library by downloading it from github and then copying it to “libraries” folder from Arduino installation path (for Windows : C:\Program Files (x86)\Arduino\libraries) .
Download the library form github .
You can connect the ENC28J60 ehernet module to the network by DHCP or by configuring a static IP.

Arduino Wake on LAN

The ENC28J60 ehernet module has 10 pins but we will use only 6 of them to connect with Arduino.
You shall make following connections:

  • SI to Arduino digital pin 11
  • CS to Arduino digital pin 10
  • Vcc to Arduino 3.3V
  • Gnd to Arduino Gnd
  • SCK to Arduino digital pin 13
  • SO to Arduino digital pin 12

To this project i added a momentary button that will trigger the sending of the Wake on LAN packet.
Connect one pin of the momentary button to 3.3V and the other pin to Arduino digital pin 5 and to Gnd through a 10K ohm resistor.

Connect the ENC28J60 ehernet module to your computer’s  RJ45 Ethernet port using a crossover UTP cable.

After making the required connections between Arduino and ENC28J60 ehernet module, you can try the examples contained in the library folder.

ENC28J60 ehernet module DHCP – Arduino sketch

Modify the sketch with your destination MAC and upload it to Arduino using Arduino Studio.
Open the Serial Monitor and set the baud rate to 57600.

#include 

// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
int buttonState = 0;         // variable for reading the pushbutton status
int wol_sent = 0;
byte Ethernet::buffer[700]; // tcp/ip send and receive buffer
static byte targetmac[] = {0xB8, 0x??, 0x??, 0x??, 0x??, 0x??};
static int CONTROL_PIN = 10;
const int buttonPin = 5;

void setup() {
  Serial.begin(57600);
  pinMode(buttonPin, INPUT);
  if (ether.begin(sizeof Ethernet::buffer, mymac, CONTROL_PIN) == 0) {
    Serial.println( "Failed to access Ethernet controller");
  }
  if (!ether.dhcpSetup()) {
    Serial.println("DHCP failed");
  }
  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed.
  if (buttonState == HIGH) {
    sendWol();
  } else {
    wol_sent = 0;
  }
  if (Serial.available() > 0) {
    int incomingByte = Serial.read();
    if (incomingByte == 'w') {
      sendWol();
    }
  }
}
void sendWol() {
  if (!wol_sent) {
    Serial.println("Magic packet sent");
    ether.sendWol(targetmac);
  }
  wol_sent = 1;
}


 

 

ENC28J60 ehernet module STATIC IP – Arduino sketch

Modify the sketch below with your destination MAC and upload it to Arduino.
You can send Wake on LAN pckets by pressing the “w” key or by pushing the momentary button.

 

 

#include 
// ethernet interface ip address
static byte myip[] = { 192, 168, 1, 30 };
// gateway ip address
static byte gwip[] = { 192, 168, 1, 1 };
// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };

int buttonState = 0;         // variable for reading the pushbutton status
int wol_sent = 0;
byte Ethernet::buffer[700]; // tcp/ip send and receive buffer
static byte targetmac[] = {0xB8, 0x??, 0x??, 0x??, 0x??, 0x??};

static int CONTROL_PIN = 10;
const int buttonPin = 5;

void setup() {
  Serial.begin(57600);
  pinMode(buttonPin, INPUT);
  if (ether.begin(sizeof Ethernet::buffer, mymac, CONTROL_PIN) == 0)
    Serial.println( "Failed to access Ethernet controller");

  ether.staticSetup(myip, gwip);
  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  // check if the pushbutton is pressed.
  if (buttonState == HIGH) {
    sendWol();
  } else {
    wol_sent = 0;
  }
  if (Serial.available() > 0) {
    int incomingByte = Serial.read();
    if (incomingByte == 'w') {
      sendWol();
    }
  }
}
void sendWol() {
  if (!wol_sent) {
    Serial.println("Magic packet sent");
    ether.sendWol(targetmac);
  }
  wol_sent = 1;
}


 

 

Activating Wake on LAN functionality on your PC

In order to activate Wake on LAN on your PC, follow these steps:

  • Activate Wake-on-LAN from Bios.
  • Expand Network adapters in the Device Manager tree, select your Ethernet adapter, right-click it and then select Properties.
  • Then select the Power Management tab and check off all three boxes shown below.
  • Next select the Advanced tab, scroll down in the Property box and select Wake on Magic Packet and ensure that it is enabled in the Value list box then click OK.
  • Disable Fast Startup which is a hybrid state first introduced in Windows 8
  • If you are using a Laptop, make sure that it is always plugged in, otherwise it will not start and the Wake on LAN function will drain your battery.
  • The computer must be in either Sleep or Hibernation mode

For windows 10 you can try this guide: Enable Wake on LAN Windows 10

 

Troubleshooting Arduino Wake on LAN ENC28J60

The packets sent have broadcast address (witch means than any computer in the network will receive them). Somewhere in that packet, your computer MAC wil be mentioned.
That means only your computer will power on and other computers on the network will ignore that magic packet.
The easiest way to test if the magic packet is sent, is to install WireSkark.
After opening Wireshark, select your connection (Should be an Ethernet one), and wait for captured data to appear.


Look to column named “Protocol” and search for the type “WOL”. If you find one, it means that you managed to successfully send a Wake on LAN magic packet.

If you can see the packets in Wireshark, but your computer doesnt power on, tou can try to install an WOL application on your smartphone and try from there.
Open Google Play or App Store, search for “Wake on LAN” and install a couple of applications based on their ratings. If one of the Apps works, you should investigate what are the differences between your Arduino and App’s magic packet.
You can see in Wireshark the packets sent by that applications. Usually they are sending two identical packets: one to broadcast address and one to your MAC address.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *