# The Familiar · Pi setup

How to turn a fresh Raspberry Pi 5 into the Familiar's brain: the show page in a
kiosk browser, the hardware bridge, a read-only card and watchdogs. Starter
files, **not yet tested on hardware**. Follow it once on the bench, fix what
breaks, and update this file.

The files in this folder:

| File | Goes to | What it is |
|---|---|---|
| `familiar_bridge.py` | `/opt/familiar/pi/` | WebSocket 127.0.0.1:8765, serial to the light node, reed switches and distance sensor |
| `familiar-bridge.service` | `/etc/systemd/system/` | runs the bridge, restarts it, watchdog every 30 s |
| `familiar-site.service` | `/etc/systemd/system/` | serves the show on 127.0.0.1:8080 |
| `serve-site.sh`, `mount-stick.sh` | `/opt/familiar/pi/` | serve the USB stick's copy if present, else the card's |
| `kiosk.sh` | `/opt/familiar/pi/` | waits for the server, keeps Chromium full screen |
| `labwc-autostart` | `~/.config/labwc/autostart` | starts `kiosk.sh` at login |
| `config.txt.composite` | read it | composite video lines per Pi model (TV options B and C only) |
| `cmdline.txt.snippets` | read it | optional kernel command line additions |

## 1. Flash and first boot (at home, on a wired network)

1. In Raspberry Pi Imager pick **Raspberry Pi OS (64-bit)** with desktop. The
   current release is based on Debian **Trixie**; Bookworm is the "Legacy"
   image. Set hostname `familiar`, user `familiar`, no Wi-Fi.
2. Boot, then update once and install the bridge's libraries from apt. No pip:
   Raspberry Pi OS only allows pip inside a virtual environment.

   ```sh
   sudo apt update && sudo apt full-upgrade -y
   sudo apt install -y python3-websockets python3-serial python3-gpiozero python3-lgpio
   ```

   Versions we expect: websockets 15.0.1 on Trixie (10.4 on Bookworm; the
   bridge works on both), pyserial 3.5, gpiozero 2.0.1. On a Pi 5, lgpio is the
   only GPIO backend gpiozero can use.

## 2. Put the show on the card

```sh
sudo mkdir -p /opt/familiar
sudo cp -r ~/spellworks /opt/familiar/site      # this whole site folder
sudo cp -r /opt/familiar/site/pi /opt/familiar/pi
sudo chmod +x /opt/familiar/pi/*.sh
sudo chown -R familiar:familiar /opt/familiar
```

Chromium will not load ES modules from `file://` pages, so the show is served
over `http://localhost`, which also counts as a secure context.

## 3. Services

```sh
sudo cp /opt/familiar/pi/familiar-site.service /opt/familiar/pi/familiar-bridge.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now familiar-site familiar-bridge
systemctl status familiar-site familiar-bridge
```

Check the bridge from the Pi's own browser with the bridge tester in the
handbook (section 05), transport "Pi socket", address `ws://localhost:8765`.

### The voice: Piper and sox

The bridge speaks every `{"t":"say"}` line with Piper, an offline voice, and
gives each of the six characters its own model and a small sox effect chain
(the child pitched up, Spellbound deep with a long echo, calm slowed down).
It needs three voice models and sox; espeak-ng is the fallback.

```sh
sudo apt install -y sox espeak-ng
sudo mkdir -p /opt/familiar/voices
cd /opt/familiar/voices
# from the Piper voices collection: the .onnx and its .onnx.json for each
#   en_US-ryan-medium    (the Familiar, Spellbound, the tube)
#   en_US-amy-medium     (the child, calm)
#   en_US-lessac-medium  (the grown up)
```

Test one character from the Pi's terminal before the show:

```sh
echo "Hello. I am the Familiar." | piper --model /opt/familiar/voices/en_US-ryan-medium.onnx --output_file /dev/shm/t.wav \
  && sox /dev/shm/t.wav /dev/shm/t2.wav pitch -150 reverb 18 && aplay /dev/shm/t2.wav
```

In the console, pick the voice engine "Piper on the Pi" in Show settings, then
the character in the bar. The kiosk starts Chromium with
`--autoplay-policy=no-user-gesture-required`, so sound and speech never wait
for a click.

## 4. Kiosk

Current Raspberry Pi OS runs the **labwc** Wayland compositor on every model
(default since the 2024-10-22 release). It is started with `--merge-config`,
so your autostart runs in addition to the system one: the desktop still starts
underneath and the full-screen browser covers it.

```sh
mkdir -p ~/.config/labwc
cp /opt/familiar/pi/labwc-autostart ~/.config/labwc/autostart
sudo raspi-config nonint do_boot_behaviour B4     # desktop autologin
sudo raspi-config nonint do_blanking 1            # 1 = screen blanking OFF
```

Run the `raspi-config` lines with `sudo` from the `familiar` account:
raspi-config uses `$SUDO_USER` to find whose settings to change.

Notes:

- The browser is `chromium` on Trixie (`chromium-browser` on older Bookworm
  images); `kiosk.sh` tries both.
- `--autoplay-policy=no-user-gesture-required` turns the autoplay policy off,
  Web Audio included, so the music box plays without a click.
- There is no global way to hide the pointer under Wayland; the show page hides
  it itself with `cursor: none`.
- Old Bookworm images on **wayfire**: add `[autostart]` and
  `kiosk = /opt/familiar/pi/kiosk.sh` to `~/.config/wayfire.ini` instead.

Optional, for a Web Serial page in the kiosk with no permission click: the
Chromium policy `SerialAllowUsbDevicesForUrls` in
`/etc/chromium/policies/managed/serial.json` (read the board's IDs with
`lsusb`). We do not need it while the bridge talks to the node.

## 5. Offline

In `/boot/firmware/config.txt`:

```ini
dtoverlay=disable-wifi
dtoverlay=disable-bt
```

Keep logs in RAM: in `/etc/systemd/journald.conf` set `Storage=volatile`.
Nothing on the Pi listens outside 127.0.0.1.

## 6. Watchdogs

In `/etc/systemd/system.conf` (or a drop-in in `/etc/systemd/system.conf.d/`):

```ini
[Manager]
RuntimeWatchdogSec=15s
```

The Pi's hardware watchdog tops out at about 16 s, so 15 s keeps it as the last
line of defence. The bridge sends `WATCHDOG=1` to systemd every 10 s
(`WatchdogSec=30` in its unit), with the standard library only.

## 7. Read-only card (do this last)

```sh
sudo raspi-config nonint do_overlayfs 0     # enable the overlay AND write-protect /boot/firmware
sudo reboot
```

Same as raspi-config > Performance Options > Overlay File System. Every write
now goes to RAM and is gone at the next boot, including logs and the browser
profile. To change anything:

```sh
sudo raspi-config nonint do_overlayfs 1 && sudo reboot   # overlay off
# ...change things, apt upgrade, copy a new site...
sudo raspi-config nonint do_overlayfs 0 && sudo reboot   # overlay on again
```

Never run apt upgrades with the overlay on.

## 8. Updating spells from a USB stick

Format a stick FAT32, label it `FAMILIAR`, copy this site folder onto it as
`spellworks/`. At boot `mount-stick.sh` mounts it read-only at `/mnt/familiar`
and `serve-site.sh` serves it if `spellworks/output/index.html` is there.
Plug in, cycle the power, done. Pull the stick and cycle again to roll back.

## Composite video (TV options B and C only) {#composite}

Option A (panel behind the glass) uses HDMI and needs none of this. For a real
tube, see `config.txt.composite`, or let raspi-config do it:

```sh
sudo raspi-config nonint do_composite 0    # writes dtoverlay=vc4-kms-v3d,composite and enable_tvout=1
sudo reboot
```

- **Pi 4:** 3.5 mm 4-pole AV jack. Composite turns HDMI off on a Pi 4, so boot
  with HDMI unplugged. Common pinout (community-sourced, check with a meter):
  tip left audio, ring 1 right audio, ring 2 ground, sleeve video.
- **Pi 5:** no AV jack; composite comes out of the two-hole **J7 "VID"** pad
  next to the HDMI sockets, and HDMI keeps working. Which pad is ground is not
  documented: test continuity to a known ground (GPIO header pin 6) first, and
  solder a 2-pin header rather than bare wires.
- **Zero 2 W:** the `TV` test pad on the underside, with a `GND` pad beside it.
- NTSC is the default, so 480i NTSC needs nothing in `cmdline.txt`.

## Sources we checked

- Raspberry Pi documentation (config.txt video, configuration, power supplies,
  performance, frequency management): github.com/raspberrypi/documentation
- raspi-config source, trixie branch: github.com/RPi-Distro/raspi-config
- Raspberry Pi OS release notes: downloads.raspberrypi.com/raspios_arm64/release_notes.txt
- websockets changelog: websockets.readthedocs.io
- gpiozero API docs: gpiozero.readthedocs.io
- systemd: freedesktop.org man pages for systemd-system.conf, systemd.service, sd_notify
- Chrome autoplay policy: developer.chrome.com/blog/autoplay
- MDN: Web Serial API, secure contexts, JavaScript modules

Still to confirm on real hardware: the Pi 5 J7 pad polarity, a tested
`video=` mode line, and whether `--password-store=basic` is ever needed.
