SD card html page load time?

Hi,
I'm working on a web server that pulls a simple html page off the SD card, about 50 lines.
It takes 1 minute - 12 seconds to load! Is that normal?

code below

 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();
     }

Is the arduino using a Wifi shield or Ethernet shield?

BTW, you should be reading bulk amount of the file instead of nibbling one byte at a time.

Here is what I use. tBuf is a 64 byte array. This gives you a 4x improvement in speed.

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

                    if(clientCount > 63) {
                      client.write((byte*)tBuf,64);
                      clientCount = 0;
                    }

                  }
                  if(clientCount > 0) {
                    client.write((byte*)tBuf,clientCount);
                  }

I'm using the wifi shield.

Thanks SurferTim, I'll give the code a try.

Oh, well, the code doesn't work. It resets the app/web server for some reason instead of loading the index.htm.
When I'm done, I guess I'll just put the html file on the arduino itself. I still have plenty of space on the mega.

SurferTim:
Here is what I use. tBuf is a 64 byte array. This gives you a 4x improvement in speed.

                  while(myFile.available()) {

tBuf[clientCount] = myFile.read();
                    clientCount++;
                    tBuf[clientCount] = 0;

if(clientCount > 63) {
                      client.write((byte*)tBuf,64);
                      clientCount = 0;
                    }

}
                  if(clientCount > 0) {
                    client.write((byte*)tBuf,clientCount);
                  }

It looks like the client.write for WIFI can't do chunks. oh well

You should post your complete code.

No, WiFi client write works differently than the rest of write. You should use write(*byte, int) instead.

liudr:
You should post your complete code.

No, WiFi client write works differently than the rest of write. You should use write(*byte, int) instead.

according to the documentation, there is no second parameter

I deleted the code but it's basically this

byte tBuf[64];
int clientCount = 0;

    File myFile = SD.open("index.htm");        // open web page file
    if (myFile) {
             while(myFile.available()) {
                    tBuf[clientCount] = myFile.read();
                    clientCount++;
                    tBuf[clientCount] = 0;

                    if(clientCount > 63) {
                      client.write((byte*)tBuf,64);
                      clientCount = 0;
                    }

            }
                  if(clientCount > 0) {
                    client.write((byte*)tBuf,clientCount);
                  }
        myFile.close();
    }

There is a write with two parameters. This is taken from WiFiClient.h.

virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buf, size_t size);

You will find the wifi shield transfer rate is much more sluggish than the ethernet shield. The wifi shield also has some nasty one to two second delays every few seconds during a large download. It is one of a few bugs in the shield's firmware.

Ah, Yeah, it's weird. The code above resets the wifi shield. It restarts the wifi init process.

mistergreen:
Ah, Yeah, it's weird. The code above resets the wifi shield. It restarts the wifi init process.

How did you know it restarts or resets?

Are you saying it restarts the Arduino again? Is it running the setup function when using that code? That could be caused by a SRAM problem. The extra 64 bytes used by that array may be enough to overflow the SRAM.

SurferTim:
Are you saying it restarts the Arduino again? Is it running the setup function when using that code? That could be caused by a SRAM problem. The extra 64 bytes used by that array may be enough to overflow the SRAM.

Yup, the Void Setup is called again. I have Serial prints that tells me. I suspected it's a memory issue but I have plenty left on the board like 5200 bytes.

Ok, I'm bumping tBuf down to 32 bytes and see.

So it is a Mega 2560 you are using? That code does not cause a restart on my Mega.

Yup, I'm on a 2560 mega clone, maybe that's the cause.

Ok, I bumped the tBuf down to 16 bytes and it's working. It takes 2-3 seconds to load now instead of a minute.
Thanks folks.

mistergreen:
Ok, I bumped the tBuf down to 16 bytes and it's working. It takes 2-3 seconds to load now instead of a minute.
Thanks folks.

Makes little sense. You are indicating you have used up 7.99KB of sram, with what? How long is your code? Why can't you post it?

It does make little sense why it's not working properly. I think it is the clone the could be the issue.
There's no reason to post hundreds line of code.

Unless you are unwilling to post the code, a few hundred lines of code is not much. I don't think blaming the clone is the right thing to do.

I agree with liudr. It is a circuit board clone, not a processor clone, at least I would hope so. You could be using more SRAM in your code than necessary with static strings, large arrays, etc.

If it is the fact you are running out of SRAM and you don't fix that, you will be running out again soon if you add anything to your code that uses SRAM.