jshu7
June 25, 2025, 6:26pm
1
Hello, Im making an esp32cam with pir sensor that automatically captures when a motion is detected, but im blocked with this error
"A fatal error occurred: Packet content transfer stopped (received 8 bytes) Failed uploading: uploading error: exit status 2"
it stops right after this prompt
WARNING: Failed to communicate with the flash chip, read/write operations will fail. Try checking the chip connections or removing any other hardware connected to IOs.
Configuring flash size...
Erasing flash (this may take a while)...
It is recommended to post the entire verbose log in code tags. I don't recognize some of what you typed, use copy/paste.
Did you do the BOOT pin procedure?
Did you make sure there were no other copies of the IDE or Serial monitor running?
Sometimes I have to try a few times.
jshu7
June 25, 2025, 7:07pm
3
#define FILE_PHOTO "/data/photo.jpg"
RTC_DATA_ATTR int bootCount = 0;
// OV2640 camera module pins (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
const int PIN_TO_SENSOR = 14; // the pin that OUTPUT pin of sensor is connected
int pinStateCurrent = LOW; // current state of pin
int pinStatePrevious = LOW; // previous state of pin
boolean takeNewPhoto = true;
// Define Firebase Data objects
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig configF;
bool taskCompleted = false;
unsigned long lastMotionTime = 0; // Store the time of the last motion event
const long MOTION_DETECTION_DELAY = 5000; // 5 seconds delay before detecting motion again
// Check if photo capture was successful
bool checkPhoto(fs::FS &fs) {
File f_pic = fs.open(FILE_PHOTO);
unsigned int pic_sz = f_pic.size();
return (pic_sz > 100);
}
// Declare function prototype for capturePhotoSaveSpiffs
void capturePhotoSaveSpiffs(void); // Add this line
// Capture Photo and Save it to SPIFFS
void capturePhotoSaveSpiffs(void) {
camera_fb_t *fb = NULL; // pointer
bool ok = 0; // Boolean indicating if the picture has been taken correctly
static int counter = 0; // To keep track of photo number
char filename[13]; // To store the generated filename
// Check if there's enough space in SPIFFS before saving
if (SPIFFS.totalBytes() - SPIFFS.usedBytes() > 1024) {
// Generate filename with the photo number
if (counter >= 999) {
counter = 0; // Reset the counter after 999 photos
}
sprintf(filename, "photo%03d.jpg", counter++); // Increment counter and create a new filename
Serial.println("Taking a photo...");
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return;
}
// Photo file name
Serial.printf("Picture file name: %s\n", filename);
// Save the photo to SPIFFS
File file = SPIFFS.open("/" + String(filename), FILE_WRITE);
if (!file) {
Serial.println("Failed to open file in writing mode");
return;
} else {
file.write(fb->buf, fb->len); // payload (image), payload length
Serial.print("The picture has been saved in ");
Serial.print(filename);
Serial.print(" - Size: ");
Serial.print(file.size());
Serial.println(" bytes");
}
// Close the file
file.close();
esp_camera_fb_return(fb);
// Check if file has been correctly saved in SPIFFS
ok = checkPhoto(SPIFFS);
} else {
Serial.println("SPIFFS is full!");
return;
}
}
void initWiFi() {
WiFi.begin(ssid, password);
unsigned long startMillis = millis();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (millis() - startMillis > 30000) { // Timeout after 30 seconds
Serial.println("WiFi connection timed out!");
break;
}
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected to WiFi!");
}
}
void initSPIFFS() {
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
ESP.restart();
} else {
delay(500);
Serial.println("SPIFFS mounted successfully");
}
}
void initCamera() {
// OV2640 camera module
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_UXGA;
config.jpeg_quality = 10;
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 12;
config.fb_count = 1;
}
// Camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
ESP.restart();
}
}
void setup() {
Serial.begin(115200);
pinMode(PIN_TO_SENSOR, INPUT); // set arduino pin to input mode to read value from OUTPUT pin of sensor
initWiFi();
initSPIFFS();
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
initCamera();
// Firebase
configF.api_key = API_KEY;
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
configF.token_status_callback = tokenStatusCallback;
Firebase.begin(&configF, &auth);
Firebase.reconnectWiFi(true);
}
void loop() {
pinStatePrevious = pinStateCurrent; // store old state
pinStateCurrent = digitalRead(PIN_TO_SENSOR); // read new state
unsigned long currentMillis = millis();
// Check if motion is detected and debounce it
if (pinStatePrevious == LOW && pinStateCurrent == HIGH && (currentMillis - lastMotionTime) > MOTION_DETECTION_DELAY) {
Serial.println("Motion detected!");
takePic();
lastMotionTime = currentMillis; // Update last motion time
} else if (pinStatePrevious == HIGH && pinStateCurrent == LOW) { // motion stopped
Serial.println("Motion stopped!");
}
}
void takePic() {
if (takeNewPhoto) {
capturePhotoSaveSpiffs();
delay(2000);
if (Firebase.ready() && !taskCompleted) {
taskCompleted = true;
Serial.print("Uploading picture... ");
if (Firebase.Storage.upload(&fbdo, STORAGE_BUCKET_ID, FILE_PHOTO, mem_storage_type_flash, FILE_PHOTO, "image/jpeg")) {
Serial.printf("\nDownload URL: %s\n", fbdo.downloadURL().c_str());
} else {
Serial.println(fbdo.errorReason());
}
}
taskCompleted = false;
}
return;
}
I took a sourcee code from someone in the forums as well, but added a few tweaks (ddebounce for the motion seensor)
jshu7
June 25, 2025, 7:19pm
4
hello, i have fixed my problem, i unplugged thee PIR sensor whilee uploading but then this problem now happens
02:42:19.965 -> Picture file name: photo019.jpg
02:42:20.045 -> The picture has been saved in photo019.jpg - Size: 0 bytes
02:42:22.108 -> Uploading picture... file not found
this problem is i think focused to the firebase
Hi @jshu7 .
Great news! Thanks for taking the time to post an update with your findings.
Since it is unrelated to the original subject of this topic, please open a new forum topic for that question. This will ensure it gets the attention of the forum helpers knowledgeable about the subject of your new question.