Photo capture with esp32-cam, SD card with motion detection
In this exciting tutorial, we will show you how to build your own security camera using an ESP32-CAM and an AM312 PIR sensor to detect motion. Best of all, photos will be saved directly to an SD card!
Components and supplies
1
ESP32 CAM
Apps and platforms
1
rogerbit.
Project description
Code
source code
c
source code
1// Librerías para la cámara 2#include "esp_camera.h" 3#include "soc/soc.h" 4#include "soc/rtc_cntl_reg.h" 5#include "driver/rtc_io.h" 6// Librerías para la trajeta MicroSD 7#include "FS.h" 8#include "SD_MMC.h" 9// Librerías para me memoria EEPROM 10#include "EEPROM.h" 11// Utilice 1 byte de espacio EEPROM 12#define EEPROM_SIZE 1 13// Contador de número de imagen 14unsigned int contadorFoto = 0; 15// definiciones de pines para CAMERA MODELO AI THINKER 16#define PWDN_GPIO_NUM 32 17#define RESET_GPIO_NUM -1 18#define XCLK_GPIO_NUM 0 19#define SIOD_GPIO_NUM 26 20#define SIOC_GPIO_NUM 27 21#define Y9_GPIO_NUM 35 22#define Y8_GPIO_NUM 34 23#define Y7_GPIO_NUM 39 24#define Y6_GPIO_NUM 36 25#define Y5_GPIO_NUM 21 26#define Y4_GPIO_NUM 19 27#define Y3_GPIO_NUM 18 28#define Y2_GPIO_NUM 5 29#define VSYNC_GPIO_NUM 25 30#define HREF_GPIO_NUM 23 31#define PCLK_GPIO_NUM 22 32int pin = 4; 33void configuracionCamara() { 34 //Configurar los parámetros de la cámara 35 // Objeto para almacenar los parámetros de configuración de la cámara. 36 camera_config_t config; 37 config.ledc_channel = LEDC_CHANNEL_0; 38 config.ledc_timer = LEDC_TIMER_0; 39 config.pin_d0 = Y2_GPIO_NUM; 40 config.pin_d1 = Y3_GPIO_NUM; 41 config.pin_d2 = Y4_GPIO_NUM; 42 config.pin_d3 = Y5_GPIO_NUM; 43 config.pin_d4 = Y6_GPIO_NUM; 44 config.pin_d5 = Y7_GPIO_NUM; 45 config.pin_d6 = Y8_GPIO_NUM; 46 config.pin_d7 = Y9_GPIO_NUM; 47 config.pin_xclk = XCLK_GPIO_NUM; 48 config.pin_pclk = PCLK_GPIO_NUM; 49 config.pin_vsync = VSYNC_GPIO_NUM; 50 config.pin_href = HREF_GPIO_NUM; 51 config.pin_sscb_sda = SIOD_GPIO_NUM; 52 config.pin_sscb_scl = SIOC_GPIO_NUM; 53 config.pin_pwdn = PWDN_GPIO_NUM; 54 config.pin_reset = RESET_GPIO_NUM; 55 config.xclk_freq_hz = 20000000; 56 config.pixel_format = PIXFORMAT_JPEG; // Las opciones son YUV422, ESCALA DE GRISES, RGB565, JPEG 57// Seleccione un tamaño de fotograma más bajo si la cámara no es compatible con PSRAM 58 if (psramFound()) { 59 config.frame_size = FRAMESIZE_UXGA; // TAMAÑO DEL MARCO_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA 60 config.jpeg_quality = 10; //10-63 un número más bajo significa mayor calidad 61 config.fb_count = 2; 62 } else { 63 config.frame_size = FRAMESIZE_SVGA; 64 config.jpeg_quality = 12; 65 config.fb_count = 1; 66 } 67// Inicializar la cámara 68 esp_err_t err = esp_camera_init(&config); 69 if (err != ESP_OK) { 70 Serial.printf("El inicio de la cámara falló con el error 0x%x", err); 71 return; 72 } 73// Ajustes de calidad de la cámara. 74 sensor_t * seteoCamara = esp_camera_sensor_get(); 75 // BRILLO (-2 to 2) 76 seteoCamara->set_brightness(seteoCamara, 0); 77 // CONTRASTE (-2 to 2) 78 seteoCamara->set_contrast(seteoCamara, 0); 79 // SATURACIÓN (-2 to 2) 80 seteoCamara->set_saturation(seteoCamara, 0); 81 // EFECTOS ESPECIALES (0 - Sin efecto, 1 - Negativo, 2 - Escala de grises, 3 - Tinte rojo, 4 - Tinte verde, 5 - Tinte azul, 6 - Sepia) 82 seteoCamara->set_special_effect(seteoCamara, 0); 83 //BALANCE DE BLANCOS (0 = Desactivar, 1 = Activar) 84 seteoCamara->set_whitebal(seteoCamara, 1); 85 // GANANCIA AWB (0 = Desactivar, 1 = Activar) 86 seteoCamara->set_awb_gain(seteoCamara, 1); 87 // MODOS WB (0 - Automático, 1 - Soleado, 2 - Nublado, 3 - Oficina, 4 - Hogar) 88 seteoCamara->set_wb_mode(seteoCamara, 0); 89 // CONTROLES DE EXPOSICIÓN (0 = Desactivar, 1 = Activar) 90 seteoCamara->set_exposure_ctrl(seteoCamara, 1); 91 // AEC2 (0 = Desactivar, 1 = Activar) 92 seteoCamara->set_aec2(seteoCamara, 0); 93 // NIVELES DE AE (-2 a 2) 94 seteoCamara->set_ae_level(seteoCamara, 0); 95 // VALORES AEC (0 a 1200) 96 seteoCamara->set_aec_value(seteoCamara, 300); 97 // CONTROLES DE GANANCIA (0 = Desactivar, 1 = Activar) 98 seteoCamara->set_gain_ctrl(seteoCamara, 1); 99 // GANANCIA AGC (0 a 30) 100 seteoCamara->set_agc_gain(seteoCamara, 0); 101 // GANANCIA TECHO (0 a 6) 102 seteoCamara->set_gainceiling(seteoCamara, (gainceiling_t)0); 103 // BPC (0 = Desactivar, 1 = Activar) 104 seteoCamara->set_bpc(seteoCamara, 0); 105 // WPC (0 = Desactivar, 1 = Activar) 106 seteoCamara->set_wpc(seteoCamara, 1); 107 // GMA RAW (0 = Desactivar, 1 = Activar) 108 seteoCamara->set_raw_gma(seteoCamara, 1); 109 // LENC (0 = Desactivar, 1 = Activar) 110 seteoCamara->set_lenc(seteoCamara, 1); 111 // ESPEJO ORIZ (0 = Desactivar, 1 = Activar) 112 seteoCamara->set_hmirror(seteoCamara, 0); 113 // VERT FLIP (0 = Desactivar, 1 = Activar) 114 seteoCamara->set_vflip(seteoCamara, 0); 115 // DCW (0 = Desactivar, 1 = Activar) 116 seteoCamara->set_dcw(seteoCamara, 1); 117 // PATRÓN DE BARRA DE COLOR (0 = Desactivar, 1 = Activar) 118 seteoCamara->set_colorbar(seteoCamara, 0); 119} 120void initMicroSDCard() { 121 // Inicia la tarjeta MicroSD 122 123 Serial.println("Montaje de la tarjeta MicroSD"); 124 if (!SD_MMC.begin()) { 125 Serial.println("Error al montar la tarjeta MicroSD"); 126 return; 127 } 128 uint8_t cardType = SD_MMC.cardType(); 129 if (cardType == CARD_NONE) { 130 Serial.println("No se encontró ninguna tarjeta MicroSD"); 131 return; 132 } 133} 134void takeNewPhoto(String path) { 135 // Tomar una foto con la cámara Configurar frame buffer 136 camera_fb_t * fb = esp_camera_fb_get(); 137 if (!fb) { 138 Serial.println("Falló la captura de la cámara"); 139 return; 140 } 141 // Guardar imagen en la tarjeta microSD 142 fs::FS &fs = SD_MMC; 143 File file = fs.open(path.c_str(), FILE_WRITE); 144 if (!file) { 145 Serial.println("No se pudo abrir el archivo en modo de escritura"); 146 } 147 else { 148 file.write(fb->buf, fb->len); // payload (image), payload length 149 Serial.printf("Archivo guardado en la ruta: %s\n", path.c_str()); 150 } 151 // Cerrar el archivo 152 file.close(); 153 // Devolver el frame buffer al controlador para su reutilización 154 esp_camera_fb_return(fb); 155} 156void setup() { 157 pinMode(pin, OUTPUT); // declaramos el pin como salida ( OUTPUT ) 158 digitalWrite (pin,LOW); 159 // Desactivar el detector de caída de tensión 160 WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); 161 // Iniciar monitor serie 162 Serial.begin(115200); 163 // Inicializa la cámara 164 Serial.print("Inicializando el módulo de la cámara..."); 165 configuracionCamara(); 166 Serial.println("Camera OK!"); 167 // Inicializa la MicroSD 168 Serial.print("Inicializando el módulo de la tarjeta MicroSD... "); 169 initMicroSDCard(); 170 // inicializar EEPROM con tamaño predefinido 171 EEPROM.begin(EEPROM_SIZE); 172 contadorFoto = EEPROM.read(0) + 1; 173 // Ruta donde se guardará la nueva imagen en la tarjeta SD 174 String path = "/foto" + String(contadorFoto) + ".jpg"; 175 Serial.printf("Nombre del archivo de imagen: %s\n", path.c_str()); 176 // Tomar y guardar foto 177 takeNewPhoto(path); 178 // Actualizar el contador de números de imagen EEPROM 179 EEPROM.write(0, contadorFoto); 180 EEPROM.commit(); 181 digitalWrite (pin,LOW); 182 // Vincular Wakeup a GPIO13 en estado HIGH 183 esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, 1); 184 digitalWrite (pin,LOW); 185 Serial.println("Entrar en modo de suspensión"); 186 delay(1000); 187 // Ingrese al modo de sueño profundo 188 esp_deep_sleep_start(); 189} 190void loop() { 191}
Comments
Only logged in users can leave comments