arduino uno web server stops responding after a week or so

Hello there Im new to arduino world, i have taken a web server sketch and heavily modified it so i can power on/off my computer using 2 relays from the web after entering a password.

the sketch works ok for about a week or so and then arduino stops serving the website.
The strange thing is that while the website isnt served anymore, if i open arduino ide and enable serial output then arduino immediately starts serving the page again!

im trying my best but i cant figure out why it stops responding.

the correct password to operate the relays is 8452 or 8425 both passwords produce the same value on the pin variable by the math operations i selected so both of them will work.

the code is the following
can anyone please help me figure out why arduino stops responding?

/*

#include <SPI.h>

#include <Ethernet.h>


EthernetServer server(80);// Server port



const byte reset = 5;// Select pin for reset

const byte power = 6;// Select pin for power
int pin =0;          // Password Value


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };// Physical MAC address

byte ip[] = { 192, 168, 1, 178 };// Fixed IP address

byte gateway[] = { 192, 168, 1, 1 };// Router Gateway Internet access

byte subnet[] = { 255, 255, 255, 0 };// Subnet mask

String readString;


void setup()

{

delay(300);// Delay for Ethernet shield initialization (Arduino has 65mS Power Up delay and W5100 reset logic has 280mS)

pinMode(reset, OUTPUT);// Define pin for reset as Output

pinMode(power, OUTPUT);// Define pin for power as Output



Serial.begin(9600);// Initialize serial communications at 9600 bps

Serial.println(F("Arduino UNO R3 with Ethernet Shiled W5100 - VU3GAO"));// Display Arduino title

Ethernet.begin(mac, ip, gateway, subnet);// Start Ethernet

server.begin();

Serial.print(F("Ethernet Shield initialized. Local IP address is:"));

Serial.println(Ethernet.localIP());// Print Server IP address

}


void loop()

{

EthernetClient client = server.available();// Create a client connection

if (client == true)

{

while (client.connected())

{

if (client.available())

{

char c = client.read();// Read char by char HTTP request

if (readString.length() < 100)

{

readString = readString + c;// Store characters to string

}

if (c == '\n')

{

Serial.println(readString);

client.println(F("http/1.1 200 ok"));// Send standard http headers

client.println(F("content-type: text/html"));

client.println();

client.println(F("<!doctype html><html>"));

client.println(F("<body bgcolor='9ec3c3'>"));

client.println(F("<center><head><title>Home Web Control</title></head>"));

client.println(F("<h2>Arduino UNO R3 Ethernet Shield Web Server(v2.16)</h2>"));

client.println(F("<h4>Home Automation With Arduino</h4>"));

client.println(F("

<hr/><p> Enter The Correct Password To Turn On And OFF <p/><hr/>


"));

client.print(F("





"));

client.print(F("<input type=button value='&nbsp;1&nbsp;' onmousedown=location.href='/?1_on'>"));
client.print(F("&nbsp;"));
client.print(F("<input type=button value='&nbsp;2&nbsp;' onmousedown=location.href='/?2_on'>"));
client.print(F("&nbsp;"));
client.print(F("<input type=button value='&nbsp;3&nbsp;' onmousedown=location.href='/?3_on'>"));
client.print(F("
"));
client.print(F("<input type=button value='&nbsp;4&nbsp;' onmousedown=location.href='/?4_on'>"));
client.print(F("&nbsp;"));
client.print(F("<input type=button value='&nbsp;5&nbsp;' onmousedown=location.href='/?5_on'>"));
client.print(F("&nbsp;"));
client.print(F("<input type=button value='&nbsp;6&nbsp;' onmousedown=location.href='/?6_on'>"));
client.print(F("
"));
client.print(F("<input type=button value='&nbsp;7&nbsp;' onmousedown=location.href='/?7_on'>"));
client.print(F("&nbsp;"));
client.print(F("<input type=button value='&nbsp;8&nbsp;' onmousedown=location.href='/?8_on'>"));
client.print(F("&nbsp;"));
client.print(F("<input type=button value='&nbsp;9&nbsp;' onmousedown=location.href='/?9_on'>"));
client.print(F("
"));
client.print(F("<input type=button value='&nbsp;*&nbsp;' onmousedown=location.href='/?AK_on'>"));
client.print(F("&nbsp;"));
client.print(F("<input type=button value='&nbsp;0&nbsp;' onmousedown=location.href='/?0_on'>"));
client.print(F("&nbsp;"));
client.print(F("<input type=button value='&nbsp;#&nbsp;' onmousedown=location.href='/?HS_on'>"));
client.print(F("

"));

client.print(F("<input type=button value='Reset' onmousedown=location.href='/?RS_on'>"));

client.print(F("&nbsp;"));

client.print(F("<input type=button value='Power' onmousedown=location.href='/?PW_on'>"));
client.print(F("








"));
client.println(F("



<hr/><p> Home Automation With Arduino <p/>"));

client.println(F("</body></html>"));

delay(1);// Page loading delay

client.stop();// Stopping client


//numbers 8,4,5,2 are make a math operation in order to have the pin variable get the number 25


          if (readString.indexOf("/?1_on") >0) //set pin value of the button 1
           {
               pin = 0;
               Serial.print("PIN= ");
               Serial.println(pin);
  delay(1); 
           }
           
           if (readString.indexOf("/?2_on") >0)  //set pin value of the button 2
           {
               pin = pin - 2;
               Serial.print("PIN= ");
               Serial.println(pin);
  delay(1); 
           }
           
           if (readString.indexOf("/?3_on") >0)  //set pin value of the button 3
           {
               pin = 0;
               Serial.print("PIN= ");
               Serial.println(pin);
  delay(1); 
           }
           
           if (readString.indexOf("/?4_on") >0)  //set pin value of the button 4
           {
               pin = pin * 4;
               Serial.print("PIN= ");
               Serial.println(pin);
  delay(1); 
           }
           
           if (readString.indexOf("/?5_on") >0)  //set pin value of the button 5
           {
               pin = pin - 5;
               Serial.print("PIN= ");
               Serial.println(pin);
  delay(1); 
           }
           
           if (readString.indexOf("/?6_on") >0)  //set pin value of the button 6
           {
               pin = 0;
               Serial.print("PIN= ");
               Serial.println(pin);
  delay(1); 
           }
           
           if (readString.indexOf("/?7_on") >0)  //set pin value of the button 7
           {
               pin = 0;
               Serial.print("PIN= ");
               Serial.println(pin);
  delay(1); 
           }
           
           if (readString.indexOf("/?8_on") >0)  //set pin value of the button 8
           {
               pin = pin + 8;
               Serial.print("PIN= ");
               Serial.println(pin);
  delay(1); 
           }
           
           if (readString.indexOf("/?9_on") >0)  //set pin value of the button 9
           {
               pin = 0;
               Serial.print("PIN= ");
               Serial.println(pin);
  delay(1); 
           }
           
           if (readString.indexOf("/?AS_on") >0)//set pin value of the button *
           {
               pin = 0;
               Serial.print("PIN= ");
               Serial.println(pin);
  delay(1); 
           }
           
           if (readString.indexOf("/?0_on") >0)  //set pin value of the button 0
           {
               pin = 0;
               Serial.print("PIN= ");
               Serial.println(pin);
  delay(1); 
           }
           
           if (readString.indexOf("/?HS_on") >0)  //set pin value of the button #
           {
               pin = 0;
               Serial.print("PIN= ");
               Serial.println(pin);
  delay(1); 
           }


if(readString.indexOf("/?RS_on") > 0 && pin == 25) //if reset button is pressed and the password is correct enable the relay
              {
               digitalWrite(reset, HIGH);
               delay(1000);
               digitalWrite (reset, LOW);
              pin = 0;
               Serial.print("PIN= ");
               Serial.println(pin); 
               
          
  delay(1); 
              }

if(readString.indexOf("/?PW_on") > 0 && pin == 25) //if power button is pressed and the password is correct enable the relay
              {
               digitalWrite(power, HIGH);
               delay(1000);
               digitalWrite (power, LOW);
               pin = 0;
               Serial.print("PIN= ");
               Serial.println(pin);
          
  delay(1); 
              }



readString = "";// Clearing string for next read

}// End of line reached

}// End of client available

}// End of client connected

}// End of client connection

}// End of loop

if i open arduino ide and enable serial output then arduino immediately starts serving the page again!

Opening the IDE serial monitor reboots the arduino.

jimacid: can anyone please help me figure out why arduino stops responding?

I'd suppose, that your programming logic allows client connections to be established, which will never be stopped then.

So perhaps your Ethernet shield networking may run out of connections after some time.

I'd better stop every client connection that had been established.

   }// End of client connected
   client.stop();// Stopping client
   delay(1);// Give some time to stop
  }// End of client connection

I'd also limit the duration of the client connection to a maximum time. So if a malware client connects with a connection and keeps the connection for unlimited time, the condition "while (client.connected())" will be true forever. I'd better use a timeout (10 seconds or so) to prevent unlimited looping in the "while (client.connected())" loop.

BTW: Using the dynamic "String" object class is a good way to fragment the RAM on the long run. I'd better avoid the usage of every "String" object and just use C-strings (nullterminated char arrays) instead.

BTW: As zoomkat just wrote, every opening of the serial monitor will reset the microcontroller and your program starts a new run after a reset.

hmm ok thats why it starts working again after opening serial monitor.

i allready updated the project with the extra piece of code you gave me, hopefully it will do the trick. we will know in about a week 8)

if it stops serving again ill try the connection timeout to see if this helps.

thank you very much both of you.

ill keep my fingers crossed

The arduino auto reset can be defeated with a resistor or capacitor if needed. You might add some socket status code for debugging purposes when the lockup occurs.