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

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.

ESP32-CAM Sendo Photo to Firebase Storage Project Overview
  • When the ESP32 first runs, it takes a new picture and saves it in the filesystem (SPIFFS);
  • 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) Use the default security rules—click Next.

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

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 Mobitz.
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 "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>

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

// Photo File Name to save in SPIFFS
#define FILE_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;

bool taskCompleted = false;

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

// 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
  do {
    // Take a photo with the camera
    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", FILE_PHOTO);
    File file = SPIFFS.open(FILE_PHOTO, 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);
      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);
  } while ( !ok );
}

void initWiFi(){
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting 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 port for debugging purposes
  Serial.begin(115200);
  initWiFi();
  initSPIFFS();
  // 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) {
    capturePhotoSaveSpiffs();
    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 to local file */, mem_storage_type_flash /* memory storage type, mem_storage_type_flash and mem_storage_type_sd */, FILE_PHOTO /* path of remote file stored in the bucket */, "image/jpeg" /* mime type */)){
      Serial.printf("\nDownload URL: %s\n", fbdo.downloadURL().c_str());
    }
    else{
      Serial.println(fbdo.errorReason());
    }
  }
}

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 <SPIFFS.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.

define STORAGE_BUCKET_ID "REPLACE_WITH_YOUR_STORAGE_BUCKET_ID"

Picture Path

The FILE_PHOTO variable defines the SPIFFS path where the picture will be saved. It will be saved with the name photo.jpg under the data folder.

#define FILE_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;

checkPhoto() Function

The checkPhoto() function checks if the picture was successfully taken and saved in SPIFFS.

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

capturePhotoSaveSpiffs() Function

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

// 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
  do {
    // Take a photo with the camera
    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", FILE_PHOTO);
    File file = SPIFFS.open(FILE_PHOTO, 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);
      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);
  } while ( !ok );
}

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...");
  }
}

initSPIFFS() Function

The initSPIFFS() function initializes the SPIFFS filesystem.

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

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, SPIFFS, and the camera.

// Serial port for debugging purposes
Serial.begin(115200);
initWiFi();
initSPIFFS();
// 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) {
  capturePhotoSaveSpiffs();
  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 to local file */, mem_storage_type_flash /* memory storage type, mem_storage_type_flash and mem_storage_type_sd */, FILE_PHOTO /* path of remote file stored in the bucket */, "image/jpeg" /* mime type */)){
    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")

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 SPIFFS or SD Card*); the path where the file will be saved in the Firebase storage; and finally, the mime type.

*we were not able to make this example work with the ESP32-CAM + microSD card. If anyone knows a solution, please share.

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 attempt to take a picture and will send it to Firebase Storage.

ESP32-CAM Picture Uploaded to Firebase Storage Demonstration Serial Monitor

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 on 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 (we’ll do this in a future tutorial).

We’ve shown a simple example about 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.



Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Build Web Server projects with the ESP32 and ESP8266 boards to control outputs and monitor sensors remotely. Learn HTML, CSS, JavaScript and client-server communication protocols DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

101 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?

  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
  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
  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
    • Hi Manuel.
      I also tried using SD card instead of SPIFFS and I also was not able to make it work.
      I’m not sure what might be wrong. But I haven’t investigated further.
      Regards,
      Sara

      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

Leave a Reply to [email protected] Cancel reply

Download our Free eBooks and Resources

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