Problems whit Ethernet Shield and SD

Hi, im testing a web server that load .htm pages from a micro sd card using a Arduino Mega and Ethernet Shield w5100, it works, but sometines take a very long time to load or the page dont load, when my wi-fi signal is low this happen more, i tried using the web server like the arduino examples, whit the html code in the arduino’s code and i dont have the problem, i would apreciate if something can help me to know what happen, i think can be a problem reading the files from the micro sd but im not sure, thanks.

I might have missed something but did you forget to post your code?

pylon: I might have missed something but did you forget to post your code?

I used the example of this tutorial to load the pages from the sd http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-web-server-image/ .

Did the IP address in that code really match your network setup?

it works, but sometines take a very long time to load or the page dont load, when my wi-fi signal is low this happen more

How does the Wifi come into that equation? Didn't you connect the accessing PC to the network by cable? We usually don't do WiFi network debugging in this forum.

The sketch you linked to uses the String class exactly the way it should never be used. You should try to eliminate the String class completely because it's implemented in a way not suitable for microcontrollers without a MMU. Additionally it suffers from a memory leak bug still contained in the current versions of the IDE. This results in unexpected behavior and crashes after a few runs of your loop routine.

Test code for faster downloading of files from the SD card to a browser.

//zoomkat 12/26/12
//SD server test code
//open serial monitor to see what the arduino receives
//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);
  //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: text/html");
          //client.println("Content-Type: image/jpeg");
          //client.println("Content-Type: image/gif");
          //client.println("Content-Type: application/x-javascript");
          //client.println("Content-Type: text");

          client.println();

          //File myFile = SD.open("boom.htm");
          File myFile = SD.open("HYPNO.JPG");
          //File myFile = SD.open("BLUEH_SL.GIF");
          //File myFile = SD.open("SERVOSLD.HTM");

          if (myFile) {

            byte clientBuf[64];
            int clientCount = 0;

            while(myFile.available())
            {
              clientBuf[clientCount] = myFile.read();
              clientCount++;

              if(clientCount > 63)
              {
                // Serial.println("Packet");
                client.write(clientBuf,64);
                clientCount = 0;
              }
            }
            //final <64 byte cleanup packet
            if(clientCount > 0) client.write(clientBuf,clientCount);            
            // close the file:
            myFile.close();
          }
          delay(1);
          //stopping client
          client.stop();
          readString="";
        }
      }
    }
  } 
}