ESP32-CAM Save Picture in Firebase Storage

In this guide, you’ll learn how to take and upload a picture to Firebase Storage using the ESP32-CAM. You’ll create a Firebase project with Storage that allows you to store your files. Then, you can access your Firebase console to visualize the pictures or create a web app to display them (we’ll do this in a future tutorial). The ESP32-CAM will be programmed using Arduino IDE.

ESP32-CAM Save Picture in Firebase Storage Tutorial Guide

Updated 19 September 2023.

Note: this project is compatible with any ESP32 Camera Board with the OV2640 camera. You just need to make sure you use the right pinout for the board you’re using.

What is Firebase?

Firebase logo

Firebase is Google’s mobile application development platform that helps you build, improve, and grow your app. It has many services used to manage data from any Android, IOS, or web application like authenticationrealtime databasehosting, storage, etc.

Project Overview

This simple tutorial exemplifies how to take and send photos taken with the ESP32-CAM to Firebase Storage. The ESP32-CAM takes a picture and sends it to Firebase every time it resets (press the RST button). The idea is that you add some sort of trigger that might be useful for your projects, like a PIR motion sensor or a pushbutton, for example.

  • When the ESP32 first runs, it takes a new picture and saves it in the filesystem (LittleFS);
  • The ESP32-CAM connects to Firebase as a user with email and password;
  • The ESP32-CAM sends the picture to Firebase Storage;
  • After that, you can go to your Firebase console to view the pictures;
  • Later, you can build a web app that you can access from anywhere to display the ESP32-CAM pictures (we’ll create this in a future tutorial).

Contents

Here’s a summary of the steps you need to follow to create this project.

  1. Create a Firebase Project
  2. Set Authentication Methods
  3. Create Storage Bucket
  4. Get Project API Key
  5. ESP32-CAM Send Pictures to Firebase Storage

1) Create a Firebase Project

1) Go to Firebase and sign in using a Google Account.

2) Click Get Started and then Add project to create a new project.

3) Give a name to your project, for example: ESP Firebase Demo.

Set Up Firebase Project for ESP32 and ESP8266 Step 1

4) Disable the option Enable Google Analytics for this project as it is not needed and click Create project.

Set Up Firebase Project for ESP32 and ESP8266 Step 2

5) It will take a few seconds to set up your project. Then, click Continue when it’s ready.

6) You’ll be redirected to your Project console page.

2) Set Authentication Methods

To allow authentication with email and password, first, you need to set authentication methods for your app.

“Most apps need to know the identity of a user. In other words, it takes care of logging in and identifying the users (in this case, the ESP32-CAM). Knowing a user’s identity allows an app to securely save user data in the cloud and provide the same personalized experience across all of the user’s devices.” To learn more about the authentication methods, you can read the documentation.

1) On the left sidebar, click on Authentication and then on Get started.

Firebase Project Authentication

2) Select the Option Email/Password.

Selecting Firebase Authentication with Email/Password

3) Enable that authentication method and click Save.

Enable Email/password authentication Firebase

4) The authentication with email and password should now be enabled.

Firebase Authentication with Email/Password enabled

5) Now, you need to add a user. Still on the Authentication tab, select the Users tab at the top. Then, click on Add User.

Firebase Authentication Add New User

6) Add an email address for the authorized user. It can be your google account email or any other email. You can also create an email for this specific project. Add a password that will allow you to sign in to your app and access the storage files. Don’t forget to save the password in a safe place because you’ll need it later. When you’re done, click Add user.

Firebase Authentication Add User with Email and Password

7) A new user was successfully created and added to the Users table.

Firebase Users Table

Notice that Firebase creates a unique UID for each registered user. The user UID allows us to identify the user and keep track of the user to provide or deny access to the project or the database. There’s also a column that registers the date of the last sign-in. At the moment, it is empty because we haven’t signed in with that user yet.

3) Create Storage Bucket

1) On the left sidebar, click on Storage and then on Get started.

Firebase Get Started with Storage

2) Select Start in test mode—click Next. We’ll change the storage rules later on.

Firebase Creating Storage Bucket Set Rules

3) Select your storage location—it should be the closest to your country.

Firebase Creating Storage Bucket Set Location

4) Wait a few seconds while it creates the storage bucket.

Firebase Creating Storage Bucket

5) The storage bucket is now set up. Copy the storage bucket ID because you’ll need it later (copy only the section highlighted with a red rectangle as shown below).

Firebase Storage Bucket ID

Storage Rules

We’ll change the storage rules so that only authenticated users can upload files to the storage bucket. Select the Rules tab.

Change your database rules. Use the following rules:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth !=null;
    }
  }
}

When you’re done, click Publish.

4) Get Project API Key

To interface with your Firebase project using the ESP32-CAM, you need to get your project API key. Follow the next steps to get your project API key.

1) On the left sidebar, click on Project Settings.

Firebase Project Settings

2) Copy the Web API Key to a safe place because you’ll need it later.

Firebase Project Settings Web API Key

5) ESP32-CAM – Send Pictures to Firebase Storage

Before proceeding with the tutorial, make sure you check the following prerequisites.

Installing the ESP32 add-on

We’ll program the ESP32-CAM board using Arduino IDE. So you need the Arduino IDE installed as well as the ESP32 add-on. Follow the next tutorial to install it, if you haven’t already.

Installing ESP Firebase Client Library

The Firebase-ESP-Client library provides several examples to interface with Firebase services. It provides an example that shows how to send files to Firebase Storage. Our code we’ll be based on that example. So, you need to make sure you have that library installed.

Installation – VS Code + PlatformIO

If you’re using VS Code with the PlatformIO extension, click on the PIO Home icon and select the Libraries tab. Search for “Firebase ESP Client “. Select the Firebase Arduino Client Library for ESP8266 and ESP32.

Install Firebase ESP Client Library VS Code

Then, click Add to Project and select the project you’re working on.

Add Firebase ESP Client Library to Project VS Code

Also, change the monitor speed to 115200 by adding the following line to the platformio.ini file of your project:

monitor_speed = 115200

Installation – Arduino IDE

If you’re using Arduino IDE, follow the next steps to install the library.

  1. Go to Sketch Include Library > Manage Libraries
  2. Search for Firebase ESP Client and install the Firebase Arduino Client Library for ESP8266 and ESP32 by Mobizt.
Install Firebase Arduino Client Library for ESP8266 and ESP32 by Mobitz

Now, you’re all set to start programming the ESP32-CAM board to send pictures to Firebase Storage.

ESP32-CAM Send Pictures to Firebase – Code

Copy the following code to your Arduino IDE, or to the main.cpp file if you’re using VS Code. It takes a picture and sends it to Firebase when it first boots.

/*********
  Rui Santos
  Complete instructions at: https://RandomNerdTutorials.com/esp32-cam-save-picture-firebase-storage/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

  Based on the example provided by the ESP Firebase Client Library
*********/

#include "Arduino.h"
#include "WiFi.h"
#include "esp_camera.h"
#include "soc/soc.h"           // Disable brownout problems
#include "soc/rtc_cntl_reg.h"  // Disable brownout problems
#include "driver/rtc_io.h"
#include <LittleFS.h>
#include <FS.h>
#include <Firebase_ESP_Client.h>
//Provide the token generation process info.
#include <addons/TokenHelper.h>

//Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

// Insert Firebase project API Key
#define API_KEY "REPLACE_WITH_YOUR_FIREBASE_PROJECT_API_KEY"

// Insert Authorized Email and Corresponding Password
#define USER_EMAIL "REPLACE_WITH_THE_AUTHORIZED_USER_EMAIL"
#define USER_PASSWORD "REPLACE_WITH_THE_AUTHORIZED_USER_PASSWORD"

// Insert Firebase storage bucket ID e.g bucket-name.appspot.com
#define STORAGE_BUCKET_ID "REPLACE_WITH_YOUR_STORAGE_BUCKET_ID"
// For example:
//#define STORAGE_BUCKET_ID "esp-iot-app.appspot.com"

// Photo File Name to save in LittleFS
#define FILE_PHOTO_PATH "/photo.jpg"
#define BUCKET_PHOTO "/data/photo.jpg"

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

boolean takeNewPhoto = true;

//Define Firebase Data objects
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig configF;

void fcsUploadCallback(FCS_UploadStatusInfo info);

bool taskCompleted = false;

// Capture Photo and Save it to LittleFS
void capturePhotoSaveLittleFS( void ) {
  // Dispose first pictures because of bad quality
  camera_fb_t* fb = NULL;
  // Skip first 3 frames (increase/decrease number as needed).
  for (int i = 0; i < 4; i++) {
    fb = esp_camera_fb_get();
    esp_camera_fb_return(fb);
    fb = NULL;
  }
    
  // Take a new photo
  fb = NULL;  
  fb = esp_camera_fb_get();  
  if(!fb) {
    Serial.println("Camera capture failed");
    delay(1000);
    ESP.restart();
  }  

  // Photo file name
  Serial.printf("Picture file name: %s\n", FILE_PHOTO_PATH);
  File file = LittleFS.open(FILE_PHOTO_PATH, FILE_WRITE);

  // Insert the data in the photo file
  if (!file) {
    Serial.println("Failed to open file in writing mode");
  }
  else {
    file.write(fb->buf, fb->len); // payload (image), payload length
    Serial.print("The picture has been saved in ");
    Serial.print(FILE_PHOTO_PATH);
    Serial.print(" - Size: ");
    Serial.print(fb->len);
    Serial.println(" bytes");
  }
  // Close the file
  file.close();
  esp_camera_fb_return(fb);
}

void initWiFi(){
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
}

void initLittleFS(){
  if (!LittleFS.begin(true)) {
    Serial.println("An Error has occurred while mounting LittleFS");
    ESP.restart();
  }
  else {
    delay(500);
    Serial.println("LittleFS 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_sccb_sda = SIOD_GPIO_NUM;
  config.pin_sccb_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;
  config.grab_mode = CAMERA_GRAB_LATEST;

  if (psramFound()) {
    config.frame_size = FRAMESIZE_UXGA;
    config.jpeg_quality = 10;
    config.fb_count = 1;
  } 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 port for debugging purposes
  Serial.begin(115200);
  initWiFi();
  initLittleFS();
  // Turn-off the 'brownout detector'
  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
  initCamera();

  //Firebase
  // Assign the api key
  configF.api_key = API_KEY;
  //Assign the user sign in credentials
  auth.user.email = USER_EMAIL;
  auth.user.password = USER_PASSWORD;
  //Assign the callback function for the long running token generation task
  configF.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h

  Firebase.begin(&configF, &auth);
  Firebase.reconnectWiFi(true);
}

void loop() {
  if (takeNewPhoto) {
    capturePhotoSaveLittleFS();
    takeNewPhoto = false;
  }
  delay(1);
  if (Firebase.ready() && !taskCompleted){
    taskCompleted = true;
    Serial.print("Uploading picture... ");

    //MIME type should be valid to avoid the download problem.
    //The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h.
    if (Firebase.Storage.upload(&fbdo, STORAGE_BUCKET_ID /* Firebase Storage bucket id */, FILE_PHOTO_PATH /* path to local file */, mem_storage_type_flash /* memory storage type, mem_storage_type_flash and mem_storage_type_sd */, BUCKET_PHOTO /* path of remote file stored in the bucket */, "image/jpeg" /* mime type */,fcsUploadCallback)){
      Serial.printf("\nDownload URL: %s\n", fbdo.downloadURL().c_str());
    }
    else{
      Serial.println(fbdo.errorReason());
    }
  }
}

// The Firebase Storage upload callback function
void fcsUploadCallback(FCS_UploadStatusInfo info){
    if (info.status == firebase_fcs_upload_status_init){
        Serial.printf("Uploading file %s (%d) to %s\n", info.localFileName.c_str(), info.fileSize, info.remoteFileName.c_str());
    }
    else if (info.status == firebase_fcs_upload_status_upload)
    {
        Serial.printf("Uploaded %d%s, Elapsed time %d ms\n", (int)info.progress, "%", info.elapsedTime);
    }
    else if (info.status == firebase_fcs_upload_status_complete)
    {
        Serial.println("Upload completed\n");
        FileMetaInfo meta = fbdo.metaData();
        Serial.printf("Name: %s\n", meta.name.c_str());
        Serial.printf("Bucket: %s\n", meta.bucket.c_str());
        Serial.printf("contentType: %s\n", meta.contentType.c_str());
        Serial.printf("Size: %d\n", meta.size);
        Serial.printf("Generation: %lu\n", meta.generation);
        Serial.printf("Metageneration: %lu\n", meta.metageneration);
        Serial.printf("ETag: %s\n", meta.etag.c_str());
        Serial.printf("CRC32: %s\n", meta.crc32.c_str());
        Serial.printf("Tokens: %s\n", meta.downloadTokens.c_str());
        Serial.printf("Download URL: %s\n\n", fbdo.downloadURL().c_str());
    }
    else if (info.status == firebase_fcs_upload_status_error){
        Serial.printf("Upload failed, %s\n", info.errorMsg.c_str());
    }
}

View raw code

You need to insert your network credentials, storage bucket ID, and project API key for the project to work.

This sketch was based on a basic example provided by the library. You can find more examples here.

How the Code Works

Continue reading to learn how the code works or skip to the demonstration section.

Libraries

First, include the required libraries.

#include "WiFi.h"
#include "esp_camera.h"
#include "Arduino.h"
#include "soc/soc.h"           // Disable brownout problems
#include "soc/rtc_cntl_reg.h"  // Disable brownout problems
#include "driver/rtc_io.h"
#include <LittleFS.h>
#include <FS.h>
#include <Firebase_ESP_Client.h>
//Provide the token generation process info.
#include <addons/TokenHelper.h>

Network Credentials

Insert your network credentials in the following variables so that the ESP can connect to the internet and communicate with Firebase.

//Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

Firebase Project API Key

Insert your Firebase project API key—see this section: 4) Get Project API Key.

// Insert Firebase project API Key
#define API_KEY "REPLACE_WITH_YOUR_FIREBASE_PROJECT_API_KEY."

User Email and Password

Insert the authorized email and the corresponding password—see this section: 2) Set Authentication Methods.

#define USER_EMAIL "REPLACE_WITH_THE_AUTHORIZED_USER_EMAIL"
#define USER_PASSWORD "REPLACE_WITH_THE_AUTHORIZED_USER_PASSWORD"

Firebase Storage Bucket ID

Insert the Firebase storage bucket ID, e.g bucket-name.appspot.com. In my case, it is esp-firebase-demo.appspot.com. (remove any slashes “/” at the end or at the beginning of the bucket ID, otherwise, it will not work).

define STORAGE_BUCKET_ID "REPLACE_WITH_YOUR_STORAGE_BUCKET_ID"

Picture Path

The FILE_PHOTO_PATH variable defines the LittleFS path where the picture will be saved. It will be saved with the name photo.jpg.

#define FILE_PHOTO "/photo.jpg"

We also have a variable to hold the path where the picture will be saved on the Storage Bucket on Firebase.

#define BUCKET_PHOTO "/data/photo.jpg"

ESP32-CAM Pin Definition

The following lines define the ESP32-CAM pins. This is the definition for the ESP32-CAM AI-Thinker module. If you’re using another ESP32-CAM module, you need to modify the pin definition—check this tutorial: ESP32-CAM Camera Boards: Pin and GPIOs Assignment Guide.

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

Other Variables

The takeNewPhoto variable checks if it is time to take a new photo. We’ll set it to true, so that it takes a picture when the board first runs.

boolean takeNewPhoto = true;

Then, we define Firebase configuration data objects.

//Define Firebase Data objects
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig configF;

The taskCompleted is a boolean variable that checks if we successfully connected to Firebase.

bool taskCompleted = false;

capturePhotoSaveLittleFS() Function

The capturePhotoSaveLittleFS() function takes a photo and saves it in the ESP32 filesystem.

// Capture Photo and Save it to LittleFS
void capturePhotoSaveLittleFS( void ) {
  // Dispose first pictures because of bad quality
  camera_fb_t* fb = NULL;
  // Skip first 3 frames (increase/decrease number as needed).
  for (int i = 0; i < 4; i++) {
    fb = esp_camera_fb_get();
    esp_camera_fb_return(fb);
    fb = NULL;
  }
    
  // Take a new photo
  fb = NULL;  
  fb = esp_camera_fb_get();  
  if(!fb) {
    Serial.println("Camera capture failed");
    delay(1000);
    ESP.restart();
  }  

  // Photo file name
  Serial.printf("Picture file name: %s\n", FILE_PHOTO_PATH);
  File file = LittleFS.open(FILE_PHOTO_PATH, FILE_WRITE);

  // Insert the data in the photo file
  if (!file) {
    Serial.println("Failed to open file in writing mode");
  }
  else {
    file.write(fb->buf, fb->len); // payload (image), payload length
    Serial.print("The picture has been saved in ");
    Serial.print(FILE_PHOTO_PATH);
    Serial.print(" - Size: ");
    Serial.print(fb->len);
    Serial.println(" bytes");
  }
  // Close the file
  file.close();
  esp_camera_fb_return(fb);
}

initWiFi() Function

The initWiFi() function initializes Wi-Fi.

void initWiFi(){
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
}

initLittleFS() Function

The initLittleFS() function initializes the LittleFS filesystem.

void initLittleFS(){
  if (!LittleFS.begin(true)) {
    Serial.println("An Error has occurred while mounting LittleFS");
    ESP.restart();
  }
  else {
    delay(500);
    Serial.println("LittleFS mounted successfully");
  }
}

initCamera() Function

The initCamera() function initializes the ESP32-CAM.

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();
  } 
}

setup()

In the setup(), initialize the Serial Monitor, Wi-Fi, LittleFS, and the camera.

// Serial port for debugging purposes
Serial.begin(115200);
initWiFi();
initLittleFS();
// Turn-off the 'brownout detector'
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
initCamera();

Then, assign the following settings to the Firebase configuration objects.

// Assign the api key
configF.api_key = API_KEY;
//Assign the user sign in credentials
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
//Assign the callback function for the long running token generation task
configF.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h

Finally, initialize Firebase.

Firebase.begin(&configF, &auth);
Firebase.reconnectWiFi(true);

loop()

In the loop(), take a new picture and save it to the filesystem.

if (takeNewPhoto) {
  capturePhotoSaveLittleFS();
  takeNewPhoto = false;
}

Finally, send the picture to Firebase.

if (Firebase.ready() && !taskCompleted){
    taskCompleted = true;
    Serial.print("Uploading picture... ");

    //MIME type should be valid to avoid the download problem.
    //The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h.
    if (Firebase.Storage.upload(&fbdo, STORAGE_BUCKET_ID /* Firebase Storage bucket id */, FILE_PHOTO_PATH /* path to local file */, mem_storage_type_flash /* memory storage type, mem_storage_type_flash and mem_storage_type_sd */, BUCKET_PHOTO /* path of remote file stored in the bucket */, "image/jpeg" /* mime type */,fcsUploadCallback)){
      Serial.printf("\nDownload URL: %s\n", fbdo.downloadURL().c_str());
    }
    else{
      Serial.println(fbdo.errorReason());
    }
  }

The command that actually sends the picture is Firebase.Storage.upload():

Firebase.Storage.upload(&fbdo, STORAGE_BUCKET_ID, FILE_PHOTO, mem_storage_type_flash, FILE_PHOTO, "image/jpeg", fcsUploadCallback)

This function returns a boolean variable indicating the success of the operation.

It accepts as the second argument, the storage bucket ID. Then, the path where the file is saved; the storage type (it can be LittleFS or SD Card); the path where the file will be saved in the Firebase storage; the mime type, and a callback function (fcsUploadCallback) (this callback function simply inform us of the uploading process state).

// The Firebase Storage upload callback function
void fcsUploadCallback(FCS_UploadStatusInfo info){
    if (info.status == firebase_fcs_upload_status_init){
        Serial.printf("Uploading file %s (%d) to %s\n", info.localFileName.c_str(), info.fileSize, info.remoteFileName.c_str());
    }
    else if (info.status == firebase_fcs_upload_status_upload)
    {
        Serial.printf("Uploaded %d%s, Elapsed time %d ms\n", (int)info.progress, "%", info.elapsedTime);
    }
    else if (info.status == firebase_fcs_upload_status_complete)
    {
        Serial.println("Upload completed\n");
        FileMetaInfo meta = fbdo.metaData();
        Serial.printf("Name: %s\n", meta.name.c_str());
        Serial.printf("Bucket: %s\n", meta.bucket.c_str());
        Serial.printf("contentType: %s\n", meta.contentType.c_str());
        Serial.printf("Size: %d\n", meta.size);
        Serial.printf("Generation: %lu\n", meta.generation);
        Serial.printf("Metageneration: %lu\n", meta.metageneration);
        Serial.printf("ETag: %s\n", meta.etag.c_str());
        Serial.printf("CRC32: %s\n", meta.crc32.c_str());
        Serial.printf("Tokens: %s\n", meta.downloadTokens.c_str());
        Serial.printf("Download URL: %s\n\n", fbdo.downloadURL().c_str());
    }
    else if (info.status == firebase_fcs_upload_status_error){
        Serial.printf("Upload failed, %s\n", info.errorMsg.c_str());
    }
}

Demonstration

After inserting the required credentials, upload the code to your ESP32-CAM. If you don’t know how to upload code to the ESP32-CAM, you can follow the next tutorial(s):

After uploading the code, open the Serial Monitor at a baud rate of 115200. Press the ESP32-CAM on-board RST button.

It will log in to Firebase, take a picture, and save it to LittleFS. Afterwards, it will upload the picture to Firebase Storage.

ESP32-CAM Upload Picture to Firebase Serial Monitor
ESP32-CAM Upload Picture to Firebase Storage

Now, go to your Firebase console, and select the Storage tab. There should be a folder called data that contains your picture.

Folder Created in Firebase Storage

You can check some metadata about the picture and view it in full size. You can also access the image by accessing the Download URL printed on the Serial Monitor.

Picture Uploaded to Firebase Storage Metada

Wrapping Up

In this tutorial, you learned how to create a Firebase project with Storage. Firebase Storage allows you to store files in the cloud. Then, you can access those files by going to the Firebase console, or you can build a web app to display those files (check this tutorial: ESP32-CAM: Display Pictures in Firebase Web App).

We’ve shown a simple example of sending a picture taken with the ESP32-CAM to the Firebase Storage. The example is as simple as possible so that you can understand the basics. The idea is to modify the project to make something useful—like taking a picture and uploading it to Firebase storage when motion is detected, when a door opens, or when you press a button.

We have other Firebase tutorials that you may like:

Learn more about the ESP32-CAM with our resources:

Learn how to create a Firebase Web App to control outputs and monitor sensors from anywhere:

We hope you found this tutorial useful.

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

221 thoughts on “ESP32-CAM Save Picture in Firebase Storage”

  1. Hi Sara
    Excellent project, and thanks for sharing.
    I did all the steps, and on the Serial Monitor I get the following message:

    SPIFFS mounted successfully
    Token info: type = id token, status = on request
    Token info: type = id token, status = ready
    Taking a photo…
    Picture file name: /data/photo.jpg
    The picture has been saved in /data/photo.jpg – Size: 164736 bytes
    Uploading picture… Invalid HTTP method/URL pair.

    Does not create a data library, and of course there is no image.
    Where I was wrong? there is a solution?

    Thanks in advance.

    Reply
          • So, is there a solution?
            Why am I receiving this message?

            Uploading picture… Invalid HTTP method/URL pair.

            Why ” Invalid HTTP method/URL pair “?
            And no data directories appear in Firebase?

          • Can you share the line of code where you insert your bucket URL?
            Just to check it out.

            I’ll delete it right after.

          • Do you mean this line?

            // Insert Firebase storage bucket ID e.g bucket-name.appspot.com
            #define STORAGE_BUCKET_ID “gs://XXXXXXXXX.com”

          • It’s not correct.
            You should not copy the gs:// part as explained in the tutorial.
            Let me know that if it works after removing the gs://
            Regards
            Sara

          • I thought I solved the problem, by deleting the beginning of gs://
            But now, get a message that I do not have permission.

            Taking a photo…
            Picture file name: /data/photo.jpg
            The picture has been saved in /data/photo.jpg – Size: 0 bytes
            Taking a photo…
            Picture file name: /data/photo.jpg
            The picture has been saved in /data/photo.jpg – Size: 109440 bytes
            Uploading picture… Permission denied.

            Where did I go wrong now?

      • i am getting the same issue.. invalid HTTP method/URL method.. what is the solution kindly guide me please.. what i use now?

        Reply
      • my code is working now.. kindly tell me how to send multiple pictures in firebase. the code only send one picture and then overlap other picture on it.

        Reply
        • Hi.
          You just need to change the name of the pictures so that they don’t overlap.
          You can add a counter to each picture name, or add data and time to the picture name.
          Regards,
          Sara

          Reply
      • i’m getting error of file not found.

        WiFi connected
        SPIFFS mounted successfully
        Token info: type = id token (GITKit token), status = on request
        Token info: type = id token (GITKit token), status = ready
        Taking a photo…
        Picture file name: /data/photo.jpg
        The picture has been saved in /data/photo.jpg – Size: 53248 bytes
        Uploading picture… File not found.

        Pls help

        Reply
        • Hi.
          Downgrade the ESP Firebase Client Library to version 4.0.3. I found that version 4.3.0 works fine while most recent versions give that error.
          Revert to version 4.3.0 and it should work fine. I just tested it and it works.
          Regards,
          Sara

          Reply
  2. For me there is a compilation error:
    Arduino\libraries\ESPAsyncWebServer-master\src\AsyncEventSource.h:25:22: fatal error: AsyncTCP.h: No such file or directory

    Why? Whats wrong?

    Reply
  3. I got the error in Arduino 1.8.19

    From an older PC I found StringArray.h and AsyncTCP.h and copied thoes two libreries ower to the new PC and it compiled.

    Taking a photo…
    Picture file name: /data/photo.jpg
    The picture has been saved in /data/photo.jpg – Size: 77696 bytes
    Uploading picture… Permission denied.

    Now I need to find out how to fix this.

    Reply
    • Make sure you’ve added a user in the authentication step and that it is the same user you added in the code.
      Regards.
      Sara

      Reply
    • Hi Svein,

      I see you solved the problem with the message,
      “Permission denied”
      I would be very grateful if you would share, how you solved the problem.

      Thanks in advance.

      Reply
  4. Taking a photo…
    Picture file name: /data/photo.jpg
    The picture has been saved in /data/photo.jpg – Size: 132736 bytes
    Uploading picture…
    Download URL: https://firebasestorage.googleapis.com/v0/b/esp-firebase-demo-da1c8.appspot.com/o/dataP102photo.jpg?alt=media&token=f43e2465-780c-43d1-a3e5-f9bd82773760

    rules_version = ‘2’;
    service firebase.storage {
    match /b/{bucket}/o {
    match /{allPaths=**} {
    allow read, write: if request.auth != null;
    }
    }
    }

    It was this line I had to change
    allow read, write: if request.auth != null;

    Reply
        • Thanks.
          My rules were different by default.
          Mine were just like the ones you’ve shared previously (the ones that worked).
          Maybe I have changed something that I can’t remember. I’ll check that out.
          Thanks.
          Regards.
          Sara

          Reply
          • Hi All,
            issue statement: web page doesn’t show picture nor time and date

            Regarding the rule: I see the method of Authorization in tutorial is “anonymous” but I kept it as email/pass

            Now, after 2^8 times I changed rules to different suggestions when T/S I ended up at:
            rules_version = ‘2’;

            service firebase.storage {
            match /b/{bucket}/o {
            match /{allPaths=**} {
            allow read, write: if true;
            }
            }
            }
            maybe not the strongest, but hey, I am uploading picture of my 3D print

            then,
            Re: problem uploading picture: reset your sign in password in Firebase project, refresh page, maybe grab a cup of tea and come back, Of course in Arduino IDE sketch change password as well. it worked for me.

            Then, I noticed I am getting the date and time near refresh button, but no picture.

            after sniffing around java consol (ctrl+shift+I) in OPERA I noticed the app does not see app.js file NADA, NULL, ZERO
            what I found was that line for app.js was between and section

            then, I had a look at index.html file from different project and I noticed that is in section, so I “monkey see, monkey do” thingy and I created folder under public file , scripts file, then I moved the app.js in to that new folder. After that I ended up with this:

            ESP32-CAM Web App

            <p><img id="img" width="500px"></p>
            <p>Last picture taken: <span id="date-time"></span></p>
            <button onclick="window.location.reload();">Refresh</button>
            <!--INCLUDE JS FILES-->
            http://scripts/app.js

            firebase deploy went off and when I reloaded the web app page, BOOM! the picture is there, what a freeing good feeling, I am having glass of refreshing beverage to celebrate success.

            full project files here boyos https://github.com/MrEstefano/esp32-camera-app

            Best
            Stef

  5. Super.
    Now it is working and I can see the picture in the monitor, but it is only the last picture. Now I would like to have new name, so I can store several pictures maybe to use date and time as name? Any suggestions?

    Reply
    • Hi.
      You just need to modify the name for each new picture.
      For example, you can have a variable that you increment and your pictures will be called photo1, photo2, photo3,…
      Or you can get epoch time and name the pictures with the epoch time they were taken…
      Maybe I’ll create a tutorial with multiple pictures…
      Regards,
      Sara

      Reply
  6. I just got a new problem:
    Taking a photo…
    Picture file name: /data/photo.jpg
    The picture has been saved in /data/photo.jpg – Size: 156928 bytes
    Uploading picture… not found

    What can that be?

    Reply
      • Hi Henry,

        You can see below Sarah’s reaction.

        Hi.
        Yes. That’s right. I was testing that right now.
        You can remove the library. I already updated the code on the blog post.
        Regards,
        Sara

        Reply
  7. Hi i get a compile error:

    ESP_CAM_Firebase_Demo:17:25: fatal error: StringArray.h: No such file or directory

    Where doI get the “StringArray.h” library.

    Reply
    • I see the same question was asked on the 13 January 2022 but no help was offered.
      The code:
      #include <StringArray.h>
      does not compile because arduino IDE can not find the file or directory.
      Error message: “StringArray.h: No such file or directory”

      Reply
      • Firebase Arduino Client Library for ESP8266 and ESP32 ver.2.3.7 if this is what you mean by ESP32 core.

        I commented out the line in the code(#include <StringArray.h>) that include that library and now it compile and the ESP32 cam does its job. Seem that that library is not even used in the code.

        Reply
        • Hi.
          Yes. That’s right. I was testing that right now.
          You can remove the library. I already updated the code on the blog post.
          Regards,
          Sara

          Reply
  8. thankyou.. for simple tutorial.. good job,, its work..

    btw, the rule must be like this..

    rules_version = ‘2’;
    service firebase.storage {
    match /b/{bucket}/o {
    match /{allPaths=**} {
    allow read, write: if request.auth != null;
    }
    }
    }

    if we found error “access denied” while uploading image..

    Reply
  9. Phew! This is a mighty long thread. I have a problem that hasn’t been explicitly discussed yet:

    In the monitor:

    Taking a photo
    Picture file name: /data/photo.jpg
    The picture has been saved in /data/photo.jpg – Size 82304 bytes
    Uploading picture… unknown error

    This isn’t very useful! I’ve checked all the ‘network credentials’ and the ‘defines’ – all are as set up correctly.

    One clue: In Firebase>storage no data folder is created. I understand that this should be created by the sketch. I see

    define FILE_PHOTO “/data/photo.jpg” and this is being used in the capturePhotoSave Spiffs function. Any clues would be very much appreciated.

    Reply
    • Hi Ben.

      What are the Storage rules you have on your project?
      On the Firebase console, go to Storage and then select the Rules tab.

      Did you copy the bucket URL correctly? WITHOUT the gs:// ??

      Regards,
      Sara

      Reply
  10. Hi Sara
    I have completed the project as detailed. When I go to Firebase-Storage-Files, the file there is named dataP102photo.jpg
    I was expecting to see a folder ‘data’ with the file ‘photo.jpg’
    There is no folder name data created.
    Jim

    Reply
    • Hi.
      Did you change anything on the code? It seems your path might be wrong.
      It is supposed to create the data folder.
      Regardsm
      Sara

      Reply
      • I have made changes to the code, but as you can see the pic file name is ‘/data/photo.jpg’ while the file name in the download url is ‘dataP102photo.jpg’.
        Jim

        Reply
      • Sorry I forgot to include the monitor info.
        16:20:23.124 > +++++++++++++++++++ Setup +++++++++++++++++++++++++++++++++
        16:20:23.124 > ESP32-CAM-Send Pics to Firebase, {main.cpp}
        16:20:23.124 > Jan 15 2022 @ 16:18:05
        16:20:23.124 > Ver 22.1.15.001, 4:00 pm
        16:20:23.124 > *** Debug= 0
        16:20:23.124 >
        16:20:24.238 > Connecting to WiFi…
        16:20:25.243 > Connecting to WiFi…
        16:20:26.247 > Connecting to WiFi…
        16:20:26.830 > SPIFFS mounted successfully
        16:20:26.830 > … listAllFiles(spiffs) …
        16:20:26.973 > FILE: /data/photo.jpg
        16:20:26.973 > … end listAllFiles(spiffs) …
        16:20:26.973 >
        16:20:27.578 > Camera init completed
        16:20:27.578 > Token info: type = id token, status = on request
        16:20:30.301 > Token info: type = id token, status = ready
        16:20:30.301 > Taking a photo…
        16:20:30.413 > Picture file name: /data/photo.jpg
        16:20:31.958 > The picture has been saved in /data/photo.jpg – Size: 0 bytes
        16:20:32.015 > Taking a photo…
        16:20:32.015 > Picture file name: /data/photo.jpg
        16:20:32.969 > The picture has been saved in /data/photo.jpg – Size: 0 bytes
        16:20:32.969 > Taking a photo…
        16:20:33.065 > Picture file name: /data/photo.jpg
        16:20:33.989 > The picture has been saved in /data/photo.jpg – Size: 0 bytes
        16:20:34.004 > Taking a photo…
        16:20:34.100 > Picture file name: /data/photo.jpg
        16:20:35.519 > The picture has been saved in /data/photo.jpg – Size: 127872 bytes
        16:20:35.758 > Uploading picture…
        16:20:41.639 > Download URL: https://firebasestorage.googleapis.com/v0/b/esp-firebase-demo-97046.appspot.com/o/dataP102photo.jpg?alt=media&token=0b7a…..

        Reply
  11. Dear Sara,
    I have a question about this project. Is it possible to send a photo to firebase with esp32 cam instead of using wifi through gsm with a gprs module like sim800l?

    Reply
      • rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
        configsip: 0, SPIWP:0xee
        clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
        mode:DIO, clock div:1
        load:0x3fff0030,len:1344
        load:0x40078000,len:13964
        load:0x40080400,len:3600
        entry 0x400805f0
        Connecting to WiFi…
        Connecting to WiFi…
        Connecting to WiFi…
        SPIFFS mounted successfully
        Token info: type = id token (GITKit token), status = on request
        Token info: type = id token (GITKit token), status = ready
        Taking a photo…
        Camera capture failed
        Uploading picture… file not found.
        why is my camera not capturing

        Reply
  12. Hi, thanks so much for the tutorial!

    Unfortunately I get this output error:


    Taking a photo…
    Picture file name: /data/photo.jpg
    The picture has been saved in /data/photo.jpg – Size: 91136 bytes

    Uploading picture… response payload read timed out due to network issue or too large data size

    Actually the network connection is fine, and also the image size should be fine.
    Do you have an idea?
    Thank you!!
    Best wishes,
    Dan

    Reply
    • Hi.
      Check that you have inserted the right credentials in your code: project API key and bucket URL.
      Regards,
      Sara

      Reply
      • Same error, yea the credentials are correct but the byte size is way larger than yours.
        I’m using the AI thinker ESP32 Cam module. i get around 90-100k bytes where your image size was around 13k bytes

        Reply
        • Hi.
          Try using a lower-quality image.
          For example, replace the following:
          if (psramFound()) {
          config.frame_size = FRAMESIZE_UXGA;
          config.jpeg_quality = 10;
          config.fb_count = 2;

          with

          if (psramFound()) {
          config.frame_size = FRAMESIZE_VGA;
          config.jpeg_quality = 20;
          config.fb_count = 2;

          Let me know if this helps.
          REgards,
          Sara

          Reply
          • Hi thanks for your help.

            Unfortunately still same error, now image size is 7424 bytes.
            Credentials are also correct.
            I also tried adding
            configF.fcs.upload_buffer_size = 512;
            without success.

            Did you sole the problem @ Abhiraj Raghunath?

          • I tried to upload a small .txt file to firebase, and I get the same error, also when using my esp32 controller it doesn’t work.

    • Hi Dan,

      “response payload read timed out” only this part of the error message is helpful, the rest is just there to confuse you. It makes you think that the problem is far more complicated than it really is!

      Try the following, it will solve the issue:

      In the above code, where you have to define the Storage_Bucket_ID,

      Do NOT use: gs://your-firebase-project-name.appspot.com
      Do NOT use: gs://your-firebase-project-name.appspot.com/
      Correct format is: your-firebase-project-name.appspot.com

      So, it should look like:
      // Enter Firebase storage bucket ID
      #define STORAGE_BUCKET_ID “your-firebase-project-name.appspot.com”

      The input parameter for the functions in the library do not require you to include the gs://

      Hope that helps, best of luck.
      Tanjid

      Reply
      • Hi Tanjid
        I have the same problem as Dan, unfortunately after I have checked my credentials, it still does not work. Do you have any tips on what I can test next?

        Reply
        • Hi Max,

          Run the same code with a deliberate mistake in your storage bucket id. Something like “xyz0987s.appsot.com”

          So your code should look:

          // Enter Firebase storage bucket ID
          #define STORAGE_BUCKET_ID “xyz0987s.appsot.com”

          If you still get the same error, then you know it is highly likely that there is a issue in the storage bucket ID format. If you get a different error, please do share.

          Reply
  13. Hello, thanks for sharing this informative tutorial!
    I’m currently trying to upload several pictures to firebase storage with system reset instead of manual and i’d love that if you will be able to help me with writing that.
    also: is there a way to diplay jpg files from firebase as a video?
    thanks in advance, Iddo

    Reply
  14. Also struggling with the error “Upload failed, response payload read timed out due to network issue or too large data size”

    Anyone able to solve this issue, please? The image size seems to be small enough ad Wi-Fi connected okay…

    Reply
      • After a few tests, a review of the library src code and some conversations with the lib maintainer, I found that changing the esp32 core SDK version works. Here are the findings:

        esp32 core SDK version
        v1.0.6 – working
        v2.0.0 – working
        v2.0.1 – working
        v2.0.2 – not working
        v2.0.3 – now working

        Not sure if esp32 core SDK is going to change or the Firebase ESP client is going to change in the future such that the latest versions of both sides can work together…

        Reply
  15. Hi Sara, is there a way for the file to be renamed each time the camera being reset, so that it doesnt replace the old photo.jpg file.
    Right know only one file can insert at the firebase storage right?

    Reply
    • Hi.
      Yes. With this example, the photo will be replaced. This is the simplest example.
      You can number the photo names with numbers or with a timestamo, so that all names are different. This way, all pictures will be saved.
      Regards,
      Sara

      Reply
  16. Hello,
    First of all, thank you very much for this tutorial!
    I’m building a solar powered remote camera that will (hopefully) take a photo everyday and upload it to Firebase. I’m also saving a copy of the photo on a local SD card.
    I modified the script so the photo is saved on the SD card instead of SPIFFS but I can’t upload it to Firebase. I get this error: “[E][sd_diskio.cpp:775] sdcard_mount(): f_mount failed: (3) The physical drive cannot work. SD Storage is not ready.”
    I tried modifying FirebaseFS.h as recommended (//The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h) – by uncommenting the part about SDMMC. But then it won’t compile (throws a lot of errors).

    Reply
  17. Hello,
    I’m having a problem..
    I get the message “ADMIN_ONLY_OPERATION” and i don’t know where is the problem.
    Any suggestions on how to fix it?

    Reply
    • Hi.
      Did you create a user in your Firebase console?
      Check the Users tab and see if it is logging in successfully. In the Users tab, it shows the time of the last login.
      Then, check the storage rules. What are the bucket storage rules that you have?
      Regards,
      Sara

      Reply
  18. Hello Gooday.
    So the whole process is working but I am having issues changing the spiffs filename as i want multiple images on the cloud storage(for my final year project)
    Please I am not really strong with c, any help is appreciated!!

    Reply
  19. response payload read timed out due to network issue or too large data size
    In my case, sometimes the ESP32 is able to send images to firebase, but sometimes it gives response payload read timed out due to network issue or too large data size.

    Reply
  20. Hello,
    I wanted to make some change in your application as follows.
    I made a physical connection between ESP32-CAM GPIO16 (seems empty) and the BBC-Microbit P0 pin. I wanted to read the 0-3v value from the P0 pin as analog.
    My goal is to change the filename based on the received analog value.

    However, somehow the GPIO16 pin value always shows 0.
    I made a GND common connection. But result is same.

    Apart from these, I also tried GPIO 13, GPIO12, GPIO 14 and GPIO 15. But the result is always the same.

    I could never understand the reason. Please help.

    Reply
  21. Hi Sara
    This is Excellent project, and thanks for sharing to us.
    Is the a way to record a video and upload it to Firebase Storage?
    Record timelapse to avi and store it to SPIFFS (for who need more GPIO to use) or SD Card and then upload it to Firebase every 5/10 second.

    Thanks you

    Reply
  22. Hello, thanks for your project.
    Now i have a problem: Failed to open file in writing mode
    How to fix it? Please, thanks.

    Taking a photo…
    Picture file name: /data/photo.jpg
    Failed to open file in writing mode
    Uploading picture…

    Reply
  23. Hello, I have this error, and don´t know how can solve it, can you help me?

    addons/TokenHelper.h: No such file or directory

    Reply
      • Hey Sara, I put the code from the link that you gave me, but I still have the same problem:

        May can you help me by Discord or something, I will thank you a lot

        This is the code:

        #include “WiFi.h”
        #include “esp_camera.h”
        #include “Arduino.h”
        #include “soc/soc.h” // Disable brownout problems
        #include “soc/rtc_cntl_reg.h” // Disable brownout problems
        #include “driver/rtc_io.h”
        #include <SPIFFS.h>
        #include <FS.h>
        #include <Firebase_ESP_Client.h>
        //Provide the token generation process info.
        #include <addons/TokenHelper.h>

        .-…………………………………
        …………………………………..

        /* mime type */)){
        Serial.printf(“\nDownload URL: %s\n”, fbdo.downloadURL().c_str());
        }
        else{
        Serial.println(fbdo.errorReason());
        }

        }
        }

        Reply
    • you may try to increase the DIV CLK by setting the camera using:

      sensor_t *s = esp_camera_sensor_get();
      s->set_reg(s, 273, 63, amount);

      set amount to 5 should be enough, you can set this parameter between 0-63
      note: don’t set this setting to high it may crash your camera

      Reply
      • ah yes you may set this setting after you call

        // Camera init
        esp_err_t err = esp_camera_init(&config);
        if (err != ESP_OK) {
        Serial.printf(“[Initializing] Camera init failed with error 0x%x\n”, err);
        ESP.restart();
        }

        sensor_t *s = esp_camera_sensor_get();
        s->set_reg(s, 273, 63, amount);

        otherwise it will crash

        Reply
    • You may try to increase DIV CLK by setting the camera using:

      // Camera init
      esp_err_t err = esp_camera_init(&config);
      if (err != ESP_OK) {
      Serial.printf(“[Initializing] Camera init failed with error 0x%x\n”, err);
      ESP.restart();
      }

      //You should put this setting after init the camera otherwise the camera will crash
      sensor_t *s = esp_camera_sensor_get();
      s->set_reg(s, 273, 63, amount);

      set the amount to 5 should be enough, you can play with this setting between 0-63 in case you facing low light condition
      note: it may crash the camera if you set the amount to high, and changing the camera resolution or other setting will reset the DIV CLK back to 0 (if you make some sort of webserver to change the camera setting)

      Reply
  24. Hello, as always you guys are great! I read through all the comments and don’t think anyone has posted this error. It appears that it is saying my credentials. As it is stating a DNS error, I thought it would be related to this… but I confirmed it matches correctly. Any thoughts or suggestions on how to fix this would be greatly appreciated. ¡Gracias!

    #define STORAGE_BUCKET_ID “esp-birdhouse.appspot.com”

    Token info: type = id token (GITKit token), status = on request
    [ 8894][E][WiFiGeneric.cpp:1360] hostByName(): DNS Failed for http://www.googleapis.com
    [ 8894][E][WiFiClientSecure.cpp:135] connect(): start_ssl_client: -1
    Token info: type = id token (GITKit token), status = error
    Token error: code: -4, message: connection lost

    Reply
      • hello Sara, i have the same issue, and i have checked my credentials and bucket, thay are correct. but i still get error, detail:
        12:19:39.745 -> Token info: type = id token (GITKit token), status = on request
        12:19:39.745 -> Token info: type = id token (GITKit token), status = error
        So, can you help me to solve this issue ?
        Regards,
        Minh Duc Le

        Reply
  25. hello Sara, i have the same issue, and i have checked my credentials and bucket, thay are correct. but i still get error, detail:
    12:19:39.745 -> Token info: type = id token (GITKit token), status = on request
    12:19:39.745 -> Token info: type = id token (GITKit token), status = error
    So, can you help me to solve this issue ?
    Regards,
    Minh Duc Le

    Reply
  26. Hi Sara, greetings from Indonesia. 1 quick question, can ESP32-CAM send the photos straight to the Google Firebase without saving it before to the SD Card?

    Reply
    • Hi.
      It should be possible but using a different method.
      The library function we’re using here expected a path to a file. So, that file must be saved somewhere on our board or on a microSD card connected to the board.
      Regards,
      Sara

      Reply
  27. I am getting this error output and no photos are posting:

    Opening port
    Port open
    11:06:28:088 -> Connecting to WiFi…
    11:06:29:080 -> Connecting to WiFi…
    11:06:30:067 -> Connecting to WiFi…
    11:06:31:094 -> Connecting to WiFi…
    E (6157) SPIFFS: mount failed, -10025
    11:06:47:684 -> SPIFFS mounted successfully
    11:06:48:307 -> Token info: type = id token (GITKit token), status = on request
    11:06:48:339 -> Token info: type = id token (GITKit token), status = error
    Token error: code: -4, message: connection lost
    Taking a photo…
    11:06:48:465 -> Picture file name: /data/photo.jpg
    11:06:49:357 -> The picture has been saved in /data/photo.jpg – Size: 94336 bytes
    11:06:50:307 -> Token info: type = id token (GITKit token), status = on request
    Token info: type = id token (GITKit token), status = error
    Token error: code: -4, message: connection lost
    11:06:52:329 -> Token info: type = id token (GITKit token), status = on request
    Token info: type = id token (GITKit token), status = error
    Token error: code: -4, message: connection lost
    11:06:54:341 -> Token info: type = id token (GITKit token), status = on request
    Token info: type = id token (GITKit token), status = error
    Token error: code: -4, message: connection lost

    Do you know what is causing this?

    Thanks.

    Reply
  28. A pleasure to greet you, I have not been able to manage to save the image in firebase, I tried with mem_storage_type_sd and mem_storage_type_flash and it gives me the following errors:
    Picture file name: /data/photo.jpg
    The picture has been saved in /data/photo.jpg – Size: 36864 bytes
    Uploading picture… SD Storage is not ready.

    and when i use flash:
    Picture file name: /data/photo.jpg
    The picture has been saved in /data/photo.jpg – Size: 36864 bytes
    Uploading picture… File not found.

    I already checked the rules and remove gs://

    rules_version = ‘2’;
    service firebase.storage {
    match /b/{bucket}/o {
    match /{allPaths=**} {
    allow read, write: if request.auth !=null;
    }
    }
    }

    I don’t know what else to try, please I need your help.

    Thank you

    Reply
    • Hi.
      Can you use the same code, but replace “SPIFFS” with “LittleFS”?
      Let me know if that will solve the problem.
      Regards,
      Sara

      Reply
      • Greetings Sara, thanks for your answer, I have tried with “LittleFS” but I still can’t get it to work, I get the following error:

        Connecting to WiFi…
        LittleFS mounted successfully
        Token info: type = id token (GITKit token), status = on request
        Token info: type = id token (GITKit token), status = ready
        Taking a photo…
        Picture file name: /data/photo.jpg
        Failed to open file in writing mode
        Taking a photo…
        Picture file name: /data/photo.jpg
        Failed to open file in writing mode
        Taking a photo…

        Reply
        • Hi.
          I’m not sure what might be wrong.
          Try changing the picture filename to data/photo.jpg instead of /data/photo.jpg.
          Not sure exactly what is the issue…
          Regards,
          Sara

          Reply
          • Hello Sara,

            I am having the same problem here is what my serial monitor shows.

            cam_hal: EV-EOF-OVF
            The picture has been saved in /data/photo.jpg – Size: 135168 bytes
            cam_hal: EV-EOF-OVF
            Uploading picture… File not found.
            Any help would be great!
            Thanks

          • Hi.
            I think there’s some sort of incompatibility with the most recent version of the ESP Firebase Client library.
            I found that version 4.3.0 works fine while most recent versions give that error.
            Revert to version 4.3.0 and it should work fine. I just tested it and it works.
            Regards,
            Sara

  29. Hello Sara, thanks for a great and very useful tutorial. Firebase side is all good I think, but I get an error when the ESP32 tries to upload the picture. Any ideas? I get this error with Platformio and Arduino IDE…I’m using a Freenove-ESP32-WROVER-DEV module and had to change the pin config to suit but it seems to take the photo ok.

    Connecting to WiFi…
    Connecting to WiFi…
    SPIFFS mounted successfully
    Token info: type = id token (GITKit token), status = on request
    Token info: type = id token (GITKit token), status = ready
    Taking a photo…
    Picture file name: /data/photo.jpg
    The picture has been saved in /data/photo.jpg – Size: 57344 bytes
    Uploading picture… File not found.

    Reply
      • Hello Sara,
        I have the same problem.
        22:33:41.198 -> Connecting to WiFi…
        22:33:41.759 -> SPIFFS mounted successfully
        22:33:42.368 -> Token info: type = id token (GITKit token), status = on request
        22:33:44.989 -> Token info: type = id token (GITKit token), status = ready
        22:33:44.989 -> Taking a photo…
        22:33:45.129 -> Picture file name: /data/photo.jpg
        22:33:48.500 -> The picture has been saved in /data/photo.jpg – Size: 289408 bytes
        22:33:48.546 -> Uploading picture… File not found.

        How do I replace in my code SPIFSS with LittleFS?

        Reply
        • Hi.
          You just need to replace the word SPIFFS with LittleFS.
          The rest remains the same.
          Let me know if that solves the problem so that I can update the tutorial.
          Regards,
          Sara

          Reply
          • Thank you Sara.
            Still not working, now with another error

            I use the Arduino IDE 1.8.19. I had to download and install the LittleFS library from the URL https://github.com/lorol/LITTLEFS because otherwise it gave compile errors
            I have replaced #include <SPIFFS.h> with #include <LITTLEFS.h> and references to SPIFFS with LITTLEFS except for those that appeared as part of the function name that I have kept.

            Here is the serial output:
            13:29:46.539 -> Connecting to WiFi…
            13:29:47.007 -> LITTLEFS mounted successfully
            13:29:47.662 -> Token info: type = id token (GITKit token), status = on request
            13:29:51.453 -> Token info: type = id token (GITKit token), status = ready
            13:29:51.453 -> Taking a photo…
            13:29:51.547 -> Picture file name: /data/photo.jpg
            13:29:51.547 -> Failed to open file in writing mode

            Any ideas?

            Thanks again

          • Hi.
            You don’t need to install that library.
            It is included by default as long as you have your ESP32 boards updated.
            Go to Tools > Board > Boards Manager > search for ESP32 and check your version.
            Regards,
            Sara

          • Hi Sara,

            After doing what you tell me, I have updated the version of the ESP32 to 2.0.7 and I have removed the LittleFS library that I had installed.
            When compiling it gives me the same error for which I added the library “LITTLEFS.h: No such file or directory”

            BR

          • Hi.
            I’m sorry for the misunderstanding.
            Use the code as it is, with SPIFFS, and downgrade the library version.
            Regards,

            Sara

          • Hi Sara,

            Changing all the SPIFFS to LittleFS does not solve the problem: (see results below) Also tried #define SPIFFS LittleFS but same result.

            Sticking with SPIFFS, I simplified the problem by downloading a very simple text.txt file to the /data folder using PlatformIO and tried to upload it to Firebase but still get ‘File not found’ I can open the text file and read and print its contents as in one of your other tutorials, before and after trying the Firebase upload. I tried changing the path/filename to every conceivable option text.txt, /text.txt, data/text.txt, /data/text.txt. /spiffs/data/text.txt etc. etc. but always get ‘File not found’ Same result on PlatfomIO and Arduino IDE 2.0.3………. looks like it’s the Firebase.Upload command where the problem lies. I can use the Firebase.Upload with a binary array using the other Firebase.Upload overload and that works fine and ends up in my firebase Storage……. so a bit stuck right now but still trying.

            Results from changing all SPIFFS to LittleFS is:
            Connecting to WiFi…
            ./components/esp_littlefs/src/littlefs/lfs.c:1229:error: Corrupted dir pair at {0x0, 0x1}
            E (4622) esp_littlefs: mount failed, (-84)
            E (4623) esp_littlefs: Failed to initialize LittleFS
            SPIFFS mounted successfully
            Token info: type = id token (GITKit token), status = on request
            Token info: type = id token (GITKit token), status = ready
            Taking a photo…
            Picture file name: /data/photo.jpg
            [ 11855][E][vfs_api.cpp:332] VFSFileImpl(): fopen(/littlefs/data/photo.jpg) failed
            Failed to open file in writing mode
            [ 11870][E][vfs_api.cpp:105] open(): /littlefs/data/photo.jpg does not exist, no permits for creation

          • Hi.
            I think it’s an issue with the most recent versions of the library-
            I found that version 4.3.0 works fine while most recent versions give that error.
            Revert to version 4.3.0 and it should work fine. I just tested it and it works.
            Regards,
            Sara

        • Hi.
          I just tested the code.
          I think it’s an issue with the most recent versions of the library.
          I found that version 4.3.0 works fine while most recent versions give that error.
          Revert to version 4.3.0 and it should work fine. I just tested it.
          Regards,
          Sara

          Reply
          • Hi Sara,
            I just tried that version of the Firebase Arduino Client Library, 4.3.0 instead of 4.3.7 and it works. Hurrah for that!!!
            I had tried the version you were using at the time of the tutorial, 2.3.7 but that has compatibility issues now and doesn’t compile.

            Thanks very much for taking the time to solve this. Your efforts are very much appreciated. You put a lot of effort into answering these comments.

            Incidentally, I have been having problems trying to use LittleFS instead of SPIFFS and still have not got to the bottom of that, so if you are looking for a tutorial idea, something with LittleFS perhaps?

  30. I just got a new problem:
    Taking a photo…
    Picture file name: /data/photo.jpg
    The picture has been saved in /data/photo.jpg – Size: 156928 bytes
    Uploading picture… File not found

    What can that be?

    Reply
  31. Hi everybody
    I just got a new problem:
    Taking a photo…
    Picture file name: /data/photo.jpg
    The picture has been saved in /data/photo.jpg – Size: 156928 bytes
    Uploading picture… File not found

    What can that be?

    Reply
    • Hi.
      I think it’s an issue with most recent versions of the library-
      I found that version 4.3.0 works fine while most recent versions give that error.
      Revert to version 4.3.0 and it should work fine. I just tested it.
      Regards,
      Sara

      Reply
  32. hy! i upload the code to my esp32-cam and every thing compile correct but for the last 2 days this is the message that it’s apeare in serial monitor “Taking a photo…
    Picture file name: /data/photo.jpg
    The picture has been saved in /data/photo.jpg – Size: 91008 bytes
    Uploading picture… File not found.”‘
    until last 2 days ago every thing work correctly but now no and i made another project in firebase and the same problem. do you know why this hapen ? thx!

    Reply
    • Hi.
      I think it’s an issue with most recent versions of the library-
      I found that version 4.3.0 works fine while most recent versions give that error.
      Revert to version 4.3.0 and it should work fine. I just tested it.
      Regards,
      Sara

      Reply
  33. 536 / 5,000
    Resultados de traducción
    Traducción
    Hello dear, when compiling the following error appears

    Archiving built core (caching) in: /tmp/arduino_cache_176618/core/core_esp32_esp32_esp32cam_CPUFreq_240,FlashMode_qio,FlashFreq_80_b9cf92f8f962fca57217cf582a443251.a
    exec: “python”: executable file not found in $PATH
    Error compiling for AI Thinker ESP32-CAM card.

    Sorry if you don’t understand, I’m writing from Chile, I’m not fluent in English, I use Google translator, and if the query was made but I didn’t find something similar, I appreciate your help, and congratulations for the work.

    Reply
  34. Taking a photo…
    Picture file name: Ĭ⸮?
    [E][vfs_api.cpp:265] VFSFileImpl(): fopen(/spiffs/data/212.jpg) failed
    The picture has been saved in /data/212.jpg – Size: 0 bytes

    Reply
  35. error on this

    Token info: type = id token (GITKit token), status = ready Uploading picture… File not found.esp32 cam project

    Reply
  36. why i am getting this ??
    The picture has been saved in /data/phot.jpg – Size: 0 bytes
    Uploading picture…
    response payload read timed out due to network issue or too large data size
    the size of picture is also not large.. tell me solutions please?

    Reply
  37. tôi muốn có một dòng code để khi bắt đầu cứ 10 giây là máy ảnh của tôi lại chụp một ảnh và tự động gửi lên firebase . mong mọi người giúp đỡ tôi , tôi xin cảm ơn

    Reply
  38. Here’s how i fixed the problem of uploading to firebase storage by the sd card (SD_MMC).

    https://github.com/mobizt/Firebase-ESP-Client/discussions/352

    i added this…
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////
    #if defined(DEFAULT_SD_FS) && defined(CARD_TYPE_SD_MMC)

    Serial.print("\nMounting SD_MMC Card... ");

    if (!Firebase.sdMMCBegin("/sdcard", false, true))
    {
    Serial.println("failed\n");
    return false;
    }
    else
    {
    Serial.println("success\n");
    return true;
    }

    #endif
    /////////////////////////////////////////////////////////////////////////////////////////

    to the end of the file “SDHelper.h”. I hope this helps..

    Reply
  39. i got this error help me please:
    E (533) esp_core_dump_fg⸮f⸮: No core dump partition found!
    E (533) esp_core_dump_flash: No core dump partition found!
    Connecting to WiFi…
    E (1587) SPIFFS: spiffs partition could not be found
    An Error has occurred while mounting SPIFFS
    ets Jun 8 2016 00:22:57

    Reply
    • Hi.
      What settings do you have selected in the Tools menu for your board?
      What partition scheme do you have select?
      Regards,
      Sara

      Reply
  40. Hy. in my serial monitor i recive the next text and i don’t find where is the problem in my code :Taking a photo…
    Picture file name: /data/photo.jpg
    Failed to open file in writing mode

    Reply
  41. Thanks so much! I had had issues with Firebase in the past, but your crisp and clear instructions in this tutorial made it all very step-by-step and tested out in under an hour on the first try!

    Reply
  42. Hy. in my serial monitor i recive the next text and i don’t find where is the problem in my code :Taking a photo…
    Picture file name: /data/photo.jpg
    Failed to open file in writing mode
    As far as I can see this problem is related to the SPIFFS.h library but I can’t find any workaround to fix it. do you think you could give me a recommendation?

    Reply
  43. Hello! I still get the serial monitor Photo file name: “/data/photo.jpg
    Failed to open file in write mode
    Uploading image…
    Download URL:”
    And I don’t understand why I keep getting “Failed to open file in write mode” until now or it was working without problems. I tried to change the version of the ESP_Firebase_client library to 4.3.0 but still the same thing.

    Reply
    • Hi.
      What is the version of the ESP32 boards that you have installed?
      Go to Tools > Boards > Boards Manager, search for ESP32 and check the version.
      Regards,
      Sara

      Reply
  44. Please help me this error anyone!
    rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
    mode:DIO, clock div:1
    load:0x3fff0030,len:1344
    load:0x40078000,len:13924
    ho 0 tail 12 room 4
    load:0x40080400,len:3600
    entry 0x400805f0
    Connecting to WiFi…
    SPIFFS mounted successfully
    Token info: type = undefined, status = error
    Token error: code: -127, message: missing required credentials e.g., database URL, host and tokens.
    Taking a photo…
    Picture file name: /data/photo.jpg
    cam_hal: EV-EOF-OVF
    Failed to open file in writing mode
    I don’t know why the first pictures is going right but the next one is not

    Reply
  45. Taking a photo…
    Picture file name: /data/photo.jpg
    Failed to open file in writing mode
    Uploading picture…
    Download URL: ………

    Hi Sara
    Iam facing this issue “Failed to open file in writing mode”.
    i have downgrade the library vesion to 4.3.0 but stiil having problem
    Also i have checked the board version it iss 2.0.8

    Reply
  46. Hello and sorry to bother you. I’m using your project as a basis for developing an application and everything is working great. I connected a ST7735 TFT display to the ESP32-CAM. I would like, when capturing an image, to have one with the resolution of the TFT and immediately after take another one with a higher resolution.
    Is it possible to sample the resolution of the ESP32 several times during the execution of the sketch? How can I do?
    Thank you for your kind cooperation

    Reply
  47. Hello Sara,

    this question is a little different, I implemented this code in an ESPCAM and worked perfectly, then, based on that, I copy / paste this code and implemented in the normal ESP32 board, modiffied and adapted for WAV file recording (audio instead of picture), I was able to save the wav file in SPIFFS and name it as I wanted, average size 641 Kb. I get connection with firebase, token, status ready, but never able to upload the file:

    File name Size

    recording.wav 655404 bytes

    Token info: type = id token (GITKit token), status = on request
    Token info: type = id token (GITKit token), status = ready
    Uploading audio…

    Of course I´ve changed the mime type, and tried with different names, also I have tried using the same name as picture and same mime type but no success, I wonder if you have worked with audio uploads from esp32 in firebase, I would appreciate if you send me a link with info or documentation in order to solve my problem.

    thanks in Advance,
    Juan

    Reply
  48. hrst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
    mode:DIO, clock div:1
    load:0x3fff0030,len:1344
    load:0x40078000,len:13964
    load:0x40080400,len:3600
    entry 0x400805f0
    Connecting to WiFi…
    Connecting to WiFi…
    Connecting to WiFi…
    SPIFFS mounted successfully
    Token info: type = id token (GITKit token), status = on request
    Token info: type = id token (GITKit token), status = ready
    Taking a photo…
    Camera capture failed
    Uploading picture… file not found.

    Reply
  49. hi sara
    i have this problem cant seem to fix can you help

    mode:DIO, clock div:1
    load:0x3fce3808,len:0x44c
    load:0x403c9700,len:0xbe4
    load:0x403cc700,len:0x2a68
    entry 0x403c98d4
    Connecting to WiFi…
    LittleFS mounted successfully
    Guru Meditation Error: Core 1 panic’ed (LoadProhibited). Exception was unhandled.

    Core 1 register dump:

    Reply
  50. Hi Sara,
    I found a problem with the Firebase _ESP-Client library
    I know it has been replaced but it is working by a customer and I should solve the problem I try to explain:

    Token info: type = token id (GITKit token), status = error

    I found that the Firebase call token, from this Library, lasts 60 minutes. At the end of this time you must request a new token or restart the system.
    Have you ever encountered this problem?
    Do you have a solution then?
    Thanks for the kind cooperation.
    Best regards.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.