Hello,
I am pretty new to Arduino, I am trying to set up a simple web server reading its contents from an sd card.
The first time I connect through a browser everything works as expected, the sd file is printed and the client disconnects. The second time I try to load the page, the SD fails to read the file, however the page loads returning the error message.
Below is my code, I would appreciate it if somebody could point out what the problem is.
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
pinMode(10, OUTPUT);
}
void loop() {
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
if (!SD.begin(4)) {
Serial.println("Card failed, or not present");
client.println("Card Fail");
} else {
File dataFile = SD.open("Wake.txt");
if (dataFile) {
while (dataFile.available()) {
client.write(dataFile.read());
}
dataFile.close();
}
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
Serial.println("client disonnected");
}
}
I am using an Arduino Uno with the Ethernet Shield and a 1GB Mico SD card.