SD card image tranfer

Dear fellow Arduino community,

I trying to read SD card image (20KB - JPEG -using SD library) and transfer through Xbee (series 2) Due to limitation on xbee, have to break to 60 bytes and send until the complete file send. I think, the image stored in ASCII character.

Code:

#include <SD.h>

File file;

void setup() {
  
  Serial.begin(115200);

  if (!SD.begin()) {
    Serial.println("begin failed");
    return;
  }
  file = SD.open("PIC00.JPG");
    

}
void loop() {
    Serial.flush();

    char buf[64];
    if(file) {
    while (file.position() < file.size())
             { 
             while (file.read(buf, sizeof(buf)) == sizeof(buf))  // read chunk of 64bytes 
             {
    
            Serial.write(buf); // Send to xbee via serial
             delay(50); 
             }
  
          }
  file.close();
    }

}

But this method, i can not see complete image transfer at Serial Write. After a while, i came to know the start of image is Y (ascii chracter) and U (end character). I can see only end start character Y can not see the proper end character.

Another method tried… but no last few KB is missing :confused:

if (myFile) {
 

    // read from the file until there's nothing else in it:
    while (myFile.available()) {

     
               Serial.write(myFile.read());
           	char buffer[32];
		int i=0;
		while(i<31 && myFile.available())
                 {
		buffer[i]=myFile.read();  
                i++; 
                delay(100);
                 }
         Serial.write(buffer);   // Send it by serial ( Xbee)
    }    
             myFile.close();
                 }

Any suggestion, welcome. :slight_smile: … last few days trying hard to make this work. Thanks a lot. :disappointed_relieved:

try this:

            Serial.write(buf,sizeof buf); // Send to xbee via serial

Without the length parameter, write expects a zero terminated string.

I trying to read SD card image (20KB - JPEG -using SD library) ... the image stored in ASCII character.

It most certainly is NOT.

                delay(100);

WTF? Why do you have a 1/10th second delay between reading each byte? A 20000 byte file is going to take more than 1/2 an hour (mostly doing NOTHING) to send.

You CAN read more than one byte at a time from the file. You CAN send more than one byte at a time to the serial port.

@Wildbill: I will try without buffer size.

@Paul: Regarding ASCII character, i saw many ASCII character when i read SD card image and see the output in Serial monitor. With SD library works well. i.e read SD card content and send over Xbee with 50 ms delay.But takes more than 15 min for 20 KB.

I agree with 100 ms too much.. the real reason: Xbee Series 2 can not transmit more than 82 bytes at a time... so, need some delay to send the image without overload the Xbee (connected to Arduino) Receiver Xbee (Co-ordinator) at another PC. It uses Python to read the incoming serial data.

Do u suggest, no delay at all? Guess, i am missing some point.

Thanks.

Cheers! chandra

I agree with 100 ms too much.. the real reason: Xbee Series 2 can not transmit more than 82 bytes at a time... so, need some delay to send the image without overload the Xbee (connected to Arduino)

Between packets. Not between characters/bytes.

Do u suggest, no delay at all? Guess, i am missing some point.

See above.

Below is some ethernet code that you might adapt to your wireless setup.

//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="";
        }
      }
    }
  } 
}