I've tried the "UDP chat program for ENC28J60" code and changed the CS pin to 53 due to Arduino Mega 2560. This is the only thing I changed about the code:
// UDP chat program using ENC28J60 tested on UNO and Nano
// **** change IP addresses and ports to suit requirements *****
#include <EthernetENC.h> // for ENC28J60
#include <EthernetUdp.h> // for UDP
// ***** IP of this machine and remote machine *********
IPAddress localIP(192, 168, 1, 177);
IPAddress remoteIP(192, 168, 1, 176);
// Enter a MAC address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
unsigned int localPort = 10000; // local port to listen on
unsigned int remotePort = 10000; // remote port to transmiit too
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial)
;
Serial.println("\nEthernet UDP chat program");
// Check for Ethernet hardware present
// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(53); // Most Arduino shields
//Ethernet.init(5); // MKR ETH Shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet
//displayMACaddress(mac);
mac[5] = localIP[3]; // change default MAC address
displayMACaddress(mac);
// start the Ethernet
Ethernet.begin(mac, localIP);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) delay(1); // do nothing, no point running without Ethernet hardware
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
Udp.begin(localPort); // start UDP
Serial.print("Ethernet UDP started ");
displayIPaddress(localIP, localPort);
//Serial.println(UDP_TX_PACKET_MAX_SIZE);
}
void loop() {
Udp.begin(localPort);
// if Serial text entered transmit as a datagram
if (Serial.available()) {
Udp.begin(localPort);
char text[20] = { 0 };
Serial.readBytesUntil('\n', text, 24);
Serial.print("Transmitting to ");
displayIPaddress(remoteIP, remotePort);
Serial.println(text);
Udp.beginPacket(remoteIP, remotePort); // transmit datagram
Udp.print(text);
Udp.endPacket();
}
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
displayIPaddress(Udp.remoteIP(), Udp.remotePort());
// read the packet into packetBuffer
char packetBuffer[100] = { 0 }; // buffer to hold incoming packet,
Udp.read(packetBuffer, packetSize); //UDP_TX_PACKET_MAX_SIZE); // receive datagram
Serial.print("Contents: ");
Serial.println(packetBuffer);
// if (strcmp(packetBuffer, "OK") != 0) {
// send a reply to the IP address and port that sent us the packet we received
// Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
// Udp.write("OK");
// Udp.endPacket();
//}
}
delay(10);
}
// print IPAdress and port
void displayIPaddress(const IPAddress address, unsigned int port) {
Serial.print(" IP ");
for (int i = 0; i < 4; i++) {
Serial.print(address[i], DEC);
if (i < 3) Serial.print(".");
}
Serial.print(" port ");
Serial.println(port);
}
void displayMACaddress(byte address[]) {
Serial.print("MAC address ");
for (int i = 0; i < 6; i++) {
Serial.print("0x");
Serial.print(address[i], HEX);
if (i < 5) Serial.print(".");
}
Serial.println();
}
The connections between Arduino and ENC28J60 are:
Arduino ENC28J60
GND GND
3.3V 3.3V
53 CS
52 SCK
51 SI
50 SO
I've tried to plug the Ethernet cable in these configurations:
[TBH I don't exactly know the difference between a router and a switch (and a hub) but I was going to figured it out later.]
The problem is I get this on the serial monitor:
Ethernet UDP chat program
MAC address 0xDE.0xAD.0xBE.0xEF.0xFE.0xB1
Ethernet cable is not connected.
Ethernet UDP started IP 192.168.1.177 port 10000
I've tried 3 different cables (CAT6a, CAT5e, CAT5). This only matters later on when I want to use PoE but still I used 3 different cables and I used them to connect my laptop to the home router and I got access to the Internet. Also I tried the example LinkStatus.ino and I get this:
/*
Link Status
This sketch prints the ethernet link status. When the
ethernet cable is connected the link status should go to "ON".
NOTE: Only WizNet W5200 and W5500 are capable of reporting
the link status. W5100 will report "Unknown".
Hardware:
- Ethernet shield or equivalent board/shield with WizNet 5200/5500
Written by Cristian Maglie
This example is public domain.
*/
#include <SPI.h>
#include <EthernetENC.h>
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(53); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
Serial.begin(115200);
}
void loop() {
auto link = Ethernet.linkStatus();
Serial.print("Link status: ");
switch (link) {
case Unknown:
Serial.println("Unknown");
break;
case LinkON:
Serial.println("ON");
break;
case LinkOFF:
Serial.println("OFF");
break;
}
delay(1000);
}
Link status: ON
Link status: ON
Link status: ON
Link status: ON
Link status: ON
I'm so confused now. The cables are working. The only difference, I see, is:
// ***** IP of this machine and remote machine *********
IPAddress localIP(192, 168, 1, 177);
IPAddress remoteIP(192, 168, 1, 176);
// Enter a MAC address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
unsigned int localPort = 10000; // local port to listen on
unsigned int remotePort = 10000; // remote port to transmiit too
mac[5] = localIP[3]; // change default MAC address
displayMACaddress(mac);
// start the Ethernet
Ethernet.begin(mac, localIP);
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) delay(1); // do nothing, no point running without Ethernet hardware
}
So, is there anything going wrong in the initialisation and that's why even the linkStatus() doesn't work anymore or am I using the hardware wrong?
And again thanks for the help so far!