speed up client.write on WiFi shield


I am using Arduino Mega R3 and WiFi shield to upload an image from SD card to a webpage.
The htm files are also stored in SD card.
I have noticed that the image takes too long to load (about 2 minutes for 10 KB jpg image!)
How can I speed the upload speed?

This is the part of my code that upload the image:

if (StrContains(HTTP_req, "GET /logo.jpg")) {
                        webFile = SD.open("logo.jpg");
                        if (webFile) {
                            client.println("HTTP/1.1 200 OK");

if (webFile) {
                        Serial.print(webFile); Serial.println(" is available");
                        while(webFile.available()) {
                            client.write(webFile.read()); // send web page to client

I have read about client.write(buffer, length) and reading more than one byte from SD card at once, such as this post:


but didn't understand how to apply them :frowning:

I hope someone can help me since I am really a beginner!

Thanks :slight_smile:

The below is for a regular Ethernet shield, but you might try the code part that puts the image file in 64 byte packets.

//zoomkat 12/26/12
//SD server test code
//open serial monitor to see what the arduino receives
//address will look like 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(){


  // disable w5100 while setting up SD
  Serial.print("Starting SD..");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);

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; 
        //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");


          //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;

              clientBuf[clientCount] = myFile.read();

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

I can do a lot better than that with the transfer code in zoomkat's ethernet code. I use a 64 byte buffer to transfer large files. Here is a 26.5K jpg image on a Mega and wifi shield. How long does it take you to get it? It isn't as fast as I hope it will eventually be, but better than 2 minutes.

However, the wifi shield server is using only one socket at the present time, and sometimes poorly. I will post my server code in the playground when I get the bugs out of it. It isn't very stable when transferring multiple big files to the same client. :frowning: