Appearance
Onboard development
Connecting
bash
ssh -p 2222 pi@controlfreak.localControllers listen for SSH on port 2222. The development password is password.
controlfreak.local resolves over mDNS. If it does not resolve, use the Pi's IP address instead.
Layout on the Pi
| Path | What it is |
|---|---|
/home/pi | Where the repo is unzipped, and the Compose project directory |
/home/pi/.env | The environment Compose reads |
/home/pi/sqlite-data/controller.db | The live database |
/home/pi/configs/ | mosquitto config, including conf.d/bridge.conf and certs/ on web enabled controllers |
/home/pi/backup/ | Database backups taken by the updater |
/certs | Claim certificates, mounted read only into controlfreak-api |
The sqlite_data/ folder the repo carries is not used on the Pi.
Setting up a new Pi
New controllers are set up by flashing a release image, not by installing from scratch.
Flashing an image
Release images are gzipped .img files named controlfreak-stable-<build>.img.gz, kept in build/img/ in the workspace. Write the newest one to the controller's drive with Raspberry Pi Imager, which takes the .img.gz directly, or with dd:
bash
gunzip -c build/img/controlfreak-stable-15b.img.gz | sudo dd of=/dev/sdX bs=4M status=progress conv=fsyncOn first boot the root filesystem grows to fill the drive, and the controller generates its own machine id and SSH host keys. Then:
- Join the controller to WiFi from the app.
- Update it from Settings, Help in the app, since the image can be older than the current release.
Building an image
An image is taken from a controller that is set up and running the release.
bash
cd rpi-controller
DRY_RUN=1 ./scripts/clean_controller.sh
./scripts/clean_controller.shclean_controller.sh runs in two phases over SSH:
- State. Clears the home directory apart from a keep list, the logs, and every container and image the updater does not manage.
- Identity. Removes the AWS IoT credentials, the SSH host keys and the WiFi profiles, and resets the machine id. It checks everything is gone, refuses to continue if anything is left, then powers the Pi off.
Do not boot the drive again, or it regenerates the identity the script cleared. Move it to a workstation and image it:
bash
sudo DRY_RUN=1 ./scripts/make-image.sh /dev/sdX controlfreak-stable-16b.img
sudo ./scripts/make-image.sh /dev/sdX controlfreak-stable-16b.imgmake-image.sh shrinks the root filesystem and partition on the source drive, copies only the used space, then runs the vendored libs/pishrink.sh to set up the first boot expand and gzip the result. It refuses to run against a disk the workstation booted from.
| Option | Effect |
|---|---|
DRY_RUN=1 | Show the plan, write nothing |
NO_SHRINK=1 | Skip pishrink, keep the raw .img |
ASSUME_YES=1 | Skip the confirmation prompt |
ARM_EXPAND=1 | Use a systemd unit for the first boot expand instead of pishrink's rc.local |
MARGIN_MB=32 | Free space left in the partition |
The source drive is shrunk in place. Flash it from the new image, or grow it back:
bash
echo "," | sudo sfdisk -N 2 --force /dev/sdX && sudo resize2fs /dev/sdX2Flash the new image to a spare drive and run it through setup, pairing and remote control before using it.
Pushing changes
bash
./flash-controller-dev.sh controlfreak.localThe script zips the working tree, copies it to /home/pi, unzips it over what is there, removes every controller container, and runs docker compose up --build -d --force-recreate. The images are built on the Pi, from your working tree.
Working with containers
Compose commands run from /home/pi and take the container name, such as controlfreak-subscriber-rs485, not the folder name.
bash
cd /home/pi
sudo docker compose ps
sudo docker logs -f controlfreak-api
sudo docker compose up -d --force-recreate controlfreak-subscriber-rs485After changing /home/pi/.env, recreate the containers that read the variable with --force-recreate. A plain restart keeps the old environment.
MQTT on the Pi
mosquitto listens on 1515 for TCP and 8080 for WebSocket, and allows anonymous connections. From the Pi:
bash
sudo docker exec -it mosquitto mosquitto_sub -p 1515 -t 'controller/#' -v
sudo docker exec -it mosquitto mosquitto_pub -p 1515 -t controller/pump1/state -m '<payload>'From a laptop on the same network, mosquitto_sub -h controlfreak.local -p 1515 works the same way.
Reaching the API
bash
curl http://controlfreak.local:8000/healthThe interactive API docs are at http://controlfreak.local:8000/docs.
Testing the AWS installer
The installer scripts are baked into the aws-installer image, so editing a script locally and running the installer again runs the old code. Rebuild the image on the Pi before each run. The build context needs the same three roots the Dockerfile copies from: aws-installer/, certs/ and libs/aws-iot-device-client.
After a run, check the generated files on the Pi, for example /home/pi/configs/conf.d/bridge.conf, rather than trusting that the run used your change.
Any copy of the build context on the Pi contains the claim private key. Delete it when you are done.
To put a controller back to its state before web enablement:
bash
DRY_RUN=1 ./scripts/pre-ota-reset.sh
./scripts/pre-ota-reset.shIt removes every provisioning certificate, removes the bridge include from mosquitto.conf and removes api-bridge. It does not touch the controller database or the thing and certificate in AWS.
Bench scripts
debug-scripts/ holds hardware investigations that run on a Pi.
| Folder | What it is for |
|---|---|
debug-scripts/rs485-bench/ | Identifying and exercising an RS485 unit on the bench. Stops controlfreak-subscriber-rs485 while it runs. |
debug-scripts/memleak/ | Reproducing kernel slab exhaustion and the restart storms it causes |
Each folder has its own README.