Go Down

Topic: Why's SD -> Ethernet Card -> Webbrowser so slow? (Read 7207 times) previous topic - next topic

Kong

Hello guys,

Not a real problem, but could somebody explain why data transfer is so terribly slow:

I've an Arduino Ethernet card with 8GB SD card.
This SD card contains a complete html code including several jpg's and png's files.
It's all working fine: the webbrowser gives a request, arduino delivers the html page via the SD card, the webbrowser asks for the images (as put in the html file) and Arduino sent the files.
But an image of approx. 150kB takes about 20 seconds to load, what's the bottleneck around here?
(Arduino, Wiznet 5100, SD card, libraries..)

In all my simplicity :smiley-slim: I think: 8 MHz arduino, pick a byte from SD, sent it over the internet etc., 150kB shouldn't take 20 seconds?


mohamed66

hi ,
how you make arduino open webpage on sd card having pictures and open it .. i tried to do that for 2 days and failed at the end
any help please

zoomkat

The below discussion may have some useful info and examples.

http://arduino.cc/forum/index.php/topic,144675.0.html
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.


SurferTim

#4
Feb 18, 2013, 01:31 pm Last Edit: Feb 18, 2013, 04:09 pm by SurferTim Reason: 1
If you want to avoid the fate of the OP, then you need to send these files more than one byte per packet. If you are sending a big file, this will take relatively forever.
Code: [Select]
           while (myFile.available()) {
             client.write(myFile.read());
           }
           // close the file:
           myFile.close();


This sends the data in 64 byte packets, rather than one byte per packet.
Code: [Select]
 Serial.println("Writing");

 byte clientBuf[64];
 int clientCount = 0;

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

   if(clientCount > 63)
   {
     Serial.println("Packet");
     client.write(clientBuf,64);
     clientCount = 0;
   }
 }

 if(clientCount > 0) client.write(clientBuf,clientCount);


edit: If you want it to work really fast, remove or comment out the Serial.println("Packet") statement. That was just for debugging.

zoomkat

I put the below in my origional code and it reduced the upload time of a 253k jpg file from 62 sec. to 15 sec.

Code: [Select]

          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;
              }
            }
            // close the file:
            myFile.close();
          }
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

SurferTim

@zoomkat: You forgot this part. Without it, it may not send the last packet. There may have been less than 64 bytes in clientBuf.
Code: [Select]
  if(clientCount > 0) client.write(clientBuf,clientCount);

Compared to 15 seconds, 62 seconds is relatively forever.

zoomkat

Quote
Without it, it may not send the last packet. There may have been less than 64 bytes in clientBuf.


Good point, I went and added it back like below. Next project is to get all the file uploading into a single function.

Code: [Select]
          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();
          }
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

blastboot

Why did you choose 64 bytes??  What is the maximum bytes per packet that ethernet shield can send??

SurferTim


Why did you choose 64 bytes??  What is the maximum bytes per packet that ethernet shield can send??

The maximum is about 1400. However, you can't use a variable declaration like this.
Code: [Select]
byte outBuf[1400];

You don't have that much memory in an Uno. So I wanted to insure it might be able to run on an Uno, but also send more data than header. The header is normally 48 bytes, so I picked 64.

blastboot

it makes sense   :)  in my code i have
Code: [Select]
int16_t c;
          while ((c = file.read()) >= 0) {
client.print((char)c);
          }

adapting it with the information you provide, is something like
Code: [Select]
byte clientBuf[64];
            int clientCount = 0;
              clientBuf[clientCount] = file.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:
            file.close();

is this right?

SurferTim

I didn't compile it, but that looks like it!  :)

blastboot

Thanks i'll try it because i have the same speed problem with images.
One more thing... what does this line exactly do?
Code: [Select]
if(clientCount > 0) client.write(clientBuf,clientCount);

SurferTim

That sends the last packet. Unless you are really lucky, your files will not be an even multiple of 64, so there may be a few bytes remaining in that buffer when the file read is finished.


Go Up