Arduino Wake on LAN ENC28J60
To power on your PC, using network connectivity, you need to activate the “Wake on LAN” functionality and to connect it to a 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 ethernet module
ENC28J60 ethernet module is a very cheap way to add Ethernet connectivity to Arduino.
The official Arduino Ethernet Library is based on W5100 chip, so we will have to use another library called EtherCard which is specially written for the ENC28J60 chip.
You can install EtherCard Library by downloading it from GitHub and then copying it to the “libraries” folder from the Arduino installation path (for Windows : C:\Program Files (x86)\Arduino\libraries).
Download the library from GitHub.
You can connect the ENC28J60 ethernet module to the network by DHCP or by configuring a static IP.
Arduino Wake on LAN
The ENC28J60 ethernet module has 10 pins but we will use only 6 of them to connect with Arduino.
You shall make the 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 the GND through a 10K ohm resistor.
Connect the ENC28J60 ethernet module to your computer’s RJ45 Ethernet port using a crossover UTP cable.
After making the required connections between Arduino and ENC28J60 ethernet module, you can try the examples contained in the library folder.
ENC28J60 ethernet 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 ethernet module STATIC IP – Arduino sketch
Modify the sketch below with your destination MAC and upload it to Arduino.
You can send Wake on LAN packets 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
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 will 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 Wireshark.
After opening Wireshark, select your connection (Should be an Ethernet one), and wait for captured data to appear.
Look to the 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 doesn’t power on, you can try to install a 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 those applications. Usually, they are sending two identical packets: one to broadcast address and one to your MAC address.
Hello,
Your article is nice. I tried it for static IP on Arduino but got error in Arduino IDE. In the article it is not mentioned which library are included. I am eagrly waiting for your reply.
Regards,
Ravi
Hello,
I had used the EtherCard library (https://github.com/njh/EtherCard).
You can find examples on the library’s github page.
Did you try to install Whire Shark (https://www.wireshark.org/) ?