Ethernet.h library and ENC28J60 Ethernet Network Module

Hello and Good day to all.

I am Thinking to buy some ethernet shield or ethernet module for my arduino mega 2560.
My local store only has the ENC28J60 Ethernet Network Module.
I was wondering if this module is supported by the Ethernet.h library.
In my particular case i am going to use the arduino only to send data to an ethernet switch using UDP.
It will be my very first time i use an ethernet shield/module and from what i have seen in the librarys integrated examples it looks easy to implement in my project.
The general idea behind my project is to read data from a serial port and save them in a string and when the string is complete, transmit it to the ethernet switch via the arduino ethernet module.

Thank you all !

No. Only the WizNet W5100, W5200, and W5500 are supported by the Ethernet library.

For the ENC28J60 you can use this library:

or this one:

use the EthernetENC library for new Arduino projects with enc28j60

What do you mean sending the data to an ethernet switch? An ethernet switch merely redirects ethernet packets to their intended destination, if your Arduino will just send packets to a switch it won't be achieving anything useful. Most ethernet switches do not have IP addresses since they work at a lower level, so using UDP to send data to a switch is unlikely to succeed. Is the switch much more than a simple switch or is the destination not eh switch at all, the switch just being where the Arduino will connect?

Thank you all for your answers.
Like i said i am totally inexperienced with networking and Ethernet So when it comes to the switch i have to say that i am still considering this part and i will consult someone that knows better :slight_smile: . There will be two pc's connected on the switch also each having a static address.

Juraj:
use the EthernetENC library for new Arduino projects with enc28j60

Can it work with the arduino mega 2560 ?

I think i found my answer here : EthernetENC - Arduino Reference
It should work with all arduino boards.

Juraj:
use the EthernetENC library for new Arduino projects with enc28j60

Please forgive my ignorance.
Is the EthernetENC.h library directly compatible with the Ethernet.h library ?

For example in the examples of the Ethernet.h library do i just need to replace
the #include <Ethernet.h> with #include <EthernetENC.h> ?

I see no examples for that library and i wonder why.

Looks like i am correct.

the description in IDE Library manager has "EthernetENC library is compatible with all Arduino architectures" and the "More info" link goes to Home · jandrassy/EthernetENC Wiki · GitHub

Thanks !

I do need some further advice though if possible please.

At the moment i think i am on the right path with this project but i would like to ask this question.
I have used the EthernetENC.h library and the arduino is transmitting udp packets directly to my pc through a crosover cable.
Would it be somehow possible that when i hook the arduino on the network switch, i will be able to read these udp packets from all the pc’s (hosts) that are connected on that switch ? (not outside the lan).
From what iv’e read so far on the internet, this is called multicasting, i think.
Here is the code that i have so far :

#include <SPI.h>        
#include <EthernetENC.h>
#include <EthernetUdp.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192,168,1,21);
IPAddress RemoteIP(192,168,1,11);

const unsigned int RemotePort = 12345;    // The port that the program on the pc will listen on
const unsigned int localPort = 54321;      // local port to listen on (not to be used since arduino will only send data)

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() 
{
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
}

void loop() 
{  
  Udp.beginPacket(RemoteIP, RemotePort);
  Udp.write ("Test Data From Arduino\n");
  Udp.endPacket();
  delay (1000);  
}

Any advices ?
Thank you in advance !

it should work if you send an UDP packet to a multicast address, but I didn't test it.

TCP/IP is done by the old C uIP (microIP) library which is in core of EthernetENC. the uIP library doc has "uIP supports broadcast packets as well as sending multicast packets."

Unfortunately i have zero knowledge when it comes to tcp/ip and very little about udp.

I googled about multicast addresses and started testing adresses metnioned here:

I have tested a few and none of them worked. For example i have set the following as "RemoteIP" in the code and this is what i could see using Wireshark software:
224.0.0.11 , 224.0.1.11 , 224.0.0.0, 232.0.0.0
all the above adresses gave me this message on Wireshark

no        time             source              destination   protocol  legnth                    info.
829	132.080587	de:ad:be:ef:fe:ed	Broadcast	ARP	60	Who has 192.168.1.1? Tell 192.168.1.21
2155	308.379944	de:ad:be:ef:fe:ed	Broadcast	ARP	60	Who has 192.168.1.1? Tell 192.168.1.21
etc...

To me it is weird that Wireshark is showing 192.168.1.1 as destination ip which is not the one assigned to the pc (192.168.1.11) nore it is the one declared as RemoteIP in the code.

What was the ip address of your arduino when you tried the broadcast? I suspect it was 192.168.1.21 and the netmask was probably 255.255.255.0. To send a broadcast message from that ip the broadcast would have to be to 192.168.1.0.
If you are trying to send a message to an address 224.x.x.x or 232.x.x.x the ip software would recognise that is not on your subnet and ask the router (192.168.1.1). Unfortunately broadcast/multicast packets are not routed so the actual broadcast would probably not be sent. I don't know why the arduino thinks 192.168.1.1 is the router - maybe default, maybe from dhcp.

countrypaul:
To send a broadcast message from that ip the broadcast would have to be to 192.168.1.0.

I don't think so! A quote from wikipedia:

[..] in order to calculate the broadcast address to transmit a packet to an entire IPv4 subnet using the private IP address space 172.16.0.0/12, which has the subnet mask 255.240.0.0, the broadcast address is calculated as 172.16.0.0 bitwise ORed with 0.15.255.255 = 172.31.255.255.

(Broadcast address - Wikipedia)

So, the broadcast would have to be to 192.168.1.255 .

Erik_Baas:
I don't think so! A quote from wikipedia:

[..] in order to calculate the broadcast address to transmit a packet to an entire IPv4 subnet using the private IP address space 172.16.0.0/12, which has the subnet mask 255.240.0.0, the broadcast address is calculated as 172.16.0.0 bitwise ORed with 0.15.255.255 = 172.31.255.255.

(Broadcast address - Wikipedia)

So, the broadcast would have to be to 192.168.1.255 .

You're right, no idea why I put a 0 there.

countrypaul:
If you are trying to send a message to an address 224.x.x.x or 232.x.x.x the ip software would recognise that is not on your subnet and ask the router (192.168.1.1). Unfortunately broadcast/multicast packets are not routed so the actual broadcast would probably not be sent. I don’t know why the arduino thinks 192.168.1.1 is the router - maybe default, maybe from dhcp.

I do not know. Right now there is no router or switch involved. Just direct connection between the arduino and the PC.
I am not going to use a router in the future. I intend to use a simple switch.

countrypaul:
What was the ip address of your arduino when you tried the broadcast? I suspect it was 192.168.1.21 and the netmask was probably 255.255.255.0.

That is correct. the arduino “local” ip is 192.168.1.21 and the “remote” ip of the pc is set to 192.168.1.11

Erik_Baas:
I don't think so! A quote from wikipedia:

[..] in order to calculate the broadcast address to transmit a packet to an entire IPv4 subnet using the private IP address space 172.16.0.0/12, which has the subnet mask 255.240.0.0, the broadcast address is calculated as 172.16.0.0 bitwise ORed with 0.15.255.255 = 172.31.255.255.

(Broadcast address - Wikipedia)

So, the broadcast would have to be to 192.168.1.255 .

I have read the link but i am unable to understand the procedure of choosing the apropriate ip address and subnet mask in order to broadcast to every host (pc) on the local network (on the network switch that i will in the future use).
I am going to read it over ant try to understand but would you please care to explain it to me also ?

Thank you !

P.S.
I suspect that not having the switch (direct connection between arduino and pc) does not affect the above procedure and the setup i will have to use after i install the switch. Is that correct ?

OK, a simple explanation - hopefully I haven't simplified it too far and introduced errors.

The basic way to determine the broadcast address for a given subnet in which a particular ip address resides ( is it to start with the subnet mask - that tells you all the addresses in your subnet by inverting all the bits. So 255.255.255.0 becomes 0.0.0.255 (or in the example from Erik, 255.240.0.0 becomes 0.15.255.255. you then OR this with the ip address you have so 0.0.0.255 OR 192.168.1.21 gives you 192.168.1.255 (and 0.15.255.255 OR 172.16.n.n gives you 172.31.255.255).

Choosing an ip address is usually a case of either using DHCP - which provides one for you - normally in a private IP range, or selecting one yourself again normally from a private ip range. Private ip ranges are by convention:

[RFC1918 name IP address range Classful description[/td]
[/tr]
24-bit block
10.0.0.0 – 10.255.255.255
single class A network
20-bit block
172.16.0.0 – 172.31.255.255
16 contiguous class B networks
16-bit block
192.168.0.0 – 192.168.255.255
256 contiguous class C networks
[/table]
The Netmask is then chosen effectively based on how many ip addresses are likely to be used, for home networks that is usually assumed to be up to 255, so the mask becomes 255.255.255.0. If you expected up to 32767 you would use 255.255.0.0 but you could choose any power of 2 less 1, so 255, 511, 2023...
All this pertains to IP V4 which is what is used on al the arduino ethernet/Wifi options.
A network address ending in 255 is usually taken as a broadcast address which is why the default for routers is often .254.
So it is common to see 192.168.1.254 for home routers (at least in the UK).

Thank you for the explanation. It makes sense.

Here are the settings i have tried but without sucess:
PC : ip 192.168.1.11 subnet 255.255.255.0
Arduino : ip 192.168.1.255 subnet default (255.255.255.0)
I get the same message on Wireshark :

no        time             source              destination   protocol  legnth                    info.
1028	465.145274	de:ad:be:ef:fe:ed	Broadcast	ARP	60	Who has 192.168.1.255? Tell 192.168.1.21

here is the code i use :

#include <SPI.h>        
#include <EthernetENC.h>
#include <EthernetUdp.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,21);
IPAddress RemoteIP(192,168,1,255);
const unsigned int RemotePort = 12345;    // The port that the program on the pc will listen on
const unsigned int localPort = 54321;      // local port to listen on (not to be used since arduino will only send data)

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup()
{
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
}
void loop()
{  
  Udp.beginPacket(RemoteIP, RemotePort);
  Udp.write ("Test Data From Arduino\n");
  Udp.endPacket();
  delay (1000);  
}

I must be missing something but i do not know what.