added scheduling

This commit is contained in:
Joren de Graaf
2026-10-10 16:03:14 +02:00
parent 7559b61399
commit 314eb6b4ec
50 changed files with 768 additions and 67 deletions
+25 -17
View File
@@ -1,24 +1,32 @@
# Waveshare firmware validation — concept archive
# Waveshare V2 firmware integration status
Reviewed `BoomCast_Waveshare_Arduino_v0.1.0-concept.zip` as supplied. The README targets the non-touch ESP32-S3-ePaper-1.54 **V2**, but the physical board revision has not been inspected. Waveshare states that V1 and V2 example programs are not interchangeable. Confirm the V2 marking on the PCB before flashing. The official V2 has 8 MiB PSRAM, 8 MiB flash, ES8311 codec, microphone, and microSD slot. The firmware's SDMMC pins 39/40/41 match Waveshare's published pinout for this board.
The owner has confirmed a non-touch Waveshare ESP32-S3-ePaper-1.54 V2 board. The firmware now lives at `../arduino/BoomCast_Waveshare_Arduino/`; that folder is currently **outside** the Django Git worktree (`django/`). Its sketch, build guide, source, pinned preparation script, local-config example, ignore rules, and hardware/API/checklist documents are there.
## Source review
The official Waveshare support was inspected at commit `aaf395c1bbd86f7c5b7a2f9f0401241ff5c44956`. The Arduino audio `board_cfg.h` and explicitly V2 ESP-IDF audio example agree on ES8311 I2S pins. The official Arduino SD example uses CLK/D0/CMD 39/40/41. The firmware uses those pins. Waveshare documents V2 as 8 MB flash and 8 MB PSRAM and says V1/V2 examples are not interchangeable.
- **Microphone and capture:** `boomcast_audio.cpp` uses Waveshare's `audio_bsp` wrapper and a 16 kHz stereo read, selecting the louder slot per frame for mono. That slot selection is a heuristic; verify actual channel mapping and recorded playback on the board. A 2.5-second PSRAM ring and 2.5-second post-trigger buffer make an intended 5-second capture. The detector uses RMS/peak thresholds and cooldown. The first capture after boot can contain less pre-roll. Return values from `audio_playback_read` are not checked, so failed or short reads could enter recordings.
- **WAV and storage:** `boomcast_queue.cpp` writes RIFF PCM mono 16-bit headers and a 16 kHz sample rate. Header arithmetic matches the intended sample count. The queue is on FAT microSD, but files are written directly to their final `.wav` names without a temporary file and atomic rename. Power loss or a full card can leave a truncated item that appears queued; `fwrite`, `fflush`, and `fclose` results are not checked.
- **Network and uploads:** `boomcast_network.cpp` reconnects Wi-Fi and starts NTP. `boomcast_uploader.cpp` sends a conventional multipart body with CRLF boundaries and a `file` part. It sends a Bearer header if a token is set. The current fields are `event_id`, `trigger_type`, optional `detected_at`, and `file`; this **does not satisfy** the new `/api/pending/` contract's `sensor_id`, `captured_at`, `duration_seconds`, or `firmware_version`. It still targets `/fireworks/classify/`.
- **TLS and credentials:** `TLS_ALLOW_INSECURE` defaults to true, and the HTTPS client has no CA certificate configured when it is false. `SENSOR_TOKEN` is in committed-style `boomcast_config.h`; move it into an ignored local header or provisioned NVS and configure a trusted CA before deployment. The setup AP also uses a fixed prototype password.
- **Retries and duplicate safety:** the SD item remains after non-2xx or transport failure, and filename-derived `event_id` is stable across retries. The main loop only calls the uploader after an explicit local UI sync request; it does not automatically drain the queue after reconnection. The uploader treats any 2xx response as success without parsing the submission ID or status, and deletes the file immediately. Add bounded retry scheduling with backoff, parse the JSON receipt, and delete only after a matching accepted or duplicate receipt.
- **Display and memory:** the ePaper shows capture and queue count, plus a transient upload message on the owner screen. It lacks distinct persistent queued, uploading, success, and failure indicators on the display. The upload path allocates the whole WAV plus multipart framing in PSRAM for each request; there is no measured heap or fragmentation data. Measure free and largest contiguous PSRAM before/after repeated uploads and Wi-Fi outages.
## Implemented in source
## Required firmware changes before an end-to-end trial
- Audio reads now check the codec return code, clear input buffers first, discard failed blocks, abort a capture after a read error, select one fixed microphone slot, and count read/recording errors. Capture duration uses the available pre-roll plus the requested post-roll; UTC capture-start and trigger timestamps are retained when time is valid.
- SD commits use checked WAV and sidecar writes, flush/fsync/close, WAV length/header validation, final renames, interrupted-commit recovery, a queue commit marker, oldest-first selection across the directory, an event-count cap, and a free-space reserve. Incomplete files are excluded from upload.
- Uploads stream file contents through `WiFiClientSecure` with a trusted CA, require HTTPS, send the actual `/api/pending/` fields, parse JSON receipts, verify the echoed event ID and submission ID, and retain files after ambiguous responses. Automatic retries use bounded exponential backoff and jitter. Source-embedded token placeholders were replaced with an ignored local header and optional NVS token; shared setup and management passwords were replaced with per-device NVS passwords.
- ePaper owner status reflects listening, recording, queue/offline, uploading, successful submission, retry failure, and audio error counters.
- Django now returns `event_id` in first and duplicate receipts and accepts an explicit `time_state=unknown` when the device clock was not synchronized. The same Pending Review and classifier pipeline remains in use.
1. Confirm the physical V2 revision and use its matching official support files. Do not copy Atom Echo GPIO assignments.
2. Point at `/api/pending/` and send `sensor_id`, stable `event_id`, UTC `captured_at`, measured `duration_seconds`, `firmware_version`, and `file`. Do not send a timestamp until NTP or the RTC is valid; preserve a reliable capture time with the queued file.
3. Configure CA-backed HTTPS and store the sensor token outside committed source. Reject HTTP on deployed builds.
4. Write queue items atomically, detect SD write errors, auto-retry with backoff, validate the JSON receipt, and retain files on ambiguous responses.
5. Show distinct recording, queued, uploading, success, and failure states; log I2S read failures and memory metrics.
## Verification performed
## Verification status
- Official Waveshare pin/support comparison at the pinned commit.
- The pinned `prepare_from_waveshare.sh` completed successfully against a checkout at that commit.
- Arduino CLI 1.5.1 compiled the sketch for ESP32-S3 with ESP32 core 3.3.0, 8 MB flash, OPI PSRAM, and the 8 MB partition scheme. The only emitted warnings were in copied Waveshare support and Espressif touch APIs.
- The `fireworks_detect` Django suite passed: 39 tests. Firmware-shaped unknown-time and receipt fields have focused backend tests.
- Firmware `.gitignore` patterns were checked for local credentials, copied support, and build artifacts.
Automated Django tests cover authenticated submission, storage, classification response handling, duplicate retry, invalid token/sensor/audio, conflict, and classifier outage. A temporary local Django instance was inspected with Playwright at desktop and 390 px mobile width; its Pending Review sensor detail rendered and showed no console errors. **No physical Waveshare board was flashed or tested.** Microphone fidelity, trigger timing, HTTPS certificate validation, firmware build, offline queue recovery, and on-device RAM stability remain hardware test items. Use a real recording and a controlled Wi-Fi outage for final acceptance.
## Still open
- **Firmware compile passed; no flash was performed.** Arduino CLI 1.5.1 compiled the sketch with ESP32 core 3.3.0, Waveshare support at the pinned commit, LVGL 8.4.0, ArduinoJson 7.4.3, 8 MB flash, and OPI PSRAM. The build reports 1,350,599 bytes of program storage (40%) and 99,368 bytes of global dynamic memory (30%). Device runtime RAM use is unmeasured.
- **No physical V2 test was performed.** The fixed microphone slot, actual audio fidelity, short successful codec reads, ePaper refresh, SD power-cut behavior, TLS handshake, and offline recovery need the real board.
- Waveshare's codec API reports read success/failure but not a byte count; a successful short read cannot be detected through the current wrapper.
- FAT does not provide an atomic transaction spanning WAV and sidecar. Valid interrupted commits are recovered; incomplete/orphan files remain on SD for manual inspection rather than being uploaded or overwritten.
- The uploader currently runs synchronously in the main loop. During a slow HTTPS attempt, the local web UI and display update may pause. Verify this on hardware and move upload to a worker task if it affects usability or watchdog health.
- Arduino source is not tracked by the current Django Git repository. Decide on a project-wide repository or a dedicated firmware repository before relying on source control for deployment.
See `../arduino/BoomCast_Waveshare_Arduino/docs/validation-checklist.md` for the pending physical acceptance checks. Do not treat this source review or the Django tests as hardware validation.