Code: Select all
// Visual Micro is in vMicro>General>Tutorial Mode
//
/*
Name: ESPCAM_SDCARD.ino
Created: 1/30/2025 8:59:52 PM
Author: FRANK_XPS_9530\Frank
This program captures a JPEG image from the camera and writes it out to the SD card
In addition it converts the JPEG image to BMP888 format (3x the size + header) and
writes it out to the SD card as well.
*/
/*02/19/25 This branch modifies the program to make continuous image captures
followed by max intensity spot search. This will be used to find the extents
of the image area
*/
#include "esp_camera.h"
#include "Arduino.h"
#include "FS.h" // SD Card ESP32
#include "SD_MMC.h" // SD Card ESP32
#include "soc/soc.h" // Disable brownout problems
#include "soc/rtc_cntl_reg.h" // Disable brownout problems
#include "driver/rtc_io.h"
#include <EEPROM.h> // read and write from flash memory
#include <elapsedMillis.h>
// define the number of bytes you want to access
#define EEPROM_SIZE 1
// 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
int pictureNumber = 0;
camera_fb_t* fb = NULL;
elapsedMillis MsSinceStart;
void setup()
{
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
Serial.begin(115200);
Serial.println("Starting Setup()");
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_128X128; // 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;
}
// 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;
}
MsSinceStart = 0;
}
void loop()
{
Serial.printf("%lu: Top of loop\n", (unsigned long)MsSinceStart);
// Take Picture with Camera
fb = esp_camera_fb_get();
delay(500);//This is key to avoid an issue with the image being very dark and green. If needed adjust total delay time.
fb = esp_camera_fb_get();
Serial.printf("Image acquired: fb = %x fb_len = %d\n", fb, fb->len);
//02/01/25 per https://www.esp32.com/viewtopic.php?t=17479 can convert JPG to BMP
uint8_t* bmp_buf = NULL; //pointer to buffer of uint8_t objects
size_t bmp_buf_len = 0;
Serial.printf("%lu: Just before BMP conversion with fb = %p, bmp_buf = %p, bmp_buf_len = %d\n", (unsigned long)MsSinceStart, fb, bmp_buf, bmp_buf_len);
bool converted = frame2bmp(fb, &bmp_buf, &bmp_buf_len);
if (!converted)
{
log_e("BMP Conversion failed");
}
else
{
Serial.printf("BMP Conversion succeeded with buf = %p, bmp_buf_len = %d\n", &bmp_buf, bmp_buf_len);
}
if (!fb)
{
Serial.println("Camera capture failed");
return;
}
Serial.printf("%lu: Just before esp_camera_fb_return(fb) with fb = %p\n", (unsigned long)MsSinceStart, fb);
esp_camera_fb_return(fb);
fb = NULL;
delay(3000);
}
Code: Select all
Starting Setup()
0: Top of loop
Image acquired: fb = 3ffb9010 fb_len = 2539
536: Just before BMP conversion with fb = 0x3ffb9010, bmp_buf = 0x0, bmp_buf_len = 0
BMP Conversion succeeded with buf = 0x3ffb2248, bmp_buf_len = 49206
554: Just before esp_camera_fb_return(fb) with fb = 0x3ffb9010
3564: Top of loop
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x400d23a8 PS : 0x00060530 A0 : 0x800d6623 A1 : 0x3ffb2240
A2 : 0x00000000 A3 : 0x00000003 A4 : 0x00000001 A5 : 0x3ffc2870
A6 : 0x3ffc2938 A7 : 0x3ffc2874 A8 : 0x800d23a6 A9 : 0x3ffb2220
A10 : 0x00000000 A11 : 0x3f400154 A12 : 0x00000000 A13 : 0x3ffb9010
A14 : 0x00000000 A15 : 0x3ffc4468 SAR : 0x00000004 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000004 LBEG : 0x40087838 LEND : 0x4008784e LCOUNT : 0xffffffff
Backtrace: 0x400d23a5:0x3ffb2240 0x400d6620:0x3ffb2270 0x4008aafe:0x3ffb2290
TIA