Ethernet Shield connects as client once, then never again.

I have installed the new version of the Ethernet Shield w/ SD Card Reader (Wiz5100) on a Duemilanove with Atmega328. I have no problems uploading the Ethernet.h library demo server sketch and having accessing it on the local network via the IP I assign in the sketch. Works great. Verified it by taking the different pins high and low and whatnot. Yay.

The client is infuriating me. I’m not sure it’s an Ethernet Shield problem though - as when I run the sketch trying to connect to a new server that I haven’t connected to before, it connects just fine. If I try to connect to that server again, it bombs out. Won’t connect again. If I cycle the power to the board 100 times, it might connect once. But, if I enter a new IP for another server, it connects just fine, but only the one time. This is leading me to believe that it might be a router / firewall issue. I’m using a Dlink DGL-4500 router, and I’ve set the arduino up to have a DHCP reservation of x.x.x.105 and then placed that address in the DMZ. It didn’t correct anything, but I’m not positive the DMZ on the router is worth a pile of beans.

Has anyone experienced this behavior before? Here’s the code I’m using - it’s basically the example sketch, modified for my IP, with gateway and subnet added in per the advice of another thread on here.

/*
  Web client
 
 This sketch connects to a website (http://www.google.com)
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 18 Dec 2009
 by David A. Mellis
 
 */

#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[] = {  0x90, 0xA2, 0xDA, 0x00, 0x85, 0x56 };
byte ip[] = { 192, 168, 2, 105 };
byte gateway[] = { 192, 168, 2, 1 };
byte subnet[] = { 255, 255, 255, 0 };
//byte server[] = { 74,220,204,126 }; // angryamerica
//byte server[] = { 207,58,139,247 };  // adafruit
//byte server[] = { 74, 125, 224, 179 };  // google
byte server[] = { 192, 168, 2, 193 };  // WHS-02

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet); // start ethernet using the mac and IP address
  Serial.begin(9600); // start the serial library:
  delay(1000); // give the ethernet hardware a second to initialize
  Serial.println("connecting...");
  if (client.connect()) {
    Serial.println("connected");
//    client.println("GET /emoncms/post.php?json= HTTP/1.1"); // the HTTP request
//    client.println("Host: www.toddknapek.com"); 
    client.println("GET /search?q=arduino HTTP/1.1"); // the HTTP request
    client.println();
  } else {
    Serial.println("connection failed");
  }
}
void loop()
{
  if (client.available()) {
  char c = client.read();
  Serial.print(c); // echo all data received to the Serial Monitor
}
if (!client.connected()) {
  Serial.println();
  Serial.println("hmmm.");
  client.stop();
  for (;;)
  ;
 }
}

I also tried zoomkat’s example code, and it connected great to his server… once. All subsequent attempts have been failures.

starting simple arduino client test

connecting...
connected
HTTP/1.1 200 OK
Date: Wed, 16 Nov 2011 07:47:12 GMT
Server: Apache
Last-Modified: Sat, 13 Nov 2010 16:31:40 GMT
Accept-Ranges: bytes
Content-Length: 51
Connection: close
Content-Type: text/plain; charset=UTF-8

Woohoo! Your arduino ethernet client works!
zoomkat
disconnecting.
==================================

starting simple arduino client test

connecting...
connection failed

disconnecting.
==================================

starting simple arduino client test

connecting...
connection failed

disconnecting.
==================================

Although, I’m not completely convinced that this is a firewall / router issue, as I also tried to connect to a Windows Home Server on my local network, and it displayed the exact same behavior - it connected perfectly the first time, and then failed on all subsequent attempts.

connecting...
connected
HTTP/1.1 404 Not Found
Content-Length: 1635
Content-Type: text/html
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Wed, 16 Nov 2011 07:56:40 GMT
Connection: close

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>The page cannot be found</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=Windows-1252">
<STYLE type="text/css">
  BODY { font: 8pt/12pt verdana }
  H1 { font: 13pt/15pt verdana }
  H2 { font: 8pt/12pt verdana }
  A:link { color: red }
  A:visited { color: maroon }
</STYLE>
</HEAD><BODY><TABLE width=500 border=0 cellspacing=10><TR><TD>

<h1>The page cannot be found</h1>
The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.
<hr>
<p>Please try the following:</p>
<ul>
<li>Make sure that the Web site address displayed in the address bar of your browser is spelled and formatted correctly.</li>
<li>If you reached this page by clicking a link, contact
 the Web site administrator to alert them that the link is incorrectly formatted.
</li>
<li>Click the <a href="javascript:history.back(1)">Back</a> button to try another link.</li>
</ul>
<h2>HTTP Error 404 - File or directory not found.
Internet Information Services (IIS)</h2>
<hr>
<p>Technical Information (for support personnel)</p>
<ul>
<li>Go to <a href="http://go.microsoft.com/fwlink/?linkid=8180">Microsoft Product Support Services</a> and perform a title search for the words <b>HTTP</b> and <b>404</b>.</li>
<li>Open <b>IIS Help</b>, which is accessible in IIS Manager (inetmgr),
 and search for topics titled <b>Web Site Setup</b>, <b>Common Administrative Tasks</b>, and <b>About Custom Error Messages</b>.</li>
</ul>

</TD></TR></TABLE></BODY></HTML>

hmmm.
connecting...
connection failed

hmmm.
connecting...
connection failed

hmmm.

Any ideas on this one?

Thanks fellas.

I hope you mean it is modified more than what you posted. The D-Link router you are using has a default localnet ip of 192.168.0.1. You did change those, correct?

byte ip[] = { 192,168,0,2};
byte gateway[] = { 192,168,0,1 };

I'd try getting rid of the DHCP reservation, and also the DMZ setting for now and retry..Make sure you shut down and reboot the router.. I think you may be correct with it being a routing issue, but I had trouble with the pachube client sketch for some reason myself, the if client.connect seems to glitch it for me..

SurferTim:
I hope you mean it is modified more than what you posted. The D-Link router you are using has a default localnet ip of 192.168.0.1. You did change those, correct?

byte ip[] = { 192,168,0,2};

byte gateway = { 192,168,0,1 };

I did. 192.168.2.x is the correct IP range for my local LAN. As I said, it connects just fine once, and then doesn’t connect again. But, the server function works perfect, as tested on my local LAN.

tek1229: I'd try getting rid of the DHCP reservation, and also the DMZ setting for now and retry..Make sure you shut down and reboot the router.. I think you may be correct with it being a routing issue, but I had trouble with the pachube client sketch for some reason myself, the if client.connect seems to glitch it for me..

Alright - I took off the DHCP reservation and took the device out of the DMZ. Still getting the same behavior, although now if I try to get it to connect to Google, it will work almost every time after an upload, but won't work after the initial try after an upload.

That's just to Google. I tried connecting to Adafruit, and it worked the one time, and hasn't again. Even after subsequent re-uploads. I checked my router logs, and am getting this error :

[INFO]  Wed Nov 16 08:04:45 2011    Blocked incoming TCP packet from 207.58.139.247:80 to 108.70.146.42:1025 with unexpected acknowledgement 1807572661 (expected 1807501267 to 1807572627)
[INFO]  Wed Nov 16 08:04:39 2011    Allowed configuration authentication by IP address 192.168.2.150
[INFO]  Wed Nov 16 08:04:38 2011    Blocked incoming TCP packet from 207.58.139.247:80 to 108.70.146.42:1025 with unexpected acknowledgement 1807572661 (expected 1807501267 to 1807572627)

207.58.139.247 is the IP for adafruit. So, it's trying to send something back, my router is just blocking it. Should I try adding that IP to an exceptions list and see if it works better? I really don't want to expose myself to anything bad.

Wonder what your router firewall protection settings are?? Try turning off SPI.... Not really opening you up to anything bad as far as I know.. Just think something is wrong with the packets..

tek1229: Wonder what your router firewall protection settings are?? Try turning off SPI.... Not really opening you up to anything bad as far as I know.. Just think something is wrong with the packets..

Yeah, I did a little digging on the D-Link site, and this seems to be a common problem for folks with a PS3. They also recommended that SPI get turned off. So I did. It works better now, but still not great. It will connect almost every time after a fresh upload, but won't connect again after pressing the reset button or re-loading the Serial Monitor.

Okay - did a little more troubleshooting, and I'm starting to think I have a bum ethernet shield. Booo.

My internet is AT&T U-verse, who supplied me with a 2wire router -> DLink DGL-4500 -> Local LAN.

I've loaded the new version of the Ethernet.h library, and the DHCP function is working fine. The ethernet shield is able to pick up an address wherever it's plugged in to. I've plugged it into the LAN via the DLink, and directly into the 2wire router, and it gets an address from both. The bummer is, it's displaying the spotty connection behavior from both devices. I thought originally it could be the 2wire device, since ultimately, everything goes through that. However, I quickly ruled that out, as I am having troubles connecting to a device on the local LAN as well, and that connection never touches the 2wire router.

How often do you guys get connection errors? I've found that when it's plugged into either device, if I try to connect more often than every 15 - 20 seconds, it won't connect. However, if I give it some time between connections, it tends to connect. Although, that's even a little spotty.

I'm able to connect as often as I want with mine.. I'm able to send data to pachube every 2 seconds if I want, and if I'm serving a web page I can keep hitting refresh as fast as i want.. Doesn't sound too good for your shield, maybe use wireshark and take a look??

Just thought of something I ready here somewhere.. Does your board have an led on pin 13?? something here about that interfering..

Yeah, it has an internal LED on Pin13. I'll do some searching for it, but I can't imagine that's an issue, as that's a standard pin for SPI. You never know though. Thanks for the tip!

So - I noticed something else. If I reset the arduino / shield using the reset button, it will connect fine. I can see the Tx/Rx LEDs light up simultaneously, and that behavior stops once I've received the response from the website that I'm connecting to.

However, once the page has been retrieved, the Rx light continues to pulse for several seconds after the data has been retrieved, as if something is still trying to send some data to the shield.

If I reset while these LEDs are still blinking, this usually results in the shield not being able to connect. If I wait for the Rx lights to stop blinking before hitting the reset button (or closing / re-opening the serial interface window) the shield will connect almost every time.

Anyone else have any insight on this? Should I just pick up another ethernet shield to see if this one is bum?

Resetting the arduino is probably not the best way to test the ethernet shield, as there reported issues with the ethernet shield sometimes powering up prior to the arduino getting powered up. Someone needs o make simple client test code to make a request every ~5000ms to test repeating client request. All the sample client code seems to be a one shot connection.

I can probably get that knocked out tomorrow. Should be simple enough to write with a delay(). Should I reset the interface between each client session, or just connect?

Thanks zoom for pointing me in the right direction about not wanting to continually reset the shield… that solved the problem for me.

I also wrote a bit of code for initializing the board, and not proceeding until a connection is made. I’m just starting out with programming, so I’m sure there’s a better way to do this, but here’s the code I wrote to kick this whole thing off.

I’m using the Arduino v1.0 rc2 Ethernet library for now - made things much easier. Love me some DHCP.

#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[] = {  0x90, 0xa2, 0xda, 0x00, 0x85, 0x56 };
//IPAddress server(74,125,224,179);    // Google
IPAddress server (207,58,139,247);   // adafruit

// 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() {
  
  // start the serial library:
  Serial.begin(115200);

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // 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();
  //  Give the ethernet shield a second to initialize
  delay(1000);
  ConnectEthernet();
}

void ConnectEthernet() {
  Serial.println("connecting...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.stop();
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
    client.stop();
    delay(3000);
    ConnectEthernet();
  }
}

Working great for me right now. Thanks for your help with this. I’m banging a remote MySQL database about every second. It’s pretty neat!