• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

AI Thinker file numbering

Q&A Forum › Category: ESP32 › AI Thinker file numbering
0 Vote Up Vote Down
Joao Ribeiro asked 2 years ago

Hi,
I am having a problem with my time lapse project. When the counter reaches 256 it starts over with 1 and the new image overwrites the old one. Is there a way to avoid it?
I tried

EEPROM_SIZE = 4
 

but it did not work. All the pictures are getting with the same time stamp. Do I have to add a reaal time clock?
Thanks in advance

Question Tags: File Numbering and overwritting
3 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Hi.
What project exactly are you following? Is it a project from the eBook or from the website? We have similar (but slightly different) projects in different places.
What is the code that you’re using? 
Regards,
Sara

0 Vote Up Vote Down
Joao Ribeiro answered 2 years ago

The code is a modification of your timelapse code. It does not use the4 byte assignment and when the camera reaches 256 it starts to overwrite alredy used archives.
 

// Time Lapse 5 min sem WiFi
// Camera OV5640 Escolhendo a placa AI Thinker ESP32

#include “esp_camera.h”
#include “esp_timer.h”
#include “img_converters.h”
#include “Arduino.h”
#include “fb_gfx.h”
#include “soc/soc.h” //disable brownout problems
#include “soc/rtc_cntl_reg.h”  //disable brownout problems
#include “FS.h”                // SD Card ESP32
#include “SD_MMC.h”            // SD Card ESP32
#include <EEPROM.h>            // read and write from flash memory
#include “esp_timer.h”

// define the number of bytes you want to access    
#define EEPROM_SIZE 4

#define CAMERA_MODEL_AI_THINKER

#define PICTURE_DELAY  300        /* Delay for picture taking (in seconds) */
#define uS_TO_S_FACTOR 1000000  /* Conversion factor for micro seconds to seconds */

const int ledPin = 4; //  Led no Pin 4

#if defined(CAMERA_MODEL_WROVER_KIT)
  #define PWDN_GPIO_NUM    -1
  #define RESET_GPIO_NUM   -1
  #define XCLK_GPIO_NUM    21
  #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      19
  #define Y4_GPIO_NUM      18
  #define Y3_GPIO_NUM       5
  #define Y2_GPIO_NUM       4
  #define VSYNC_GPIO_NUM   25
  #define HREF_GPIO_NUM    23
  #define PCLK_GPIO_NUM    22

#elif defined(CAMERA_MODEL_M5STACK_PSRAM)
  #define PWDN_GPIO_NUM     -1
  #define RESET_GPIO_NUM    15
  #define XCLK_GPIO_NUM     27
  #define SIOD_GPIO_NUM     25
  #define SIOC_GPIO_NUM     23
 
  #define Y9_GPIO_NUM       19
  #define Y8_GPIO_NUM       36
  #define Y7_GPIO_NUM       18
  #define Y6_GPIO_NUM       39
  #define Y5_GPIO_NUM        5
  #define Y4_GPIO_NUM       34
  #define Y3_GPIO_NUM       35
  #define Y2_GPIO_NUM       32
  #define VSYNC_GPIO_NUM    22
  #define HREF_GPIO_NUM     26
  #define PCLK_GPIO_NUM     21

#elif defined(CAMERA_MODEL_M5STACK_WITHOUT_PSRAM)
  #define PWDN_GPIO_NUM     -1
  #define RESET_GPIO_NUM    15
  #define XCLK_GPIO_NUM     27
  #define SIOD_GPIO_NUM     25
  #define SIOC_GPIO_NUM     23
 
  #define Y9_GPIO_NUM       19
  #define Y8_GPIO_NUM       36
  #define Y7_GPIO_NUM       18
  #define Y6_GPIO_NUM       39
  #define Y5_GPIO_NUM        5
  #define Y4_GPIO_NUM       34
  #define Y3_GPIO_NUM       35
  #define Y2_GPIO_NUM       17
  #define VSYNC_GPIO_NUM    22
  #define HREF_GPIO_NUM     26
  #define PCLK_GPIO_NUM     21

#elif defined(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
#else
  #error “Camera model not selected”
#endif

int pictureNumber = 0;

void setup() {
  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
 
  Serial.begin(115200);
  Serial.setDebugOutput(false);

   
  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;
 

//**************************************************************************
  // Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);  
  //************************************************************************
 

  if(psramFound()){
    config.frame_size = FRAMESIZE_QSXGA;
    config.jpeg_quality = 10;
    config.fb_count = 1;
    Serial.printf(“QSXGA – QSXGA – ALTA – QSXGA \n”);
  } else {
    config.frame_size = FRAMESIZE_XGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
    Serial.printf(“XGA – XGA – BAIXA – XGA \n”);
  }
 
  //*****************************************************************************
  // Camera init
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf(“Camera init failed with error 0x%x”, err);
    return;
  }

// ******************************************************************************
//  Inicializacao do cartao sd para liberar o LED do pino 4
  SD_MMC.begin(“/sdcard”, true);
//**********************************************************************************

  uint8_t cardType = SD_MMC.cardType();
  if(cardType == CARD_NONE){
    Serial.println(“No SD Card attached”);
    return;
  }
   
  camera_fb_t * fb = NULL;

   // Take Picture with Camera
  fb = esp_camera_fb_get();  
  if(!fb) {
    Serial.println(“Camera capture failed”);
    return;
  }
  // initialize EEPROM with predefined size As in Line 20
  EEPROM.begin(EEPROM_SIZE);
  pictureNumber = EEPROM.read(0) + 1;

//*************************************************************************************************
  // Path where new picture will be saved in SD Card
  String path = “/C#-18–” + String(pictureNumber) +”.jpg”; //=========Nome do arquivo a ser salvo========
//************************************************************************************************

  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); // Funcao passa como parameros (sddr, val). A var pictureNumber def linha 118
    EEPROM.commit();  // Funcao commit da classe EEPROM retorna booleano e só true quando tiver dados
  }
  file.close();
  esp_camera_fb_return(fb);
 
}

void loop() {
  esp_sleep_enable_timer_wakeup(300 * uS_TO_S_FACTOR); // Numero de segundos que a camera dormira
  delay(1000);  //  Milisegundos que a camera permanecera ativa
  //**************************************************************************
  // Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
  digitalWrite(4, LOW);  
  //************************************************************************
  esp_deep_sleep_start();   // Manda a camera dormir novamente
}

0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Hi.
 
Each “slot” of EEPROM can only save one-byte numbers. There are several workarounds to be able to store numbers bigger than 255. You can take a look at this : https://playground.arduino.cc/Code/EEPROMWriteAnything/
 
But, I would consider using another method to save the number permanently, like the Preferences.h library. We have a tutorial about that here: https://randomnerdtutorials.com/esp32-save-data-permanently-preferences/
 
I hope this helps.
 
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.