Learn how to build a web server with the ESP32-CAM board that allows you to send a command to take a photo and visualize the latest captured photo in your browser saved in SPIFFS. We also added the option to rotate the image if necessary.

We have other ESP32-CAM projects in our blog that you might like. In fact you can take this project further, by adding a PIR sensor to take a photo when motion is detected, a physical pushbutton to take a photo, or also include video streaming capabilities in another URL path.

Watch the Video Demonstration

Watch the following video demonstration to see what you’re going to build throughout this tutorial.

Parts Required

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Project Overview

The following image shows the web server we’ll build in this tutorial.

When you access the web server, you’ll see three buttons:

Note: as mentioned previously the latest photo captured is stored in the ESP32 SPIFFS, so even if you restart your board, you can always access the last saved photo.

Installing the ESP32 add-on

We’ll program the ESP32 board using Arduino IDE. So, you need the Arduino IDE installed as well as the ESP32 add-on:

To build the web server, we’ll use the ESPAsyncWebServer library. This library also requires the AsyncTCP Library to work properly. Follow the next steps to install those libraries.

Installing the ESPAsyncWebServer library

Follow the next steps to install the ESPAsyncWebServer library:

Alternatively, after downloading the library, you can go to Sketch > Include Library > Add .ZIP library… and select the library you’ve just downloaded.

Installing the Async TCP Library for ESP32

The ESPAsyncWebServer library requires the AsyncTCP library to work. Follow the next steps to install that library:

Alternatively, after downloading the library, you can go to Sketch > Include Library > Add .ZIP library… and select the library you’ve just downloaded.

ESP32-CAM Take and Display Photo Web Server Sketch

Copy the following code to your Arduino IDE. This code builds a web server that allows you to take a photo with your ESP32-CAM and display the last photo taken. Depending on the orientation of your ESP32-CAM, you may want to rotate the picture, so we also included that feature.

How the Code Works

First, include the required libraries to work with the camera, to build the web server and to use SPIFFS.

Next, write your network credentials in the following variables, so that the ESP32-CAM can connect to your local network.

Create an AsyncWebServer object on port 80.

The takeNewPhoto boolean variable indicates when it’s time to take a new photo.

Then, define the path and name of the photo to be saved in SPIFFS.

Next, define the camera pins for the ESP32-CAM AI THINKER module.

Building the Web Page

Next, we have the HTML to build the web page:

We won’t go into much detail on how this HTML works. We’ll just take a quick overview.

Basically, create three buttons: ROTATE; CAPTURE PHOTO and REFRESH PAGE. Each photo calls a different JavaScript function: rotatePhoto(), capturePhoto() and reload().

The capturePhoto() function sends a request on the /capture URL to the ESP32, so it takes a new photo.

The rotatePhoto() function rotates the photo.

We’re not sure what’s the “best” way to rotate a photo with JavaScript. This method works perfectly, but there may be better ways to do this. If you have any suggestion please share with us.

Finally, the following section displays the photo.

When, you click the REFRESH button, it will load the latest image.

setup()

In the setup(), initialize a Serial communication:

Connect the ESP32-CAM to your local network:

Initialize SPIFFS:

Print the ESP32-CAM local IP address:

The lines that follow, configure and initialize the camera with the right settings.

Handle the Web Server

Next, we need to handle what happens when the ESP32-CAM receives a request on a URL.

When the ESP32-CAM receives a request on the root / URL, we send the HTML text to build the web page.

When we press the “CAPTURE” button on the web server, we send a request to the ESP32 /capture URL. When that happens, we set the takeNewPhoto variable to true, so that we know it is time to take a new photo.

In case there’s a request on the /saved-photo URL, send the photo saved in SPIFFS to a connected client:

Finally, start the web server.

In the loop(), if the takeNewPhoto variable is True, we call the capturePhotoSaveSpiffs() to take a new photo and save it to SPIFFS. Then, set the takeNewPhoto variable to false.

Take a Photo

There are two other functions in the sketch: checkPhoto() and capturePhotoSaveSpiffs().

The checkPhoto() function checks if the photo was successfully saved to SPIFFS.

The capturePhotoSaveSpiffs() function takes a photo and saves it to SPIFFS.

This function was based on this sketch by dualvim.

ESP32-CAM Upload Code

To upload code to the ESP32-CAM board, connect it to your computer using an FTDI programmer. Follow the next schematic diagram:

Important: GPIO 0 needs to be connected to GND so that you’re able to upload code.

To upload the code, follow the next steps:

Important: if you can’t upload the code, double-check that GPIO 0 is connected to GND and that the RX and TX connections are correct. Check if you’ve selected the right settings in the Tools menu. You should also press the on-board Reset button to restart your ESP32 in flashing mode.

Open your browser and type the ESP32-CAM IP Address. Then, click the “CAPTURE PHOTO” to take a new photo and wait a few seconds for the photo to be saved in SPIFFS.

Then, if you press the “REFRESH PAGE” button, the page will update with the latest saved photo. If you need to adjust the image orientation, you can always use the “ROTATE” button to do it so.

In your Arduino IDE Serial Monitor window, you should see similar messages:

If you’re getting any of the following errors, read our ESP32-CAM Troubleshooting Guide: Most Common Problems Fixed

Wrapping Up

We hope you’ve found this example useful. We’ve tried to keep it as simple as possible so it is easy for you to modify and include it in your own projects. You can combine this example with the ESP32-CAM PIR Motion Detector with Photo Capture to capture and display a new photo when motion is detected.

For more ESP32-CAM projects you can subscribe to our newsletter. If you don’t have an ESP32-CAM yet, you can get one for approximately $6.

If there is any project you’d like to see with the ESP32-CAM or if you’d like to share your project with us, write a comment below.

Learn more about the ESP32 with our “Learn ESP32 with Arduino IDE” course or check our free ESP32 resources.

Thank you for reading.

This content was originally published here.