Ethernet problems with Arduino Mega

Hi all, I have the Arduino Mega, with the Arduino Ethernet Shield (v1).

I connected the ethernet card to the Arduino, and connected the RJ-45 cable.

I read plenty of tutorials online, but none of them give me the expected result, all of the options are tried, end up stopping at some place throughout the code.

This is the first piece of the code I use:

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

// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {

  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// Set the static IP address to use if the DHCP fails to assign

IPAddress ip(172, 26, 34, 177); 

IPAddress myDns(8, 8, 8, 8);

// initialize the library instance:

EthernetClient client;

IPAddress server(127,0,0,1);

unsigned long lastConnectionTime = 0;           // last time you connected to the server, in milliseconds

const unsigned long postingInterval = 10*1000;  // delay between updates, in milliseconds

void setup() {

  Ethernet.init(32);   

  Serial.begin(9600);
  while (!Serial) {

    ; // wait for serial port to connect. Needed for native USB port only

  }

  // start the Ethernet connection:

  Serial.println("Initialize Ethernet with DHCP:");

  if (Ethernet.begin(mac) == 0) {

    Serial.println("Failed to configure Ethernet using DHCP");

    // 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.");

    }

    Serial.println("Will I be printed?");

  }

void loop() {

  // if there's incoming data from the net connection.

  // send it out the serial port.  This is for debugging

  // purposes only:

  if (client.available()) {

    char c = client.read();

    Serial.write(c);

  }

  // if ten seconds have passed since your last connection,

  // then connect again and send data:

  if (millis() - lastConnectionTime > postingInterval) {

    httpRequest();

  }

}

// this method makes a HTTP connection to the server:
void httpRequest() {

  // close any connection before send a new request.

  // This will free the socket on the WiFi shield

  client.stop();

  // if there's a successful connection:

  if (client.connect(server, 80)) {

    Serial.println("connecting...");

    // send the HTTP GET request:

    client.println("GET /latest.txt HTTP/1.1");

    client.println("Host: www.arduino.cc");

    client.println("User-Agent: arduino-ethernet");

    client.println("Connection: close");

    client.println();

    // note the time that the connection was made:

    lastConnectionTime = millis();

  } else {

    // if you couldn't make a connection:

    Serial.println("connection failed");

  }
}

And the last line of "Will I be printed?" doesn't get printed out, so as far as I can tell, the code breaks right before that. The only thing I see in my Serial window (/console) is:

Initialize Ethernet with DHCP:

I want to run some super basic interent connectivity check / HTML page update, just to see the basics work, but it doesn't..

remove Ethernet.init(32);

try this code

#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup() {
  Serial.begin(115200);
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) yield();
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
  } else {
    Serial.print(F("\tIP (through DHCP) "));
    Serial.println(Ethernet.localIP());
  }
}

void loop() {}

open the Serial monitor at 115200 bauds

Unfortuantely, the output I see is only:

Initialize Ethernet with DHCP:
Initialize Ethernet with DHCP:

(yes, twice)
and I added Serial.println("arrived here"); right before the closing braces of setup(), and this line doesn't get outputted.

Like that:

// ... rest of the code before ..
else {
    Serial.print(F("\tIP (through DHCP) "));
    Serial.println(Ethernet.localIP());
  }

  Serial.println("arrived here"); // added this line, I don't see the output.
}

void loop() {}

p.s. I changed the Serial monitor to 115200 baud of course.

Can you show a picture of the connection?
What’s on the other end of the Ethernet cable?

What made you think that '32' was the right value? The "TelnetClient" example that comes with the Ethernet library contains:

  // 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

Since you are using an Arduino shield, the value should probably be 10, not 32.

The ethernet cable is plugged directly to the wall in my office. It is an RJ-45 port that its cable is going through the wall to our network cabinet of the office.

I checked beforehand by connecting the cable to my laptop, and I do get internet connectivity through that port.

I read online that the Arduino Mega and Ethernet Shield V1 requires Ethernet.init(32).
I used (10) before and it didn't work so I tried 32.

I changed it back to 10 now and I still get no output (just blank squares)

Ask your IT specialist if you have specific security measures. It’s very frequent to protect the intranet by not letting unknown/uncertified devices just connect through the wall plug and get internal network access or internet access (can be a number of defensive measures).
Your PC might have the right certificate or be known and trusted, it does not mean anything will work fine

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