# s60sc/esp32-cam_mjpeg2sd architecture

> Architecture diagram of the GitHub repository s60sc/esp32-cam_mjpeg2sd, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/s60sc/esp32-cam_mjpeg2sd
- Repository: https://github.com/s60sc/esp32-cam_mjpeg2sd

## Overview

#### Purpose and Firmware Entry
This is a monolithic Arduino firmware application for ESP32/ESP32-S3 camera boards. It captures JPEG frames, records them as AVI on SD storage, streams live or recorded media, and optionally integrates sensors, actuators, notifications, and remote services. The sketch entry is `ESP32-CAM_MJPEG2SD.ino`; compile-time board/feature selection and shared state are centered in `appGlobals.h`, `globals.h`, and `camera_pins.h`.

#### Startup, Configuration, and Runtime Coordination
Startup initializes hardware, storage, camera, networking, and enabled feature modules, then runs concurrent firmware tasks/services. Persistent settings are managed separately from compile-time feature flags: `prefs.cpp` owns configuration persistence, while `setupAssist.cpp` supports first-boot/AP setup. `appSpecific.cpp` provides application-level setup behavior. Configuration changes made through the UI are persisted and commonly require reboot to reinitialize hardware-dependent services.

#### Camera Capture and Recording Pipeline
The core media path is implemented by `mjpeg2sd.cpp` and `avi.cpp`. Camera JPEG frames are acquired, buffered in PSRAM, and assembled into sector-aligned AVI output on SD to reduce write overhead. Optional audio and telemetry are synchronized into associated media/artifact files. Recording may be manually requested, continuously scheduled, or event-triggered. Camera board mappings form the hardware boundary in `camera_pins.h`.

#### Motion and Event Control
`motionDetect.cpp` samples camera JPEGs, decodes reduced grayscale representations, and compares them with prior samples to decide when to start or stop recording. This control path can be augmented by external sensors/peripherals and can drive alerting or recording lifecycle decisions. The recording subsystem is therefore a shared resource: live streaming may coexist, but recording takes priority under constrained CPU, PSRAM, SD, and network bandwidth.

#### Media Delivery and Storage Access
Browser-facing HTTP/WebSocket control and MJPEG playback/live streaming are served by `webServer.cpp` and `streamServer.cpp`; static UI assets reside in `data/MJPEG2SD.htm`, `data/Auxil.htm`, and `data/common.js`. `utilsFS.cpp` mediates SD-backed file operations. Recorded AVI files and companion telemetry artifacts are browsed, downloaded, deleted, or transferred. Alternative delivery/access boundaries include `rtsp.cpp` for RTSP and `webDav.cpp` for SD-card WebDAV access.

#### Network, Remote Control, and Integrations
Wi-Fi is the normal transport, with optional Ethernet on supported ESP32-S3 hardware. Network services reuse the selected interface. External integrations are modular and feature-gated: `mqtt.cpp` publishes state/accepts commands, `ftp.cpp` transfers recordings, `smtp.cpp` and `telegram.cpp` send alerts, and `externalHeartbeat.cpp` posts device status to an external endpoint. HTTPS/TLS certificate handling is isolated in `certificates.cpp`; it materially increases memory and processing requirements.

#### Optional Device and Extension Subsystems
Hardware expansion is separated from core capture: `peripherals.cpp` handles sensors/actuators, `periphsI2C.cpp` handles shared or separate I2C devices, `audio.cpp` records/streams microphone audio and supports intercom, and `telemetry.cpp` emits recording-aligned sensor data. `uart.cpp` supports an auxiliary-board boundary for remote-control, servo, or photogrammetry commands; `mcpwm.cpp` supports motor control. `photogram.cpp` coordinates turntable/DSLR-oriented capture workflows.

Main entry points: [`mjpeg2sd.cpp`](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/mjpeg2sd.cpp) (MJPEG capture & recording), [`ESP32-CAM_MJPEG2SD.ino`](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/ESP32-CAM_MJPEG2SD.ino) (Arduino entry), [`utilsFS.cpp`](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/utilsFS.cpp) (SD file access), [`webServer.cpp`](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/webServer.cpp) (HTTP/WebSocket control)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Firmware core"]
  node_entry["Arduino entry<br/>firmware entry"]
  node_globals["Build flags &amp; state<br/>runtime coordination<br/>[appGlobals.h]"]
  node_prefs[("Preferences<br/>settings persistence<br/>[prefs.cpp]")]
  node_setup["Setup assistance<br/>first-boot setup<br/>[setupAssist.cpp]"]
  node_app["Application setup<br/>application behavior<br/>[appSpecific.cpp]"]
end

subgraph group_media["Media pipeline"]
  node_camera_pins["Camera hardware map<br/>board boundary<br/>[camera_pins.h]"]
  node_capture["MJPEG capture &amp; recording<br/>media coordinator<br/>[mjpeg2sd.cpp]"]
  node_avi["AVI writer<br/>recording encoder<br/>[avi.cpp]"]
  node_storage[("SD file access<br/>storage adapter<br/>[utilsFS.cpp]")]
  node_motion["Motion detection<br/>event controller<br/>[motionDetect.cpp]"]
end

subgraph group_access["Network access"]
  node_web["HTTP/WebSocket control<br/>web control server<br/>[webServer.cpp]"]
  node_stream["Live &amp; playback streaming<br/>MJPEG server<br/>[streamServer.cpp]"]
  node_ui["Browser UI assets<br/>static UI<br/>[MJPEG2SD.htm]"]
  node_rtsp["RTSP delivery<br/>media protocol<br/>[rtsp.cpp]"]
  node_webdav["WebDAV SD access<br/>storage protocol<br/>[webDav.cpp]"]
end

subgraph group_extensions["Optional extensions"]
  node_peripherals["Sensors &amp; actuators<br/>hardware extension<br/>[peripherals.cpp]"]
  node_audio["Audio &amp; intercom<br/>audio extension<br/>[audio.cpp]"]
  node_telemetry["Recording telemetry<br/>artifact producer<br/>[telemetry.cpp]"]
  node_remote["Remote integrations<br/>feature-gated services<br/>[mqtt.cpp]"]
  node_photogram["Photogrammetry control<br/>capture workflow<br/>[photogram.cpp]"]
end

node_entry -->|"uses"| node_globals
node_entry -->|"loads settings"| node_prefs
node_entry -->|"first boot"| node_setup
node_entry -->|"initializes"| node_app
node_entry -->|"starts"| node_capture
node_globals -->|"selects board map"| node_camera_pins
node_capture -->|"acquires JPEGs"| node_camera_pins
node_capture -->|"writes frames"| node_avi
node_avi -->|"stores AVI"| node_storage
node_motion -->|"starts/stops recording"| node_capture
node_peripherals -.->|"augments events"| node_motion
node_audio -.->|"synchronized audio"| node_avi
node_telemetry -.->|"companion artifacts"| node_storage
node_web -->|"serves"| node_ui
node_ui -->|"control changes"| node_web
node_web -->|"persists settings"| node_prefs
node_stream -->|"live frames"| node_capture
node_stream -->|"recorded playback"| node_storage
node_web -->|"browse/download/delete"| node_storage
node_rtsp -.->|"streams frames"| node_capture
node_webdav -.->|"exposes SD card"| node_storage
node_remote -.->|"commands &amp; state"| node_capture
node_remote -.->|"transfers recordings"| node_storage
node_photogram -.->|"coordinates captures"| node_capture

click node_entry "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/ESP32-CAM_MJPEG2SD.ino"
click node_globals "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/appGlobals.h"
click node_prefs "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/prefs.cpp"
click node_setup "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/setupAssist.cpp"
click node_app "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/appSpecific.cpp"
click node_camera_pins "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/camera_pins.h"
click node_capture "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/mjpeg2sd.cpp"
click node_avi "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/avi.cpp"
click node_storage "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/utilsFS.cpp"
click node_motion "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/motionDetect.cpp"
click node_web "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/webServer.cpp"
click node_stream "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/streamServer.cpp"
click node_ui "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/data/MJPEG2SD.htm"
click node_rtsp "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/rtsp.cpp"
click node_webdav "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/webDav.cpp"
click node_peripherals "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/peripherals.cpp"
click node_audio "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/audio.cpp"
click node_telemetry "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/telemetry.cpp"
click node_remote "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/mqtt.cpp"
click node_photogram "https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/photogram.cpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_globals,node_prefs,node_setup,node_app toneBlue
class node_camera_pins,node_capture,node_avi,node_storage,node_motion toneAmber
class node_web,node_stream,node_ui,node_rtsp,node_webdav toneMint
class node_peripherals,node_audio,node_telemetry,node_remote,node_photogram toneRose
```

## Components

### Firmware core

- [Arduino entry](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/ESP32-CAM_MJPEG2SD.ino): firmware entry · `ESP32-CAM_MJPEG2SD.ino`
- [Build flags & state](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/appGlobals.h): runtime coordination · Board selection, feature flags, and shared state. · `appGlobals.h`
- [Preferences](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/prefs.cpp): settings persistence · `prefs.cpp`
- [Setup assistance](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/setupAssist.cpp): first-boot setup · `setupAssist.cpp`
- [Application setup](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/appSpecific.cpp): application behavior · `appSpecific.cpp`

### Media pipeline

- [Camera hardware map](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/camera_pins.h): board boundary · `camera_pins.h`
- [MJPEG capture & recording](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/mjpeg2sd.cpp): media coordinator · Buffers JPEG frames in PSRAM and controls recording. · `mjpeg2sd.cpp`
- [AVI writer](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/avi.cpp): recording encoder · Builds sector-aligned AVI output. · `avi.cpp`
- [SD file access](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/utilsFS.cpp): storage adapter · `utilsFS.cpp`
- [Motion detection](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/motionDetect.cpp): event controller · Analyzes reduced JPEG grayscale samples to control recording. · `motionDetect.cpp`

### Network access

- [HTTP/WebSocket control](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/webServer.cpp): web control server · `webServer.cpp`
- [Live & playback streaming](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/streamServer.cpp): MJPEG server · `streamServer.cpp`
- [Browser UI assets](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/data/MJPEG2SD.htm): static UI · `data/MJPEG2SD.htm`
- [RTSP delivery](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/rtsp.cpp): media protocol · `rtsp.cpp`
- [WebDAV SD access](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/webDav.cpp): storage protocol · `webDav.cpp`

### Optional extensions

- [Sensors & actuators](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/peripherals.cpp): hardware extension · `peripherals.cpp`
- [Audio & intercom](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/audio.cpp): audio extension · Provides recording, streaming, and intercom audio. · `audio.cpp`
- [Recording telemetry](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/telemetry.cpp): artifact producer · `telemetry.cpp`
- [Remote integrations](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/mqtt.cpp): feature-gated services · MQTT, FTP, alerting, heartbeat, and TLS-enabled remote access. · `mqtt.cpp`
- [Photogrammetry control](https://github.com/s60sc/esp32-cam_mjpeg2sd/blob/master/photogram.cpp): capture workflow · `photogram.cpp`

## Connections

- Arduino entry → Build flags & state: uses
- Arduino entry → Preferences: loads settings
- Arduino entry → Setup assistance: first boot
- Arduino entry → Application setup: initializes
- Arduino entry → MJPEG capture & recording: starts
- Build flags & state → Camera hardware map: selects board map
- MJPEG capture & recording → Camera hardware map: acquires JPEGs
- MJPEG capture & recording → AVI writer: writes frames
- AVI writer → SD file access: stores AVI
- Motion detection → MJPEG capture & recording: starts/stops recording
- Sensors & actuators → Motion detection: augments events
- Audio & intercom → AVI writer: synchronized audio
- Recording telemetry → SD file access: companion artifacts
- HTTP/WebSocket control → Browser UI assets: serves
- Browser UI assets → HTTP/WebSocket control: control changes
- HTTP/WebSocket control → Preferences: persists settings
- Live & playback streaming → MJPEG capture & recording: live frames
- Live & playback streaming → SD file access: recorded playback
- HTTP/WebSocket control → SD file access: browse/download/delete
- RTSP delivery → MJPEG capture & recording: streams frames
- WebDAV SD access → SD file access: exposes SD card
- Remote integrations → MJPEG capture & recording: commands & state
- Remote integrations → SD file access: transfers recordings
- Photogrammetry control → MJPEG capture & recording: coordinates captures
