Skip to content

Local development ​

The whole controller stack runs on a laptop with no hardware attached. sequentulator emulates the Sequent boards and smulator emulates the LCD, and the services talk to them through shared memory files in /tmp.

Everything here runs from the root of rpi-controller.

Prerequisites ​

  • Docker with Compose
  • Python 3.11 with pyserial, for the board emulator
  • make

Configure ​

bash
cp .env.template .env

Then set the two addresses the services use to find each other. The dev stack runs on a Compose network where containers resolve by name, and the broker address is a bare hostname, not a URL.

bash
API_URL=http://controlfreak-api:8000
MQTT_BROKER_URL=mosquitto
VariableValueWhat it does
USE_EMULATOR1Services use sequentulator and smulator instead of real hardware
RS485_EMULATOR01 answers every RS485 port from an emulated unit
RS485_POLL_SECONDS15How often each RS485 port is polled
API_URLhttp://controlfreak-api:8000Where services reach controlfreak-api
MQTT_BROKER_URL, MQTT_BROKER_PORTmosquitto, 1515Where services reach the broker
DATABASE_PATHsqlite:////app/data/controller.dbThe API's database, inside the container
DOCKER_REGISTRYemptyRegistry baked into locally built images. Empty means krumcontrolfreak.
AWS_IOT_*Only needed to run the AWS installer against a real IoT endpoint

Build and start ​

bash
make dev-build
make dev

make dev-build builds the mqtt-builder base image, then every service on top of it. make dev starts the stack. On first start the API seeds a default database into database-api/data/controller.db.

Run the emulators ​

Each emulator is a terminal UI. Run each one in its own terminal:

bash
make emulator    # sequentulator, the boards
make smulator    # smulator, the LCD

sequentulator shows the relays, valves, flow sensors, thermistors and RS485 ports, and takes commands at its prompt. Type help for the list. Its state lives in /tmp/sequentulator/sequentulator.mmap, and smulator uses /tmp/smulator/smulator.mmap. Both folders are mounted into the containers that need them.

To emulate RS485 devices as well, set RS485_EMULATOR=1 in .env and recreate the RS485 containers:

bash
docker compose -f docker-compose.dev.yaml up -d --force-recreate \
  controlfreak-subscriber-rs485 controlfreak-publisher-rs485

What is running ​

ServiceAddress on the laptop
REST APIhttp://localhost:8000, docs at http://localhost:8000/docs
MQTT over TCPlocalhost:1515
MQTT over WebSocketws://localhost:8080

The local broker allows anonymous connections.

bash
docker compose -f docker-compose.dev.yaml ps
docker logs -f controlfreak-api
mosquitto_sub -h localhost -p 1515 -t 'controller/#' -v

Making changes ​

What changedWhat to do
database-api/Nothing. The source is mounted into the container and uvicorn reloads on save.
A single service, such as scheduler/ or a publisherRebuild and restart just that service.
shared-lib/, sequentulator/, smulator/ or the vendored firmwareRebuild everything with make dev-build, since these are baked into mqtt-builder.
.envRecreate the affected containers with --force-recreate.

Rebuilding one service:

bash
docker compose -f docker-compose.dev.yaml up -d --build --no-deps controlfreak-scheduler

Use the container name, controlfreak-scheduler, not the folder name.

Connecting the mobile app ​

Point the app at the laptop by setting EXPO_PUBLIC_DEFAULT_HOST in mobile-controller/.env to the laptop's LAN address, then start the app as usual.

Stopping ​

bash
make dev-down

The database in database-api/data/ survives a restart. Delete database-api/data/controller.db to start over from the seeded defaults.

Troubleshooting ​

Services cannot open the emulator files. The folders in /tmp can end up owned by root after a container creates them. Reset them:

bash
sudo rm -f /tmp/sequentulator/sequentulator.lock /tmp/sequentulator/sequentulator.mmap
mkdir -p /tmp/sequentulator
sudo chown -R $USER:$USER /tmp/sequentulator

Do the same for /tmp/smulator if the display emulator has the problem.

A service keeps restarting. Every container restarts on failure, so a crash shows up as a loop. Check its logs with docker logs <container>. The usual causes are the API not being up yet, which clears on its own, or a missing emulator file, which the fix above solves.

A change to shared-lib does not show up. The service is still using the old mqtt-builder. Run make dev-build.