Ethernet reconnect

Hey guys,
it turns out that the Ethrenet-Shield I use (official Arduino Ethernet shield) seems to work fine as long as I attempt to connect to it one time. With one connection it runs totally fine, but wen the connection gets lost, the Ethernet shield doesn`t realize the connection is gone.

Now I have a two questions:

Is there

  • a way to make sure the ethernet shield understands a connection got lost ? At the moment it just sends and receives data to another device which is not listening/sending any more, which is a deadlock, because I cannot reconnect and replace the current connection

  • any smart Approach how to deal with the situation? What is the common way? My next approach would be to implement a heartbeat and check via that if there is still a listener on the other side. In case several heartbeats don`t get throught, I know the connection is lost and I disconnect on the Ethernet-Shield, waiting for a new connection. Would that be a common solution?

My server code does. The top code has all the fixes. The bottom code is simpler but is missing only the checkSockStatus function.
http://playground.arduino.cc/Code/WebServerST

So this is sort of a heartbeat implementation by checking if there is still any data transmitted.
that might work fine for me, but doesn`t feel like a 'nice' solution from an IT point of view.
But it should work.

Anyway, I`d like to find out if there is another solution to realize lost Connection via the 'Client.connected()'. function.

anyone with an idea?

No, client.connected() does not detect a stalled server or broken connection. The client.connected() call will only return false if the connection is closed by a "close" packet. If the server is stalled or the connection breaks, you will never receive that 'close' packet.

Hello everyone,

I'm trying to make my Arduino to reconnect to host if connection is lost. I don't want to use delay, trying to use millis() instead with 5 seconds of delay. For some reason it works weird: if host is unavailable, I'm getting message to serial "host unavailable" every 5 seconds as i wanted for about 10 times, and then something happens and it looks like Arduino sends this message with no delay, and when I turn on host back it doesn't reconnect, still sending message in endless loop.

Any idea why it happens?

Here is my code:

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


bool clientFbConnected = false;
bool setFbReconnect = false;
int  timeFbReconnect = 0;
int  ReconnectDelay = 5000;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 200);
IPAddress myDns(192,168,1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
EthernetClient clientFb;
IPAddress LegrandIp(192, 168, 1, 222);

// connecting to a gateway
void getFbConnection(){
  
  // setting timer 
  if(!setFbReconnect){
    setFbReconnect = true;
    timeFbReconnect = millis();
    return;
  }
  // checking if timer expired
  if(setFbReconnect && ((millis()-timeFbReconnect)>ReconnectDelay)){
    
    setFbReconnect = false;
    
    // try to connect, report to serial
    if (clientFb.connect(LegrandIp, 20000)) {
      Serial.println("Feedback connected");
      
    } else {
    // if you didn't get a connection to the server:
    Serial.println("Feedback connection failed");
    }
    
  }
}

// receiving bytes from Receive connection
void receiveData(){
  if (clientFb.available() > 0) {
      // read the bytes incoming from the client:
      char thisChar = clientFb.read();
      Serial.write(thisChar);
  }
}

void setup() {
  // initialize the ethernet device
  Ethernet.begin(mac, ip, myDns, gateway, subnet);
}

void loop() {
  if (clientFb.connected()) 
    receiveData();
  else 
    getFbConnection();
  }
    timeFbReconnect = millis();

What type does millis() return? What type variable are you storing the value in? What happens when the types aren't the same?

PaulS:

    timeFbReconnect = millis();

What type does millis() return? What type variable are you storing the value in? What happens when the types aren't the same?

Yeah just found it, I have to use unsigned long, thank you!