Uno with Ethernet shield will create a file but not write to it on an SD card

Hi all

I've amended a sketch based on an article at Arduino Webserver Input and Output everything works fine, however I've introduced data logging to an SD card on the shield and whilst it will create a file it won't write to it and the web browser interface freezes, if I comment out the code to write to the SD card and instead write the log to the serial monitor it all works perfectly. I've loaded another sketch onto the arduino which purely logs data to the SD card and that works without a problem so I can assume that the Uno and the Ethernet card along with the SD card works as it should.

Here is the code within the void loop() - webFile is declared before Void setup() as File as is previousMillis and interval (both as Long), Declaring two File variables/objects (is it a variable or an object?) and using a separate one for each call to the SD card doesn't work, nor does moving the section in front of the EthernetClient line below

Thanks in anticipation, be kind please as it's only my second post.

void loop()
{
    EthernetClient 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
                char c = client.read(); // read 1 byte (character) from client
                // limit the size of the stored received HTTP request
                // 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
                    client.println("HTTP/1.1 200 OK");
                    // remainder of header follows below, depending on if
                    // web page or XML page is requested
                    // Ajax request - send XML file
                    if (StrContains(HTTP_req, "ajax_inputs")) {
                        // send rest of HTTP header
                        client.println("Content-Type: text/xml");
                        client.println("Connection: keep-alive");
                        client.println();
                        SetLEDs();
                        // send XML file containing input states
                        XML_response(client);
                    }
                    else {  // web page request
                        // send rest of HTTP header
                        client.println("Content-Type: text/html");
                        client.println("Connection: keep-alive");
                        client.println();
                        // send web page
                        webFile = SD.open("index.htm");        // open web page file
                        if (webFile) {
                            while(webFile.available()) {
                                client.write(webFile.read()); // send web page to client
                            }
                            webFile.close();
                        }
                    }
                    // display received HTTP request on serial port
                    Serial.print(HTTP_req);
                    // 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)
    
      //**********************************************************************
   // check to see if it's time to save the log; that is, if the 
   // difference between the current time and the last time you saved it 
   // is bigger than the interval then
   // save the log to the SD card.
   unsigned long currentMillis = millis();
  
   if(currentMillis - previousMillis > interval) {
         // save the last time you saved the log 
         previousMillis = currentMillis;   
    
         sensors.requestTemperatures();
         // Prepare string to be saved on file
         String dataString ="";
         dataString +=sensors.getTempCByIndex(0); 
     
         webFile = SD.open("datalog.txt", FILE_WRITE);        // open file
        if (webFile) {
            while(webFile.available()) {
                webFile.println(dataString);
            }
            webFile.close();
        }
        // If the file isn't open, issue an error:
        else {
             Serial.println("error opening datalog.txt");
        } 
   }
   //*********************************************************************
}

Can you please post the whole program. I appreciate that you have described some of the missing code but there is nothing like actually seeing it and possibly running it.

From the piece of code we have now it seems you are using the same fileID. Second you are checking available() on a file opened for writing. available is only available for reading (meaning there is data ready for reading operations) not writing.

UKHeliBob:
Can you please post the whole program. I appreciate that you have described some of the missing code but there is nothing like actually seeing it and possibly running it.

Please find files attached, there's also a web page stored on the SD card which is updated via ajax using the DOM, I've attached both files for you.

I tried using the same file ID and different file ID's but both threw up the same error, whilst there's mention that you can open up two files at once there's nothing that I could find about File ID's

eth_websrv_SD_Ajax_in_out.ino (11.3 KB)

index.htm.txt (5.66 KB)

I've simplified the sketch so that the web page is served on demand and doesn't rely on AJAX and it now logs quite happily to a CSV file on the SD card, and is also accessible via a web browser so success in proof of concept, now I need to expand on the Sketch.

Thanks for the input, it's appreciated.

TempServer.ino (2.84 KB)