ESP32cam FTPserver and sensor problem

Hi everyone..
I have a problem, with the following components:
-ESP32cam ai-thinker (using camera and SD)
-CDM324 with amplifier (IF pin connected to GPIO_16)

The idea is that the CDM324 sensor sends a frequency to esp32, which can process, and according to the result, take the photo and store in the SD. And at the same time, be an FTPserver.

It would be two tasks, which cannot be done only in the loop ...
So for this, I have enabled in the "setup" instance, a task in core 0, so that it receives the frequency, processes it, takes a photo and saves it in the SD, while in the "loop", the FTPserver
The problem is that before any movement (frequency) that the sensor captures, the esp32 takes a photo, and returns any value (noise). I realize this because as soon as the amplifier LED turns on, take the picture.

The strange thing is that if I remove the ftpserver function, and the whole previous process I move it to the loop, it works, but using the deep sleep function and wake up (and I suppose it works because we tried it on the street).

Should I assign the pin as input? How do I name it? GPIO_NUM_16 or only 16? To capture the frequency, I use the pulsein, which, as I said, in Arduino and in ESP32 (task in setup or loop) works.

This is the code (which works) to read the frequency (thanks Markus Kantola) if i use only one core and won't use FTPserver:

void setup() {
  pinMode(16, INPUT); // or GPIO_NUM_16?
  ...
}
  void loop() {
  
    //read input 5 times with 10mS delay between reads
           // in worst case it takes 1.1 sec ... usually less
    a = pulseIn(16, HIGH, 50000); // or GPIO_NUM_16?
    b = pulseIn(16, LOW, 50000);
    cc = mm/(a+b);
    Serial.println (String(cc));
      delay (viive);
    a = pulseIn(16, HIGH, 50000); // wait 50000uS for pulse (0,05 sec)
    b = pulseIn(16, LOW, 50000);  // to complete (over 10Hz needed)
    dd = mm/(a+b);              // as 0.05 LOW + 0.05 HIGH = 0.1 sec
    Serial.println (String(dd));
     delay (viive);
    a = pulseIn(16, HIGH, 50000);
    b = pulseIn(16, LOW, 50000);
    ee = mm/(a+b);
    Serial.println (String(ee));
  delay (viive);
    a = pulseIn(16, HIGH, 50000);
    b = pulseIn(16, LOW, 50000);
    ff = mm/(a+b);
    Serial.println (String(ff));
  delay (viive);
    a = pulseIn(16, HIGH, 50000);
    b = pulseIn(16, LOW, 50000);
    gg = mm/(a+b);
    Serial.println (String(gg));
}

If i use both cores (which does not work properly):

TaskHandle_t Task_1;

void Task( void * parameter )
{
    for (;;){

    //read input 5 times with 10mS delay between reads
    // in worst case it takes 1.1 sec ... usually less
    a = pulseIn(16, HIGH, 50000); // or GPIO_NUM_16?
    b = pulseIn(16, LOW, 50000);
    cc = mm/(a+b);
    Serial.println (String(cc));
      delay (viive);
    a = pulseIn(16, HIGH, 50000); // wait 50000uS for pulse (0,05 sec)
    b = pulseIn(16, LOW, 50000);  // to complete (over 10Hz needed)
    dd = mm/(a+b);              // as 0.05 LOW + 0.05 HIGH = 0.1 sec
    Serial.println (String(dd));
     delay (viive);
    a = pulseIn(16, HIGH, 50000);
    b = pulseIn(16, LOW, 50000);
    ee = mm/(a+b);
    Serial.println (String(ee));
  delay (viive);
    a = pulseIn(16, HIGH, 50000);
    b = pulseIn(16, LOW, 50000);
    ff = mm/(a+b);
    Serial.println (String(ff));
  delay (viive);
    a = pulseIn(16, HIGH, 50000);
    b = pulseIn(16, LOW, 50000);
    gg = mm/(a+b);
    Serial.println (String(gg));

    ..... // frequency processing to convert to speed (ex)
    
    ex = c+0.5; // ex is a value processed with frequencies

    if (ex > 20)  { 
      camera_fb_t * fb = NULL;
  
      // Take Picture with Camera
      fb = esp_camera_fb_get();  
      if(!fb) {
        Serial.println("Camera capture failed");
      }
      // initialize EEPROM with predefined size
      EEPROM.begin(EEPROM_SIZE);
      pictureNumber = EEPROM.read(0) + 1;
    
      // Path where new picture will be saved in SD Card
      String path = "/picture" + String(pictureNumber) + "_" + String(ex) +"kmh.jpg";
    
      fs::FS &fs = SD_MMC; 
      Serial.printf("Picture file name: %s\n", path.c_str());
      
      File file = fs.open(path.c_str(), FILE_WRITE);
      if(!file){
        Serial.println("Failed to open file in writing mode");
      } 
      else {
        file.write(fb->buf, fb->len); // payload (image), payload length
        Serial.printf("Saved file to path: %s\n", path.c_str());
        EEPROM.write(0, pictureNumber);
        EEPROM.commit();
      }
      file.close();
      esp_camera_fb_return(fb); 
    
      Serial.println("\t\tEn nucleo ->" + String(xPortGetCoreID())); //to visualize which core is performing this task
      delay(1000);
    }
  }
  vTaskDelay(10);
}

void setup() {
    //all serial and setting initializations
    pinMode(16, INPUT); //or GPIO_NUM_16?
    ....
    
    xTaskCreatePinnedToCore(
    Task,
    "Task",
    10000,
    NULL,
    1,
    &Task_1,
    0);
    
    delay(500);
      
    //other initializations
    ....
}

void loop()
{

  if (WiFi.status() != WL_CONNECTED) {
    init_wifi();
    Serial.println("***** WiFi reconnect *****");
  }

  wakeup = millis();
  if (wakeup - last_wakeup > (10 * 60 * 1000) ) {       // 10 minutes
    last_wakeup = millis();

  }
  ftpSrv.handleFTP();
  delay(10);

}

And if I want to see the values of cc, dd, ee, ff, gg on the serial monitor, it only returns "inf". Clarification: To see the reading values, esp32 is connected and powered by arduino (using this method). And the cdm324 sensor, powered by 12v battery, regulated to 5v, sending frequency by "IF" to GPIO_16. I can't find another way to visualize reading values in real time.

Another query, the function of deep sleep and awakening, only activates nucleus 1? or both? Can I define which core to wake up?

Please, for a year now I am with this project and I think I am in the last stage to finish it. I would greatly appreciate any help.

Of course you know, that when using freeRTOS with a ESp32 that loop() should remain empty. As loop(), under freeRTOS, is ran with a priority of 1.

If you are using freeRTOS why use millis() as a delay when vTaskDelayUntil() is a much better alternative?

I always include : #include "sdkconfig.h"
#include "esp_system.h"

That way you can use ESP32 pinout naming such as GPIO_NUM_4

In your task you do realize that }
vTaskDelay(10); } never runs?

Instead you might want to consider

vTaskDelay(10);
} // this being the close of the for loop
vTaskDelete( NULL );
}

There are numerous errors, in your code, using freeRTOS. Perhaps the freeRTOS SPI could be of use to you: FreeRTOS API categories

And you do realize this 'delay(1000);' is a large delay when you want to run ever 10mS.

You'll also get better performance out of using log prints instead of serial prints.

Here is a link to the ESP32 API API Reference - ESP32 - — ESP-IDF Programming Guide latest documentation

You will find well written documentation on deep sleep.

And if I want to see the values of cc, dd, ee, ff, gg on the serial monitor, it only returns "inf".[/quote
You have a math error.