0

I have been experiment with the arduino uno board from few days but can't seem to make it serve web page stored in SD card of ethernet shield.

If I print the contents of the file to serial it works properly, even embedding the HTML code works fine, but client.write() just produces a never ending garbage text of ÿÿÿÿÿÿÿÿÿÿÿÿ

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 115);
EthernetServer server(80);

File webFile;

void setup()
{
    pinMode(4, OUTPUT);
    digitalWrite(4, HIGH);
    Ethernet.begin(mac, ip);
    server.begin();
    Serial.begin(9600);

// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
    Serial.println("ERROR - SD card initialization failed!");
    return;
}

Serial.println("SUCCESS - SD card initialized.");
// 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
            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");
                Serial.println("starting to open htm");
                client.println();
                // send web page
                webFile = SD.open("index.htm");        // open web page file
                if (webFile) {
                    while(webFile.available()) {
                        client.write(webFile.read()); // send web page to client
                    }
                    webFile.close();
                }
                break;
            }
            // every line of text received from the client ends with \r\n
            if (c == '\n') {
                // last character 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 connection
} // end if (client)
}

Sorry for the poor formatting, I just copied it from tutorial, please help me out.

1
  • Where are you viewing the results of client.write()? From a web browser? Have you tried opening the file before you start writing to the client, maybe in setup()? In case opening the file takes too long and is causing the process to time out? Show us the contents of "index.htm". Commented Feb 29, 2016 at 12:54

2 Answers 2

1

"ÿ" is character code 255, the erased state of a Flash/EPROM/SD. Check if your SD card format OK, 'index.htm' file is there, and your code can read it.

1
  • SD card is formated in FAT16, and I've been able to get the data of files on serial output | replacing client.write() to Serial.write() ; i.e. it seems file is accessible Commented Feb 29, 2016 at 8:16
1

Me I solve the problem by using : "/index~1.htm" I see that the file name is seen like that using the cardinfo.ino from Example

1
  • 1
    because you copied a file with 4 characters length "html" extension to file system with max 3 chars extension
    – Juraj
    Commented May 28, 2018 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.