Files
Hodor/README.md
T
Aida_Lashkari 194671d387 Add BOOT-button entry to the setup portal and gate the Wi-Fi card behind login
Hold BOOT (GPIO0) for 10s while running to reboot into the captive
portal without needing the current media login (physical access is the
proof). This also covers the "forgotten login" case.

button.c (new)
- button_task polls GPIO0 (pull-up, 50 ms), ignores a press held since
  boot so download-mode entry is unaffected, and calls
  wifi_request_portal_physical() after 10s.
- Started from app_main() right after wifi_start_provisioning_if_needed(),
  only when not provisioning, so it also works while the station is
  retrying an unreachable network.

wifi.c
- wifi_request_portal_physical(): stores a one-shot "portal requested by
  button" flag in NVS (namespace "portal") and restarts. NVS is used
  instead of RTC_DATA_ATTR because the flag was lost across the reset
  on this board.
- wifi_start_provisioning_if_needed(): reads and erases the flag at boot
  (portal_flag_take) and sets s_physical for that portal session only.
  Logs the reset reason.
- Portal without the button (30 failed reconnects, wifi_request_portal())
  stays non-physical and asks for the current login.
- /save (Wi-Fi card) now requires cam_auth_verify() unless s_physical.
- /auth uses cam_auth_set() when s_physical, else cam_auth_change().
- Shared lockout: 5 wrong logins block /save and /auth until restart
  (not applied when s_physical). Failed attempts are delayed by 1 s.
- Physical portal times out after 180 s (normal portal: 300 s).
- Root page gets "var PHYS=0/1" injected before its script via chunked
  send; with PHYS=1 the "current login" fields are hidden and a banner
  is shown.
- html: added current-login fields (wu/wp) to the Wi-Fi form, wrapped
  current-login fields in .cur, /save errors are shown to the user.
- read_body()/send_text() moved above the handlers.

cam_auth.c
- New cam_auth_set(): validates and stores a new login with no check of
  the current one. cam_auth_change() now = cam_auth_verify() +
  cam_auth_set().

Requirements
- cam_auth_load() must run before wifi_start_provisioning_if_needed(),
  otherwise cam_auth_verify() treats auth as disabled and accepts any
  login.
- cam_auth.h: declare cam_auth_set(); declare
  wifi_request_portal_physical() where button.c can see it.

Tested on board: long press reboots into the AP "ESP32_Setup" instead of
reconnecting to the stored network. Not yet tested: Wi-Fi card login
gating, lockout, 180 s timeout, portal page rendering on a phone.
2026-10-11 15:26:53 +03:30

130 lines
6.6 KiB
Markdown

# 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=<password>`. 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.