client server code freezes

I am trying to have one arduino have a webserver which a user can press an app button and turn on two leds using two arduinos.

I pretty much have it figured out, but for some reason after you do the server side, and it sends a message to the second arduino, the second arduino freezes after a while.

I checked the freeRam, and there is plenty of that so I figure it has something to do the buffer. I did my best to make sure everything was read, and the buffer was emptied, but for some reason i still can’t keep the arduino freezing, nor can I figure out the reason behind this problem.

I posted the full code In the attachments, and the screen shot of where the slave froze, i started both arduinos at the same time, so you can see how the master kept going. I did noticed when the slave freezes both the green Link lights on both of the arduino wifi shields turn red for a few seconds then turn green again. But the slave is frozen. I can’t figure out why it would go another 700,000 cycles after the last change was made before freezing.

MASTER

SETUP STARTING
Attempting to connect to SSID: ProbablyYours
Connected to wifi
SSID: ProbablyYours
IP Address: 192.168.0.177
signal strength (RSSI):-60 dBm
0   41875   30000   1095   master arduino
1   115950   60000   1095   master arduino
new server side client
turn led on, and tell slave to do the same
client disonnected
Arduino Controllie identified
arudino 2 confirmed led was turned on
currentLine.lenght()==0
 finished waiting
Finished loop
2   170288   90000   1095   master arduino
3   244303   120000   1095   master arduino
4   318353   150000   1095   master arduino
5   392423   180000   1095   master arduino
6   466480   210000   1095   master arduino
7   540547   240000   1095   master arduino
8   614578   270000   1095   master arduino
9   688640   300000   1095   master arduino
10   762697   330000   1095   master arduino
11   836767   360000   1095   master arduino
12   901178   390000   1095   master arduino
13   974445   420000   1095   master arduino

SLAVE

Attempting to connect to SSID: ProbablyYours
Connected to wifi
SSID: ProbablyYours
IP Address: 192.168.0.174
signal strength (RSSI):-54 dBm
0   53239   30000   1342   slave arduino // freezes
1   125966   60000   1342   slave arduino // freezes
ArduinoThatControls Communicating
led high
currentLine.lenght() is 0
confirming the light is on
client disonnected
2   192189   90000   1342   slave arduino // freezes
3   264188   120000   1342   slave arduino // freezes
4   336240   150000   1342   slave arduino // freezes
5   408301   180000   1342   slave arduino // freezes
6   480348   210000   1342   slave arduino // freezes
7   552409   240000   1342   slave arduino // freezes
8   624433   270000   1342   slave arduino // freezes
9   696494   300000   1342   slave arduino // freezes
10   768547   330000   1342   slave arduino // freezes
11   840608   360000   1342   slave arduino // freezes

The screenshots are from

if(currentMillis - previousMillis >= interval) // print this every 2 seconds
  { previousMillis = currentMillis;   
    Serial.print(PrintCount);
    Serial.print(F("   "));
    Serial.print(LoopCount);
    Serial.print(F("   "));
    Serial.print(millis());
    Serial.print(F("   "));
    Serial.print(freeRam());
    Serial.print(F("   "));
    Serial.println(F("master arduino"));
    PrintCount++;
  }
  LoopCount++;

theMASTER.ino (9.51 KB)

theSLAVE.ino (6.2 KB)

below is the main part from the master

void SendFreezeCode()
{ //WiFiClient client = server.available();
  if (client.connect(ArduinoThatFreezes, 80)) 
  { digitalWrite(WiFiPin,HIGH);
    Serial.println("Arduino Controllie identified"); // Make a HTTP request:
    String currentLine="";
    boolean PrintOK=1;
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println("Connection: close");
    if (relaytheH==HIGH)
    client.print("GET /H"); // turn led high on arduino that freezes
    else
    client.print("GET /L"); // turn led high off arduino that freezes
    client.println("Connection: close\r\n");
    unsigned long StartTime =millis();// used so that after info is sent, it waits a second to see if a responce follows.
    boolean currentLineIsBlank=0;
    boolean TimeOut=0;
    int TimeToWait=10000; // wait one second after info is sent to see if responce comes
    while (client.connected() || TimeOut==0)// tired to be clever about not using a delay, didn't seem to work
    {    
      if (client.available()) // if there's bytes to read from the client,
      {             
        char c = client.read();             // read a byte, then
        //Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') // you're starting a new line
        { 
          if (currentLine.length() == 0)
          { 
            Serial.println(F("currentLine.lenght()==0"));
            break;
          }
          else
          { 
            //Serial.println(currentLine.length());
            //Serial.println(currentLine);
            currentLine="";     // clear string
          }
        }
        else if (c != '\r') // if you got anything else but a carriage return character,
        {
          currentLine+=c;
          StartTime=millis();
        }
        if (PrintOK==1)
        {
          if (currentLine.endsWith("GET /Gh")) // confirmed The H was recieved by arudino2
          { 
            PrintOK=LOW;
            Serial.println(F("arudino 2 confirmed led was turned on"));
          }
          if (currentLine.endsWith("GET /Gl")) // confirmed the L was recieved by arduino2
          { 
            PrintOK=LOW;
            Serial.println(F("arudino 2 confirmed led was turned off"));
          }
        }
      }
      if (millis()-StartTime>=TimeToWait)
      { TimeOut=1; // Timed out, probably got disconnected but still waiting for some reason
        Serial.println(F("============Timed the F out  ============="));
      }
    }
    //client.println("Connection: close\r\n");
    Serial.println(F(" finished waiting"));
  }
  else 
  { // if you didn't get a connection to the server:
    Serial.println(F("No Controllie Arduino Detected"));
  }
  Serial.println(F("Finished loop"));
  delay(150);
  client.stop();
  digitalWrite(WiFiPin,LOW);
}

void SimpleServerController()
{
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {    // if you get a client,
    digitalWrite(WiFiPin,HIGH);
    unsigned long InternetStart=millis();
    boolean PrintOK=HIGH; // only print once
    boolean TimeOut=0;
    Serial.println("new server side client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected() && TimeOut==0) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        //Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> turn the LED on pin 9 on
");
            client.print("Click <a href=\"/L\">here</a> turn the LED on pin 9 off
");
            //client.println("Connection: close\r\n");
            
            // The HTTP response ends with another blank line:
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            //Serial.println(currentLine);
            currentLine = "";
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
        if (PrintOK==HIGH); // only print once
        { // Check to see if the client request was "GET /H" or "GET /L":
          if (currentLine.endsWith("GET /H")) { // GET /H turns the LED on
            digitalWrite(ServerLedPin, HIGH);
            relaytheH=HIGH;
            PrintOK=LOW;
            Serial.println(F("turn led on, and tell slave to do the same"));
          }
          if (currentLine.endsWith("GET /L")) { // GET /L turns the LED off
            digitalWrite(ServerLedPin, LOW);  
            relaytheL=HIGH;
            PrintOK=LOW;
            Serial.println(F("turne off, and tell slave to do the same"));
          }
        }
      }
      if (millis()-InternetStart>=TimeOutTime)
      { TimeOut=1; // Timed out, probably got disconnected but still waiting for some reason
        Serial.println(F("============Timed the F out ============="));
      }
    }
    // close the connection:
    delay(150);
    client.stop();
    Serial.println("client disonnected");
  }
  digitalWrite(WiFiPin,LOW);
}

and this is the main part from the slave that eventually causes it to freeze. if this part isn't ran, it doesn't freeze.

void ListenForController()
{
  WiFiClient client = server.available();   // listen for incoming clients
  if (client) // if you get a client,
  {    
    digitalWrite(WiFiPin,HIGH);
    unsigned long InternetStart=millis();
    boolean PrintOk=HIGH; // only print once
    boolean TimeOut=0;
    unsigned long st=millis();
    Serial.println(F("ArduinoThatControls Communicating"));           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()&& TimeOut==0) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        //Serial.write(c);  // print it out the serial monitor
        if (c == '\n' )  // if the byte is a newline character
        {                    
          if (currentLine.length() == 0) 
          { 
            Serial.println(F("currentLine.lenght() is 0"));
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            //client.println("Connection: close");
            if (relaytheH==HIGH)
            { 
              Serial.println(F("confirming the light is on"));
              client.print("GET /Gh"); // confirms got the H
              relaytheH=LOW;
            }
            if (relaytheL==HIGH)
            { Serial.println(F("confirming the light is off"));
              client.print("GET /Gl"); // confirms got the L
              relaytheL=LOW;
            }
            client.println();
            client.println("Connection: close\r\n");
            break;
          } 
          else  currentLine = "";
        } 
        else if (c != '\r')  // if you got anything else but a carriage return character,
        currentLine += c;      // add it to the end of the currentLine
        if (PrintOk==HIGH)  // Check to see if the client request was "GET /H" or "GET /L":
        {
          if (currentLine.endsWith("GET /H")) // GET /H turns the LED on
          { 
            digitalWrite(ServerLedPin, HIGH);
            relaytheH=HIGH;
            PrintOk=LOW;
            Serial.println(F("led high"));
          }
          if (currentLine.endsWith("GET /L")) // GET /L turns the LED off
          { 
            digitalWrite(ServerLedPin, LOW);  
            relaytheL=HIGH;
            PrintOk=LOW;
            Serial.println(F("led low"));
          }
        }
      }
      if (millis()-InternetStart>=TimeOutTime)
      { TimeOut=1; // Timed out, probably got disconnected but still waiting for some reason
        Serial.println(F("============Timed the F out ============="));
      } //else Serial.println(st+m9-millis());
    }
    // close the connection:
    delay(250);
    client.stop();
    Serial.println(F("client disonnected"));
  }
  digitalWrite(WiFiPin,LOW);
}

This is the most frustrating thing i’ve ever dealt with. why is it freezing after the server code has been run, and why does it take over 700,000 cycles through the loop() to freeze after the server code has run?

slave code that eventually freezes

void ListenForController()
{
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {    // if you get a client,
    digitalWrite(WiFiPin,HIGH);
    unsigned long InternetStart=millis();
    boolean PrintOK=HIGH; // only print once
    boolean TimeOut=0;
    boolean ProperDisconnect=0;
    int Nodelay=150;
    Serial.println("new server side client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected() && TimeOut==0) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        //Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

            // the content of the HTTP response follows the header:
            if (relaytheH==HIGH)
            { 
              Serial.println(F("confirming the light is on"));
              client.print("GET /Gh"); // confirms got the H
              client.print("
");
              relaytheH=LOW;
            }
            if (relaytheL==HIGH)
            { Serial.println(F("confirming the light is off"));
              client.print("GET /Gl"); // confirms got the L
              client.print("
");
              relaytheL=LOW;
            }
            //client.println('\n');
            //client.println("k");
            //client.println("</html>");
            client.print("EatIt");
            client.print("
");
            client.println();
            
            // The HTTP response ends with another blank line:
            ProperDisconnect=1;
            InternetStart=millis()+TimeOutTime-Nodelay;
          }
           // if you got a newline, then clear currentLine:
            //Serial.println(currentLine);
            currentLine = "";
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        ProperDisconnect=0;
        InternetStart=millis();
      }
        if (PrintOK==HIGH); // only print once
        { // Check to see if the client request was "GET /H" or "GET /L":
          if (currentLine.endsWith("GET /H")) { // GET /H turns the LED on
            digitalWrite(ServerLedPin, HIGH);
            relaytheH=HIGH;
            PrintOK=LOW;
            Serial.println(F("turn led on, and tell slave to do the same"));
          }
          if (currentLine.endsWith("GET /L")) { // GET /L turns the LED off
            digitalWrite(ServerLedPin, LOW);  
            relaytheL=HIGH;
            PrintOK=LOW;
            Serial.println(F("turne off, and tell slave to do the same"));
          }
        }
      }
      if (millis()-InternetStart>=TimeOutTime)
      { if (ProperDisconnect==0)
        Serial.println(F("============Timed the F out ============="));
        TimeOut=1; // Timed out, probably got disconnected but still waiting for some reason
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
  digitalWrite(WiFiPin,LOW);
}

master code that talks to the slave

void SendFreezeCode()
{ //WiFiClient client = server.available();
  if (client.connect(ArduinoThatFreezes, 80)) 
  { digitalWrite(WiFiPin,HIGH);
    Serial.println("Arduino Controllie identified"); // Make a HTTP request:
    String currentLine="";
    boolean PrintOK=1;
    if (relaytheH==HIGH)
    client.print("GET /H"); // turn led high on arduino that freezes
    else
    client.print("GET /L"); // turn led high off arduino that freezes
    client.println("HTTP/1.1");
    client.println("Connection: close");
    client.println();
    
    unsigned long InternetStart =millis();// used so that after info is sent, it waits a second to see if a responce follows.
    boolean TimeOut=0;
    boolean ProperDisconnect=0;
    int Nodelay=150;
    int TimeToWait=4000; // wait one second after info is sent to see if responce comes
    while (client.connected() || TimeOut==0)// tired to be clever about not using a delay, didn't seem to work
    { if (client.available()) // if there's bytes to read from the client,
      {             
        char c = client.read();             // read a byte, then
        //Serial.write(c);
        //Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') // you're starting a new line
        { 
            Serial.println(currentLine);
            currentLine="";     // clear string
        }
        else if (c != '\r') // if you got anything else but a carriage return character,
        {
          currentLine+=c;
          InternetStart=millis();
          ProperDisconnect=0;
        }
        if (PrintOK==1)
        {
          if (currentLine.endsWith("GET /Gh")) // confirmed The H was recieved by arudino2
          { 
            PrintOK=LOW;
            Serial.println(F("arudino 2 confirmed led was turned on"));
          }
          else if (currentLine.endsWith("GET /Gl")) // confirmed the L was recieved by arduino2
          { 
            PrintOK=LOW;
            Serial.println(F("arudino 2 confirmed led was turned off"));
          }
        }
        else if (currentLine.endsWith("EatIt")) // confirmed the L was recieved by arduino2
          { Serial.println(F("currentLine.lenght()==0"));
            InternetStart=millis()+TimeOutTime-Nodelay;
            ProperDisconnect=1;
            //break;
          }
      }
      if (millis()-InternetStart>=TimeOutTime)
      { if (ProperDisconnect==0)
        Serial.println(F("============Timed the F out ============="));
        TimeOut=1; // Timed out, probably got disconnected but still waiting for some reason
      }
    }
  }
  else 
  { // if you didn't get a connection to the server:
    Serial.println(F("No Controllie Arduino Detected"));
  }
  Serial.println(F("disconnecting from client"));
  client.stop();
  digitalWrite(WiFiPin,LOW);
}

Hi

Checking freeRAM() within the loop will not identify heap memory fragmentation leading to a crash in SendFreezeCode() and ListenForController().

Both of these procedures define String currentLine = “” and proceed to do this nasty trick “currentLine+=c”. That will cause the heap to grow and free heap memory to fragment. You need to check freeRam within these two procedure and within the loops that are doing “currentLine+=c”. You can suppress any freeRAM() reporting unless it drops below say 500 bytes.

Using “currentLine.reserve(xxx)” for the largest expected String length may avoid heap memory fragmentation and your crash. But if a browser sends you a super long string nothing will avoid a crash if you just add every character to the String using “currentLine+=c”.

A hacker’s browser can send a URL request up to 4K long and it will crash your system. My web server sets a limit of 80 bytes for a URL (indeed any line in an http request) and processes very long input lines as separate disjointed 80 byte lines.

I am a great advocate of using Strings - but continuously increasing the length of a String using “currentLine+=c” within a loop (without .reserve()) will expand heap memory, fragment heap free space and cause a crash when the heap overflows into the stack memory area.

You might find this procedure a useful way to process incoming http request lines without being trapped into reading/processing a very long String line that crashes your system.

String ExtractECLineMax(int p_length) {
  //We read html requests char by char because readStringUntil might pull out too
  //much data and cause a memory crash. An excessive long first line could be a hacker.
  //We restrict each line to p_length bytes.
  //We return content only - line feeds are ignored

  //G_HTML_Req_EOL is a global boolean varaiable that indicates
  //whether this function read a whole line or just the first p_length bytes.

  String l_req_line = "";
  l_req_line.reserve(p_length + 1);
  char l_char;
  G_HTML_Req_EOL = false;
  for (byte l_count = 0; l_count < p_length; l_count++) {
    if (G_EthernetClient.available() == 0)
      break;
    //
    l_char = G_EthernetClient.read();
    if (l_char == '\n') { //return if line feed char found
      G_HTML_Req_EOL = true;
      break;
    }
    if (l_char != '\r') //ignore carriage return chars
      l_req_line += l_char;
    //
  }

  return l_req_line;
} //ExtractECLineMax

I hope this helps.

Catweazle NZ

Checking freeRAM() within the loop will not identify heap memory fragmentation leading to a crash in SendFreezeCode() and ListenForController().

Both of these procedures define String currentLine = “” and proceed to do this nasty trick “currentLine+=c”. That will cause the heap to grow and free heap memory to fragment. You need to check freeRam within these two procedure and within the loops that are doing “currentLine+=c”. You can suppress any freeRAM() reporting unless it drops below say 500 bytes.

Yes, but the program is able to exit out of the ListenForController() loop without any problem. To my understanding, once it exits out of that loop, all memory used is returned as if it were never used. Would the fragmenation take 700,000 cycles (about 6 minutes) after the loop to cause the crash if no other call to currentLine was made afterward? If it were made, it would print Serial.println(“new server side client”); correct?

I am a great advocate of using Strings - but continuously increasing the length of a String using “currentLine+=c” within a loop (without .reserve()) will expand heap memory, fragment heap free space and cause a crash when the heap overflows into the stack memory area.

I’m having a hard time figuring out where to put the code you posted.

String ExtractECLineMax(int p_length) {

means that it can’t go in the ListenForController() loop, but if i put this outside of the loop, how would it call (G_EthernetClient.available() == 0) which I am assuming in my case is WiFiClient client = server.available(); but wouldn’t that mean to read it here would mean it would have to be sent twice?

is there a huge difference in your method and if I modified

else if (c != ‘\r’) { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}

to

else if (c != ‘\r’) { // if you got anything else but a carriage return character,
if (currentLine.lenght<80)
currentLine += c; // add it to the end of the currentLine
}

Hi

You need to debug the procedure ListenForController().

I suggest you add some more brackets into your boolean expressions such as:

while ((client.connected()) && (TimeOut==0)) 

if ((millis()-InternetStart)>=TimeOutTime)

to be certain of correct evaluation order.

To ensure the last line above is evaluated correctly and you are not getting overflows or underflows perhaps:

unsigned long elapsed = millis()-InternetStart;
Serial.println(elapsed);
elapsed -= TimeOutTime;
if (elapsed > 0)

We cannot tell what the value of TimeOutTime is and if it could possibly be set to a very large number that would cause very long loop execution. Check that.

If you minimise the use of "currentLine += c;" somehow you minimise the likelihood of a fragmented free heap SRAM crash.

Your algorithm logic is a bit hard to follow. You might be stuck in an infinite loop if your timeout function does not work.

Consider rewriting the algorithm to read the first line of the http request using my algorithm and parse it to find "GET /H" or "GET /L" as soon as you get the client object. Then generate the http response, discard any remaining input (using a simple loop) and stop the client object. If the only significant loop you have is the call to my read line procedure you are not going to get infinitely stuck in a loop because of poor coding logic.

Hope this helps.

Cheers

Catweazle NZ

To my understanding, once it exits out of that loop, all memory used is returned as if it were never used.

Only if there are no global dynamic objects in existence before the procedure is called that are updated during the procedure execution and before the procedure exits. This is why you should never use global String variables.

Updating global dynamic objects within a procedure changes the characteristics of heap memory such that heap memory created by the procedure may not necessarily be released when the procedure exits.

For example if you expand a global String within a low level procedure and if the updated String object is created on the top of the heap then the heap will remain at that size forever until such time as the global String is updated again and happens to be re-assigned a block of memory very much lower in the heap.

Catweazle NZ

You need to debug the procedure ListenForController().

I suggest you add some more brackets into your boolean expressions such as:

I debugged it to the point of tears. I first substituted the String for char and wrote the code for that, which took a few hours, but that still froze the program. I then took out the substitution code and the String code and still it froze. See below, there are no Strings used, but once I call on the server code, the arduino only run for 6 minutes until it suddenly crashes.

/*
  WiFi Web Server LED Blink

 A simple web server that lets you blink an LED via the web.
 This sketch will print the IP address of your WiFi Shield (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to turn on and off the LED on pin 9.

 If the IP address of your shield is yourAddress:
 http://yourAddress/H turns the LED on
 http://yourAddress/L turns it off

 This example is written for a network using WPA encryption. For
 WEP or WPA, change the Wifi.begin() call accordingly.

 Circuit:
 * WiFi shield attached
 * LED attached to pin 9

 created 25 Nov 2012
 by Tom Igoe
 */
#include <SPI.h>
#include <WiFi.h>

char ssid[] = "";      //  your network SSID (name)
char pass[] = "";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)
unsigned long previousMillis=0;
int interval=30000;
unsigned long PrintCount=0;
unsigned long LoopCount=0;
int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  Serial.begin(9600);      // initialize serial communication
  pinMode(9, OUTPUT);      // set the LED pin mode

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);       // don't continue
  }

  String fv = WiFi.firmwareVersion();
  if ( fv != "1.1.0" )
    Serial.println("Please upgrade the firmware");

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}


void loop()
{
  ListenForController();
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= interval) // print this every 2 seconds
  { previousMillis = currentMillis;   
    Serial.print(PrintCount);
    Serial.print(F("   "));
    Serial.print(LoopCount);
    Serial.print(F("   "));
    Serial.print(millis());
    Serial.print(F("   "));
    Serial.print(freeRam());
    Serial.print(F("   "));
    Serial.println(F("slave arduino // freezes"));
    PrintCount++;
  }
  LoopCount++;
}

void ListenForController()
{
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    //String currentLine = "";                // make a String to hold incoming data from the client
    boolean lenght=1;
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (lenght == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> turn the LED on pin 9 on
");
            client.print("Click <a href=\"/L\">here</a> turn the LED on pin 9 off
");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
           // currentLine = "";
           lenght=0;
          }
        }
        else if (c != '\r') {    // if you got anything else but a carriage return character,
        //  currentLine += c;      // add it to the end of the currentLine
        lenght=1;
        }

        // Check to see if the client request was "GET /H" or "GET /L":
     /*   if (currentLine.endsWith("GET /H")) {
          digitalWrite(9, HIGH);               // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(9, LOW);                // GET /L turns the LED off
        }*/
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}
  int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval); 
}

this is the serial print out, once it reached the 6 minute mark, it quit responding

Attempting to connect to Network named: ProbablyYours
SSID: ProbablyYours
IP Address: 192.168.0.176
signal strength (RSSI):-57 dBm
To see this page in action, open a browser to http://192.168.0.176
0   19935   30000   6846   slave arduino // freezes
new client
GET /H HTTP/1.1
Host: 192.168.0.176
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://192.168.0.176/
Connection: keep-alive

client disonnected
1   82426   60000   6846   slave arduino // freezes
2   145563   90000   6846   slave arduino // freezes
3   208691   120000   6846   slave arduino // freezes
4   271798   150000   6846   slave arduino // freezes
5   334930   180000   6846   slave arduino // freezes
6   398067   210000   6846   slave arduino // freezes
7   461199   240000   6846   slave arduino // freezes
8   524327   270000   6846   slave arduino // freezes
9   587460   300000   6846   slave arduino // freezes
10   650595   330000   6846   slave arduino // freezes