Go Down

Topic: arduino rev3 w/ ethernet  (Read 488 times) previous topic - next topic

fabiankyle

ive using some SD card with my code but it can't find the index.htm file that i need to use with the server.. i also use the sample sketch but it has the same error in serial monitor and produce a blank page when i browse the Ip address.. what should i do with this,,

ps. the file name in the code and the file name and types of in the SD card was the same. i dont know why its now working even in a default sketch.

SurferTim

#1
Aug 22, 2015, 01:42 pm Last Edit: Aug 22, 2015, 02:14 pm by SurferTim
What sample sketch?

Use a simple index.htm doc as a test.
Code: [Select]
<HTML><BODY>
This is a test
</BODY></HTML>


edit: The reason I ask about the example sketch you are trying is most ethernet shield example sketches do not access the SD card. That requires additional code.

fabiankyle

#2
Aug 22, 2015, 02:53 pm Last Edit: Aug 22, 2015, 02:56 pm by fabiankyle
the sample sketch i use is this i just found this at this site http://startingelectronics.org/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-web-server/

#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(10, 0, 0, 20); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

File webFile;

void setup()
{
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    Serial.begin(9600);       // for debugging
   
    // 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.");
    // 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");
                    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)
}


ps. is there any possibility that the sd card is need to be formatted it is in default FAT32.. let me know if i will format it to other parameter

fabiankyle

and i also use a simple index.htm webpage

SurferTim

You might want to try a little error checking in that sketch. Send a message to the serial monitor if the sketch is unable to open the index.htm file.
Code: [Select]
                    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();
                    }
                    else Serial.println("File open fail");

Go Up