I can perfectly read the text file stored in SD card and display in serial monitor, but not client browser window(I mean firefox in my PC). The output I receive isnumber 72
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(169,254,204,102); // 169.254.204.102
EthernetServer server(80);
File myFile;
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
Serial.begin(9600);
Serial.print("Initializing SD card...");
pinMode(53, OUTPUT);
if (!SD.begin(4))
{
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("text.txt");
if (myFile)
{
Serial.println("text.txt:");
while (myFile.available())
{
Serial.write(myFile.read());
}
myFile.close();
}
else
{
Serial.println("error opening text.txt");
}
}
void loop() {
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
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"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
myFile = SD.open("text.txt");
client.print(myFile.read());
myFile.close();
client.println("
");
client.println("</html>");
myFile.close();
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
Here is my web server code. It displays file types correctly, including .txt files. It may be more than you need, but you can remove the parts you don't need. Feel free to hack away at it. http://playground.arduino.cc/Code/WebServerST
I recently took one of the WebServer applications floating around, and modified it, so it is now a library that can be used in any Arduino application. The web content (HTML, CSS, images, etc.) are kept in separate files, rather than being coded directly into the application itself. It also supports a symbol table, used by the server, to replace tokens in the HTML with "live data" provided by the application.
The advantage is you can create your HTML, CSS and images, then use the converter I included in the zip file to convert those files into c-code that gets compiled into your application. This is WAY easier than doing it HTML line-by-HTML line programmatically. You would not have to touch the WebServer code itself, just write your application, creating an instance of the library, and point it at your content.
The only "gotcha" w.r.t. to this thread, is that I wrote this for a Due, so it does not have all the FLASH write voodoo required for an AVR-based Arduino. That would be easy enough to put back in, if someone wanted to do it - just follow the example in the other WebServer applications for using F() and the program_flash utilities for storing all strings, etc.
The read() method returns an int, so that it can return an error value if there is nothing to read. By not storing the value in an intermediate variable of the right type, the write() overload for an int, not the one for a char, is invoked.
Some test code that uploads three types of files from the SD card to the browser.
//zoomkat 4-1-12
//simple button GET for servo and pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html, or use ' instead of "
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
String readString;
//////////////////////
void setup(){
Serial.begin(9600);
// disable w5100 while setting up SD
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
Serial.print("Starting SD..");
if(!SD.begin(4)) Serial.println("failed");
else Serial.println("ok");
Ethernet.begin(mac, ip, gateway, gateway, subnet);
digitalWrite(10,HIGH);
//delay(2000);
server.begin();
Serial.println("Ready");
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
///////////////
Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: image/jpeg");
client.println();
File myFile = SD.open("HYPNO.JPG");
if (myFile) {
//Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
client.write(myFile.read());
}
// close the file:
myFile.close();
}
delay(1);
//stopping client
client.stop();
readString="";
//}
}
}
}
}
}