ESP32 cam keeps rebooting when PIR sensor is triggered

1

So some background - I have a esp32 cam with a PIR sensor. The esp is in deep sleep, then is waken up by a PIR sensor (GPIO 13) and the esp takes an image. What I want it to do is then monitor for another 15s for movement. If there is movement it should take another photo and so forth. After the 15s the esp should send the photos to a server (havent gotten to this part yet).

The problem is that when motion is detected after wake up the camera fails to initialise and the esp automatically reboots.

The code is

#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 brownour problems
#include "soc/rtc_cntl_reg.h"  // Disable brownour problems
#include "driver/rtc_io.h"
#include <EEPROM.h>            // read and write from flash memory
#include "DHT.h"               //Vir temperatuur sensor
#include <WiFi.h>"
#define EEPROM_SIZE 1
 
RTC_DATA_ATTR int bootCount = 0;

// 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;

void initSDCard( void ) {

  if( !SD_MMC.begin() ) { // fast 4bit mode
    if( !SD_MMC.begin( "/sdcard", true ) ) { // slow 1bit mode
  Serial.println( "SD card init failed" );
    return;
    }
  }
}

void captureImage(void){
   Serial.setDebugOutput(true);
 
  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;
  
  pinMode(4, INPUT);
  digitalWrite(4, LOW);
  rtc_gpio_hold_dis(GPIO_NUM_4);
 
  if(psramFound()){
    config.frame_size = FRAMESIZE_VGA; // 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;
  }
 
  Serial.println("Starting SD Card");
 
  delay(500);
 
  initSDCard(); //initialise SD card
   
  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
  EEPROM.begin(EEPROM_SIZE);
  pictureNumber = EEPROM.read(0) + 1;    //keeps count of picture number
 
  // Path where new picture will be saved in SD Card
  String path = "/picture" + String(pictureNumber) +".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);
  
  delay(1000);
  
  // Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);
  rtc_gpio_hold_en(GPIO_NUM_4);
  }

void setup() {

WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
Serial.begin(115200);

 captureImage();

 int start = millis();
 int endtime = start;

 while(endtime - start <= 15000){
  
  int motion = digitalRead(13);

  if(motion == HIGH){
    Serial.println("Detected");

    captureImage();

    }
    
    else{
     Serial.println("No motion Detected");
     endtime = millis();
    }
  }

   esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, HIGH); //PIR sensor will trigger wake up from deep sleep.
  
   Serial.print("going to sleep");
  
   esp_deep_sleep_start();

   
}
 

void loop() {}

So when motion is detected I get this error message:

No motion Detected

No motion Detected

Detected

[E][camera.c:1249] esp_camera_init(): Camera probe failed with error 0x103

E (5592) i2c: i2c driver install erroGuru Meditation Error: Core 1 panic'ed (IntegerDivideByZero). Exception was unhandled. Core 1 register dump: PC : 0x40089b04 PS : 0x00060031 A0 : 0x80089fb1 A1 : 0x3ffbe770
A2 : 0x3ffbe7af A3 : 0x00000000 A4 : 0x00060023 A5 : 0x3ffb8058
A6 : 0x00000000 A7 : 0x00000000 A8 : 0x3ffb843c A9 : 0x00000001
A10 : 0x00000000 A11 : 0x3ffb0060 A12 : 0x00000020 A13 : 0x80000020
A14 : 0x00000008 A15 : 0x00000001 SAR : 0x0000001c EXCCAUSE: 0x00000006
EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xfffffffe
Core 1 was running in ISR context: EPC1 : 0x40089b04 EPC2 : 0x00000000 EPC3 : 0x00000000 EPC4 : 0x4008b8f7

Backtrace: 0x40089b04:0x3ffbe770 0x40089fae:0x3ffbe7a0 0x4008754d:0x3ffbe7d0 0x400e27d9:0x3ffb1910 0x400e34ec:0x3ffb1930 0x400e39e7:0x3ffb1950 0x4000bd83:0x3ffb1970 0x4000117d:0x3ffb1990 0x400592fe:0x3ffb19b0 0x4005937a:0x3ffb19d0 0x40058bbf:0x3ffb19f0 0x400e9761:0x3ffb1a20 0x400eef4a:0x3ffb1a50 0x400ef08d:0x3ffb1d60 0x40087152:0x3ffb1d90 0x400ffb55:0x3ffb1de0 0x400f93bb:0x3ffb1e10 0x400f898a:0x3ffb1e50 0x400f9140:0x3ffb1e90 0x400d1cde:0x3ffb1ec0 0x400d1f3f:0x3ffb1f80 0x400d3ae7:0x3ffb1fb0 0x4008eb75:0x3ffb1fd0

Rebooting... ets Jun 8 2016 00:22:57

I am fairly new to programming so any suggestions on how to fix the code or achieve the goal will be greatly appreciated.

arduino
camera
esp32
asked on Stack Overflow Jul 8, 2020 by Quintus

1 Answer

0

According header file definition

  • Currently this function can only be called once and there is
  • no way to de-initialize this module.

esp_err_t esp_camera_init(const camera_config_t* config);

The problem is that camera chip is already initialized and need to be reset first after wake up. Not all of the models has RESET pin (camera module).
Solution can be ,

  • reset camera and initilize again , if RESET pin is present
  • Not go to deep sleep. Only light

Question is which delay you want to reach between motion detection and start capturing If is short delay is important, use light sleep, if not, and 1-2 sec is acceptable use solution above or Manually restart ESP with EEPROM flag written. Than after start, immediatelly you can start capturing based on the EEPROM flag

answered on Stack Overflow Dec 24, 2020 by Yuri Kovalenko

User contributions licensed under CC BY-SA 3.0