ESP32cam FTPserver and sensor problem

Chuma87
Posts: 8
Joined: Sun Jan 26, 2020 6:34 pm

ESP32cam FTPserver and sensor problem

Postby Chuma87 » Fri Feb 28, 2020 12:00 pm

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_13)

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_13 or only 13? 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:

Code: Select all

void setup() {
  pinMode(13, INPUT); // or GPIO_NUM_13?
  ...
}
  void loop() {
  
    //read input 5 times with 10mS delay between reads
           // in worst case it takes 1.1 sec ... usually less
    a = pulseIn(13, HIGH, 50000); // or GPIO_NUM_13?
    b = pulseIn(13, LOW, 50000);
    cc = mm/(a+b);
    Serial.println (String(cc));
      delay (viive);
    a = pulseIn(13, HIGH, 50000); // wait 50000uS for pulse (0,05 sec)
    b = pulseIn(13, 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(13, HIGH, 50000);
    b = pulseIn(13, LOW, 50000);
    ee = mm/(a+b);
    Serial.println (String(ee));
  delay (viive);
    a = pulseIn(13, HIGH, 50000);
    b = pulseIn(13, LOW, 50000);
    ff = mm/(a+b);
    Serial.println (String(ff));
  delay (viive);
    a = pulseIn(13, HIGH, 50000);
    b = pulseIn(13, LOW, 50000);
    gg = mm/(a+b);
    Serial.println (String(gg));
}
If i use both cores (which does not work properly):

Code: Select all

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(13, HIGH, 50000); // or GPIO_NUM_13?
    b = pulseIn(13, LOW, 50000);
    cc = mm/(a+b);
    Serial.println (String(cc));
      delay (viive);
    a = pulseIn(13, HIGH, 50000); // wait 50000uS for pulse (0,05 sec)
    b = pulseIn(13, 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(13, HIGH, 50000);
    b = pulseIn(13, LOW, 50000);
    ee = mm/(a+b);
    Serial.println (String(ee));
  delay (viive);
    a = pulseIn(13, HIGH, 50000);
    b = pulseIn(13, LOW, 50000);
    ff = mm/(a+b);
    Serial.println (String(ff));
  delay (viive);
    a = pulseIn(13, HIGH, 50000);
    b = pulseIn(13, 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(13, INPUT); //or GPIO_NUM_13?
    ....
    
    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_13. 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.
Last edited by Chuma87 on Sat Feb 29, 2020 3:22 pm, edited 1 time in total.

ESP_Sprite
Posts: 9723
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32cam FTPserver and sensor problem

Postby ESP_Sprite » Sat Feb 29, 2020 9:36 am

Doe your project use psram? If so, you cannot use GPIO16.

Chuma87
Posts: 8
Joined: Sun Jan 26, 2020 6:34 pm

Re: ESP32cam FTPserver and sensor problem

Postby Chuma87 » Sat Feb 29, 2020 3:02 pm

I apologize. I don't know why I put 16, in the project I use pin 13.

Code: Select all

  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;
  }
In case I use PSRAM, can I use another pin? Would you tell me which one?

Chuma87
Posts: 8
Joined: Sun Jan 26, 2020 6:34 pm

Re: ESP32cam FTPserver and sensor problem

Postby Chuma87 » Tue Mar 03, 2020 2:19 am

No body?

Who is online

Users browsing this forum: No registered users and 77 guests