Servidor de Video Streaming con ESP32-CAM

Descripción del Proyecto: Servidor de Video Streaming con ESP32-CAM

Este código permite convertir un módulo ESP32-CAM (AI Thinker) en un punto de acceso Wi-Fi (Access Point) que transmite video en tiempo real a través de un servidor web local.

1. Estructura y Componentes del Código

El programa se divide en cuatro secciones principales:

A. Definición de Pines (Hardware)

Dado que el ESP32-CAM no tiene todos los pines conectados de forma estándar, se definen los GPIOs específicos para el modelo AI Thinker. Estos pines controlan la interfaz paralela de la cámara (D0-D7), las señales de sincronización (VSYNC, HREF, PCLK) y el bus de comunicación SCCB (protocolo similar a I2C).

B. Configuración de Red (Wi-Fi)

El código configura el módulo en modo SoftAP (Punto de Acceso):

  • SSID: ESP32-CAM2

  • Password: 12345678

    Esto significa que no necesitas un router; puedes conectar tu celular o PC directamente a la red que genera el ESP32.

C. El Servidor Web y el Manejador de Stream

La función stream_handler es el «corazón» del video. Utiliza una técnica llamada MJPEG (Motion JPEG):

  1. Establece el tipo de respuesta HTTP como multipart/x-mixed-replace. Esto le dice al navegador que recibirá una serie de imágenes que deben reemplazar a la anterior continuamente.

  2. Entra en un bucle while(true) donde captura una foto (esp_camera_fb_get), la envía como un «frame» de video y luego libera la memoria.

D. Configuración Inicial (setup)

  • Inicialización de Cámara: Configura parámetros críticos como el formato (JPEG), la resolución (QVGA – 320×240) y la calidad de compresión.

  • Arranque del Servidor: Inicia el servicio web en el puerto 80 y registra la ruta raíz (/) para que, al acceder a la IP del ESP32, se dispare automáticamente el streaming.


2. Especificaciones Técnicas

Característica Descripción
Microcontrolador ESP32-S
Sensor de Imagen OV2640
Formato de Salida JPEG (necesario para streaming fluido)
Resolución QVGA ($320 \times 240$ px)
Modo Wi-Fi Access Point (AP)
Protocolo de Video HTTP Multipart Stream (MJPEG)

3. Funcionamiento Paso a Paso

  1. Al encender el dispositivo, el ESP32 inicializa el sensor de la cámara.

  2. Crea una red Wi-Fi propia. El usuario debe conectarse a ella.

  3. El usuario abre un navegador y escribe la dirección IP (usualmente 192.168.4.1).

  4. El servidor responde enviando ráfagas constantes de imágenes JPEG a través de la conexión HTTP.

  5. El navegador interpreta estas imágenes como un flujo de video en vivo.

4. Notas de Implementación

  • Bucle Vacío: El loop() se mantiene con un delay(10000) ya que el servidor web corre de forma asíncrona en segundo plano mediante tareas del sistema operativo (FreeRTOS).

  • Estabilidad: Se utiliza fb_count = 1 para ahorrar memoria RAM, lo cual es vital en el ESP32-CAM para evitar reinicios inesperados por falta de memoria (heap).

 

 

#include «esp_camera.h»
#include <WiFi.h>
#include «esp_http_server.h»
// Pines ESP32-CAM 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
const char* ssid = «ESP32-CAM2»;
const char* password = «12345678»;
httpd_handle_t server = NULL;
// ===========================
static esp_err_t stream_handler(httpd_req_t* req) {
  camera_fb_t* fb;
  httpd_resp_set_type(req, «multipart/x-mixed-replace; boundary=frame»);
  while (true) {
    fb = esp_camera_fb_get();
    if (!fb) return ESP_FAIL;
    char buf[64];
    int len = snprintf(buf, 64,
                       «–frame\r\nContent-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n»,
                       fb->len);
    httpd_resp_send_chunk(req, buf, len);
    httpd_resp_send_chunk(req, (const char*)fb->buf, fb->len);
    httpd_resp_send_chunk(req, «\r\n», 2);
    esp_camera_fb_return(fb);
  }
}
// ===========================
void startCameraServer() {
  httpd_config_t config = HTTPD_DEFAULT_CONFIG();
  httpd_uri_t uri = {
    .uri = «/»,
    .method = HTTP_GET,
    .handler = stream_handler,
    .user_ctx = NULL
  };
  if (httpd_start(&server, &config) == ESP_OK) {
    httpd_register_uri_handler(server, &uri);
  }
}
// ===========================
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.pixel_format = PIXFORMAT_JPEG;
  config.frame_size = FRAMESIZE_QVGA;
  config.jpeg_quality = 12;
  config.fb_count = 1;
  if (esp_camera_init(&config) != ESP_OK) {
    Serial.println(«Error cámara»);
    return;
  }
  WiFi.softAP(ssid, password);
  Serial.println(WiFi.softAPIP());
  startCameraServer();
}
// ===========================
void loop() {
  delay(10000);
}

 

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *