Ethernet Shield TCP Comm

Hi,

I have a small project. There are 1 Mega, 1 analog proximity sensor and 1 ethernet shield. Mega listens sensor as you guess. If there is an interrupt then mega connects to TCP server and it sends id of itself (Here this is is 2 at code). After first successful tcp connection to TCP server, it keeps in touch with server while there are interrupts from sensor. But if there isn't any interrupt in 60 seconds from last interrupt to current time then it disconnects from server (I do it because this system will work 7/24 and if there is constant connection with TCP server then there could be possible errors because of so long time connection)

My system works 4-5 days very well but sometimes it stops (I check it from TCP Server logs) . I try to understand where problem is. May be problem is related with my TCP Server software which i have developed but there is not any exception log with it. So i want to listen your advices about my code. Are there any problem at my code? Or what can you advice to me for a stable TCP client application with Arduino to complete such system? Your advices are very dear for me :

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

int sensorPin = A2;    
int ledPin = 13;      
int MachineID=2;
int sensorValue = 0;  
int dummy=0;
unsigned long LastTime=0;
unsigned long CurrentTime =0;
unsigned long ElapsedTime =0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 10, 10, 111 };
byte server[] = { 10, 10, 10, 112 }; 
//byte server[] = { 10, 10, 10, 30 }; 
EthernetClient client;
void setup()
{
	Ethernet.begin(mac, ip);
	Serial.begin(9600);
        digitalWrite(ledPin, LOW);
	delay(1000);
	Serial.println("connecting...");
	if (client.connect(server, 8080)) {
		Serial.println("connected");
		digitalWrite(ledPin, HIGH);
                LastTime = millis();	
    }
	else {
		Serial.println("connection failed");
	}
}
void loop()
{
    dummy=SensorControl();
    if (dummy>0) {
        LastTime = millis();
      if (client.connected()) {
        digitalWrite(ledPin, HIGH); 
        client.write("2");  
      }
    else{
      if (client.connect(server, 8080)) {
        digitalWrite(ledPin, HIGH); 
        client.write("2"); 
      }
    }
  }
  else{
    CurrentTime= millis();
      if(CurrentTime-LastTime>60000){
        digitalWrite(ledPin, LOW);
        client.stop();
        delay(400);
   }
    }
}

int SensorControl()
{
          sensorValue=0;
          sensorValue = analogRead(sensorPin);
          if (sensorValue>1015){
            LastTime = millis();            
              while (sensorValue>1015){
                sensorValue = analogRead(sensorPin);
              }
              delay(400);
              return MachineID;  
        }
        else {  
          return 0;
        }
}

And i turn LED on which is connected on mega and ethernet shiled if there is a connection between mega and TCP server. Although the led which is at Mega at port 13 shines very well, the led which is at ethernet shiled at port 13 doesn't shine very well. It can be power issue?I supply this system from USB but i will buy an DC adaptor at 1 A range

Thanks in advance

The Ethernet.begin() function should be called in setup(), but the client.connect() not. You should do that every time you want to connect.
I know there is an example to connect in the setup() function, but that is confusing.

Google for "WebserverST" in the Playground section. It detects open sockets and closes them so they can be used again.

Peter_n:
The Ethernet.begin() function should be called in setup(), but the client.connect() not. You should do that every time you want to connect.
I know there is an example to connect in the setup() function, but that is confusing.

Google for "WebserverST" in the Playground section. It detects open sockets and closes them so they can be used again.

Thanks for dear advice. I will remove client.connect() from setup as your advice. And can you see another issue at my code? Especially reading an analog sensor is headache. You should wait until item remove in fron of sensor and after it you should give waiting time to sensor to wait another item.

My system works in last 10 days without any problem. But i want that it works 365 days without any problem.

I still wait your dear advices.

Regards