#include "esp_camera.h"
#include
// Camera pin configuration (standard for ESP32-CAM)
#define CAMERA_MODEL_AI_THINKER // Define camera model
#include "camera_pins.h" // Include the camera pins for the specific model
// Wi-Fi credentials
const char* ssid = "your_SSID"; // Replace with your Wi-Fi SSID
const char* password = "your_PASSWORD"; // Replace with your Wi-Fi password
// Web server port and path
WiFiServer server(80); // HTTP server runs on port 80
void startCameraServer() {
server.begin();
Serial.println("Camera server started");
}
void setup() {
// Start serial communication
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.println("Connected to Wi-Fi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Initialize the camera
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = 12;
config.pin_d1 = 13;
config.pin_d2 = 14;
config.pin_d3 = 15;
config.pin_d4 = 16;
config.pin_d5 = 17;
config.pin_d6 = 18;
config.pin_d7 = 19;
config.pin_xclk = 21;
config.pin_pclk = 22;
config.pin_vsync = 23;
config.pin_href = 25;
config.pin_sscb_sda = 26;
config.pin_sscb_scl = 27;
config.pin_pwdn = 32;
config.pin_reset = -1; // No reset pin
config.xclk_freq_hz = 20000000;
config.frame_size = FRAMESIZE_QVGA;
config.pixel_format = PIXFORMAT_JPEG;
// Initialize the camera
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
return;
}
// Start the camera server
startCameraServer();
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait for the client to send a request
Serial.println("New Client.");
while (!client.available()) {
delay(1);
}
// Send HTTP headers
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: multipart/x-mixed-replace; boundary=frame");
client.println();
// Stream the video feed to the client
while (client.connected()) {
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return;
}
// Send the frame to the client
client.println("--frame");
client.println("Content-Type: image/jpeg");
client.println("Content-Length: " + String(fb->len));
client.println();
client.write(fb->buf, fb->len); // Send the frame
client.println();
// Release the frame buffer back to the pool
esp_camera_fb_return(fb);
}
// Close the connection
client.stop();
Serial.println("Client Disconnected.");
}