Ethernet shield client.connect() fails endlessly

I have some code based on this Pachube example: http://arduino.cc/en/Tutorial/PachubeCient

My problem is that the code runs fine for a few hours (or even less or longer), but then get stuck in the else-part in the sendData() function.

// if there's a successful connection:
if (client.connect()) {
...
}
else {
  // if you couldn't make a connection:
  Serial.println("connection failed");
}

I.e. the ethernet shield is not able to connect to the server, but I wonder why the Arduino does not recover from this state as this is re-checked in every loop?
Only solution is a HW reset of the Arduino.
Am I missing something obvious?

It could be that the Wiznet chip has gone into some weird state.

After reading similar complaints from others, I set up my web-based system to do the Ethernet shield reset via a digital pin, and wrote my code to do both a hardware reset and software re-initialization if a few retries didn't establish the connection.

Can that really be the case?
I mean, is it a so uncommon setup to have an auduino+ethernet shield and wanting this to run continuously, that this behaviour has not been found/fixed before/already?

I have a similar problem but it depends heavily on the cabling. When using cat5 cables to my server I can run for days in a row without reboot. However when using "IP over 220Volt" boxes, the connection drops after 15 minutes - 8 hours. have build in an automatic reset when this occors (see below), reboots takes less than a second.

// definition
void(* resetFunc) (void) = 0; //declare reset function @ address 0
// just call it this way to reset the board
resetFunc();

Rob

How about messing with the ethernet initialization?
E.g. calling the Ethernet.begin(mac, ip); every time the data are to be sent?

So now it ran 24h straight (with no change to the code) - but it did crash after those 24h (24h30min to be exact).

Would this error occur if the Arduino tries to send data while still receiving the GET response, and would that also result in a lock-up situation?

additional to my reset post earlier http://support.atmel.no/bin/customer?=&action=viewKbEntry&id=21, teh option I posted does not completely reset the Arduino. Be aware that the code at the ATMEL site can lock your Arduino as it resets every 30 millisec. You have no time to upload a new sketch!

Would this error occur if the Arduino tries to send data while still receiving the GET response, and would that also result in a lock-up situation?

You might be out of sockets, you need to close them properly. Can you post your whole code as the snippet does not give enough clues,

Rob

I seem to have stumbled over an old/known issue:
http://sirleech.wordpress.com/2010/06/24/pachube-cloud-sensor-build/

Code that is failing is like this:

void setup() {
  // start the ethernet connection and serial port:
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);
  // give the ethernet module time to boot up:
  delay(1000);
}

void loop() {
  // read the analog sensor:
  int sensorReading = analogRead(A0);  

  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }

  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    sendData(sensorReading);
  }
  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
}

// this method makes a HTTP connection to the server:
void sendData(int thisData) {
  // if there's a successful connection:
  if (client.connect()) {
    Serial.println("connecting...");
    // send the HTTP PUT request.
    // fill in your feed address here:
    client.print("PUT /api/YOUR_FEED_HERE.csv HTTP/1.1\n");
    client.print("Host: www.pachube.com\n");
    // fill in your Pachube API key here:
    client.print("X-PachubeApiKey: YOUR_KEY_HERE\n");
    client.print("Content-Length: ");

    // calculate the length of the sensor reading in bytes:
    int thisLength = getLength(thisData);
    client.println(thisLength, DEC);

    // last pieces of the HTTP PUT request:
    client.print("Content-Type: text/csv\n");
    client.println("Connection: close\n");

    // here's the actual content of the PUT request:
    client.println(thisData, DEC);

    // note the time that the connection was made:
    lastConnectionTime = millis();
  }
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}

At random intervals - anything from minutes to days (>24h) - the ethernet shield fail to establish the connection permanently in the sendData() function.

Was thinking if it would make any difference to call the Ethernet.begin() if the connection is not established say after 2-3 upload attempts. If the ethernet shield is in f-mode, it might not make a difference?
Have not had time to look at the softReset option mentioned, but it would probably be ok, since I am interested in keeping all the variables intact.

I added an ethernet reset function, and it seems to work... let's see if that verdict is premature.

void resetEthernet() {
  client.stop();
  delay(1000);
  Ethernet.begin(mac, ip, gateway, subnet);
  delay(1000);
}

Not sure if the delays are needed in there at all.

Function is called if sendData() is not able to connect within 3 minutes.

Hi, I am also using Arduino (Uno with v021 code) and Ethernet shield. Using same code as Spelle. I was getting Pachube Freezing occasionally (1-2 times/week?) , and it appears to be the Ethernet shield locking up? I have bent the shield reset pin out of the way and connected it to pin 9 of Arduino, so that I can reset the Shield from the Arduino when there are failed connections. I have not yet implemented the watchdog timer reset in the Arduino, does anyone know if I still need to burn a new bootloader into the latest version of the UNO?

I am using Arduino UNO and Ethernet Shield rev5...
I was getting a similar problem with the ethernet failing to connect.... occasionally, but with a much higher incidence of failures with higher ambient temperature?
It appears that I had two issues....

  1. Power Supply. I was using a 12v DC 600mA wall wart adapter to power the Arduino/Ethernet Shield combination. Trouble is that the current drawn by the Ethernet Shield/Arduino combination is about 300mA+? Voltage being dropped by the regulator on the Arduino board (12v to 5v) was too high, and in effect with this voltage drop (7v)the regulator can only supply 400mA Max (ref the MC33269D-5.0 Data sheet) the regulator was getting v.hot and I am certain that this was causing problems... I have now changed to a 7.5V 1A wall wart adapter and so the Arduino regulator is much cooler and happier!! and hopefully I will now get less problems.
  2. Having checked with Ran Talbot he confirmed that by using the Arduino pin 9 output to reset the Ethernet shield, His setup ran with no problems for a long time.."The system ran solidly for almost a year, recovering from power outages and internet link failures without manual intervention".. Trouble is that with the Arduino UNO and Ethernet Shield rev5, there are two reset connections between the boards so it is necessary to bend out the reset pin on the power connector and ALSO to cut pin 5 on the ICSP connector. I have now done this so that the pin 9 reset is working... see http://community.pachube.com/arduino/ethernet/watchdog#comment-1363

Can someone please help me by sending a detialed tutorials on how to control a LED tthrough a web by using arduino and ethernet shield.. and to display the status of the LED.PLEASE HELP.... :frowning: :frowning: :-[
please email me at gorospejma@yahoo.com..tnx a lot Godbless you..

I also had a problem with client.available(), but after doing what I want, I just call client.stop(); and client.available() then works again next time it loops.

Not sure if it is the best way to do it, but it works for me.