Hello all, thanks for looking and for any insight.
Background: I am attempting to build a datalogger using the Ethernet Shield. The hope is to use a blank SD card, initialize by creating an index file called index.htm . This file will be served up as a webpage and links will be provided to datafiles which may be downloadable as txt or csv files. I have been working from code from Starting Electronics, Instructables, and the Arduino Forum. My sketches are copy pasted with a few lines added. Citations should be at the top of each.
The current challenge is this: I have created a sketch (SD Index Maker), which opens a new file and writes to it. I have included html tags such that the file is served as a webpage index.htm. However, if I try the file as it is I receive and error message I included (Error opening index.htm) suggesting the file was in fact not written to. However, if I // out some of the IndexFile.println() lines so that there are only four html statements, then the error does not occur, and the webpage can be accessed. Why is this? Is there a way for me to create an index that contains many links and text?
here is the code:
//SD Index with links
//SD Browser Index maker
//Special thanks to the participants in [Ethernet + SD] Download zip files from SD - Storage - Arduino Forum
//for their code and ideas.
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 68); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File IndexFile;
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for debugging
Serial.println("This is SD Index Web Page with Links");
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
//Create index file
IndexFile = SD.open("index.htm", FILE_WRITE);
//if file opened okay, write to it
if (IndexFile) {
Serial.print("creating IndexFile...");
IndexFile.println("");
IndexFile.println("");
IndexFile.println(" ");
IndexFile.println(" Arduino SD Web Index");
IndexFile.println(" ");
IndexFile.println(" ");
IndexFile.println("
Arduino Data Station Datalogue Index
");IndexFile.println(" ");
IndexFile.println(" ");
IndexFile.println("
File List Variable
");IndexFile.println(" ");
IndexFile.println(" ");
IndexFile.println("
Go to page 2.
");IndexFile.println(" ");
IndexFile.println(" ");
IndexFile.println("
Go to datafile.
");IndexFile.println(" ");
IndexFile.println("");
IndexFile.close();
Serial.println("done creating index.htm.");
} else {
Serial.println("Error opening index.htm");
}
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
}
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
// last line of client request is blank and ends with \n
// respond to client only after last line received
Serial.print(c);
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// send web page
IndexFile = SD.open("index.htm"); //open index file web page
if(IndexFile) {
while (IndexFile.available()) {
client.write(IndexFile.read());
}
IndexFile.close();
Serial.println("index.htm accessed.");
}
break;
}
// every line of text recieved from the client ends with \r\n
if (c == '\n') {
//last charatcer 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 conection
} //end if (client)
}
SD_Index_Maker.ino (4.28 KB)