0

I have an ethernet shield on an Arduino Mega 2560. I am running the code below. This is a slightly modified version of the DHCP ip printer example, namely I am setting a static IP instead of DHCP.

I was expecting to get:

Start Ethernet
My IP address: 192.168.178.163.

on the serial monitor. Instead I got:

Start Ethernet
My IP address: 0.237.237.237.

Any idea what the reason might be? If I am using DHCP (the unmodified example, commented lines in the code below), the code seems to freeze on the Ethernet.begin() call.The "Failed to configure ..." is not even printed.

Here is the sketch:

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
  0x90, 0xA2, 0xDA, 0x10, 0xC1, 0x2C
};
byte ip[] = {192, 168, 178, 163};

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  // this check is only needed on the Leonardo:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Start Ethernet");
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  /*if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for (;;)
      ;
  }*/
  // print your local IP address:
  printIPAddress();
}

void loop() {

  switch (Ethernet.maintain())
  {
    case 1:
      //renewed fail
      Serial.println("Error: renewed fail");
      break;

    case 2:
      //renewed success
      Serial.println("Renewed success");

      //print your local IP address:
      printIPAddress();
      break;

    case 3:
      //rebind fail
      Serial.println("Error: rebind fail");
      break;

    case 4:
      //rebind success
      Serial.println("Rebind success");

      //print your local IP address:
      printIPAddress();
      break;

    default:
      //nothing happened
      break;

  }
}

void printIPAddress()
{
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }

  Serial.println();
}

The situation is similar to Ethernet shield not getting IP, but I checked that all pins are connected properly. Also setting pin 10 to high does not change anything.

  • Which Ethernet controller chip does your Ethernet Shield have? The common ones are W5100, W5200 and W5500. – per1234 Nov 5 '16 at 23:15
  • It is the W5500 – Alex Nov 6 '16 at 18:51
  • Check out the same issue here. – Manav Feb 6 '17 at 9:41
  • This happens when the network isn't bridged. You can check this link here – Manav Feb 6 '17 at 9:42
2

I found the solution in modrego's answer on Arduino Ethernet Shield 2 stuck on EthernetServer::begin(). I need the Ethernet2 library for the W5500 Ethernet controller. The Ethernet library is for W5100 only.

This library is not included with the arduino.cc library(as of IDE version 1.6.12 and older, this will likely change in future versions). I had to install it. You can do this by:

  1. Sketch > Include Library > Manager Libraries...
  2. Type "ethernet2" in the Filter your search... field
  3. Click on the Ethernet2 entry.
  4. Click the Install button.
  5. Click the Close button.

You will need to change the line:

#include <Ethernet.h>

to:

#include <Ethernet2.h>

Other files included in example sketches for the Ethernet library will need similar modifications.

  • Thanks, this is what I needed to do too. Doesn't really seem to be any official docs stating this. – Joel Dec 27 '17 at 14:35
0

Note that because the W5100 and SD card share the SPI bus, only one can be active at a time. If you are using both peripherals in your program, this should be taken care of by the corresponding libraries. If you're not using one of the peripherals in your program, however, you'll need to explicitly deselect it. To do this with the SD card, set pin 4 as an output and write a high to it. For the W5100, set digital pin 10 as a high output.

Ref. https://www.arduino.cc/en/Main/ArduinoEthernetShield

  • I think the relevant documentation is: arduino.cc/en/Main/ArduinoEthernetShieldV2 . I tried setting 10 to LOW and 4 to HIGH, as well as the other way round, no effect. The documentation also says port 53 needs to be an output, but that does not have any influence either. – Alex Nov 6 '16 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.