Question

I bought an Arduino Ethernet Shield and I am having problems with a web server that utilizes files on the SD card. There is a server running on port 80 serving data but when I load it, I seem to get data like

File ·ot found

File not found

File not found

File not found

title>04 NotÁFhund<šhtml¦ File not found

File not found

File not found

<”html> * File „ot foundž/p> žtitle>404 Not F·und

or it starts downloading similar stuff to above (I'm assuming the headers got corrupt like the actual content has)

If I don't use the SD card and I just serve pre-written web pages that are written into the sketch then as long as the SD card isn't in the slot, the pages show correctly. Also, the SD library says that it cannot see "index.html" when it is existant

I am using a Transcend Micro SDHC 4GB (FAT32) and the Ethernet Shield R3 on an Arduino Uno and I have tried formatting the Micro SD card. My sketch is below.

#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(192, 168, 1, 130); // 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.html")) {
      Serial.println("ERROR - Can't find index.html!");
  }      
  Serial.print("Running on ");
  Serial.println(Ethernet.localIP());
}

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();
                    client.println("<html><head><title>404 Not Found</title></head><body><p>File not found</p></body></html>");
                    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)
}
Was it helpful?

Solution

My experience with SD Cards, is that reading from them is more stable than writing to them. So, if you have an SDHC-USB converter that can plug into your PC (and many vendor/models exists) then format and initialize the SD Card at your PC.

There is a free software package called SDFormatter V3.1 that does a good job of formatting the SDHC card with a FAT32. Do NOT trust that the disk is formatted correctly after using it several times in the SD slot.

Use 8.3 file naming conventions, and format the file as a FAT32 device. Finally, load the web pages that you want to deliver onto the root of the SD card.

Now, plug the SD card into the Arduino slot and try reading from the card. Do NOT initialize or format the card at the Arduino. Just allocate, or begin the SDHC device and read the card's root directory files.

Please post your test program. The program with Ethernet stuff does not appear to correctly "begin" the SD card. This is why you should get reading the card and printing a file to your monitor, prior to getting the Ethernet connection working (which it looks like it is, so once your file test program works you should be done!)

With a FAT32 formatted card, write a test program that will open a file and write its contents on the Serial line back to your PC. Debug this functionality before working on the wireless/ethernet connection.

OTHER TIPS

There are two issues to look for: 1) your shield does not have the fix for the 5100 chip bug, 2) the SPI bus requires that the SS (slave select) of both devices are not active (low) simultaneously, SS = digital pin 10 for the 5100 and SS = digital pin 4 for the SD (the one included in the shield).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top