Help to understand code: buffer = cam.readPicture (bytesToRead);

Code help request:
Hello everyone,
I'm trying to modify the classic code used for managing the shots with VC0706 that
are sent via HTTP Post, so that the shot is saved on SD and then sent.
What I wanted to do is send my target file "myFile = SD.open (fileName, FILE_READ);"
but when i have to manage the buffer i don't understand how to replace this part:
uint16_t jpglen = cam.frameLength ();
And
buffer = cam.readPicture (bytesToRead);
the code is always in error because they point to the camera which in theory is not available for the tests and therefore I must necessarily point to the file on SD put on purpose for the tests.
Thanks for your help, here is the part of the code in question:

if (client.connect(serverName, 80))     {
    Serial.println("connected");
    uint16_t jpglen = cam.frameLength();   // <<<<<<<<<<<<<<
    Serial.print("Storing ");
    Serial.print(jpglen, DEC);
    Serial.print(" byte image.");
    uint32_t len = jpglen + 177; // 177 is the content without the image data

 ....codice tagliato

    // Read all the data up to # bytes!
    byte wCount = 0; // For counting # of writes
    while (jpglen > 0) {
      // read 32 bytes at a time;
      uint8_t *buffer;
      uint8_t bytesToRead = min(32, jpglen);     // change 32 to 64 for a speedup but may not work with all setups!     
      buffer = cam.readPicture(bytesToRead);  //<<<<<<<<<<<<<<
      //Scrittura
      client.write(buffer, bytesToRead);  
      if (++wCount >= 64) { // Every 2K, give a little feedback so it doesn't appear locked up
        Serial.print('.');
        wCount = 0;
      }
      jpglen -= bytesToRead;
    }

What does in theory mean? Why wouldn’t the camera instance be available if you want to save the picture’s data?

If the data is already on the SD card, just use the SD API of your library to find the length (SD - size() - Arduino Reference) of the file and get the bytes into a buffer (SD - read() - Arduino Reference)

Thanks JML, in theory because the cam is mounted on MKR1400 board and I'm testing the code on MKR1010 but in both side the file be stored on the SD card. So now using MKR1010 i need to pick up the file from SD and test the code HTTP post.
Let me try to follow your suggestion, I hope to solve my doubt!

OK, so yes it's just a matter of accessing the file and its bytes

I'm stuck because the code is ok here:

if (client.connect(serverName, 80))     {
    Serial.println("connected");
    uint16_t jpglen = myFile.size();

but note here:

   // Read all the data up to # bytes!
    byte wCount = 0; // For counting # of writes
    while (jpglen > 0) {
      // read 32 bytes at a time;
      uint8_t *buffer;
      uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups!      
      buffer = myFile.size(bytesToRead); // <<<<<<<<<<<<<<< error here!

The message is: no matching function for call to 'SDLib::File::size(uint8_t&)'

that does not mean anything :slight_smile:
you are trying to use the size function to read bytes...

try something like this

// assuming myFile is correctly open

const byte bufferSize = 64; // or 32
uint8_t buffer[bufferSize];
uint16_t jpglen = myFile.size();

while (jpglen > 0) {
  uint8_t bytesToRead = min(bufferSize, jpglen);
  jpglen -= bytesToRead;
  myFile.read(buffer, bytesToRead);// see https://www.arduino.cc/reference/en/libraries/sd/read/
  ...
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.