Error: invalid conversion

It's me again,

Since I can't get everything from scratch anymore, I've been trying to learn things with code that actually works. I'm reading and trying to understand line by line. It's been like this, slow, but it's good.

Even better with the help of some of you.

This code below receives via serial a photo taken by an esp32-cam.

I don't remember where I got the original code from. What I do know is that he sent the photo over the internet via FTP.

In my case I just want to save the photo on the SD card of the esp32 wroom receiver, in which I will write this code.

This line is not compiling as it says that
myFile.println((unsigned char*)tempImageBuffer, ImgMetaData.imSize));

Because it has arguments accepted only by FTP_upload()

I couldn't test the reception of the photo coming from esp-cam because it gets to this point and doesn't compile.

Can anyone fix it ?

I know I could save to the esp-cam's own SD card, but I want to save to the esp32 wroom's SD card. So, I'm going to see how the serial communication works, the photo transfer and the saving.

#include "SerialTransfer.h"
#include <SPI.h>
#include <mySD.h>   

#define CHIPSELECT       5
#define MOSI            23
#define MISO            19
#define CLOCK           18

File myFile;

SerialTransfer myTransfer;
struct img_meta_data{
  uint16_t counter;
  uint16_t imSize;
  uint16_t numLoops;
  uint16_t sizeLastLoop;
} ImgMetaData;
uint16_t packetCounter=1;
uint16_t bufferPointer=0;
char tempImageBuffer[32000];

// picture name
String picPrefix ="";
String pic_name;  

void setup(){

  Serial.begin(115200);                                                     
  Serial2.begin(9600, SERIAL_8N1, 17, 16);
  myTransfer.begin(Serial2);

  pinMode(CHIPSELECT, OUTPUT);

  // Inicializa SD card
  if (!SD.begin(5, 23, 19, 18)) {
   Serial.println("Falha na inicialização do SD card");
  } else {
    Serial.println("SD card OK");
    delay(100); 
    }
}

void loop(){
  if(myTransfer.available())  {
    myTransfer.rxObj(ImgMetaData, sizeof(ImgMetaData));
 
     if(ImgMetaData.counter==1){  
      copyToImageBuff(MAX_PACKET_SIZE-sizeof(ImgMetaData));
      }else{
       if(ImgMetaData.counter==packetCounter){  
        if(packetCounter<ImgMetaData.numLoops){        
          copyToImageBuff(MAX_PACKET_SIZE-sizeof(ImgMetaData));
        }else if(ImgMetaData.counter==packetCounter){
          copyToImageBuff(ImgMetaData.sizeLastLoop);     
        }
      }
    }
 
    if(packetCounter>ImgMetaData.numLoops){  
      pic_name  = picPrefix;
      pic_name += ".jpg"; //getDateTime() + ".jpg";  
      sdcard();     
      packetCounter=1;
      bufferPointer=0;
      delay(2000);
      //while(1){}
    }  
  } 
}
  
void copyToImageBuff(uint16_t dataLenght){
  for(int y=0;y<dataLenght;y++){
    tempImageBuffer[bufferPointer+y] = myTransfer.rxBuff[y+sizeof(ImgMetaData)];
  } 
  bufferPointer+=dataLenght;
  packetCounter++;   
 }
}

void sdcard(){

     myFile = SD.open(pic_name.c_str(), FILE_WRITE);
     if (myFile) {
     const char *f_name = pic_name.c_str();
     myFile.println((unsigned char*)tempImageBuffer, ImgMetaData.imSize));
     delay(100);
     myFile.close();
     }
}  
  

Please post the full error message copied from the IDE using the "Copy error message" button, using code tags when you post it

just do

myFile.write((unsigned char*)tempImageBuffer, ImgMetaData.imSize));

I have no results with these two TX and RX codes.

The first one I record on the esp cam. It should take 1 picture and send it by serial to esp32 wroom.

In turn, the esp32 wroom would save this photo on the SD CARD module.

But nothing happens.

I couldn't understand at what moment the photo is taken, I think every 5 seconds because there is a delay of 5000ms in the looping (maybe that's it).

I also tried to change the hardware serial comm(1) to comm(2) because I think serial 1 is the one I use to see the serial monitor. But it was no use.

Is there a fix ?

Code ESP-CAM

#include "esp_camera.h"
#include "Arduino.h"
#include "soc/soc.h"           // Disable brownour problems
#include "soc/rtc_cntl_reg.h"  // Disable brownour problems
#include "driver/rtc_io.h"
#include "SerialTransfer.h"

// Pin definition for CAMERA_MODEL_AI_THINKER
#define PWDN_GPIO_NUM     32
#define RESET_GPIO_NUM    -1
#define XCLK_GPIO_NUM      0
#define SIOD_GPIO_NUM     26
#define SIOC_GPIO_NUM     27

#define Y9_GPIO_NUM       35
#define Y8_GPIO_NUM       34
#define Y7_GPIO_NUM       39
#define Y6_GPIO_NUM       36
#define Y5_GPIO_NUM       21
#define Y4_GPIO_NUM       19
#define Y3_GPIO_NUM       18
#define Y2_GPIO_NUM        5
#define VSYNC_GPIO_NUM    25
#define HREF_GPIO_NUM     23
#define PCLK_GPIO_NUM     22

SerialTransfer myTransfer;
HardwareSerial Comm(1);
struct img_meta_data{
  uint16_t counter;
  uint16_t imSize;
  uint16_t numLoops;
  uint16_t sizeLastLoop;
} ImgMetaData;
const uint16_t PIXELS_PER_PACKET = MAX_PACKET_SIZE - sizeof(ImgMetaData);

void setup(){
  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
 
  Serial.begin(115200);
  Comm.begin(9600, SERIAL_8N1, 14, 15);
  myTransfer.begin(Comm);
 
  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;
 
//  if(psramFound()){
//    config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
//    config.jpeg_quality = 10;
//    config.fb_count = 2;
//  } else {
//    config.frame_size = FRAMESIZE_SVGA;
//    config.jpeg_quality = 12;
//    config.fb_count = 1;
//  }

    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
 
  // Init Camera
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK){
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }
}

void loop() {

//  // read from port 1, send to port 0:
//  if (Comm.available()) {
//    int inByte = Comm.read();
//    Serial.write(inByte);
//  }

//  // read from port 0, send to port 1:
//  if (Serial.available()) {
//    int inByte = Serial.read();
//    //Serial.write(inByte);
//    Comm.write(inByte);
//  }
  
  
  uint16_t startIndex = 0;
  camera_fb_t * fb = NULL;
  //Take Picture with Camera
  fb = esp_camera_fb_get(); 
   
  ImgMetaData.imSize       = fb->len;                             //sizeof(myFb);
  ImgMetaData.numLoops     = (fb->len / PIXELS_PER_PACKET) + 1;   //(sizeof(myFb)/PIXELS_PER_PACKET) + 1; 
  ImgMetaData.sizeLastLoop = fb->len % PIXELS_PER_PACKET;         //(sizeof(myFb)%PIXELS_PER_PACKET);

  for(ImgMetaData.counter=1; ImgMetaData.counter<=ImgMetaData.numLoops; ImgMetaData.counter++){
    myTransfer.txObj(ImgMetaData, sizeof(ImgMetaData));
    stuffPixels(fb->buf, startIndex, sizeof(ImgMetaData), PIXELS_PER_PACKET);   
    myTransfer.sendData(MAX_PACKET_SIZE);
     
    startIndex += PIXELS_PER_PACKET;    
    delay(100);  
  }
  esp_camera_fb_return(fb);   //clear camera memory
  delay(5000);
}

void stuffPixels(const uint8_t * pixelBuff, const uint16_t &bufStartIndex, const uint16_t &txStartIndex, const uint16_t &len){
  uint16_t txi = txStartIndex;
  for (uint16_t i=bufStartIndex; i<(bufStartIndex + len); i++)  {
    myTransfer.txBuff[txi] = pixelBuff[i];
    txi++;
  }
}

Code Esp32wroom

#include "SerialTransfer.h"
#include <SPI.h>
#include <mySD.h>   

#define CHIPSELECT       5
#define MOSI            23
#define MISO            19
#define CLOCK           18

File myFile;

SerialTransfer myTransfer;

struct img_meta_data{
  uint16_t counter;
  uint16_t imSize;
  uint16_t numLoops;
  uint16_t sizeLastLoop;
} ImgMetaData;
uint16_t packetCounter=1;
uint16_t bufferPointer=0;
char tempImageBuffer[32000];

// picture name
String picPrefix = "";
String pic_name;  

void setup(){

  Serial.begin(115200);                                                     
  Serial2.begin(9600, SERIAL_8N1, 17, 16);
  myTransfer.begin(Serial2);

  pinMode(CHIPSELECT, OUTPUT);

  // Inicializa SD card
  if (!SD.begin(5, 23, 19, 18)) {
   Serial.println("Falha na inicialização do SD card");
  } else {
    Serial.println("SD card OK");
    delay(100); 
    }
}

void loop(){

// read from port 1, send to port 0:
//  if (Serial2.available()) {
//    int inByte = Serial2.read();
//    Serial.write(inByte);
//  }

// // read from port 0, send to port 1:
//  if (Serial.available()) {
//    int inByte = Serial.read();
//    //Serial.write(inByte);
//    Serial2.write(inByte);
//  }


  if(myTransfer.available())  {
    myTransfer.rxObj(ImgMetaData, sizeof(ImgMetaData));
 
     if(ImgMetaData.counter==1){  
      copyToImageBuff(MAX_PACKET_SIZE-sizeof(ImgMetaData));
      }else{
       if(ImgMetaData.counter==packetCounter){  
        if(packetCounter<ImgMetaData.numLoops){        
          copyToImageBuff(MAX_PACKET_SIZE-sizeof(ImgMetaData));
        }else if(ImgMetaData.counter==packetCounter){
          copyToImageBuff(ImgMetaData.sizeLastLoop);     
        }
      }
    }
 
    if(packetCounter>ImgMetaData.numLoops){  
      pic_name  = picPrefix;  
      salvafoto();     
      packetCounter=1;
      bufferPointer=0;
      delay(2000);
    }  
  } 

}
  
void copyToImageBuff(uint16_t dataLenght){
  for(int y=0;y<dataLenght;y++){
    tempImageBuffer[bufferPointer+y] = myTransfer.rxBuff[y+sizeof(ImgMetaData)];
  } 
  bufferPointer+=dataLenght;
  packetCounter++;   

}

void salvafoto() {

     myFile = SD.open(pic_name.c_str(), FILE_WRITE);
     if (myFile) {
      const char *f_name = pic_name.c_str();
      myFile.write((unsigned char*)tempImageBuffer, ImgMetaData.imSize);
      Serial.write((unsigned char*)tempImageBuffer, ImgMetaData.imSize);
      delay(100);
      myFile.close();
     }
}  

The ultimate goal is if the esp32 wroom gave the following command to ESP-CAM:

"With this command that I send you now, take 1 photo and send it to me through the serial and I will save it here in my SD CARD module"

The command to take the picture can be any int.

That's what I'm trying to understand and do.

Someone help ?

I managed to find something. I placed this small function between the arrows to see what was happening in the myTransfer communication, and I realized that really every 5 seconds data was transiting, but encrypted. I think it's the pictures the esp32 cam is taking and sending. This maybe shows that the problem is here at the reception in the esp32 wroom.

Because it's not recording anything on the SD CARD.

Code RX (esp32 wroom):

#include "SerialTransfer.h"
#include <SPI.h>
#include <mySD.h>   

#define CHIPSELECT       5
#define MOSI            23
#define MISO            19
#define CLOCK           18

File myFile;

SerialTransfer myTransfer;

struct img_meta_data{
  uint16_t counter;
  uint16_t imSize;
  uint16_t numLoops;
  uint16_t sizeLastLoop;
} ImgMetaData;
uint16_t packetCounter=1;
uint16_t bufferPointer=0;
char tempImageBuffer[32000];

// picture name
String picPrefix = "";
String pic_name;  

void setup(){

  Serial.begin(115200);                                                     
  Serial2.begin(9600, SERIAL_8N1, 17, 16);
  myTransfer.begin(Serial2);

  pinMode(CHIPSELECT, OUTPUT);

  // Inicializa SD card
  if (!SD.begin(5, 23, 19, 18)) {
   Serial.println("Falha na inicialização do SD card");
  } else {
    Serial.println("SD card OK");
    delay(100); 
    }
}

void loop(){

if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }

  
  if(myTransfer.available())  {
    myTransfer.rxObj(ImgMetaData, sizeof(ImgMetaData));
 
     if(ImgMetaData.counter==1){  
      copyToImageBuff(MAX_PACKET_SIZE-sizeof(ImgMetaData));
      }else{
       if(ImgMetaData.counter==packetCounter){  
        if(packetCounter<ImgMetaData.numLoops){        
          copyToImageBuff(MAX_PACKET_SIZE-sizeof(ImgMetaData));
        }else if(ImgMetaData.counter==packetCounter){
          copyToImageBuff(ImgMetaData.sizeLastLoop);     
        }
      }
    }
 
    if(packetCounter>ImgMetaData.numLoops){  
      pic_name  = picPrefix;  
      salvafoto();     
      packetCounter=1;
      bufferPointer=0;
      delay(2000);
    }  
  } 

}
  
void copyToImageBuff(uint16_t dataLenght){
  for(int y=0;y<dataLenght;y++){
    tempImageBuffer[bufferPointer+y] = myTransfer.rxBuff[y+sizeof(ImgMetaData)];
  } 
  bufferPointer+=dataLenght;
  packetCounter++;   

}

void salvafoto() {

     myFile = SD.open(pic_name.c_str(), FILE_WRITE);
     if (myFile) {
      const char *f_name = pic_name.c_str();
      myFile.write((unsigned char*)tempImageBuffer, ImgMetaData.imSize);
      delay(100);
      myFile.close();
     }
}  
  

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