Problems connecting PC to Arduino MEGA with Ethernet shield 2

Hey,

I am trying to set up my Arduino Mega with an Arduino Ethernet Shield 2 (W5500) so that I can send UDP messages from another program through an ethernet cable to control the pins of the Mega.

I'm having trouble getting even the first steps and trials to work. I have found a couple of other threads related to my problem and have tried some of the suggested solutions but nothing works, I just keep getting the LinkOFF message but my cable is for sure connected and the shield leds are on. I have manually set the IP and the mac address is the one from the sticker. This is the code I'm running:

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


#define SDSelect  4

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xA8, 0x61, 0x0A, 0xAF, 0x03, 0xBC
};
IPAddress ip(192, 168, 0, 3);
IPAddress gateway(192, 168, 0, 4);
IPAddress subnet(255, 255, 255, 0);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged";        // a string to send back

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

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  Ethernet.init(10);  // 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
  pinMode (SDSelect, OUTPUT);
  digitalWrite (SDSelect, HIGH);

  // start the Ethernet
  Ethernet.begin(mac, ip);

  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // Check for Ethernet hardware present
  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.");
  }

  if (Ethernet.linkStatus() == LinkON) {
    Serial.println("Ethernet cable is found, all good.");
  }

  if (Ethernet.linkStatus() == Unknown) {
    Serial.println("Link status unknown.");
  }
  // start UDP
  Udp.begin(localPort);
}

void loop() {
  // 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 ");
    IPAddress remote = Udp.remoteIP();
    for (int i=0; i < 4; i++) {
      Serial.print(remote[i], DEC);
      if (i < 3) {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBuffer
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
  delay(10);
}

Post a simple schematic showing how the network is interconnected. By chance does it go from the Mega to your computer, if so you need a NUL cable.
image

try a short delay after begin before requesting the link status

The Mega is connected to the computer with USB, and the shield is connected to the computer with ethernet cable. Thats all the connections at the moment.
Is a NUL cable the same as an ethernet crossover cable? Because I've tried both a normal and a crossover cable and i get the same result unfortunately.

I tried now to add delays between the steps and i get the same result, but i noticed a weird thing: after i have altered the code i get the LinkON message one time when i upload the code and then immediately after i get the LinkOFF message. And then I get the LinkOFF message every time i reboot the Arduino.
I'm really lost here...

Okay I got it to work! The delay before checking the link status had to be 3000 or more. Now i get the LinkON message.

But now i cant seem to get the UDP packages to be read though....

do you have a SD card in the holder and SD CS pin is not set HIGH?

Yes. Since you know the IP address of your project you can try pinging it with your computer. In Linux open terminal and type ping ip-address in windoz do that in DOS.

I tried pinging it with the IP that i've manually set but i get 'request timed out' both with straight and crossover cable. Even though I get the LinkON message on boot.

What can be wrong?
The shield is mounted straight on top of the Mega and its connected with usb to the computer and I'm using a short cat 6 cable (tried another longer one as well).
I don't have a SD-card installed and i've pulled the pin 4 high.

This is the code im running:


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


#define SDSelect  4

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xA8, 0x61, 0x0A, 0xAF, 0x03, 0xBC
  // 0x3C, 0x7C, 0x3F, 0x5D, 0x66, 0xAE
};
IPAddress ip(192, 168, 1, 56);
// IPAddress ip(169, 254, 239, 235);
IPAddress dns(8, 8, 8, 8);
IPAddress gateway(192, 168, 1, 57);
IPAddress subnet(255, 255, 255, 0);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[255];  // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged";        // a string to send back

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


void setup() {
  Ethernet.init(10);  // Most Arduino shields
  delay(10);

  pinMode (SDSelect, OUTPUT);
  digitalWrite (SDSelect, HIGH);

  delay(10);

  // start the Ethernet
  Ethernet.begin(mac, ip, dns, gateway, subnet);

  delay(10);

  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  delay(10);

  // Check for Ethernet hardware present
  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
    }
  }

  else if (Ethernet.hardwareStatus() == EthernetW5500) {
    Serial.println("W5500 Ethernet controller detected.");
  }

  delay(3000);

  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  else if (Ethernet.linkStatus() == LinkON) {
    Serial.println("Ethernet cable is found, all good.");
  }

  else if (Ethernet.linkStatus() == Unknown) {
    Serial.println("Link status unknown.");
  }


  while(!Ethernet.linkStatus() == LinkON) {
     ;
   }

  delay(100);

  Serial.println("onwards");

  // start UDP
  Udp.begin(localPort);

  delay(100);
}

void loop() {

  // if (Udp.parsePacket()) {
  //   int len = Udp.read(packetBuffer, 255);
  //   if (len > 0) {
  //     packetBuffer[len] = 0;
  //     Serial.println(packetBuffer); 
  //   }
  // }


  // 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 ");
    IPAddress remote = Udp.remoteIP();
    for (int i=0; i < 4; i++) {
      Serial.print(remote[i], DEC);
      if (i < 3) {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBuffer
    Udp.read(packetBuffer, 255);
    Serial.println("Contents:");
    Serial.println(packetBuffer);



    // send a reply to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
    Udp.beginPacket((192, 168, 1, 56), (8877));
    Udp.write(ReplyBuffer);
    Udp.endPacket();

  delay(10);
}

The first byte of the MAC address must have bit0=0 and bit1=1 so try

0xA2, 0x61, 0x0A, 0xAF, 0x03, 0xBC

You must use the crossover cable.

Thank you but unfortunately no luck with the crossover cable and that MAC address either. The MAC address I used before is the one from the sticker on the shield.

Apparently you used 0xA8, 0x61, 0x0A, 0xAF, 0x03, 0xBC as your MAC address and a cross over cable, try it the way @jim-p suggests. By not following suggestions expect to not to get your problem solved. The experts have been through this many times, if you had there experience you would not be asking the question.

Oh i think you might have misunderstood my response. I meant to say that I tried @jim-p suggestion after reading it and got the same result. I'm very grateful for all advice since I need it badly! English is not my first language either

It is possible you have a bad Ethernet module, can you try a different one? Double check the wiring and remove any other hardware until you get it working. Try using DHCP, the IP address
192, 168, 0, 3 is used by my router. You can check your routing table and see if it has been assigned to another unit. You can only have ONE unit with a given address.

I noticed that in your original code that the network address was 192.168.0.x but in your recent code it was changed to 192.168.1.x
Was that a mistake?

Ah yes that is just a result of me trying different way to solve the problem. So the dedicated IP is just changed since the first code.

Well it needs to be the same as your computer.
Is the computer 1 or 0?

Thank you all for your help. Everything worked now as it should once i ran the arduino on an external power supply. I haven't tried a different USB cable yet so i don't know if that was the problem or if there's an issue elsewhere. But finally i can get on with my work!

I really appreciate all the help!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.