enc28J60 and Arduino (4)

by luca
19 comments

In the following post, I’m going to show you how to power on a PC using the Wake on Lan feature and your Arduino.
Almost all the actual network adapters have a feature called Wake on Lan, that is the ability to “wake” the PC with a special command sent through the network.

Requirements

Two main requirements to use this feature:

  • if your network adapter is not embedded in the motherboard, you have to connect them with a cable:

  • the WOL functionality has to be enabled in the BIOS:

A typical limit in WOL usage is that network adapter isn’t able to wake the PC if it was shut down by power loss. Actually, recent motherboards can enable WOL functionality even after a power loss; you can configure this feature in motherboard’s BIOS:

Magic Packet

The wake command is issued sending on the network a specific packet, called Magic Packet. This packet is receved by all the devices connected to local network because it presents – as destination MAC address – the broadcast address (FF:FF:FF:FF:FF:FF). It includes six bytes valued FF, followed by the target PC’s MAC address repeated 16 times.

Even if it’s not important which high level protocols you choose, it’s a convention to use UDP protocol and destionation port 7 or 9.

Arduino

[highlight color=”yellow”]Note: EtherCard library had a bug in the Magic Packet creation. I solved the problem and my change has been accepted in the official repository: so make sure to download library’s latest version[/highlight]

You can use sendWol() method to send the Magic Packet; that method needs target PC’s MAC address as a parameter:

static byte targetmac[] = {0x00,0x1A,0x4B,0x38,0x0F,0x5C};
...
ether.sendWol(targetmac);

As usual, you can find a complete example in the GitHub repository related to this tutorial.

Using a network analysis tool like Wireshark, you can inspect the packet sent by Arduino and note that it’s correct:

  • in red the destination broadcast MAC address
  • in green the choice of IP and UDP protocol
  • in light blue the target PC’s MAC address repeated 16 times

Related Posts

19 comments

Arup Tuesday February 28th, 2012 - 10:28 AM

Nice work. Next would be interfacing an GPRS shield with the same Arduino so that remote wakeup can be achieved. Of course after PC powers on, we can use it and access files remotely using remote desktop software.

Reply
luca Tuesday February 28th, 2012 - 10:38 AM

Thanks Arup! It’s a good idea, a GSM shield has just been shipped to my home 😉 Stay tuned for a new blog post about it!

Reply
Gonzalo Wednesday March 7th, 2012 - 10:56 PM

Great Job Luca , will work with wiz5100 ethernet shield?

Reply
luca Wednesday March 7th, 2012 - 11:06 PM

Thanks Gonzalo,

no, it won’t work because W5100 chip is very different from ENC28J60… as it is the chip chosen for the “official” ethernet shield, you can find a lot of documentation on Playground.

Reply
Christian Leicht Jørgensen Wednesday April 11th, 2012 - 09:04 PM

Thank you for this nice piece of code.
It’s a shame it doesn’t work on the W5100 chip.
Have some of you found a way to get it to work on the W5100 chip?

Reply
luca Thursday April 12th, 2012 - 10:34 AM

Hi Christian,

it seems WZ5100 does not supporto WOL functionality…
I found an article from Wiznet official blog about W5200 that adds WOL:

http://blog.wiznet.co.kr/w5200-arduino-ethernet-library

Reply
Tiry Tuesday October 2nd, 2012 - 09:15 PM

thank you very much for correcting the library. I tried to fix it also, it is more useful now! Work fine with ENC2860 connected to Arduino. WOL ok for me. Thank’s again !

Reply
luca Tuesday October 2nd, 2012 - 10:04 PM

Thanks!

Reply
lucadentella.it – Il blog in cinese ;) Wednesday October 10th, 2012 - 01:43 PM

[…] all’utente zcbzjx, il mio tutorial su come inviare pacchetti di Wake on Lan usando Arduino+enc28j60 è disponibile in lingua […]

Reply
gl Sunday May 5th, 2013 - 05:17 PM

when i try to upload i get a ethernet not declared message.. and nu upload to the arduino uno

i have the library in the correct folder?

using IDE 1.0.4

Reply
luca Tuesday May 7th, 2013 - 08:22 AM

Hi! Did you follow this tutorial about how to install the library?

Reply
Francesco Tuesday May 21st, 2013 - 10:09 AM

ciao,
interessante articolo.
io ho una scheda (non shield) con enc 28j60.
volevo fare un wake-on-lan però in modo che ricevendo un magic su porta 9 ad esempio dal router, mi spedisca un magic packet come nell’esempio contenente il MAC del pc che voglio accendere. Quindi su ricezione del pacchetto e non su pressione del tasto ‘w’
Inoltre non ho DHCP quindi dovrei impostare
IP, Subnet mask e Gateway. Come faccio?
Ma un elenco completo (con descrizione) delle primitive dentro a EtherCard dove lo trovo?
Grazie, ciao

Reply
luca Wednesday May 22nd, 2013 - 09:37 AM

Ciao Francesco

non ho capito bene la tua necessità: vuoi comandare il wake-on-lan da remoto? Ad es. pubblicando una pagina web da Arduino che puoi raggiungere via Internet con un pulsante “wake” che fa inviare il magic packet? In questo caso puoi combinare i miei due tutorial (wake on lan e accensione led da pagina web)

Reply
Clubbby Thursday October 3rd, 2013 - 06:38 PM

Thanks for this. I just had it in my head to make my media PC turn on over WOL along with an IR receiver waiting for a “Power” signal from our remote.

I already have several ENC28J60’s and thanks to you I know that I can use one for just this task (assuming the media PC supports WOL, will check when I get home).

Reply
Clubbby Monday October 7th, 2013 - 11:58 PM

I built this out yesterday and it works like a charm! Again, thanks.

Reply
enrico Tuesday April 29th, 2014 - 11:36 PM

si puo ank fare via usb?

Reply
Landy Sunday June 8th, 2014 - 12:53 PM

I’m having trouble running this, even after downloading the library it reports errors. Could you help out?

Reply
luca Sunday June 8th, 2014 - 08:06 PM

Hi, which errors?

Reply
Angelo Sunday February 26th, 2017 - 11:24 AM

Ciao Luca, ti scrivo perchè ho un problema che non riesco a risolvere e tu sicuramente avrai una soluzione.
Vorrei realizzare un progettino che oltre a pilotare dei relè connessi con Arduino Mega, mi faccia uno scan degli indirizzi IP fissi della mia rete LAN e mi torni come informazione tutti quelli che sono attivi e connessi.
Tutta la parte dei relè e delle informazioni l’ho risolta. Il problema è che quando cerco di utilizzare il PING per vedere cosa è connesso in LAN tutto il resto del programma non funziona più.
E’ come se vi fosse incompatibilià tra l’utilizzo di UDP e l’utilizzo di .
Dopo avere fatto ping ai vari indirizzi fissi, come facci a fare tornare la w5100 in posizione di ascolto?

grazie anticipatamente
Angelo

Reply

Leave a Comment

3 × one =