Ethernet Shield + Socket -> sometimes freeze

Hello!

I’ve three (arduino + ethernet shield + relay) in a network with server socket configured. A linux server creates a socket sometimes in a day and send a message to the arduinos pointing them to close the relay circuit some seconds (pin 7, in my case). The rest of the time Arduinos are waiting…

Sometimes (frequently) I must to turn off / turn on one of the arduinos because It’s frozen and doesn’t response a ping from the linux server. This is the way I know one of the Arduinos is not working.

My Question: Can I detect from my arduino if It is not present in the network and reboot it by software?

My code in Arduino (I modified a chat server to make It works):

/*
Chat Server

A simple server that distributes any incoming messages to all
connected clients. To use telnet to your device’s IP address and type.
You can see the client’s input in the serial monitor as well.
Using an Arduino Wiznet Ethernet shield.

Circuit:

  • Ethernet shield attached to pins 10, 11, 12, 13
  • Analog inputs attached to pins A0 through A5 (optional)

created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe

*/

#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.
// gateway and subnet are optional:
#if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io
;
#else
byte mac = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE};
#endif

//#define USE_DHCP

IPAddress ip(192, 168, 4, 232);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 252, 0);
// fill in your Domain Name Server address here:
IPAddress myDns(8, 8, 8, 8); // google puble dns

// Change default port to 5000
EthernetServer server(5002);
boolean alreadyConnected = false; // whether or not the client was connected previously

String readString = “”;
String auxiliar = “”;
int pinOut = 7;

void setup() {
// initialize the ethernet device
#if defined USE_DHCP
#if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io
Ethernet.begin();
#else
Ethernet.begin(mac);
#endif
#else
#if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io
Ethernet.begin(ip, myDns, gateway, subnet);
#else
Ethernet.begin(mac, ip, myDns, gateway, subnet);
#endif
#endif

// start listening for clients
server.begin();
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode(7, OUTPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

Serial.print(“Chat server address:”);
Serial.println(Ethernet.localIP());
}

void loop() {
// wait for a new client:
EthernetClient client = server.available();

// when the client sends the first byte, say hello:
if (client) {
if (!alreadyConnected) {
// clead out the input buffer:
client.flush();
//Serial.println(“We have a new client”);
client.println(“Hello, client!”);
alreadyConnected = true;
//Serial.print("Dentro de cabecera: ");
//Serial.println(readString);
}

if (client.available() > 0) {
// read the bytes incoming from the client:
int segundosEncendido = 6000;
int longitud, i;
auxiliar = “deberia”;
char thisChar = client.read();
if (thisChar != ‘.’){
readString += thisChar;
}else {
readString += ‘\0’;
longitud = readString.length();
//Serial.print("Longitud del string: ");
//Serial.println(longitud);
//Serial.println(readString);
if(readString.indexOf(“deberia”) < 0){
String segundosString = “”;
for(i=4; i<9; i++){
if(isdigit(readString*)){*
_ segundosString += readString*;_
_
}_
_
}_
_ segundosEncendido = 1000 * segundosString.toInt();_
_
Serial.print(“Encendemos LUZ”);_
_
digitalWrite(pinOut, HIGH);_
_
delay(segundosEncendido); _
_
Serial.print(“Apagamos LUZ”);_
_
digitalWrite(pinOut, LOW); _
_
segundosString = “”; _
_
}_
_
readString = “”;_
_
}_
_
}_
_
}_
_
}[/color]*_
Thank you in advance.
Regards.

Could be you are running out of sockets or memory. I see no client.stop() call.