Ethernet fails connecting after a while

The EtherTen combines an Uno-equivalent Arduino-compatible board and Wiznet-based Ethernet support, along with a microSD card slot and Power-over-Ethernet support. [Product page]

Ethernet fails connecting after a while

Postby mr-russ » Tue Jan 24, 2012 10:14 am

Hi,

I'm using a freetronics EtherTen. The issues I'm seeing seem to be commonly commented on about bugs in the Ethernet code library. And my debugging so far indicates that the Ethernet library is just not giving the results you expect. I'll explain the debugging later. The symptoms are that after anywhere from 5 minutes - 5 days, the Ethernet refuses to make a successful connection to a server until the board is reset. I'm using Arduino 1.0 + Ethernet patches from git. I'm running on Ubuntu 10.04. My first question is, what are other running for software to keep their ethernet running un-interrupted. I know Jon has light switches running using EtherTens, I can't believe he resets them every now and again to make sure they stay running.

Now what appears to be happening in code;

I've reduced MAX_SOCK_NUM to 1 as I believe it's related to re-use of sockets. I've reduced my default delay from 60seconds to 0. And I've adjusted the return values of client.connect() to be -1 to -4 for each of the possible return locations that send a failure.

We successfully connect a number of times. Could be thousands. Then we attempt to connect and we get -4 returned from the client.connect() code;
Code: Select all
  while (status() != SnSR::ESTABLISHED) {
    delay(1);
    if (status() == SnSR::CLOSED) {
      _sock = MAX_SOCK_NUM;
      return -4;
    }
  }


So it looks like we are getting a connection that's not completely closing for one reason or another. It's probably in time_wait or the like; The code there supposedly sets us in the CLOSED state before returning.

Next time through the loop() function, we get a -2 returned;

Code: Select all
  if (_sock == MAX_SOCK_NUM) {
    for (int i = 0; i < MAX_SOCK_NUM; i++) {
      Serial.println(W5100.readSnSR(i), DEC);
    }
    return -2;
  }


This is the state the code is now stuck in. Every call to client.connect() will result in a -2 returned.
I've also got the code printing what connection status code the wiznet is seeing. For about 20seconds after the first failure I see 0x17 (ESTABLISHED). And after about 20 seconds, I start seeing 0x1C (CLOSE_WAIT) which doesn't seem to ever end.

Does anybody have any ideas what's going on here?

My main sketch code without the DHT reading library;

Code: Select all
#define MAXTIMINGS 85

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

// Ethernet setup
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server(172,17,73,202); // Bob
// initialize the library instance:
EthernetClient client;

class DHT {
 private:
  uint8_t data[5];
  uint8_t _pin;
  boolean read(void);
  unsigned long _lastreadtime;


 public:
  DHT(uint8_t pin);
  uint16_t readTemperature(void);
  uint16_t readHumidity(void);

};


DHT dht(7);

void setup() {
 
 
 
  IPAddress myIp(172,17,73,150); 
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
   
  pinMode(A0, INPUT); 
  Serial.begin(9600);
  Ethernet.begin(mac, myIp);
    // give the ethernet module time to boot up:
  delay(1000);
}


void loop() {
  // Read the temperature and Humidity
  uint16_t h = dht.readHumidity();
  uint16_t t = dht.readTemperature();
  int16_t status = 0;
 
  Serial.println("Looping...");
 
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
 
 
  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    // It failed, dont send any results to the server
    Serial.println("Failed to collected data");
  } else {
    // if you're not connected, and ten seconds have passed since
    // your last connection, then connect again and send data:
    status = client.connect(server, 80);
    Serial.print("Status: ");
    Serial.println(status, DEC);
 
    if (status > 0) {
      unsigned long timeout = millis() + 60000;
      Serial.println("connecting...");
      // send the HTTP PUT request.
      // fill in your feed address here:
      client.print("GET /log.php?t=");
      client.print(t, DEC);
     
      client.print("&h=");
      client.print(h, DEC);
      client.println(" HTTP/1.0\n");
   
      // Get all client data
      // How long to wait for it all to arrive?
      while (millis() - timeout >= 60000 && client.connected()) {
        if (client.available()) {
          char c = client.read();
          Serial.print(c);
        }
      }
      Serial.println();
      Serial.print(millis());
      Serial.print(" disconnecting.");

      client.stop();
      Serial.println(millis());
    } else {
   
      // if you couldn't make a connection:
      Serial.println("connection failed");
    }   
  }
  // Only complete one read per minutes
  delay(0);
 
}
mr-russ
 
Posts: 16
Joined: Wed Jan 18, 2012 8:46 am

Re: Ethernet fails connecting after a while

Postby agascoigne » Thu Jan 26, 2012 10:05 am

Having the same problem. It seems that the Wiznet module freezes.

There are a few tutorials on how to bend a pin on an ethernet shield and use a watchdog timer to reset the module, but is something like this possible on the etherten?

Alternatively, is there another way to periodically reset the wiznet module?

cheers for any help - i've spent the last month tearing my hair out with this one!
agascoigne
 
Posts: 3
Joined: Thu Jan 26, 2012 10:03 am

Re: Ethernet fails connecting after a while

Postby EpicTinker » Fri Jan 27, 2012 3:37 am

Have you tried compiling with an older IDE, like release 22 and see if that makes a difference?

I had a lot of problems with networking when compiled using 23, so I went back to 22, and everything worked.
EpicTinker
 
Posts: 9
Joined: Thu Nov 03, 2011 4:36 pm

Re: Ethernet fails connecting after a while

Postby agascoigne » Fri Jan 27, 2012 4:02 am

Yeah, I have done. Reading around the net it seems to be common to the wiznet W5100.
Is there a way with these boards to reset the Wiznet module? (possibly even by killing the power shortly?) Hitting the reset button on the board is not sufficient - it only resets the rest of the code but the wiznet still hangs.

I dont really understand how the watchdog works, but maybe this would be an alternative way to do it?

cheers for your help

Andre
agascoigne
 
Posts: 3
Joined: Thu Jan 26, 2012 10:03 am

Re: Ethernet fails connecting after a while

Postby mr-russ » Sat Jan 28, 2012 4:32 am

Exciting news of one kind for you all. I think I've debugged it to the point where it appears the Wiznet 5100 leaves connections in the CLOSE_WAIT state, even though you called disconnect and there aren't any available bytes on the connection. I'm not 100% sure if that's a bug in the Ethernet Arduino library, or on Wiznet. I'm still working on understanding the Ethernet code enough to be able to confirm further. But when using EthernetClient I have the code change below in the connect method. With it you get a connection that closes, thinks it in ESTABLISHED for 13 seconds, then stays in CLOSE_WAIT indefinitely. So in connect we allow the re-use of CLOSE_WAIT connection and everything seems to work properly.

When setting MAX_SOCK_NUM to 1, you can see the behaviour I described above by Serial.print the return code of connect and also the current connection status.

Code: Select all
int EthernetClient::connect(IPAddress ip, uint16_t port) {
  if (_sock != MAX_SOCK_NUM)
    return -1;

  for (int i = 0; i < MAX_SOCK_NUM; i++) {
    uint8_t s = W5100.readSnSR(i);
      if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
//      if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
      _sock = i;
      break;
    }
  }



I will soon be testing the item in Ethernet Server mode over the coming weeks to see if I experience the same there. But my reading of the net suggests the CLOSE_WAIT issue exists on the server side, but the Ethernet Server code already accounts for that. Not having looked myself yet, I can't confirm.

If somebody is willing to confirm the fix I have above, or offer any comments on why it's not correct, that would be most helpful.
mr-russ
 
Posts: 16
Joined: Wed Jan 18, 2012 8:46 am

Re: Ethernet fails connecting after a while

Postby davidw » Fri Feb 10, 2012 3:34 am

Hi, I haven't got the expertise to complete the same level of debugging thats being decribed here but I too have experienced the same problem of the ethernet port 'locking up'. My sketch is monitoring multiple i2c expansion ports and generates emails and sms's containing change of state information. All is working OK except every now and then, maybe several days later, I loose ip connectivity. This is fixed by resetting the etherten. I have a LED configured to show me the sketch is running ok so to me it definitely appears to be an issue with the ethernet port. davidw.
davidw
 
Posts: 5
Joined: Mon Jan 23, 2012 9:47 am

Re: Ethernet fails connecting after a while

Postby lnsundh » Tue Feb 21, 2012 9:51 am

I have the same problem. Does anyone have a solution for it? Is there code to replace in order to get it work?

I would be very thankful for an answer!
lnsundh
 
Posts: 1
Joined: Tue Feb 21, 2012 9:46 am

Re: Ethernet fails connecting after a while

Postby Sleurhutje » Thu Mar 01, 2012 9:26 am

I'm having the same kind of problems. However, problem is that the Ethernet library/WizNet configuration is loosing the second byte of the gateway address and is set to 0. It doesn't matter if I'm using DHCP or fixed IP. Half the times the EtherTen is powered up, things go right. But when things go wrong, resetting doesn't change anything. When powering off, I have to wait at least 20 seconds before powering up again otherwise the second byte of the gateway address will be set to 0 for sure. Also when out the blue he connection fails, the second byte of the gateway address is set to 0 and communication fails.

I've tried different networks with diffrent types of connections, routers, gateways, DHCP servers etc. All result with the same failure. :(

Example of the preset IP (response from the script):
IP: 172.16.0.187
Mask: 255.255.255.0
GW: 172.16.0.252
DNS: 172.16.0.20

When teh connection fails:
IP: 172.16.0.187
Mask: 255.255.255.0
GW: 172.0.0.252
DNS: 172.16.0.20
Sleurhutje
 
Posts: 28
Joined: Thu Mar 01, 2012 9:18 am

Re: Ethernet fails connecting after a while

Postby cjdelphi » Mon Mar 19, 2012 10:33 am

mr-russ wrote:Exciting news of one kind for you all. I think I've debugged it to the point where it appears the Wiznet 5100 leaves connections in the CLOSE_WAIT state, even though you called disconnect and there aren't any available bytes on the connection. I'm not 100% sure if that's a bug in the Ethernet Arduino library, or on Wiznet. I'm still working on understanding the Ethernet code enough to be able to confirm further. But when using EthernetClient I have the code change below in the connect method. With it you get a connection that closes, thinks it in ESTABLISHED for 13 seconds, then stays in CLOSE_WAIT indefinitely. So in connect we allow the re-use of CLOSE_WAIT connection and everything seems to work properly.

When setting MAX_SOCK_NUM to 1, you can see the behaviour I described above by Serial.print the return code of connect and also the current connection status.

Code: Select all
int EthernetClient::connect(IPAddress ip, uint16_t port) {
  if (_sock != MAX_SOCK_NUM)
    return -1;

  for (int i = 0; i < MAX_SOCK_NUM; i++) {
    uint8_t s = W5100.readSnSR(i);
      if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
//      if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
      _sock = i;
      break;
    }
  }



I will soon be testing the item in Ethernet Server mode over the coming weeks to see if I experience the same there. But my reading of the net suggests the CLOSE_WAIT issue exists on the server side, but the Ethernet Server code already accounts for that. Not having looked myself yet, I can't confirm.

If somebody is willing to confirm the fix I have above, or offer any comments on why it's not correct, that would be most helpful.


Genius :) - problem solved, muchos gracias, since it's the code that's at fault, i'm betting the ethernet shields on a standard arduino board will suffer the same fate.

at least there's no more hang ups now, there's a small delay if you repeatedly pound the socket with connections but within 3 seconds it recovers, great job on that modification :) - next time i'll use something like netstat and look at the unclosed connections.
cjdelphi
 
Posts: 36
Joined: Mon Mar 19, 2012 10:11 am

Re: Ethernet fails connecting after a while

Postby Sleurhutje » Thu Mar 29, 2012 2:32 pm

@mr-russ: You're my hero. :ugeek:

Added the SnSR::CLOSE_WAIT to the EthernetClient.cpp (in Arduino 1.0 Ethernet Library line 44) and the EtherTen is running fine for a continuous 72 hours. Normally it would crash within 24 hours. Even in a high latency network no connection failures. Sometime the wait state takes up 60 seconds but all keeps going on without crashing.
Sleurhutje
 
Posts: 28
Joined: Thu Mar 01, 2012 9:18 am

Next

Return to EtherTen

Who is online

Users browsing this forum: No registered users and 1 guest