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");
}
}
//*********************************************************************
}