# Guide: local RTSP firmware for AK3918AV130 / SC3332 Tuya cameras This turns a cheap Tuya "2K" Wi-Fi camera into a **local-only** camera: no cloud and no app. You get a password-protected RTSP stream at the sensor's full 2304×1296, continuous and motion recording to the SD card, SSH access, Wi-Fi settings you can change without reflashing, and firmware updates over Wi-Fi or from an SD card. You may not have to open the camera at all: a stock camera can be installed from the SD card alone (§5a). Otherwise you open it **once**, for the first install. After that, every update and recovery happens over Wi-Fi or the SD card slot. > **Status:** RTSP, Wi-Fi, SSH and repartitioning are confirmed on hardware. Full resolution > (24 MB ION heap), RTSP auth, over-the-air updates, the SD updater, `wifi-set`/`wifi.txt` and > SD recording are built but not yet confirmed on a device. The recorder's file writing, > rotation, size limits and motion pre-roll were checked in a host simulation (all output > decodes with ffmpeg). Motion detection itself only runs on the camera. Report back if you try them. > What's in the image, file by file: [IMAGE_CONTENTS.md](IMAGE_CONTENTS.md). --- ## 0. Does this apply to my camera? All of the following must be true. Check them in the serial boot log (§2): | Check | Expected | |---|---| | U-Boot banner | `U-Boot 2019.10.0-V4.0.27`, `Model: EVB_CBD_AK3918AV130` | | Flash | `SF: Detected ZB25VQ64 … total 8 MiB` | | RAM | `DRAM: 64 MiB` | | Kernel | `Linux version 4.4.302-cip94` | | Sensor | `sc3332 Probed success` | | Wi-Fi | ATBM6x6x (USB) | A different sensor, flash size or kernel means **stop**. The modules and libraries won't match. ## 1. What you need - A Linux PC with `squashfs-tools`, `mtd-utils`, `device-tree-compiler` (`fdtput`/`fdtdump`), `openssl`, `python3`. Add `flashrom` if you use a programmer. - A **3.3 V USB-serial adapter** (no 5 V!), connected to the board's TX/RX/GND pads at 115200 8N1. Needed for the first install. - *Optional:* a CH341A programmer with a SOIC-8 clip, for the easiest backup and restore. - A microSD card, **FAT32**. - This repository (the `toolkit/` directory). ## 2. Open it, connect serial, stop U-Boot 1. Open the case and find the UART pads (usually labelled TX/RX/GND near the SoC). Connect adapter RX→board TX and TX→board RX, plus GND. **Don't connect VCC.** 2. `picocom -b 115200 /dev/ttyUSB0` (or screen or minicom), then power the camera. 3. Press **Ctrl+C** repeatedly while it boots until you get the `=>` prompt. 4. Run `printenv` and save the output. You'll want your original `mtdparts` and `bootcmd`. ## 3. Back up your flash (mandatory) Your backup is the only way back to stock, and the build uses **your own** device-specific partitions (Tuya keys, MAC address). Never use someone else's dump. **With a programmer** (camera unpowered, clip on the flash chip): ```sh flashrom -p ch341a_spi -r dump1.bin flashrom -p ch341a_spi -r dump2.bin cmp dump1.bin dump2.bin && cp dump1.bin camera_flash.bin # the two reads must be identical ``` **Without a programmer:** boot to a root shell over serial (README.md §2: `init=/bin/sh` plus the mounts shown there), then copy every partition to the SD card: ```sh insmod /usr/modules/mmc_core.ko; insmod /usr/modules/mmc_block.ko; insmod /usr/modules/ak_mci.ko mount -t vfat /dev/mmcblk0p1 /mnt || mount -t vfat /dev/mmcblk0 /mnt for i in 0 1 2 3 4 5 6 7; do cat /dev/mtd$i > /mnt/mtd$i.bin; done; sync; umount /mnt ``` On the PC, join them in order: `cat mtd0.bin mtd1.bin … mtd7.bin > camera_flash.bin`. The result must be exactly 8388608 bytes. Keep `camera_flash.bin` somewhere safe. ## 4. Build your image ```sh cd toolkit ./fetch-deps.sh # once: SDK libs, OpenIPC modules, toolchains ./build-deps.sh # once: mtdflash, dropbear, RTSP server (from source) ./split.sh ../camera_flash.bin parts/stock # carve your dump into partitions WIFI_SSID='YourNetwork' WIFI_PSK='YourWifiPassword' \ ROOT_PW='choose-a-root-password' RTSP_PASS='choose-a-stream-password' \ ./variants/rtsp.sh ``` - Output, collected in `sdcard/` next to `toolkit/` (`STAGE_DIR=` to change). Every build refreshes all of them: - `full.bin`: the whole 8 MiB chip - `app.bin`, `dtb.bin`, `data.bin`, `env.bin`, `MD5SUMS`: single partitions - `sd-install-backup/`, `sd-install-direct/`: SD-card-only installs for stock cameras (§5a) - `full.bin` already has the right U-Boot settings (partition layout + SD-update `bootcmd`) baked in, so flashing it whole needs no typing at the U-Boot prompt. - To leave Wi-Fi out of the image and set it from the SD card instead, use `WIFI_SSID=` (empty) and see §7. - All build settings are listed in IMAGE_CONTENTS.md §8. Almost all of them can be changed later over SSH with `camcfg` (§6b), without rebuilding. - **These files contain your passwords and your camera's keys. Don't share them.** ## 5. First install Pick one. All of them end in the same firmware and layout. | Method | Needs | Writes | Backup of the original | Section | |---|---|---|---|---| | Programmer | CH341A + clip | whole chip (`full.bin`) | your own dump (§3) | §5 | | Serial + SD | UART adapter, SD card | DTB, APP, DATA + U-Boot settings typed by hand | your own dump (§3) | §5 | | **SD only, with backup** | SD card | U-Boot writes an installer, which dumps the full 8 MiB to the card, then installs | **full 8 MiB, made automatically** | §5a | | **SD only, direct** | SD card | U-Boot writes APP+DATA itself; first boot writes DTB + settings | everything except the original APP | §5a | **Already installed?** Use updates (§8), never an install method again: they reset DATA. **With a programmer** it's one command, with no serial and no U-Boot typing: ```sh flashrom -p ch341a_spi -w sdcard/full.bin ``` `full.bin` also resets DATA to empty. That doesn't matter on a first install, but don't use it later just to update: your `camcfg` settings and SSH host keys live there. Use §8 instead. **Without a programmer:** copy `app.bin`, `data.bin` and `dtb.bin` from `sdcard/` to the root of the FAT32 card. Insert it, power on, stop at `=>` (Ctrl+C), then: ``` mmc dev 0 fatls mmc 0 sf probe 0 fatload mmc 0 ${loadaddr} dtb.bin sf erase 0x34000 0x10000 sf write ${loadaddr} 0x34000 ${filesize} fatload mmc 0 ${loadaddr} app.bin sf erase 0x300000 0x2e0000 sf write ${loadaddr} 0x300000 ${filesize} fatload mmc 0 ${loadaddr} data.bin sf erase 0x5e0000 0x220000 sf write ${loadaddr} 0x5e0000 ${filesize} setenv mtdparts 'mtdparts=spi-nor:200k@0x0(UBOOT),4k@0x32000(ENV),4k@0x33000(ENVBK),64k@0x34000(DTB),1536k@0x44000(KERNEL),1008k@0x1c4000(ROOTFS),256k@0x2c0000(CONFIG),2944k@0x300000(APP),2176k@0x5e0000(DATA)' setenv sdscript version setenv bootcmd 'mmc dev 0; mw.b ${loadaddr} 0 0x1000; fatload mmc 0 ${loadaddr} camupdate.env; env import -t ${loadaddr} 0x1000; run sdscript; setenv sdscript version; run boot_normal' saveenv reset ``` Notes: - If `fatls mmc 0` shows nothing, the card has a partition table. Use `mmc 0:1` everywhere. - The new `bootcmd` checks the SD card for an update file before every boot. That's what makes recovery possible later without serial (§8). With no card, or no file, it just boots. - Writing only DTB, APP and DATA never touches U-Boot, so U-Boot always stays reachable. **Check it worked:** watch the serial log. The camera should join Wi-Fi and print its IP. You can also get a shell on the serial console and run `cat /tmp/local_bootstrap.log`. You can close the case now. ## 5a. Install with only an SD card (stock camera, no opening, no serial) The stock U-Boot has a hidden factory-recovery feature. At every power-on it looks for `ht_recover_fw.conf` on a **FAT32** SD card. If the file is valid and its build id is new, it writes the firmware next to it into flash, then boots. The build makes two ready-to-copy sets: | | `sd-install-backup/` (recommended) | `sd-install-direct/` | |---|---|---| | U-Boot writes | a temporary installer rootfs (1 MiB) | the final APP + DATA (5 MiB) | | Then | the installer, with the original APP still intact, **dumps all 8 MiB to the card**, then writes APP+DATA, DTB, the stock rootfs and the U-Boot settings, and reboots | the new firmware's first boot backs up the rest, writes DTB + U-Boot settings, and reboots | | Backup on the card | `original-backup/full.bin`: the **complete chip**; restore stock with `flashrom -w full.bin` | partitions except APP (the Tuya firmware is gone) | | Most of the work done by | Linux (our installer) | U-Boot itself (fewest steps outside U-Boot) | **Steps (both):** 1. Format a microSD card as **FAT32**. Copy the **contents** of the chosen folder to its root. For the backup variant that's `ht_recover_fw.conf`, `root.sqsh4` and the `camerarev/` folder; for direct, `ht_recover_fw.conf` and `usr.sqsh4`. Card size: at least 64 MB (the backup needs 8 MiB). Optional: add a `wifi.txt` (§7). 2. Insert it into the powered-off camera and power on. **Wait about 5 minutes** without cutting power. The camera reboots by itself when done. 3. Find it in your router's DHCP list and continue with §6. Keep the card's `original-backup/` folder somewhere safe: it has your camera's keys. **What to expect in detail (backup variant):** about 20 s of U-Boot writing, then the installer: - **backup:** about 30 s; writes `original-backup/` with MD5SUMS and a README - **check:** verifies `camerarev/MD5SUMS` and copies everything to RAM - **write:** about 2 minutes of flash writes, then a reboot The log is in `camerarev-install.log` on the card. If anything is wrong (card not FAT32, a corrupt file), it stops **before writing anything** and the camera stays stock. Fix the card and power-cycle to retry. Notes: - **Power loss:** at any point before the very last step (the U-Boot settings), the next power-on simply runs the install again. An existing backup is kept, never overwritten. - **Leaving the card in is harmless:** the install saves its build id, so U-Boot skips it from then on. - **Don't reuse an install set:** don't put a newer `sd-install-*` set on an installed camera. It would reinstall and **reset DATA**. For updates use §8. - **Which dump the build needs:** a dump of this camera model (§3–§4): yours, or one you trust from an identical camera. - Neither variant ever writes U-Boot, the kernel or CONFIG, so your device keys stay. - The U-Boot settings come from the dump the build used (its unused MAC address included). - The backup variant writes back the stock rootfs from that dump. Its `README.txt` says whether your U-Boot and kernel are identical to that dump, which tells you the rootfs in `full.bin` matches yours too. - **Check the model first:** see §0. Without serial you can't see the boot log, so you're relying on the camera being the same model and firmware as the dump. ## 6. Using the camera | What | How | |---|---| | Main stream (2304×1296) | `rtsp://admin:@/vs0` | | Substream (640×360) | `rtsp://admin:@/vs1` | | Shell | `ssh root@` (password `ROOT_PW`) | | Change any setting | `camcfg` (§6b) | | Status at a glance | `camcfg status` | | Change root password | `camcfg password` (or `passwd`) | | Restart the stream | `camcfg apply`, or `killall ak_rtsp_server_sample` (it restarts in 5 s) | | Edit any other read-only file permanently | `rw /path/to/file`, then edit it, then `reboot` | | Logs | `/tmp/local_bootstrap.log`, `/tmp/rtsp.log` | VLC, ffmpeg, Frigate, Home Assistant, Blue Iris and similar tools all work with these URLs. Find the IP in your router's DHCP list. ## 6b. Settings: `camcfg` `camcfg` changes every setting over SSH: streams, login, recording, time and Wi-Fi. It checks every value before saving, applies the change (the stream restarts in about 5–10 s), and keeps ONVIF in sync. Your changes are stored **separately** from the firmware defaults (`/data/camcfg.conf`), so they survive reboots **and firmware updates**, and new defaults from an update still apply to everything you didn't change. ```sh camcfg show # everything; * marks what you changed camcfg show record # one section camcfg get main.bitrate camcfg set main.bitrate=6000 # several at once is fine: camcfg set main.codec=h265 main.bitrate=4500 main.resolution=2304x1296 camcfg set rtsp.password=NewStreamPass camcfg set record.continuous_percent=60 record.motion_percent=35 camcfg set system.timezone=CET-1CEST,M3.5.0,M10.5.0/3 camcfg unset main.bitrate # back to the firmware default camcfg reset # drop all your changes camcfg keys # every key and its allowed values camcfg export # your changes as text (no password) camcfg wifi "Network" "Password" now # = wifi-set (§7) camcfg status # IP, Wi-Fi signal, stream, recorder, SD usage ``` Add `-n` after the command (`camcfg set -n …`) to save without restarting the stream. If any value is invalid, nothing is saved. To copy settings to another camera: `ssh root@cam1 camcfg export | grep -v '^#' | xargs ssh root@cam2 camcfg set`. | Key | Values | Default | |---|---|---| | `main.codec` / `sub.codec` | `h264`, `h265` (both supported by the hardware; check your player handles H.265) | h264 | | `main.resolution` / `sub.resolution` | `WxH`, multiples of 8, max 2304x1296 / 1280x720 | 2304x1296 / 640x360 | | `main.bitrate` / `sub.bitrate` | kbps, 256–16000 / 64–4000 | 8000 / 400 | | `main.fps` / `sub.fps` | 1–25 (the stock ISP tuning caps it at 15) | 15 | | `main.level` / `sub.level` | encoder level 10–62 (50 = 5.0) | 50 / 30 | | `rtsp.user`, `rtsp.password` | stream and ONVIF login; empty user = no authentication | admin / root password at build | | `rtsp.port` | 1–65535 | 554 | | `audio.code_type`, `audio.sample_rate` | `aac`/`g711a`/`g711u`, `8000`/`16000` | aac, 16000 | | `record.*` | all recording keys from §6a | see §6a | | `system.timezone` | POSIX TZ: `UTC0`, `CET-1CEST,M3.5.0,M10.5.0/3`, `EST5EDT,M3.2.0,M11.1.0`, … | UTC0 | | `system.ntp` | NTP server | pool.ntp.org | `main.*` is short for `video0.*` and `sub.*` for `video1.*`; `bitrate`, `codec`, `level` are short for `target_kbps`, `code_type`, `enc_level`. The full names work too (`camcfg set video0.target_kbps=6000`). Rough guide for the main stream: H.264 6–8 Mbps, or H.265 4–5 Mbps, for good detail. Each viewer uses that much Wi-Fi. ## 6a. Recording to the SD card With a FAT32 or exFAT card inserted, the camera records by default in two loops that share the card: | Folder | What | Default share of the card | |---|---|---| | `rec/continuous/YYYYMMDD/HHMMSS.ts` | everything, 1-minute files; oldest deleted when full | 70% | | `rec/motion/YYYYMMDD/HHMMSS.ts` | motion clips, starting 5 s before the motion and ending 10 s after it | 25% | At least 200 MB always stays free. Files are MPEG-TS: VLC plays them directly, and `ffmpeg -i x.ts -c copy x.mp4` converts one without re-encoding. Copy them off with `scp root@:/mnt/sd/rec/motion/…` or by reading the card on a PC. **Settings:** `camcfg set record.KEY=VALUE`, e.g. `camcfg set record.mode=motion record.motion_sensitivity=70` (§6b). | Key (`record.…`) | Meaning | |---|---| | `mode` | `off`, `continuous`, `motion` or `both` | | `stream` | `0` main (2304×1296, ~3.6 GB/hour at 8 Mbps H.264, ~2 GB/hour at 4.5 Mbps H.265), `1` substream (much smaller) | | `continuous_percent`, `motion_percent` | **the split**: share of the card each loop may use | | `continuous_max_mb`, `motion_max_mb` | fixed sizes instead (if > 0 they override the percent) | | `continuous_path`, `motion_path` | where each loop writes. For two real partitions, create a 2nd partition on the card (mounted at `/mnt/sd2`) and set `motion_path = /mnt/sd2/motion` | | `min_free_mb` | free space always kept | | `segment_seconds` | length of continuous files | | `motion_pre_seconds`, `motion_post_seconds`, `motion_clip_max_seconds` | how much before and after the motion each clip covers, and maximum clip length | | `motion_sensitivity` | 0–100 (higher = more sensitive) | | `motion_threshold`, `motion_min_cells`, `motion_trigger_frames` | fine tuning: per-cell level, how many of the 32×24 grid cells must move, for how many checks in a row (10 checks per second) | | `motion_debug` | `1` logs the motion level every second in `/tmp/rtsp.log`, for tuning | | `motion_hook` | a script called with `start`/`stop` on motion (default `/data/motion_hook.sh`, if it exists). Use it for MQTT or a webhook | The current motion state is also in `/tmp/motion` (`1