[SOLVED] Arduino hangs after some time when constantly using the Ethernet

I have a small test application (a modified version of the EthernetClient example) that is permanently using the Ethernet library. Here is the sketch:

#include <SPI.h>
#include <Ethernet.h>
 
#define GREEN_PIN 41
 
byte mac[] = { ... };
 
// google.de
IPAddress server(173, 194, 69, 94);
int       port = 80;
 
EthernetClient client;
boolean        ethernetAvailable;
char           testCharacter = ' ';
 
void setup() {
  Ethernet.begin(mac);
 
  pinMode(GREEN_PIN, OUTPUT);
  digitalWrite(GREEN_PIN, HIGH);
 
  delay(1000);
}
 
void loop() {
  if (client.connected()) {
    if (client.available()) {
      char c         = client.read();
      testCharacter += c;
    } // no else
    
    digitalWrite(GREEN_PIN, LOW);
  } else {
    client.stop();
 
    digitalWrite(GREEN_PIN, HIGH);
    delay(1000);
  
    if (client.connect(server, port)) {
      client.println("GET / HTTP/1.0");
      client.println();
    } // no else
  }
}

Link to Gist: Demonstration: This code hangs on all arduino versions after some time... · GitHub

This sketch runs for some time and the LED on the GREEN_PIN blinks. Between a few minutes or days the arduino hangs and I don't know why.
I have testst this sketch with this setups:

  • Arduino Ethernet (1.0.3)
  • Arduino Mega (1.0.3) + Ethernet Shield
  • Arduino Due (1.5.1) + Ethernet Shield
  • Arduino Due (1.5.2) + Ethernet Shield
  • Arduino Mega (1.0.3) + Wifi Shield [with a Wifi setup of course]

It's always the same problem, it does what it should for a while and than it hangs. I also logged the output of client.read(); into a file with a serial monitoring tool it works for some time but then stops working while reading. I also tried this using a static IP and none from DHCP, the same issue.

I this a bug or is there any change I have to apply to get this sketch working?

Best regards.

I use this code. You can mix and match the setup to suit your needs. There is both GET and POST code here:
http://playground.arduino.cc/Code/WebClient
And it has the connection fail timeout to prevent those embarrasing lockups when the connection breaks (hardware fail).

Did you find anything ?

I'm having the same type of issue, I use the client.println in a loop and basicly just send the status of a digital pin over and over. Most of the time that works great but after some time (20 mins - 6 hours) the Arduino locks up

I've tried three different arduinos (ethernet and ethermega)

@aselby: Did you try the code in that link? If you expect specific answers, you should provide specific information, like posting your code.

For me SurferTim's code worked.