I like the way the new code box copy and paste works. OK, I don't ![]()
/*--------------------------------------------------------------[color=#222222][/color]
Program: eth_websrv_SD[color=#222222][/color]
[color=#222222][/color]
Description: Arduino web server that serves up a basic web[color=#222222][/color]
page. The web page is stored on the SD card.[color=#222222][/color]
[color=#222222][/color]
Hardware: Arduino Uno and official Arduino Ethernet[color=#222222][/color]
shield. Should work with other Arduinos and[color=#222222][/color]
compatible Ethernet shields.[color=#222222][/color]
2Gb micro SD card formatted FAT16[color=#222222][/color]
[color=#222222][/color]
Software: Developed using Arduino 1.0.3 software[color=#222222][/color]
Should be compatible with Arduino 1.0 +[color=#222222][/color]
SD card contains web page called index.htm[color=#222222][/color]
[color=#222222][/color]
References: - WebServer example by David A. Mellis and [color=#222222][/color]
modified by Tom Igoe[color=#222222][/color]
- SD card examples by David A. Mellis and[color=#222222][/color]
Tom Igoe[color=#222222][/color]
- Ethernet library documentation:[color=#222222][/color]
http://arduino.cc/en/Reference/Ethernet[color=#222222][/color]
- SD Card library documentation:[color=#222222][/color]
http://arduino.cc/en/Reference/SD[color=#222222][/color]
[color=#222222][/color]
Date: 10 January 2013[color=#222222][/color]
[color=#222222][/color]
Author: W.A. Smith, http://startingelectronics.com[color=#222222][/color]
--------------------------------------------------------------*/[color=#222222][/color]
[color=#222222][/color]
#include <SPI.h>[color=#222222][/color]
#include <Ethernet.h>[color=#222222][/color]
#include <SD.h>[color=#222222][/color]
[color=#222222][/color]
// MAC address from Ethernet shield sticker under board[color=#222222][/color]
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };[color=#222222][/color]
IPAddress ip(10, 10, 1, 222); // IP address, may need to change depending on network[color=#222222][/color]
EthernetServer server(80); // create a server at port 80[color=#222222][/color]
[color=#222222][/color]
File webFile;[color=#222222][/color]
[color=#222222][/color]
void setup()[color=#222222][/color]
{[color=#222222][/color]
Ethernet.begin(mac, ip); // initialize Ethernet device[color=#222222][/color]
server.begin(); // start to listen for clients[color=#222222][/color]
Serial.begin(9600); // for debugging[color=#222222][/color]
[color=#222222][/color]
// initialize SD card[color=#222222][/color]
Serial.println("Initializing SD card...");[color=#222222][/color]
if (!SD.begin(4)) {[color=#222222][/color]
Serial.println("ERROR - SD card initialization failed!");[color=#222222][/color]
return; // init failed[color=#222222][/color]
}[color=#222222][/color]
Serial.println("SUCCESS - SD card initialized.");[color=#222222][/color]
// check for index.htm file[color=#222222][/color]
if (!SD.exists("index.htm")) {[color=#222222][/color]
Serial.println("ERROR - Can't find index.htm file!");[color=#222222][/color]
return; // can't find index file[color=#222222][/color]
}[color=#222222][/color]
Serial.println("SUCCESS - Found index.htm file.");[color=#222222][/color]
}[color=#222222][/color]
[color=#222222][/color]
void loop()[color=#222222][/color]
{[color=#222222][/color]
[color=#222222][/color]
String requestString = "" ;//String to store chars[color=#222222][/color]
EthernetClient client = server.available(); // try to get client[color=#222222][/color]
[color=#222222][/color]
if (client) { // got client?[color=#222222][/color]
boolean currentLineIsBlank = true;[color=#222222][/color]
while (client.connected()) {[color=#222222][/color]
if (client.available()) { // client data available to read[color=#222222][/color]
char c = client.read(); // read 1 byte (character) from client[color=#222222][/color]
// last line of client request is blank and ends with \n[color=#222222][/color]
// respond to client only after last line received[color=#222222][/color]
requestString = requestString + c;[color=#222222][/color]
if (c == '\n' && currentLineIsBlank) [color=#222222][/color]
{if (requestString.indexOf("dygraph")>0)[color=#222222][/color]
{// send a standard http response header[color=#222222][/color]
client.println("HTTP/1.1 200 OK");[color=#222222][/color]
client.println("Content-Type: application/javascript");[color=#222222][/color]
client.println("Connection: close");[color=#222222][/color]
client.println();[color=#222222][/color]
webFile = SD.open("dygraph.js"); // open web page file[color=#222222][/color]
if (webFile) {[color=#222222][/color]
while(webFile.available()) {[color=#222222][/color]
client.write(webFile.read()); // send web page to client[color=#222222][/color]
}[color=#222222][/color]
webFile.close();[color=#222222][/color]
} [color=#222222][/color]
else[color=#222222][/color]
{[color=#222222][/color]
// send a standard http response header[color=#222222][/color]
client.println("HTTP/1.1 200 OK");[color=#222222][/color]
client.println("Content-Type: text/html");[color=#222222][/color]
client.println("Connection: close");[color=#222222][/color]
client.println();[color=#222222][/color]
// send web page[color=#222222][/color]
webFile = SD.open("index.htm"); // open web page file[color=#222222][/color]
if (webFile) [color=#222222][/color]
while(webFile.available()) [color=#222222][/color]
client.write(webFile.read()); // send web page to client[color=#222222][/color]
[color=#222222][/color]
webFile.close();[color=#222222][/color]
}[color=#222222][/color]
}[color=#222222][/color]
break;[color=#222222][/color]
}[color=#222222][/color]
[color=#222222][/color]
[color=#222222][/color]
// every line of text received from the client ends with \r\n[color=#222222][/color]
if (c == '\n') {[color=#222222][/color]
// last character on line of received text[color=#222222][/color]
// starting new line with next character read[color=#222222][/color]
currentLineIsBlank = true;[color=#222222][/color]
} [color=#222222][/color]
else if (c != '\r') {[color=#222222][/color]
// a text character was received from client[color=#222222][/color]
currentLineIsBlank = false;[color=#222222][/color]
}[color=#222222][/color]
} // end if (client.available())[color=#222222][/color]
} // end while (client.connected())[color=#222222][/color]
delay(1); // give the web browser time to receive the data[color=#222222][/color]
client.stop(); // close the connection[color=#222222][/color]
} // end if (client)[color=#222222][/color]
}