Ethernet Shield unstable request timed out

Hi Everyone,

I have been struggle almost two week still cannot solve problem, I have create a program that can trigger a Light to turn on via GET request from URL, The light will turn on for amount of time (Default 10 sec) that have been configured via C# (Write and read from Serial) after that Light will be turn off but there are two light we will dealing with so Example

http://192.168.1.22/Red and Configured with delay time 10 sec

Red Light will turn on for 10 Seconds

if http://192.168.1.22/Green and Configured with delay time 5 sec

Green Light will turn on for 5 Seconds

But if Ethernet Shield receive GET request from http://192.168.1.22/Red and while Red light turn on for 5 second and instantly if Ethernet Shield receive GET request from http://192.168.1.22/Green the red light will switch off immediately (even there are 5 second left until it off) (timer will be reset) and Green light will turn on

The problem is after it switch between both channel of light frequently Ethernet Shield will not response and request timed out as well as cannot ping from PC So I am not sure how to optimize my code to overcome problem

Here is my code

FirsPage

int ports = 80;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x29 };  
byte ipAddr[] =  { 192,168,1,29};                        
byte subnet[] = {255,255,255,0};           
byte gateways[] = {192,168,1,1};                      
byte dnsserver[] = {192,168,1,1};                       
EthernetServer server(80);            
String readString;
byte clientID;


void setup()
{
 // disable SD card
 pinMode(4, OUTPUT);
 digitalWrite(4, HIGH);

 Serial.begin(9600);

 ReadConfig();


  pinMode(CH1, OUTPUT);
  pinMode(CH2, OUTPUT);


  server = EthernetServer(ports);
  Ethernet.begin(mac, ipAddr, dnsserver, gateways, subnet);
  server.begin();
  delay(2000);
}

void loop()
{

    while (Serial.available() > 0)
    {

          ReadString = Serial.readString();

          if(ReadString.substring(0,2) == "#p") {

            ip = ReadString.substring(2);
            strcpy(buf, (char*)(ip.c_str()));
            eeprom_write_string(1, buf);

          }  
          if(ReadString.substring(0,2) == "#s") {

            submask = ReadString.substring(2);
            strcpy(buf, (char*)(submask.c_str()));
            eeprom_write_string(15, buf);
            

          }
           if(ReadString.substring(0,2) == "#g") {

            gateway = ReadString.substring(2);

             gateway = ReadString.substring(2);
            strcpy(buf, (char*)(gateway.c_str()));
            eeprom_write_string(30, buf);

          }
           if(ReadString.substring(0,2) == "#r") {

             port = ReadString.substring(2);
             port = ReadString.substring(2);
            strcpy(buf, (char*)(port.c_str()));
            eeprom_write_string(45, buf);

          }
           if(ReadString.substring(0,2) == "#1") {

            Channel1 = ReadString.substring(2);

             Channel1 = ReadString.substring(2);
            strcpy(buf, (char*)(Channel1.c_str()));
            eeprom_write_string(50, buf);

          }
            if(ReadString.substring(0,2) == "#2") {

            Channel2 = ReadString.substring(2);

             Channel2 = ReadString.substring(2);
            strcpy(buf, (char*)(Channel2.c_str()));
            eeprom_write_string(60, buf);


          }
            if(ReadString.substring(0,2) == "#3") {

            Channel3 = ReadString.substring(2);

             Channel3 = ReadString.substring(2);
            strcpy(buf, (char*)(Channel3.c_str()));
            eeprom_write_string(70, buf);


          }
            if(ReadString.substring(0,2) == "#4") {

            Channel4 = ReadString.substring(2);
            strcpy(buf, (char*)(Channel4.c_str()));
            eeprom_write_string(80, buf);


          }
           if(ReadString.substring(0,2) == "#a") {

            Relay1 = ReadString.substring(2);
            strcpy(buf, (char*)(Relay1.c_str()));
            eeprom_write_string(90, buf);

          }

          if(ReadString.substring(0,2) == "#b") {

            Relay2 = ReadString.substring(2);
            strcpy(buf, (char*)(Relay2.c_str()));
            eeprom_write_string(100, buf);

          }

          if(ReadString.substring(0,2) == "#c") {

            Relay3 = ReadString.substring(2);
            strcpy(buf, (char*)(Relay3.c_str()));
            eeprom_write_string(110, buf);

          }

          if(ReadString.substring(0,2) == "#d") {

            Relay4 = ReadString.substring(2);
            strcpy(buf, (char*)(Relay4.c_str()));
            eeprom_write_string(120, buf);

          }  
    }

Second Page

// Create a client connection
  EthernetClient client = server.available();
  clientID = client.getSocketNumber();
  
  if (client) {
    if (client.connected()) {   

       
      if (client.available()) {
        char c = client.read();
     
        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
          //Serial.print(c);
         }

         //if HTTP request has ended
         if (c == '\n') {          
           Serial.println(readString); //print to serial monitor for debuging
     
           client.println("HTTP/1.1 200 OK"); //send new page
           client.println("Content-Type: text/html");
           client.println("Connection: keep-alive");
           client.println("Keep-Alive: timeout=50, max=100");
           client.println("Connection: close");  
           client.println();     
           client.println("<HTML>");
           client.println("<HEAD>");
           client.println("<TITLE>CCTV Light Control Server</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println(F("<H4>CCTV Light Control Server</H4>"));  
           client.println("
");  
           client.println("<a href=\"/" + Channel1 + "\"\">Channel 1</a> | "); 
           client.println("<a href=\"/" + Channel2 + "\"\">Channel 2</a>");  
           client.println("
");     
           client.println("
"); 
           client.println("<a href=\"/" + Channel3 + "\"\">Channel 3</a> | "); 
           client.println("<a href=\"/" + Channel4 + "\"\">Channel 4</a>"); 
           client.println("
"); 
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           //client.flush();
           client.stop();
           
           //controls the Arduino if you press the buttons
           if (readString.indexOf(Channel1) >0){
             readString=""; 
             int  val2 = digitalRead(CH2);




             if(val2 == 1) {
               StartdelayTime2 = false;
               digitalWrite(CH2, LOW);
             }

           
            
            StartdelayTime1 = true;
            count=0;
            //timer.enable(interruptID);  
           }
            if (readString.indexOf(Channel2) >0){

             readString="";
             int  val1 = digitalRead(CH1);


             if(val1 == 1) {
               StartdelayTime1 = false;
               digitalWrite(CH1, LOW);
             }

        

          //client.stop();
          StartdelayTime2 = true;
          count2=0;
          //timer.enable(interruptID2);
              
            }
         
           
            //clearing string for next read
            readString=""; 
           
         }
       }

       if (!client.connected()) {
          Serial.println();
          Serial.println("disconnecting.");
          client.flush();
          client.stop();
       }
    }
}

Third Page

if( StartdelayTime1 == true) {
    
  singleDelay.start(Relay1.toInt()*1000); // calls while running are ignored

              digitalWrite(CH1,1);      
             if(singleDelay.elapsed()) {
              
               digitalWrite(CH1, 0); 
                StartdelayTime1 = false;
             }
  }

   if( StartdelayTime2 == true) {

    singleDelay.start(Relay2.toInt()*1000); // calls while running are ignored

              digitalWrite(CH2,1);
             if(singleDelay.elapsed()) {
              
               digitalWrite(CH2, 0); 
                StartdelayTime2 = false;
             }
  }

} //End Loop

Thank you very much!!

Thank you very much!!

Thank you for posting your code properly. NOT.

Post ALL of the code in ONE code block. You do NOT want us to have to sew that code back together.

You REALLY need to learn about the F() macro and keep string literals out of SRAM.

You REALLY need to ditch the String class. Learn to use strings - NULL terminated arrays of chars.