7. Chapter Camera Web Server

In this section, we’ll use ESP32-S3’s video function as an example to study.

7.1. Project Camera Web Server

Connect ESP32-S3 using USB and check its IP address through serial monitor. Use web page to access IP address to obtain video and image data.

7.1.1. Component List

ESP32-S3 WROOM x1

Chapter32_00

USB cable x1

Chapter00_01

7.1.2. Circuit

Connect Freenove ESP32-S3 to the computer using the USB cable.

../../../_images/Chapter32_01.png

7.1.3. Sketch

7.1.3.1. Sketch_As_CameraWebServer

../../../_images/Chapter32_02.png

Before running the program, please modify your router’s name and password in the box shown in the illustration above to make sure that your Sketch can compile and work successfully.

Compile and upload codes to ESP32-S3, open the serial monitor and set the baud rate to 115200, and the serial monitor will print out a network link address.

../../../_images/Chapter32_03.png

If your ESP32-S3 has been in the process of connecting to router, but the information above has not been printed out, please re-check whether the router name and password have been entered correctly and press the reset key on ESP32-S3 WROOM to wait for a successful connection prompt.

Open a web browser, enter the IP address printed by the serial monitor in the address bar, and access it.

Taking the Google browser as an example, here’s what the browser prints out after successful access to ESP32-S3’s IP.

../../../_images/Chapter32_04.png

Click on Start Stream. The effect is shown in the image below.

../../../_images/Chapter32_05.png

Note

If sketch compilation fails due to ESP32-S3 support package, follow the steps of the image to open the CameraWebServer. This sketch is the same as described in the tutorial above.

../../../_images/Chapter32_06.png

The following is the main program code. You need include other code files in the same folder when write your own code.

  1/**********************************************************************
  2  Filename    : Camera Web Server
  3  Description : The camera images captured by the ESP32S3 are displayed on the web page.
  4  Auther      : www.freenove.com
  5  Modification: 2026/05/16
  6**********************************************************************/
  7#include "esp_camera.h"
  8#include <WiFi.h>
  9#include "board_config.h"
 10// ===================
 11// Select camera model
 12// ===================
 13//#define CAMERA_MODEL_WROVER_KIT // Has PSRAM
 14//#define CAMERA_MODEL_ESP_EYE // Has PSRAM
 15#define CAMERA_MODEL_ESP32S3_EYE // Has PSRAM
 16//#define CAMERA_MODEL_M5STACK_PSRAM // Has PSRAM
 17//#define CAMERA_MODEL_M5STACK_V2_PSRAM // M5Camera version B Has PSRAM
 18//#define CAMERA_MODEL_M5STACK_WIDE // Has PSRAM
 19//#define CAMERA_MODEL_M5STACK_ESP32CAM // No PSRAM
 20//#define CAMERA_MODEL_M5STACK_UNITCAM // No PSRAM
 21//#define CAMERA_MODEL_AI_THINKER // Has PSRAM
 22//#define CAMERA_MODEL_TTGO_T_JOURNAL // No PSRAM
 23// ** Espressif Internal Boards **
 24//#define CAMERA_MODEL_ESP32_CAM_BOARD
 25//#define CAMERA_MODEL_ESP32S2_CAM_BOARD
 26//#define CAMERA_MODEL_ESP32S3_CAM_LCD
 27
 28#include "camera_pins.h"
 29
 30// ===========================
 31// Enter your WiFi credentials
 32// ===========================
 33const char* ssid     = "********";
 34const char* password = "********";
 35camera_config_t config;
 36
 37void startCameraServer();
 38void camera_init();
 39
 40void setup() {
 41  Serial.begin(115200);
 42  Serial.setDebugOutput(true);
 43  Serial.println();
 44
 45  camera_init();
 46
 47  WiFi.begin(ssid, password);
 48  WiFi.setSleep(false);
 49
 50  while (WiFi.status() != WL_CONNECTED) {
 51    delay(500);
 52    Serial.print(".");
 53  }
 54  while (WiFi.STA.hasIP() != true) {
 55    Serial.print(".");
 56    delay(500);
 57  }
 58  Serial.println("");
 59  Serial.println("WiFi connected");
 60
 61  startCameraServer();
 62
 63  Serial.print("Camera Ready! Use 'http://");
 64  Serial.print(WiFi.localIP());
 65  Serial.println("' to connect");
 66}
 67
 68void loop() {
 69  // Do nothing. Everything is done in another task by the web server
 70  delay(10000);
 71}
 72
 73void camera_init() {
 74  config.ledc_channel = LEDC_CHANNEL_0;
 75  config.ledc_timer = LEDC_TIMER_0;
 76  config.pin_d0 = Y2_GPIO_NUM;
 77  config.pin_d1 = Y3_GPIO_NUM;
 78  config.pin_d2 = Y4_GPIO_NUM;
 79  config.pin_d3 = Y5_GPIO_NUM;
 80  config.pin_d4 = Y6_GPIO_NUM;
 81  config.pin_d5 = Y7_GPIO_NUM;
 82  config.pin_d6 = Y8_GPIO_NUM;
 83  config.pin_d7 = Y9_GPIO_NUM;
 84  config.pin_xclk = XCLK_GPIO_NUM;
 85  config.pin_pclk = PCLK_GPIO_NUM;
 86  config.pin_vsync = VSYNC_GPIO_NUM;
 87  config.pin_href = HREF_GPIO_NUM;
 88  config.pin_sccb_sda = SIOD_GPIO_NUM;
 89  config.pin_sccb_scl = SIOC_GPIO_NUM;
 90  config.pin_pwdn = PWDN_GPIO_NUM;
 91  config.pin_reset = RESET_GPIO_NUM;
 92  config.xclk_freq_hz = 10000000;
 93  config.frame_size = FRAMESIZE_QVGA;
 94  config.pixel_format = PIXFORMAT_JPEG; // for streaming
 95  config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
 96  config.fb_location = CAMERA_FB_IN_PSRAM;
 97  config.jpeg_quality = 10;
 98  config.fb_count = 1;
 99
100  // camera init
101  esp_err_t err = esp_camera_init(&config);
102  if (err != ESP_OK) {
103    if(err==ESP_ERR_NOT_SUPPORTED){
104      config.pixel_format = PIXFORMAT_RGB565;
105      esp_err_t err = esp_camera_init(&config);
106      if (err != ESP_OK) {
107        Serial.printf("Camera init failed with error 0x%x", err);
108        return;
109      }
110    }
111  }
112
113  sensor_t * s = esp_camera_sensor_get();
114  // drop down frame size for higher initial frame rate
115  uint16_t pid = s->id.PID;
116  if(pid == OV2640_PID){
117    s->set_hmirror(s, 1);
118    s->set_vflip(s, 1);     
119  }
120  else if(pid == OV3660_PID){
121    s->set_hmirror(s, 1);
122    s->set_vflip(s, 0);     
123  }
124  else if(pid == GC2145_PID){
125    s->set_hmirror(s, 0);
126    delay(500);
127    s->set_vflip(s, 0);      
128  }
129  else if(pid == GC0308_PID){
130    s->set_hmirror(s, 0);
131    delay(500);
132    s->set_vflip(s, 0);     
133  }
134  else{
135    s->set_hmirror(s, 1);
136    s->set_vflip(s, 0);       
137  }
138  s->set_brightness(s, 1);  // Slightly increase brightness
139  s->set_saturation(s, 0);  // Reduce saturation
140  s->set_ae_level(s, -3);   // Set exposure compensation level
141}

Add procedure files and API interface files related to ESP32-S3 camera.

#include "esp_camera.h"
#include <WiFi.h>

#define CAMERA_MODEL_ESP32S3_EYE // Has PSRAM

#include "camera_pins.h"

Enter the name and password of the router

1const char* ssid     = "********";
2const char* password = "********";

Initialize serial port, set baud rate to 115200; open the debug and output function of the serial.

1
2void setup() {
3  Serial.begin(115200);

Configure parameters including interface pins of the camera. Note: It is generally not recommended to change them.

 1  Serial.println();
 2
 3  camera_init();
 4
 5  WiFi.begin(ssid, password);
 6  WiFi.setSleep(false);
 7
 8  while (WiFi.status() != WL_CONNECTED) {
 9    delay(500);
10    Serial.print(".");
11  }
12  while (WiFi.STA.hasIP() != true) {
13    Serial.print(".");
14    delay(500);
15  }
16  Serial.println("");
17  Serial.println("WiFi connected");
18
19  startCameraServer();
20
21  Serial.print("Camera Ready! Use 'http://");
22  Serial.print(WiFi.localIP());
23  Serial.println("' to connect");
24}
25
26void loop() {

ESP32-S3 connects to the router and prints a successful connection prompt. If it has not been successfully connected, press the reset key on the ESP32-S3 WROOM.

 1config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
 2config.fb_location = CAMERA_FB_IN_PSRAM;
 3config.jpeg_quality = 10;
 4config.fb_count = 1;
 5
 6// camera init
 7esp_err_t err = esp_camera_init(&config);
 8if (err != ESP_OK) {
 9  if(err==ESP_ERR_NOT_SUPPORTED){
10    config.pixel_format = PIXFORMAT_RGB565;
11    esp_err_t err = esp_camera_init(&config);
12    if (err != ESP_OK) {
13      Serial.printf("Camera init failed with error 0x%x", err);

Open the video streams server function of the camera and print its IP address via serial port.

1    }
2  }
3}
4
5sensor_t * s = esp_camera_sensor_get();

Configure the display image information of the camera.

The set_vflip() function sets whether the image is flipped 180°, with 0 for no flip and 1 for flip 180°.

The set_brightness() function sets the brightness of the image, with values ranging from -2 to 2.

The set_saturation() function sets the color saturation of the image, with values ranging from -2 to 2.

1config.pin_sccb_scl = SIOC_GPIO_NUM;
2config.pin_pwdn = PWDN_GPIO_NUM;
3config.pin_reset = RESET_GPIO_NUM;
4config.xclk_freq_hz = 10000000;
5config.frame_size = FRAMESIZE_QVGA;

Modify the resolution and sharpness of the images captured by the camera. The sharpness ranges from 10 to 63, and the smaller the number, the sharper the picture. The larger the number, the blurrier the picture. Please refer to the table below.

config.frame_size = FRAMESIZE_VGA;
config.jpeg_quality = 10;

7.1.3.2. Reference

Image resolution

Sharpness

Image resolution

Sharpness

FRAMESIZE_96x96

96x96

FRAMESIZE_HVGA

480x320

FRAMESIZE_QQVGA

160x120

FRAMESIZE_VGA

640x480

FRAMESIZE_QCIF

176x144

FRAMESIZE_SVGA

800x600

FRAMESIZE_HQVGA

240x176

FRAMESIZE_XGA

1024x768

FRAMESIZE_240x240

240x240

FRAMESIZE_HD

1280x720

FRAMESIZE_QVGA

320x240

FRAMESIZE_SXGA

1280x1024

FRAMESIZE_CIF

400x296

FRAMESIZE_UXGA

1600x1200

We recommend that the resolution not exceed VGA(640x480).

7.2. Project Video Web Server

Connect to ESP32-S3 using USB and view its IP address through a serial monitor. Access IP addresses through web pages to obtain real-time video data.

7.2.1. Component List

ESP32-S3 WROOM x1

USB cable x1

SDcard x1

Chapter00_00

Chapter00_01

Chapter28_01

7.2.2. Circuit

Connect Freenove ESP32-S3 to the computer using the USB cable.

../../../_images/Chapter32_01.png

7.2.3. Sketch

7.2.3.1. Sketch_As_VideoWebServer

../../../_images/Chapter32_07.png

Before running the program, please modify your router’s name and password in the box shown in the illustration above to make sure that your Sketch can compile and work successfully.

Compile and upload codes to ESP32-S3, open the serial monitor and set the baud rate to 115200, and the serial monitor will print out a network link address.

../../../_images/Chapter32_08.png

If your ESP32-S3 has been in the process of connecting to router, but the information above has not been printed out, please re-check whether the router name and password have been entered correctly and press the reset key on ESP32-S3 WROOM to wait for a successful connection prompt.

Open a web browser, enter the IP address printed by the serial monitor in the address bar, and access it.

Taking the Google browser as an example, here’s what the browser prints out after successful access to ESP32-S3’s IP.

The effect is shown in the image below.

../../../_images/Chapter32_09.png

The following is the main program code. You need include other code files in the same folder when write your own code.

  1/**********************************************************************
  2  Filename    : Video Web Server
  3  Description : The camera images captured by the ESP32S3 are displayed on the web page.
  4  Auther      : www.freenove.com
  5  Modification: 2026/05/16
  6**********************************************************************/
  7#include "esp_camera.h"
  8#include <WiFi.h>
  9#include "sd_read_write.h"
 10
 11// Select camera model
 12#define CAMERA_MODEL_ESP32S3_EYE // Has PSRAM
 13
 14#include "camera_pins.h"
 15
 16const char* ssid     = "********";   //input your wifi name
 17const char* password = "********";   //input your wifi passwords
 18camera_config_t config;
 19
 20void cameraInit(void);
 21void startCameraServer();
 22
 23void setup() {
 24  Serial.begin(115200);
 25  Serial.setDebugOutput(true);
 26  Serial.println();
 27
 28  cameraInit();
 29  sdmmcInit();
 30  removeDir(SD_MMC, "/video");
 31  createDir(SD_MMC, "/video");
 32
 33  WiFi.begin(ssid, password);
 34
 35  while (WiFi.status() != WL_CONNECTED) {
 36    delay(500);
 37    Serial.print(".");
 38  }
 39  Serial.println("");
 40  Serial.println("WiFi connected");
 41
 42  startCameraServer();
 43
 44  Serial.print("Camera Ready! Use 'http://");
 45  Serial.print(WiFi.localIP());
 46  Serial.println("' to connect");
 47}
 48
 49void loop() {
 50  // put your main code here, to run repeatedly:
 51  delay(10000);
 52}
 53
 54void cameraInit(void){
 55  config.ledc_channel = LEDC_CHANNEL_0;
 56  config.ledc_timer = LEDC_TIMER_0;
 57  config.pin_d0 = Y2_GPIO_NUM;
 58  config.pin_d1 = Y3_GPIO_NUM;
 59  config.pin_d2 = Y4_GPIO_NUM;
 60  config.pin_d3 = Y5_GPIO_NUM;
 61  config.pin_d4 = Y6_GPIO_NUM;
 62  config.pin_d5 = Y7_GPIO_NUM;
 63  config.pin_d6 = Y8_GPIO_NUM;
 64  config.pin_d7 = Y9_GPIO_NUM;
 65  config.pin_xclk = XCLK_GPIO_NUM;
 66  config.pin_pclk = PCLK_GPIO_NUM;
 67  config.pin_vsync = VSYNC_GPIO_NUM;
 68  config.pin_href = HREF_GPIO_NUM;
 69  config.pin_sccb_sda = SIOD_GPIO_NUM;
 70  config.pin_sccb_scl = SIOC_GPIO_NUM;
 71  config.pin_pwdn = PWDN_GPIO_NUM;
 72  config.pin_reset = RESET_GPIO_NUM;
 73  config.xclk_freq_hz = 10000000;
 74  config.frame_size = FRAMESIZE_QVGA;
 75  config.pixel_format = PIXFORMAT_JPEG; // for streaming
 76  config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
 77  config.fb_location = CAMERA_FB_IN_PSRAM;
 78  config.jpeg_quality = 10;
 79  config.fb_count = 1;
 80
 81  // camera init
 82  esp_err_t err = esp_camera_init(&config);
 83  if (err != ESP_OK) {
 84    if(err==ESP_ERR_NOT_SUPPORTED){
 85      config.pixel_format = PIXFORMAT_RGB565;
 86      esp_err_t err = esp_camera_init(&config);
 87      if (err != ESP_OK) {
 88        Serial.printf("Camera init failed with error 0x%x", err);
 89        return;
 90      }
 91    }
 92  }
 93
 94  sensor_t * s = esp_camera_sensor_get();
 95  // drop down frame size for higher initial frame rate
 96  uint16_t pid = s->id.PID;
 97  if(pid == OV2640_PID){
 98    s->set_hmirror(s, 1);
 99    s->set_vflip(s, 1);     
100  }
101  else if(pid == OV3660_PID){
102    s->set_hmirror(s, 1);
103    s->set_vflip(s, 0);     
104  }
105  else if(pid == GC2145_PID){
106    s->set_hmirror(s, 0);
107    delay(500);
108    s->set_vflip(s, 0);      
109  }
110  else if(pid == GC0308_PID){
111    s->set_hmirror(s, 0);
112    delay(500);
113    s->set_vflip(s, 0);     
114  }
115  else{
116    s->set_hmirror(s, 1);
117    s->set_vflip(s, 0);       
118  }
119  s->set_brightness(s, 1);  // Slightly increase brightness
120  s->set_saturation(s, 0);  // Reduce saturation
121  s->set_ae_level(s, -3);   // Set exposure compensation level
122}

Configure parameters including interface pins of the camera. Note: It is generally not recommended to change them.

 1void cameraInit(void){
 2  config.ledc_channel = LEDC_CHANNEL_0;
 3  config.ledc_timer = LEDC_TIMER_0;
 4  config.pin_d0 = Y2_GPIO_NUM;
 5  config.pin_d1 = Y3_GPIO_NUM;
 6  config.pin_d2 = Y4_GPIO_NUM;
 7  config.pin_d3 = Y5_GPIO_NUM;
 8  config.pin_d4 = Y6_GPIO_NUM;
 9  config.pin_d5 = Y7_GPIO_NUM;
10  config.pin_d6 = Y8_GPIO_NUM;
11  config.pin_d7 = Y9_GPIO_NUM;
12  config.pin_xclk = XCLK_GPIO_NUM;
13  config.pin_pclk = PCLK_GPIO_NUM;
14  config.pin_vsync = VSYNC_GPIO_NUM;
15  config.pin_href = HREF_GPIO_NUM;
16  config.pin_sccb_sda = SIOD_GPIO_NUM;
17  config.pin_sccb_scl = SIOC_GPIO_NUM;
18  config.pin_pwdn = PWDN_GPIO_NUM;
19  config.pin_reset = RESET_GPIO_NUM;
20  config.xclk_freq_hz = 10000000;
21  config.frame_size = FRAMESIZE_QVGA;
22  config.pixel_format = PIXFORMAT_JPEG; // for streaming
23  config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
24  config.fb_location = CAMERA_FB_IN_PSRAM;
25  config.jpeg_quality = 10;
26  config.fb_count = 1;

ESP32-S3 connects to the router and prints a successful connection prompt. If it has not been successfully connected, press the reset key on the ESP32-S3 WROOM.

1
2WiFi.begin(ssid, password);
3
4while (WiFi.status() != WL_CONNECTED) {
5  delay(500);
6  Serial.print(".");
7}
8Serial.println("");

Open the video streams server function of the camera and print its IP address via serial port.

1
2startCameraServer();
3
4Serial.print("Camera Ready! Use 'http://");
5Serial.print(WiFi.localIP());

7.3. Project Camera and SDcard

In this chapter, we continue to use the camera and SD card. We will use the onboard button as the shutter. When the button is pressed, the ESP32-S3 takes a photo and stores the photo in the SD folder.

7.3.1. Component List

ESP32-S3 WROOM x1

USB cable x1

SDcard x1

Chapter00_00

Chapter00_01

Chapter28_01

7.3.2. Circuit

Connect Freenove ESP32-S3 to the computer using the USB cable.

../../../_images/Chapter32_01.png

7.3.3. Sketch

This code uses a library named “Freenove_WS2812_Lib_for_ESP32”, if you have not installed it, please do so first.

Library is an important feature of the open source world, and we know that Arduino is an open source platform that everyone can contribute to. Libraries are generally licensed under the LGPL, which means you can use them for free to apply to your creations.

7.3.3.1. How to install the library

There are two ways to add libraries.

The first way, open the Arduino IDE, click Sketch -> Include Library -> Manager Libraries.

../../../_images/Chapter32_10.png

In the pop-up window, Library Manager, search for the name of the Library, “Freenove WS2812 Lib for ESP32”. Then click Install.

../../../_images/Chapter32_11.png

The second way, open Arduino IDE, click Sketch->Include Library->Add .ZIP Library, In the pop-up window, find the file named “./Libraries/Freenove_WS2812_Lib_for_ESP32.Zip” which locates in this directory, and click OPEN.

../../../_images/Chapter32_12.png

7.3.3.2. Sketch_Camera_SDcard

../../../_images/Chapter32_13.png

Compile and upload the code to the ESP32-S3.

If your camera is not installed properly, causing the camera to fail to initialize, or you have not inserted the SD card into the ESP32-S3 in advance, the on-board colored lights will turn on red as a reminder. If all is well, the onboard colored light will light up green. When the onboard BOOT button is pressed, the ESP32-S3 will capture the current camera image and save it in the “Camera” folder of the SD card. At the same time, the onboard LED lights up blue, and returns to green after taking a photo.

../../../_images/Chapter32_14.png

As shown in the image below, after uploading the code to the ESP32-S3, the ESP32-S3 will automatically create a folder named “camera” in the SD card. Every time the BOOT button is pressed, the on-board colored light turns on blue, and ESP32-S3 collects a photo information and stores it in the “camera” folder. Press the button once to take a photo.

When we press the RST button to reset the ESP32-S3, we can see that there are some photo files in the SD card folder. These photos you can read directly through the card reader.

../../../_images/Chapter32_15.png

The following is the main program code. You need include other code files in the same folder when write your own code.

  1/**********************************************************************
  2  Filename    : Camera and SDcard
  3  Description : Use the onboard buttons to take photo and save them to an SD card.
  4  Auther      : www.freenove.com
  5  Modification: 2026/05/16
  6**********************************************************************/
  7#include "esp_camera.h"
  8#define CAMERA_MODEL_ESP32S3_EYE
  9#include "camera_pins.h"
 10#include "ws2812.h"
 11#include "sd_read_write.h"
 12
 13#define BUTTON_PIN  0
 14camera_config_t config;
 15
 16void setup() {
 17  Serial.begin(115200);
 18  Serial.setDebugOutput(false);
 19  Serial.println();
 20  pinMode(BUTTON_PIN, INPUT_PULLUP);
 21  ws2812Init();
 22  sdmmcInit();
 23  //removeDir(SD_MMC, "/camera");
 24  createDir(SD_MMC, "/camera");
 25  listDir(SD_MMC, "/camera", 0);
 26  if(cameraSetup()==1){
 27    ws2812SetColor(2);
 28  }
 29  else{
 30    ws2812SetColor(1);
 31    return;
 32  }
 33}
 34
 35void loop() {
 36  if(digitalRead(BUTTON_PIN)==LOW){
 37    delay(20);
 38    if(digitalRead(BUTTON_PIN)==LOW){
 39	  	ws2812SetColor(3);
 40      while(digitalRead(BUTTON_PIN)==LOW);
 41      camera_fb_t * fb = esp_camera_fb_get();
 42      if (fb != NULL) {
 43        int photo_index = readFileNum(SD_MMC, "/camera");
 44        if(photo_index != -1) {
 45          String path = "/camera/" + String(photo_index) + ".jpg";
 46
 47          if (fb->format == PIXFORMAT_JPEG) {
 48            writejpg(SD_MMC, path.c_str(), fb->buf, fb->len);
 49            Serial.println("Direct JPEG save.");
 50          } 
 51          else {
 52            uint8_t *jpg_buf = NULL;
 53            size_t jpg_len = 0;
 54
 55            if (frame2jpg(fb, 80, &jpg_buf, &jpg_len)) {
 56              writejpg(SD_MMC, path.c_str(), jpg_buf, jpg_len);
 57              free(jpg_buf);
 58            } else {
 59              Serial.println("JPEG compression failed!");
 60            }
 61          }
 62        }
 63        esp_camera_fb_return(fb);
 64      }
 65      else {
 66        Serial.println("Camera capture failed.");
 67      }
 68      ws2812SetColor(2);
 69    }
 70  }
 71}
 72
 73int cameraSetup(void) {
 74  config.ledc_channel = LEDC_CHANNEL_0;
 75  config.ledc_timer = LEDC_TIMER_0;
 76  config.pin_d0 = Y2_GPIO_NUM;
 77  config.pin_d1 = Y3_GPIO_NUM;
 78  config.pin_d2 = Y4_GPIO_NUM;
 79  config.pin_d3 = Y5_GPIO_NUM;
 80  config.pin_d4 = Y6_GPIO_NUM;
 81  config.pin_d5 = Y7_GPIO_NUM;
 82  config.pin_d6 = Y8_GPIO_NUM;
 83  config.pin_d7 = Y9_GPIO_NUM;
 84  config.pin_xclk = XCLK_GPIO_NUM;
 85  config.pin_pclk = PCLK_GPIO_NUM;
 86  config.pin_vsync = VSYNC_GPIO_NUM;
 87  config.pin_href = HREF_GPIO_NUM;
 88  config.pin_sccb_sda = SIOD_GPIO_NUM;
 89  config.pin_sccb_scl = SIOC_GPIO_NUM;
 90  config.pin_pwdn = PWDN_GPIO_NUM;
 91  config.pin_reset = RESET_GPIO_NUM;
 92  config.xclk_freq_hz = 10000000;
 93  config.frame_size = FRAMESIZE_QVGA;
 94  config.pixel_format = PIXFORMAT_JPEG; // for streaming
 95  config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
 96  config.fb_location = CAMERA_FB_IN_PSRAM;
 97  config.jpeg_quality = 10;
 98  config.fb_count = 1;
 99
100  // camera init
101  esp_err_t err = esp_camera_init(&config);
102  if (err != ESP_OK) {
103    if(err==ESP_ERR_NOT_SUPPORTED){
104      config.pixel_format = PIXFORMAT_RGB565;
105      esp_err_t err = esp_camera_init(&config);
106      if (err != ESP_OK) {
107        Serial.printf("Camera init failed with error 0x%x", err);
108        return 0;
109      }
110    }
111  }
112
113  sensor_t * s = esp_camera_sensor_get();
114  // drop down frame size for higher initial frame rate
115  uint16_t pid = s->id.PID;
116  if(pid == OV2640_PID){
117    s->set_hmirror(s, 1);
118    s->set_vflip(s, 1);     
119  }
120  else if(pid == OV3660_PID){
121    s->set_hmirror(s, 1);
122    s->set_vflip(s, 0);     
123  }
124  else if(pid == GC2145_PID){
125    s->set_hmirror(s, 0);
126    delay(500);
127    s->set_vflip(s, 0);      
128  }
129  else if(pid == GC0308_PID){
130    s->set_hmirror(s, 0);
131    delay(500);
132    s->set_vflip(s, 0);     
133  }
134  else{
135    s->set_hmirror(s, 1);
136    s->set_vflip(s, 0);       
137  }
138  s->set_brightness(s, 1);  // Slightly increase brightness
139  s->set_saturation(s, 0);  // Reduce saturation
140  s->set_ae_level(s, -3);   // Set exposure compensation level
141  return 1;
142}

Configure camera parameters, including camera interface pins and other information. Altering them is generally not recommended. Returns 1 if the camera is initialized successfully, and returns 0 if it fails.

 1              Serial.println("JPEG compression failed!");
 2            }
 3          }
 4        }
 5        esp_camera_fb_return(fb);
 6      }
 7      else {
 8        Serial.println("Camera capture failed.");
 9      }
10      ws2812SetColor(2);
11    }
12  }
13}
14
15int cameraSetup(void) {
16  config.ledc_channel = LEDC_CHANNEL_0;
17  config.ledc_timer = LEDC_TIMER_0;
18  config.pin_d0 = Y2_GPIO_NUM;
19  config.pin_d1 = Y3_GPIO_NUM;
20  config.pin_d2 = Y4_GPIO_NUM;
21  config.pin_d3 = Y5_GPIO_NUM;
22  config.pin_d4 = Y6_GPIO_NUM;
23  config.pin_d5 = Y7_GPIO_NUM;
24  config.pin_d6 = Y8_GPIO_NUM;
25  config.pin_d7 = Y9_GPIO_NUM;
26  config.pin_xclk = XCLK_GPIO_NUM;
27  config.pin_pclk = PCLK_GPIO_NUM;
28  config.pin_vsync = VSYNC_GPIO_NUM;
29  config.pin_href = HREF_GPIO_NUM;
30  config.pin_sccb_sda = SIOD_GPIO_NUM;
31  config.pin_sccb_scl = SIOC_GPIO_NUM;
32  config.pin_pwdn = PWDN_GPIO_NUM;
33  config.pin_reset = RESET_GPIO_NUM;
34  config.xclk_freq_hz = 10000000;
35  config.frame_size = FRAMESIZE_QVGA;
36  config.pixel_format = PIXFORMAT_JPEG; // for streaming
37  config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
38  config.fb_location = CAMERA_FB_IN_PSRAM;
39  config.jpeg_quality = 10;
40  config.fb_count = 1;
41
42  // camera init
43  esp_err_t err = esp_camera_init(&config);
44  if (err != ESP_OK) {
45    if(err==ESP_ERR_NOT_SUPPORTED){
46      config.pixel_format = PIXFORMAT_RGB565;
47      esp_err_t err = esp_camera_init(&config);
48      if (err != ESP_OK) {
49        Serial.printf("Camera init failed with error 0x%x", err);
50        return 0;
51      }
52    }
53  }
54
55  sensor_t * s = esp_camera_sensor_get();
56  // drop down frame size for higher initial frame rate

Initialize the serial port, buttons, lights and SD card.

1void setup() {
2  Serial.begin(115200);
3  Serial.setDebugOutput(false);
4  Serial.println();
5  pinMode(BUTTON_PIN, INPUT_PULLUP);
6  ws2812Init();

Call ws2812SetColor() to set the color of the LED. When the parameter is 0, the LED is turned off, when the parameter is 1, the red light is displayed, when the parameter is 2, the green light is displayed, and when the parameter is 3, the blue light is displayed.

1if(cameraSetup()==1){

Get the camera data once, then read the file number in the camera folder of the SD card, and create a new file based on this, write the camera data into it, and finally return the camera structure pointer. If the camera data cannot be obtained, the prompt information will be printed directly.

 1while(digitalRead(BUTTON_PIN)==LOW);
 2camera_fb_t * fb = esp_camera_fb_get();
 3if (fb != NULL) {
 4  int photo_index = readFileNum(SD_MMC, "/camera");
 5  if(photo_index != -1) {
 6    String path = "/camera/" + String(photo_index) + ".jpg";
 7
 8    if (fb->format == PIXFORMAT_JPEG) {
 9      writejpg(SD_MMC, path.c_str(), fb->buf, fb->len);
10      Serial.println("Direct JPEG save.");
11    } 
12    else {
13      uint8_t *jpg_buf = NULL;
14      size_t jpg_len = 0;