I'm working on a monitoring project that logs some data to the SD card of the official Ethernet Shield as csv file, I want to show the logs contents on a web page that is produced by the arduino , but when I try to open the csv file for reading after the client is connected to the server , I get an error but when I try to open it on startup its working OK.
I have read that I should switch between ethernet SS PIN and SD card SS pin but I cannot manage to make it work !!
Is it possible to switch ethernet SS pin
while the client is connected and then switch it back after reading from SD card without losing the client connection??
here is my code :
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2, 230);
EthernetServer server(80);
File myFile;
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop()
{
EthernetClient client = server.available();
if (client)
{
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && current_line_is_blank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<h2>CSV Data : </h2>");
// csv file data
myFile = SD.open("datalog.csv");
if (myFile) {
Serial.println("datalog.csv:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
client.println(myFile.read());
}
myFile.close();
} else {
Serial.println("error opening datalog.csv"); // ################### I GOT THIS ERROR
}
break;
}
if (c == '\n') {
current_line_is_blank = true;
} else if (c != '\r') {
current_line_is_blank = false;
}
}
}
delay(1);
client.stop();
}
}