Cannot establish client connection to Ethernet shield...

The problem: cannot establish client connection to Ethernet shield.

Hardware: Mega2560, official Arduino Ethernet 2 shield

Libraries: Ethernet (not Ethernet2)

I'm using the “ChatServer” example found with the library, with the following modifications:
byte mac = {0xA8,0x61,0x0A,0xAE,0x66,0xF9};
IPAddress ip(192, 168, 0, 3);
IPAddress myDns(192, 168, 0, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

When I execute the code, I get:
Ethernet cable is not connected.
Chat server address:192.168.0.3

BUT...

I can ping the shield (going through my local router) from my development host and I receive a quick reply. So question #1 is... how can the cable be disconnected if pings are working? The port lights are lit and flashing too...

I think I need to answer this question before moving on, but the next problem down the line is that traffic sent to the correct EndPoint (192.168.0.3:23) is successfully transmitted (verified by WireShark) but the Arduino server fails to respond, it always times out:

“A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond to 192.168.0.3:23”

Another question I have is - all the official Arduino documentation clearly states that the Ethernet chip select pin is 10, and the onboard SD card reader chip select pin is 4. Really? Then why is the default SD chip select pin actually used 53? I can change this in Sd2Card.h by defining #define SDCARD_SS_PIN 4 in file Sd2Card.h, but shouldn't this be, ah, something done automatically in the library, or at the very least well documented somewhere? And even knowing this, I cannot initialize the SD card reader, no-matter what CS pin I assign. Yes, I put the Ethernet CS pin (10) high while attempting to init the SD card reader.

Now, I would think that official hardware fresh out of the box, using the latest Arduino IDE and libraries and documentation, and a standard (and very simple) stock example sketch would “just work” no problem, but NOPE...

There is nothing else connected to this system, no other peripherals. No breadboard, no patch cables, nada.

A different but related question... I would like to temporarily incorporate the Ethernet library sources directly in my build (as opposed to a library reference) for debugging purposes. What is the best way to do this? Maybe I can just flip a compile-time switch and include all the library symbols into my build? The reason I want to do this is to go snooping for myself with the Visual Micro debugger.
Thanks for reading. If you have answers to any of these question, they will be much appreciated. :frowning:

if you use SD card and Ethernet. for the first of the to initialize, you must disable the other on SPI bus.
there are many different boards now which use this libraries and have very different pinouts. so the defaults are usually not right.

  pinMode(SDCARD_SS_PIN, OUTPUT);
  digitalWrite(SDCARD_SS_PIN, HIGH); // to disable SD card while Ethernet initializes

  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    while (true);
  }
  Serial.println("Ethernet connected");

  Serial.print("Initializing SD card...");
  if (!SD.begin(SDCARD_SS_PIN)) {
    Serial.println("initialization failed!");
    // don't continue:
    while (true);
  }
  Serial.println("initialization done.");

On the assumption that the order (Ethernet then SD or SD then Ethernet) is unimportant, I have been attempting to initialize the SD card reader first, and yes indeed I so set the Ethernet CS pin (10) as (OUTPUT, HIGH) before doing so. I will try initializing the Ethernet first and see if there is any difference, and come back with and answer. Thanks for your help.

“Now, I would think that official hardware fresh out of the box, using the latest Arduino IDE and libraries and documentation, and a standard (and very simple) stock example sketch would “just work” no problem, but NOPE…”

Well, an invalid assumption has been bade somewhere. A quotable quote from an electrical engineer I used to work with in the past is “Bottom line, things work the way they are wired”. Below is some old code from back when I tinkered with a W5100 ethernet board. Note the part where pin 10 is manipulated to allow the bootup with the SD card in the SD card slot. This worked on a uno type arduino board. I can’t remember if I ever ran the ethernet board on my mega, so I can’t speak to the mega operations. A simple test for your code would see if it works with the SD card removed. If the code does not work with the SD card removed, then other issues may be involved.

//zoomkat 4-1-12
//Simple browser file upload from the SD card
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html, or use ' instead of " 
//click in the below LAN url to test
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
String readString; 

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

void setup(){

  Serial.begin(9600);

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  Serial.print("Starting SD..");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  digitalWrite(10,HIGH);

  //delay(2000);
  server.begin();
  Serial.println("Ready");

}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 
        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: image/jpeg");
          client.println();

          File myFile = SD.open("HYPNO.JPG");
          if (myFile) {
            //Serial.println("test.txt:");
            // read from the file until there's nothing else in it:
            while (myFile.available()) {
              client.write(myFile.read());
            }
            // close the file:
            myFile.close();

          }
            delay(1);
            //stopping client
            client.stop();
            readString="";
          //}
        }
      }
    }
  } 
}

Dear Zoomkat,

I have tried a lot to combine the SD card module and Ethernet module. In one of my project i am using Enc28j60 Module and SD card module with Arduino Uno and Mega. But each time it is showing failed. Any type of help will be appreciable.

Regards

Aasrpdhuailnto:
Dear Zoomkat,

I have tried a lot to combine the SD card module and Ethernet module. In one of my project i am using Enc28j60 Module and SD card module with Arduino Uno and Mega. But each time it is showing failed. Any type of help will be appreciable.

Regards

some details about hardware, code, error?

Hardware used - Arduino Mega 2560, ENC28J60 (Ethernet Module), SD Card Module.

Ethernet Module and Sd Card Module are working fine separately. But When i am trying to combine both things they both are not working. I have tried different CS Pin But i haven’t got any result. Both are not working.

Any type of help will be appreciable.

Aasrpdhuailnto:
Hardware used - Arduino Mega 2560, ENC28J60 (Ethernet Module), SD Card Module.

Ethernet Module and Sd Card Module are working fine separately. But When i am trying to combine both things they both are not working. I have tried different CS Pin But i haven't got any result. Both are not working.

Any type of help will be appreciable.

some 5 V adapters for SD card block other SPI devices. which one exactly do you have?