Unable to configure ethernet using arduino UNO R3

I have purchased Arduino Uno R3 board and LAN shield from below locations

AruinoUNO R3- http://www.ebay.in/itm/W5100-Ethernet-Shield-for-Arduino-Uno-And-Mega-KG027-/121654747580?

Arduino LAN shoeld- http://www.amazon.in/gp/product/B00H1HR576?psc=1&redirect=true&ref_=oh_aui_detailpage_o01_s00

I am trying to configure Ethernet using below 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[] = {
    0x4,0x2,0x45,0x68,0x5E,0x96,0x56
    };
    
    // 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);
      delay(5000);
      // this check is only needed on the Leonardo:
      while (!Serial) {
        ; // wait for serial port to connect. Needed for Leonardo only
      }
    
      // start the Ethernet connection:
      Serial.println("trying to configure ethernet...");
      while(Ethernet.begin(mac) == 0) {
        Serial.println("Failed to configure Ethernet using DHCP, retrying after 5 sec");
        // no point in carrying on, so do nothing forevermore:
        delay(5000);
        Serial.println("trying to configure ethernet...");
      }
      // print your local IP address:
      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();
    }
    
    void loop() {
    
    }

Its generates below output in the Serial Monitor

    trying to configure ethernet...
    Failed to configure Ethernet using DHCP, retrying after 5 sec
    trying to configure ethernet...
    Failed to configure Ethernet using DHCP, retrying after 5 sec
    trying to configure ethernet...
    Failed to configure Ethernet using DHCP, retrying after 5 sec
    trying to configure ethernet...
    Failed to configure Ethernet using DHCP, retrying after 5 sec
    trying to configure ethernet...
    Failed to configure Ethernet using DHCP, retrying after 5 sec
    trying to configure ethernet...
    Failed to configure Ethernet using DHCP, retrying after 5 sec
    trying to configure ethernet...
    Failed to configure Ethernet using DHCP, retrying after 5 sec
    trying to configure ethernet...

I have captured the LED indicators on video on youtube, it shows Tx and Rx LEDs glowing priodically. Please find link below
"

"
Looks like Adruino not able to connect to Ethernet...
I am using Arduino IDE 1.6.5 on Windows 7.
Is there any debug tool for Arduino that can be used to figure out the issue?

Try this test sketch. It checks the SPI bus and SPI side of the w5100. If it displays 192.168.0.2, then that part is working. If it displays anything else, it is failing.

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

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,2);

void setup() {
  Serial.begin(9600);

  // disable SD card if one in the slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.println("Starting w5100");
  Ethernet.begin(mac,ip);

  Serial.println(Ethernet.localIP());
}

void loop() {
}

@ SurferTim : It has printed the ip address as expected :).

Starting w5100
192.168.0.2

I have added further code and tried connecting to google using below code

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

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,2);
IPAddress server(74,125,232,128); 

// 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() {
  Serial.begin(9600);

  // disable SD card if one in the slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.println("Starting w5100");
  Ethernet.begin(mac,ip);

  Serial.println(Ethernet.localIP());

Serial.println("Connecting to google....");
int ret;
  if (ret = client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
    Serial.println(ret);
  }
  
}

void loop() {

  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}
Produced below output

Starting w5100
192.168.0.2
Connecting to google....
connection failed
0

disconnecting.

So it could assign the ip address but failing to go online.

Is 192.168.0.2 in your localnet IP range?
Is 192.168.0.1 the gateway for your localnet?

I printed out the gateway and subnet mask, it turns out to be 192.168.1.1. So I changed the ip address to 192.168.1.10. This ip is not used by any other device. I also tried assigning dns server but no change in output (Although adding DNS server does not matter at this point). Also I noticed that I could not even ping from my local desktop to "192.168.1.10". Although both are in the same Local Area Network.

Starting w5100
ip-192.168.1.10
Subnet mask-255.255.255.0
Gateway-192.168.1.1
DNS-8.8.8.8
Connecting to google....
connection failed
0

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

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,10);
IPAddress dns_server(8,8,8,8);
IPAddress server(74,125,232,128); 

// 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() {
  Serial.begin(9600);

  // disable SD card if one in the slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.println("Starting w5100");
  Ethernet.begin(mac,ip,dns_server);
Serial.print("ip-");
  Serial.println( Ethernet.localIP());
  Serial.print("Subnet mask-");
  Serial.println( Ethernet.subnetMask());
  Serial.print("Gateway-");
  Serial.println( Ethernet.gatewayIP());
Serial.print("DNS-");
  Serial.println( Ethernet.dnsServerIP());

Serial.println("Connecting to google....");
int ret;
  if (ret = client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
    Serial.println(ret);
  }
  
}

void loop() {

  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}

Do you have the Arduino connected to a router? Or is it connected to your computer?

I have router connected to switch and switch in turn connected to pc and Arduino independently, as below

Router -> Swtch
Switch -> PC(Computer)
Switch -> Arduino

I tried directly connecting Arduino to Router but still same issue

Starting w5100
ip-192.168.1.200
Subnet mask-255.255.255.0
Gateway-192.168.1.1
DNS-8.8.8.8
Connecting to google....
connection failed
0

disconnecting.

I think its picking up the subnet mask and gateway from router but maybe its following some different protocol to communicate with router? Let me know if I should provide the video with more details? or any other way to debug

The ethernet library generates the gateway, and dns server from your specified IP. If you do not specify the netmask, the default netmask is 255.255.255.0.

Just checked the subnet mask and gateway from desktop, it looks inline with these default values

C:\Users\dell>ipconfig

Windows IP Configuration


Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::192c:f19b:72d:ef72%13
   IPv4 Address. . . . . . . . . . . : 192.168.1.38
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : fe80::1%13
                                       192.168.1.1

Can you please help debugging the issue. What should be the next thing to check.

Do the lights on the front of the RJ45 socket light when you are connected to the router? Does the router show a connection light?

Try another CAT5 cable if you haven't already. A known good one is best.

Check the w5100 for solder bridges.

With your arduino connected to your router, try the below code unmodified to see if you can get a response from the dyndns server.

//zoomkat 3-1-13
//simple client checkip test
//for use with IDE 1.0.1 or later
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields

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

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

char serverName[] = "checkip.dyndns.com"; // test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client ip test 3/1/13"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET / HTTP/1.0"); //download text
    client.println("Host: checkip.dyndns.com");
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

I finally got things working, below are my findings

1.Directly connecting router to Arduino has no success if you are manually setting the IP address, BUT when you use "Ethernet.begin(mac)" to obtain ip address by directly connecting Arduino to router( and not through network Switch) everything works perfectly. Earlier when I was using "Ethernet.begin(mac)" I was connecting through network Switch.

  1. It looks like WP5100 has some limitations with respect to support to number of routers and network switches as indicated by K5CZ "http://forum.arduino.cc/index.php?topic=207953.msg1529200#msg1529200"

So after connecting directly to router + using Ethernet.begin(mac) to obtain IP address dynamically things went smoothly.

Thanks SurferTim and zoomkat for your help and time.

Below is the final working code

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,9);
IPAddress dns_server(8,8,8,8);
IPAddress server(74,125,232,128); 

// 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() {
  Serial.begin(9600);

  // disable SD card if one in the slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.println("Obtaining DHCP config...");
  if (Ethernet.begin(mac) == 0) {
    
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
Serial.print("ip-");
  Serial.println( Ethernet.localIP());
  Serial.print("Subnet mask-");
  Serial.println( Ethernet.subnetMask());
  Serial.print("Gateway-");
  Serial.println( Ethernet.gatewayIP());
Serial.print("DNS-");
  Serial.println( Ethernet.dnsServerIP());

Serial.println("Connecting to google....");
int ret;
  if (ret = client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
    Serial.println(ret);
  }
  
}

void loop() {

  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}