#include "esp_camera.
h"
#include <WiFi.h>
#include <WebServer.h>
// Replace with your network credentials
const char* ssid = "OnePlus Nord CE3 5G";
const char* password = "chetan145";
// Use AI Thinker pin configuration
#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
WebServer server(80);
// Store captured image
camera_fb_t* last_fb = nullptr;
unsigned long lastCaptureTime = 0;
void startCameraServer();
void setup() {
Serial.begin(115200);
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.frame_size = FRAMESIZE_QVGA;
config.pixel_format = PIXFORMAT_JPEG;
config.fb_location = CAMERA_FB_IN_PSRAM;
config.jpeg_quality = 10;
config.fb_count = 2;
// Initialize camera
if (esp_camera_init(&config) != ESP_OK) {
Serial.println("Camera init failed");
return;
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
Serial.println("\nWiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
startCameraServer();
void loop() {
server.handleClient();
// Capture every 10 seconds
if (millis() - lastCaptureTime >= 10000) {
lastCaptureTime = millis();
if (last_fb) {
esp_camera_fb_return(last_fb);
last_fb = nullptr;
last_fb = esp_camera_fb_get();
if (!last_fb) {
Serial.println("Failed to capture image");
} else {
Serial.println("Image captured");
}
void handleRoot() {
String html = "<!DOCTYPE html><html><head>";
html += "<meta http-equiv='refresh' content='10'>"; // Refresh every 10s
html += "<style>img { max-width: 100%; height: auto; }</style>";
html += "</head><body><h2>ESP32-CAM Auto Capture</h2>";
html += "<p>Auto-refreshes every 10 seconds. Below is the latest image:</p>";
html += "<img src='/image' alt='Captured Image'>";
html += "</body></html>";
server.send(200, "text/html", html);
void handleImage() {
if (!last_fb) {
server.send(503, "text/plain", "No image available");
return;
WiFiClient client = server.client();
server.sendHeader("Content-Type", "image/jpeg");
server.sendHeader("Content-Disposition", "inline; filename=capture.jpg");
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1");
server.setContentLength(last_fb->len);
server.send(200);
client.write((const char*)last_fb->buf, last_fb->len);
void startCameraServer() {
server.on("/", HTTP_GET, handleRoot);
server.on("/image", HTTP_GET, handleImage);
server.begin();
Serial.println("Web server started");