# ESP32-S3-CAM Door Intercom A door-opener / intercom device used mainly from Home Assistant, as close to real time as possible. Video (MJPEG) and two-way audio over HTTP and RTSP, with a captive-portal setup page. ## Project briefing ### Hardware - Waveshare ESP32-S3-CAM - Camera: GC2145 (YUV422 -> `frame2jpg()` -> JPEG -> MJPEG) - Speaker codec: ES8311 (I2C 0x18) - Mic ADC: ES7210 (I2C 0x40) - CH32V003 IO expander on the same I2C bus (SDA 8, SCL 7) - Speaker amp enable: `PA_CTRL` = EXIO_4 (bit 3 vs bit 4 not yet confirmed) - I2S: MCLK 10, BCLK 11, WS 12, DOUT 14, DIN 13, 16 kHz / 16-bit - BOOT button: GPIO0 (not used by anything else on the board, checked on the schematic) ### Software - ESP-IDF 5.x (built with v5.5.3), `esp_codec_dev` 1.6.2 (local component) - Project: `D:\ViraTak\Projects\Hodor_Firmeware\Hodor` (`main/main.c`) ### Source files | File | Role | |---|---| | `main.c` | Camera, `audio_init()` (full-duplex I2S), `play_task` fed by `s_play_sb`, `mic_task` feeding HTTP `/mic` and RTSP. HTTP servers: `:80` `/pic` `/stream` `/setup` (POST), `:81` `/mic` (WAV stream), `:82` `/spk` (POST) `/vol`. No WebSocket. | | `wifi.c` | Station mode + captive portal (AP `ESP32_Setup`, DNS hijack, setup page with a Wi-Fi card and a "change login" card). Opens the portal after 30 failed reconnects or on request. | | `button.c` | BOOT long-press handler, opens the portal in "physical presence" mode (see below). | | `cam_stream.c/.h` | One shared JPEG producer for `/pic`, `/stream` and RTSP. RTSP server on `:554`, TCP-interleaved only, max 2 sessions. `track0` = MJPEG (RTP/JPEG), `track1` = mic audio (G.711 PCMA by default; `RTSP_AUDIO_FORMAT` also allows PCMU or L16 16 kHz), `track2` = ONVIF-style back channel (offered only if the client sends `Require: www.onvif.org/ver20/backchannel`; G.711 from the client plays on the speaker through `rtsp_set_speaker_sink()`). | | `cam_auth.c/.h` | RTSP Digest (own MD5, no qop) and HTTP Basic or `?key=`. Login stored in NVS as user + MD5 hash. Defaults come from `CAM_USER`/`CAM_PASS`. | | `video.html` / `audio.html` | Pass the password as `?key=`. | ### Decisions - IP-address filtering was dropped. - Muting the mic while the speaker plays was rejected. - Home Assistant route: its ONVIF integration needs H.264 and its stream pipeline does not take MJPEG/G.711, so use go2rtc (`rtsp://user:pass@IP:554/mjpeg/1` plus an `ffmpeg:...#video=h264` source) with the WebRTC Camera card (`media: video,audio,microphone`; HTTPS needed for the mic). ## Setup portal and the BOOT button ### Opening the portal | How | Wi-Fi card | Login card | |---|---|---| | Hold BOOT for 3 s while running | open, no login asked | open, no current login asked | | 30 failed reconnects, or `wifi_request_portal()` | needs current login | needs current login | Join the open AP `ESP32_Setup` and open `http://192.168.4.1/` (most phones pop it up automatically). ### How it works - A long press stores a one-shot flag in NVS (namespace `portal`) and restarts. NVS is used because an RTC-memory flag was lost across the reset on this board. - At boot the flag is read and erased, and `s_physical` is set for that portal session only. A power cycle is a normal boot. - With physical presence the page hides the "current login" fields and shows a green banner. - 5 wrong logins lock `/save` and `/auth` until restart (not applied in the physical case). Failed attempts are delayed 1 s. - Portal timeouts: 180 s when opened by the button, 300 s otherwise. The device then restarts. - GPIO0 is a strapping pin. A press held from boot is ignored by the task, so download-mode entry is unaffected. ### Requirements - `cam_auth_load()` must run **before** `wifi_start_provisioning_if_needed()`. Otherwise `cam_auth_verify()` treats auth as disabled and accepts any login. - `audio_init()` must be enabled in `app_main`. With it commented out, mic and speaker are silent. - `button_start()` is called right after `wifi_start_provisioning_if_needed()`, only when not provisioning, so the button also works while the device keeps retrying an unreachable network. ### app_main order ```c bsp_i2c_init(); bsp_io_expander_init(); init_camera(); audio_init(bsp_i2c_get_handle()); cam_auth_load(CAM_USER, CAM_PASS); cam_feed_start(); wifi_start_provisioning_if_needed(); if (!wifi_is_provisioning()) button_start(); while (!wifi_wait_connected(5000)) { /* portal mode never leaves this loop */ } start_camera_server(); // :80 xTaskCreate(mic_task, ...); start_audio_server(); // :81, :82 rtsp_set_speaker_sink(rtsp_speaker_sink); rtsp_server_start(); ``` ## Status **Working on the board** - Compiles and runs on ESP-IDF 5.5.3; camera, Wi-Fi station, RTSP server start. - Mic over RTSP. - Mic and speaker once `audio_init()` is enabled. - BOOT long press reboots into the `ESP32_Setup` portal instead of reconnecting to the stored network. **Not yet tested on the board** - The back channel with go2rtc (`a=sendonly` direction is unverified). - Wi-Fi card login gating, lockout, 180 s timeout, portal page rendering on a phone. - Digest/Basic auth against real clients (verified on a PC only: G.711 conversions, MD5/Digest, auth logic). **Known behaviour** - About 1 s delay in audio and video in the Home Assistant path, attributed to go2rtc's ffmpeg MJPEG-to-H.264 transcode (about 0.5 to 1 s with default x264 settings). - Earlier KMPlayer playback showed about 2 s, probably player buffering. KMPlayer cannot send audio, so the speaker only works via `audio.html` (`/spk`) or the go2rtc back channel. ## Latency notes Compare in this order to find the slow stage: 1. Browser: `/stream` and `audio.html` directly (almost no client buffering). 2. `ffplay -fflags nobuffer -flags low_delay -probesize 32 -analyzeduration 0 -rtsp_transport tcp rtsp://USER:PASS@IP:554/mjpeg/1` 3. go2rtc / Home Assistant. If the device itself is slow, check: camera `fb_count = 2` with `CAMERA_GRAB_LATEST`, JPEG encode time at 640x480, I2S DMA size (aim for ~20 ms chunks), mic packet size (20 ms), `TCP_NODELAY` on RTSP/HTTP sockets, and the speaker stream-buffer trigger level. For go2rtc: use a low-latency x264 setup (`ultrafast` preset, `-tune zerolatency`), or hardware encoding if available. ## Open ideas - Confirm the `PA_CTRL` bit (EXIO_4 bit 3 vs 4) - Raise the default speaker volume (~85) - Test go2rtc two-way audio - Beep through the speaker at the 3 s mark of the BOOT hold - Echo cancellation (esp-sr) - HTTPS - `RTSP_MAX_SESSIONS` 3 and `CONFIG_LWIP_MAX_SOCKETS` 16 Done: lock the portal's Wi-Fi card behind the login; hardware reset button for a forgotten login (BOOT hold). ## Notes Never commit passwords. `CAM_USER`/`CAM_PASS` are only the built-in defaults; change the login on the setup page.