Ethernet Shield Not Connecting

I've been working this problem for the past several days and getting nowhere. This issue has come up before in the forum but none of the solutions are working for me. I have an ethernet shield mounted on an UNO. The micro-SD card section works fine but the ethernet section is another story. It is unable to establish a connection in the program below. The output from the program is:

Better client test 4/04/12
Send an e in serial monitor to test
read an 'e'
connection failed

disconnecting.

I ran IPCONFIG to determine the correct addresses.
The "byte dns[]" line has been commented out because it produces the following error:
"'byte dns []' redeclared as different kind of symbol"
although I've read that this line may not be necessary.
Also, there was no MAC address sticker on the ethernet card but I understand that this may also not be necessary.
Would appreciate any help you can offer on this.

//zoomkat 4-04-12
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 
//the arduino lan IP address { 192, 168, 1, 102 } may need 
//to be modified modified to work with your router.

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

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE }; //physical mac address
byte ip[] = { 
  192, 168, 1, 105 }; // ip in lan assigned to arduino
byte gateway[] = { 192, 168, 1, 1}; // internet access via router
//byte dns[] = {  192, 168, 1, 1 }; //use router ip 
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 
//  208, 104, 2, 86 }; // zoomkat web page server IP address
    209, 191, 122, 70 }; // yahoo
EthernetClient client;
//////////////////////

void setup(){

  pinMode(10, OUTPUT);//sets w5100chip 
//  digitalWrite(10,HIGH); // disables w5100 
  pinMode(4, OUTPUT); // sets SD chip
  digitalWrite(4,HIGH);//disables SD chip
  
  //Ethernet.begin(mac, ip);
  Ethernet.begin(mac, ip, dns, gateway, subnet);
  Serial.begin(9600); 
 
  Serial.println("Better client test 4/04/12"); // 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
    {
      Serial.println("read 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(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    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
// add counter as an attempt to bail out if too much input comes in
//
  int counter = 0;
  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 
    counter = counter + 1;
    if (counter >= 500 ) {
      exit;  
    }
  }
  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

In that code you have the myServer set to "209, 191, 122, 70" which appears to be a yahoo IP, but doesn't resolve correctly as-is. Are you trying to access a web site (virtual host), that is hosted on a yahoo server? If so, you need to add the http-header "host". E.g.

client.println("Host: example.com");

Actually, I'm just trying to verify that my ethernet card is working and able to connect the network. Right now it appears that the only connection is via the USB cable and not the network cable.

I'm not sure how I would fit the code you suggested into the program. I'm hesitant to make any change to the code in this area as others in the forum have got it working as is. I did change the Yahoo address to a valid one, 209.191.122.60, but still no luck.

Have you verified that your local network's subnet is 192.168.1.x?

I was successfully able to ping 192.168.1.1 and 192.168.1.105.

Looks like there is a problem with your Shield, the Cable, or your router. I uploaded your code as-is in your original post. The only thing I changed was the subnet.

Here's the response I got:

read an 'e'
connected
HTTP/1.0 302 Redirect
Date: Sat, 07 Jul 2012 01:36:45 GMT
Connection: close
Server: YTS/1.20.10
Cache-Control: no-store
Content-Type: text/html
Content-Language: en
Location: http://failsafe.fp.yahoo.com/404.html
Content-Length: 227

<HEAD><TITLE>Redirect</TITLE></HEAD>
<BODY BGCOLOR="white" FGCOLOR="black">
<FONT FACE="Helvetica,Arial"><B>
 "<em>http://failsafe.fp.yahoo.com/404.html</em>".<p></B></FONT>

<!-- default "Redirect" response (302) -->
</BODY>
(strange character repeated here, but breaking up post)
disconnecting.
==================

This is the same behavior I saw with my browser.

For the record, the is the code I used:

//zoomkat 4-04-12
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 
//the arduino lan IP address { 192, 168, 1, 102 } may need 
//to be modified modified to work with your router.

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

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE }; //physical mac address
byte ip[] = { 
  10, 0, 1, 105 }; // ip in lan assigned to arduino
byte gateway[] = { 10, 0, 1, 1}; // internet access via router
//byte dns[] = {  192, 168, 1, 1 }; //use router ip 
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 
//  208, 104, 2, 86 }; // zoomkat web page server IP address
    209, 191, 122, 70 }; // yahoo
EthernetClient client;
//////////////////////

void setup(){

  pinMode(10, OUTPUT);//sets w5100chip 
//  digitalWrite(10,HIGH); // disables w5100 
  pinMode(4, OUTPUT); // sets SD chip
  digitalWrite(4,HIGH);//disables SD chip
  
  //Ethernet.begin(mac, ip);
  Ethernet.begin(mac, ip, dns, gateway, subnet);
  Serial.begin(9600); 
 
  Serial.println("Better client test 4/04/12"); // 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
    {
      Serial.println("read 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(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    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
// add counter as an attempt to bail out if too much input comes in
//
  int counter = 0;
  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 
    counter = counter + 1;
    if (counter >= 500 ) {
      exit;  
    }
  }
  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

James, thanks for testing this.
I replaced the cable but it's still not working. I'll order another ethernet shield and hopefully have better luck.
Lawren5

Below is some simple server test code you can try to see if it works with your ethernet shield.

// zoomkat meta refresh server test code
// arduino IDE 1.0
// for W5100 ethernet shield
// the IP address will be dependent on your local network/router
// port 80 is default for HTTP, but can be changed as needed
// use IP address like http://192.168.1.102:84 in your brouser
// or http://zoomkat.no-ip.com:84 with dynamic IP service
// use the \ slash to escape the " in the html
// meta refresh set for 2 seconds

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

int x=0; //set refresh counter to 0
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,102); // ip in lan
EthernetServer server(84); //server is using port 84

void setup()
{
  // start the server
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
     while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        if (c == '\n') {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          //meta-refresh page every 2 seconds
          x=x+1; //page upload counter
          client.println("<HTML>");
          client.print("<HEAD>");
          client.print("<meta http-equiv=\"refresh\" content=\"2\">");
          client.print("<TITLE />Zoomkat's meta-refresh test</title>");
          client.print("</head>");
          client.println("<BODY>");
          client.print("Zoomkat's meta-refresh test IDE 1.0");
          client.println("
");
                    
          client.print("page refresh number ");
          client.println(x); //current refresh count
          client.println("
");
          client.println("
");
          
          client.print("Zoomkat's arduino analog input values:");
          client.println("
");
          client.println("
");
          
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("
");
            }
           break;
          client.println("</BODY>");
          client.println("</HTML>");
         }
        }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

I tried running the zoomkat program but nothing happened. One thing I did notice was that each time I loaded the program, a message appeared for about two seconds saying "Local Area Connection, A network cable is unplugged".

Where are you plugging in the other end of the cable?

Into the network port of my laptop.

lawren5:
Into the network port of my laptop.

And is your laptop setup to do internet connection sharing?

lawren5:
Into the network port of my laptop.

You may need an ethernet crossover cable to make a connection directly to your laptop.

No, it was not set up for internet sharing so I made the change. However, the program is still not working and continues to give the "unplugged" message.

I'm not familiar with an ethernet crossover cable is. My laptop is an older model running XP if that indicates anything.

lawren5:
No, it was not set up for internet sharing so I made the change. However, the program is still not working and continues to give the "unplugged" message.

Well, your next step is to modify the IP settings for the Ethernet Shield based on whatever the ICS is going to provide. You aren't getting your internet from your router, you are getting it from your laptop. The best thing to do would be to plug the Ethernet shield directly into a router.

I'm not familiar with an ethernet crossover cable is. My laptop is an older model running XP if that indicates anything.

Ethernet setups are somewhat client/host based as to ethernet cable connections. Hubs/routers generally act in the host mode and the pc/arduino act in the client mode. Normal ethernet cables have the end connectors wired for client/host connections. The pc/arduino connection is client/client, so an end connecton on the erhernet cable will need a couple of wires flipped to support the client/client connection.

Yes, I did change the IP settings and pinged them to make sure they were good.
If I plug the ethernet shield directly into my router, how do I monitor the output to determine if things are working?

lawren5:
Yes, I did change the IP settings and pinged them to make sure they were good.
If I plug the ethernet shield directly into my router, how do I monitor the output to determine if things are working?

Almost all my code also prints output to the serial monitior for seeing what is happening.

lawren5:
Yes, I did change the IP settings and pinged them to make sure they were good.
If I plug the ethernet shield directly into my router, how do I monitor the output to determine if things are working?

How would you monitor it while you were connecting the Ethernet to your laptop? Are you running a packet sniffer on the laptop to monitor the Ethernet traffic? (I'm guessing no.)

You plug the Ethernet into the Router and USB into your Laptop. Serial.print() sends data over the serial port, not ethernet.

OK, we're getting somewhere. I plugged the ethernet directly into the router and it worked when I ran the program that James ran successfully earlier (the one where you type an "e" into the serial monitor). The result was a webpage displayed in the serial monitor in html code.

So the ethernet board appears to be good.