WebServer with SD card logging problem

I am using Arduino Uno with Ethernet shield. I have bought DHT21 sensor for logging room temperature and humidity on SD card. At the same time I want that the log file could be accessed via web browser. I have written the code below, but I am facing some problems. The program works for a minute or two, but then the web server is not responsive any more and also writing to file on SD card is not working. I am writing to SD card once a minute (for this purpose I am using Timer1). I cannot find a bug. Can somebody help me? What could be a problem?
Thank you for your help.

#include <TimerOne.h>
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include "DHT.h"
#include <Time.h> 

#define DHTPIN 5     // what pin we're connected to
#define DHTTYPE DHT21   // DHT21 AM2301

// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ   20
#define TEMP_CORRECTION  5.5
#define INT_COUNTER 7  // 7 for 1 minute

DHT dht(DHTPIN, DHTTYPE);

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,10,80);   // IP address, may need to change depending on network
EthernetServer server(80);       // create a server at port 80
File logFile;                    // handle to log file on SD card
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0;              // index into HTTP_req buffer

EthernetClient client;

int timer1_counter;
int counter = INT_COUNTER;

void setup()
{
    // disable Ethernet chip
    pinMode(10, OUTPUT);
    digitalWrite(10, HIGH);
        
    //setTime(12,0,0,1,1,11); // set time to noon Jan 1 2011   
    setTime(10,29,0,7,10,13); // set time to noon Jan 1 2011
    
    dht.begin();
    
    // initialize SD card
    if (!SD.begin(4)) {
        return;    // init failed
    }

    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    
    Timer1.initialize(8388480);  // max interval, aprox. 8s
    Timer1.attachInterrupt(timerIsr);
}


void timerIsr()
{
    counter--;
    if (counter == 0) {
      counter = INT_COUNTER;
      float h = dht.readHumidity();
      float t = dht.readTemperature() - TEMP_CORRECTION;
      logFile = SD.open("log.txt", FILE_WRITE);
      saveTimestamp();
      logFile.print("\t");
      logFile.print(t);
      logFile.print("\t");
      logFile.println(h);
      logFile.close();
    }
}

void loop()
{    
    
    client = server.available();  // try to get client
    
    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                //Timer1.detachInterrupt();
                char c = client.read(); // read 1 byte (character) from client
                // buffer first part of HTTP request in HTTP_req array (string)
                // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
                if (req_index < (REQ_BUF_SZ - 1)) {
                    HTTP_req[req_index] = c;          // save HTTP request character
                    req_index++;
                }        
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header

                    // open requested web page file
                    if (StrContains(HTTP_req, "GET / ")) {
                        //         || StrContains(HTTP_req, "GET /index.htm")) {    Ce bi imel htm zacetno stran
                                                
                        float h = dht.readHumidity();
                        float t = dht.readTemperature() - TEMP_CORRECTION;   
                        
                        client.println("HTTP/1.1 200 OK");
                        client.println("Content-Type: text/html");
                        client.println("Connection: close");
                        client.println();
                        // send web page
                        client.println("<!DOCTYPE html>");
                        client.println("<html>");
                        client.println("<head>");
                        client.println("<title>Temperature/Humidity</title>");
                        client.println("</head>");
                        client.println("<body>");
                        client.println("<p>");
                        digitalClockDisplay();
                        client.println("</p>");
                        client.print("<p>Temperature: ");
                        client.print(t);
                        client.println(" *C</p>");
                        client.print("<p>Humidity: ");
                        client.print(h);
                        client.println(" %</p>");
                        client.println("<p> Open <a href=\"log.txt\">log file</a>.</p>");
                        client.println("</body>");
                        client.println("</html>");                        
                    }
                    else if (StrContains(HTTP_req, "GET /log.txt")) {
                        logFile = SD.open("log.txt");        // open log file
                        
                        if (logFile) {
                          while(logFile.available()) {
                              client.write(logFile.read());
                          }                      
                        }
                        logFile.close();
                    }
                    
                    // reset buffer index and all buffer elements to 0
                    req_index = 0;
                    StrClear(HTTP_req, REQ_BUF_SZ);
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}

// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
    for (int i = 0; i < length; i++) {
        str[i] = 0;
    }
}

// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
    char found = 0;
    char index = 0;
    char len;

    len = strlen(str);
    
    if (strlen(sfind) > len) {
        return 0;
    }
    while (index < len) {
        if (str[index] == sfind[found]) {
            found++;
            if (strlen(sfind) == found) {
                return 1;
            }
        }
        else {
            found = 0;
        }
        index++;
    }

    return 0;
}

void digitalClockDisplay(){
  // digital clock display of the time
  client.print(hour());
  printDigits(minute());
  printDigits(second());
  client.print(" ");
  client.print(day());
  client.print(".");
  client.print(month());
  client.print(".");
  client.print(year());
  client.println();
}

void printDigits(int digits){
  // utility function for clock display: prints preceding colon and leading 0
  client.print(":");
  if(digits < 10)
    client.print('0');
  client.print(digits);
}

void saveTimestamp() {
  logFile.print(hour());
  printDigitsFile(minute());
  printDigitsFile(second());
  logFile.print("\t");
  logFile.print(day());
  logFile.print(".");
  logFile.print(month());
  logFile.print(".");
  logFile.print(year());
}

void printDigitsFile(int digits){
  // utility function for clock display: prints preceding colon and leading 0
  logFile.print(":");
  if(digits < 10)
    logFile.print('0');
  logFile.print(digits);
}

An ISR is supposed to be fast - like getting rid of the Jehovah's Witnesses that rang the door bell while you're watching the footbal game. Reading from the DHT11/DHT21 is not fast. Logging to the SD card is not fast.

Accessing the SD in the interrupt is a bad idea unless you check the status of the w5100 slave select pin (D10) first. If the w5100 slave select is LOW, the w5100 was using the SPI bus when the interrupt occurred. It would be unwise to attempt reading or writing to the SD in that situation.

Thank you for your answers. I will try to implement SuferTim's idea of checking slave select pin when interrupt occurs. Is there any other way to concurrently save humidity/temperature data to SD card and showing this data on web.

andyb1: Is there any other way to concurrently save humidity/temperature data to SD card and showing this data on web.

Use the technique demonstrated in blink without delay to read the sample values at regular intervals and log them however you want. If you want to read them back and include them in the response to the HTTP request then you can do that. None of this involves running any code in an interrupt context as far as I can see, and the fact that the Timer library does that renders it (IMO) unsuitable for use in the vast majority of sketches.

Thank you all for your help. Now it works as I expected. I am using "blink without delay" concept and I also check status of D10 pin everytime I want to use SD card.

I am using "blink without delay" concept and I also check status of D10 pin everytime I want to use SD card.

You need check D10 only when accessing the SD in the interrupt. If you have eliminated the SD access in the interrupt function, you can quit checking D10.