How to open jpeg as a binary file and select x num of bytes

Hi,

I use the code below to send a string as a payload over a wireless radio on my arduino and it works ok.

Now, I have a jpeg image of 5kb saved on an SD on my arduino. I would also like to send this image as a payload. However the payload is limited to 200 bytes.

I was thinking of breaking up the jpeg into 200 byte segments and transmitting those.

These were the steps I was planning to follow.

  1. Opening the jpeg on the SD card as a binary file.

  2. Selecting the first 200 bytes.

  3. Then using the code below to send that the data. Note the code above is an example of how to send a string over the radio.

  4. Then I could try expand it to get the next 200 bytes.

I was hoping somebody may be able to help me code the procedure.

Many thanks

I am sure that I have seen this question before.

The first thing that I would suggest is that you don't use a String to hold the data but an array of bytes. This avoids the need to convert the String to an array.

Where are you stuck ?

Do you know how to open a file to read it ? Do you know how to read a byte from a file and put it into an array ? Do you know how to increment a counter ? Do you know how to detect that the value of the counter has reached a certain value and do something when it has ? Do you know how to detect that the end of a file has been reached ?

Hello Hellibob,

Thanks for you reply. I'm a pretty novice programmer.

Would you mind demonstrating how to open the jpeg and read 200 bytes into the array? That way I can try transmitting the data.

I think once I can do this I will be able to figure out how to loop and count to the end of the file myself (hopefully).

Tina

Largely taken from the SD card ReadWrite example.

Untested and very much in need to error checking at various points

NOTE : if it actually works, it reads 200 bytes and puts them into the array but does not even check that the file has been opened and does not close it but it illustrates putting each byte into the array. What you do with it then is up to you.

#include <SPI.h>
#include <SD.h>

File myFile;
byte myBytes[200];

void setup()
{
  Serial.begin(9600);
  Serial.println("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);

  if (!SD.begin(4))
  {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  //open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile)
  {
    for (int aByte = 0; aByte < 200; aByte++)
    {
      myBytes[aByte] = myFile.read();  //put the byte read from the file into the array
    }
    //at this point there is 200 bytes of data in the myBytes array
  }
}

void loop()
{
}

Hi UHeliBob,

Thanks for your reply. That worked really well.

I have a further question if that is ok. In my application the files are different sizes.

I wanted to create a loop that gets the first 200 bytes does something and then gets the next 200 bytes until the end of the file is reached.

That brings me to these 2 questions;

  1. Can arduino read the size of a file to know how many times to loop?

  2. The files are not of even size so the last iteration would not have 200 bytes extactly, how could I account for that?

Many thanks T

  1. Can arduino read the size of a file to know how many times to loop?

Yes.

  1. The files are not of even size so the last iteration would not have 200 bytes extactly, how could I account for that?

Read 200 bytes as many times as you can, then read the remainder.

Some useful stuff here