Electronic – Arduino Ethernet shield: it just won’t work!

arduinoethernet

I have an Arduino Ethernet shield on top of an Arduino Mega2560 and have connected the Mega2560 through serial to my laptop and through the UTP port on the Ethernet shield to my router.

I've tried the "search Arduino on Google" example (where I corrected Google's IP address, the example was old), and it doesn't work (it doesn't seem to connect to the Internet. On the serial, I get: "connecting… /connection failed / disconnecting.").

My UTP cable is working fine… I've tested it on my laptop.

The Mega2560 is connected to my router (which has everything disabled so that's not the problem ) and when I ping from my laptop the IP address I gave the Arduino assembly, it doesn't ping back.

Why? Maybe there is something I'm missing? Here is my code:

Code:

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,177 };
byte server[] = { 209,85,148,147 }; // Google

// 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):
Client client(server, 80);

void setup() {
  // Start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // Start the serial library:
  Serial.begin(9600);
  // Give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // If you get a connection, report back via serial:
  if (client.connect()) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  }
  else {
    // If you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

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:
    for(;;)
      ;
  }
}

Best Answer

The Arduino uses SPI to communicate between the devices. Since the design of the Arduino Mega changed and the older pinout relates to older shields, in order to use it with newer shields you have to hack it a little, to work around the problem.

A lot of people had this question before, and you can still find the answer here: http://mcukits.com/2009/04/06/arduino-ethernet-shield-mega-hack/