EtherCard is a driver for the Microchip ENC28J60 chip, compatible with Arduino IDE. It is adapted and extended from code written by Guido Socher and Pascal Stang.
High-level routines are provided to allow a variety of purposes including simple data transfer through to HTTP handling.
License: GPL2
EtherCard is available for installation in the Arduino Library Manager. Alternatively it can be downloaded directly from GitHub:
ethercard.zip
See the comments in the example sketches for details about how to try them out.
ENC28J60 | Arduino Uno | Notes |
---|---|---|
VCC | 3.3V | |
GND | GND | |
SCK | Pin 13 | |
MISO | Pin 12 | |
MOSI | Pin 11 | |
CS | Pin 10 | Selectable with the ether.begin() function |
ENC28J60 | Arduino Mega | Notes |
---|---|---|
VCC | 3.3V | |
GND | GND | |
SCK | Pin 52 | |
MISO | Pin 50 | |
MOSI | Pin 51 | |
CS | Pin 53 | Selectable with the ether.begin() function |
Full API documentation for this library is at: http://www.aelius.com/njh/ethercard/
Several example sketches are provided with the library which demonstrate various features. Below are descriptions on how to use the library.
Note: ether
is a globally defined instance of the EtherCard
class and may be used to access the library.
Initiate To initiate the library call ether.begin()
.
uint8_t Ethernet::buffer[700]; // configure buffer size to 700 octets
static uint8_t mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; // define (unique on LAN) hardware (MAC) address
uint8_type vers = ether.begin(sizeof Ethernet::buffer, mymac);
if(vers == 0)
{
// handle failure to initiate network interface
}
To configure IP address via DHCP use ether.dhcpSetup()
.
if(!ether.dhcpSetup())
{
// handle failure to obtain IP address via DHCP
}
ether.printIp("IP: ", ether.myip); // output IP address to Serial
ether.printIp("GW: ", ether.gwip); // output gateway address to Serial
ether.printIp("Mask: ", ether.netmask); // output netmask to Serial
ether.printIp("DHCP server: ", ether.dhcpip); // output IP address of the DHCP server
To configure a static IP address use ether.staticSetup()
.
const static uint8_t ip[] = {192,168,0,100};
const static uint8_t gw[] = {192,168,0,254};
const static uint8_t dns[] = {192,168,0,1};
if(!ether.staticSetup(ip, gw, dns);
{
// handle failure to configure static IP address (current implementation always returns true!)
}
To send a UDP packet use ether.sendUdp()
.
char payload[] = "My UDP message";
uint8_t nSourcePort = 1234;
uint8_t nDestinationPort = 5678;
uint8_t ipDestinationAddress[IP_LEN];
ether.parseIp(ipDestinationAddress, "192.168.0.200");
ether.sendUdp(payload, sizeof(payload), nSourcePort, ipDestinationAddress, nDestinationPort);
To perform a DNS lookup use ether.dnsLookup()
.
if(!ether.dnsLookup("google.com"))
{
// handle failure of DNS lookup
}
ether.printIp("Server: ", ether.hisip); // Result of DNS lookup is placed in the hisip member of EtherCard.
Currently the library does not have a local network ARP cache implemented. This means if sending UDP:
If you are wondering why your local UDP packets are not being received, this is why! (See #59, #181, #269, #309, #351, #368).
The general workaround is to use a gateway and send UDP packets only to non-local network addresses.
There are other Arduino libraries for the ENC28J60 that are worth mentioning:
Read more about the differences at this blog post.