Problems with Ethernet Shield

I’m having trouble with the Ethernet shield and would really appreciate some help.

What I have:
Arduino Diecimila
Arduino ETHShield SD
Arduino IDE Release 19
A NATed network with router and gateway at 192.168.1.1

My code based on Matt Richardson’s Snail Mail Push Alert project:

/*
  Snail Mail Push Alerts by Matt Richardson
  Adapted from the Arduino WebClient example by David A. Mellis
  
  This sketch is part of a project which sends a push alert to your
  iPhone when your snail mail is delivered.
  This sketch requests a URL when a mailbox has been opened.
  The URL is a PHP script based on the ProwlPHP class.
  
  December 31, 2010
 */

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

#define switchPin 7  // Snap action switch which closes when the mailbox door is opened.

byte mac[] = {  0x90, 0xA2, 0xDA, 0x00, 0x26, 0xC0 };
byte ip[] = { 192, 168, 1, 33 }; // local Arduino IP
byte gateway[] = { 192, 168, 1, 1 }; // local Arduino Gateway
byte subnet[] = { 255, 255, 255, 0 }; // local Arduino Gateway
byte server[] = { 69, 89, 25, 197 }; // IP of your web server


Client client(server, 80);

void setup() {
  Ethernet.begin(mac, ip, gateway, subnet );
  Serial.begin(9600);
  delay(1000);
  pinMode(switchPin, INPUT);
}

void loop()
{
  if (digitalRead(switchPin) == HIGH) // if mailbox is opened:
  {
      Serial.println("Mailbox door opened");
    if (client.connect()) { //connect to server
      Serial.println("connected to server");
      // Make a HTTP request:
      client.println("GET /example.php HTTP/1.0"); //location of ProwlPHP script
      client.println("Host: xxxxxx.com"); //location of ProwlPHP script
      client.println();
    } 
    else {
      Serial.println("connection failed");
    }
    delay(1000);
    Serial.print("Response from server: ");
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }
  
    // if the server's disconnected, stop the client:
    if (!client.connected()) {
      Serial.println("disconnecting from server");
      client.stop();
    }
    while (digitalRead(switchPin) == HIGH)
    {
      // hold here until mailbox is closed again.
    }
    Serial.println("Mailbox door closed");
    delay(500);
  }
}

What is happening:
Always get the “Connection Failed” error.

What I’ve tried:

  • Added gateway and subnet to “begin”
  • Verified the MAC address agrees with the sticker on the Ethernet Card
  • Tried various IP addresses to ensure I didn’t have come crypto-gear camping out on the one I was using
  • Tried various Server addresses with addresses on the local network and the Internet
  • Downgraded to release 19 of the IDE in case there were compatibility issues with the library and release 22

With no more descriptive error information available I"m running out of ideas. Any suggestions for additional things to try? I’m expecting to find I’ve done something stupid that I can’t see.

Thanks.

[/list]

Below is a simple client test code you can try to troubleshoot where the problen is.

//zoomkat 11-13-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives
//push the shield reset button to run client again

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 }; // Arduino IP address
byte server[] = { 208, 104, 2, 86 }; // zoomkat's web site

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  Serial.println("starting simple arduino client test");
  Serial.println();

  delay(1000);

  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    Serial.println("==================================");
    Serial.println("");
    client.stop();
    for(;;);
  }
}

Thank you for the test code.

When I ran it, I got the same error and, as I should have done sooner rather than later, I started examining my assumptions.

One of them was that, since other devices were connecting though the switch I was using that there was no problem there. When I started testing back up the network chain I found I'd managed to pick a bad port on a working switch. Doh!

As soon as I plugged the Arduino into a good port the test script worked and my script also worked without change.

Thank you for your help and sorry for wasting your time. You did prompt me to go back to basics and find my problem.

Thanks!