Appearance
Architecture
A ControlFREAK controller is a Raspberry Pi running a stack of Docker containers. It drives pool and spa equipment through Sequent Systems boards, and clients reach it either directly on the home network or through AWS IoT Core.
- The controller is the source of truth. Device state, settings, schedules and themes live in the controller's own SQLite database. AWS holds no device state and there is no device shadow.
- There are two paths to a controller, carrying the same operations over different transports. Local goes straight to the Pi on the LAN. Remote goes through AWS IoT Core. Clients try local first because it costs nothing, and fall back to remote.
- Everything on the Pi talks through the local broker and the local API. Services do not call each other. They publish and subscribe on mosquitto and read and write state through
controlfreak-api.
Containers on the Pi
The stack is defined in rpi-controller/docker-compose.yaml and mqtt-sub-pubs/mqtt-compose.yaml. Every container runs on the host network. The list of containers the updater manages lives in shared-lib/cfp_shared/configs/config.prod.json.
Core
| Container | Source | Role |
|---|---|---|
mosquitto | eclipse-mosquitto:2.0.18 | Local MQTT broker. TCP on 1515, WebSocket on 8080. Also runs the AWS bridge on web enabled controllers. |
controlfreak-api | database-api/ | FastAPI on 8000 over SQLite at /home/pi/sqlite-data. Devices, ports, schedules, themes, tethering, freeze protection, health, logs. Also starts the on demand containers. |
controlfreak-display | display-driver/ | LCD and physical buttons. Owns pairing mode. |
Feature services
Each one subscribes on the local broker, reads its rules from the API, and publishes commands back onto the broker.
| Container | Source | Role |
|---|---|---|
controlfreak-scheduler | scheduler/ | Runs schedules |
controlfreak-tether | tether/ | Runs tethering rules, where one device follows another |
controlfreak-themer | themer/ | Runs light themes and show sequences |
controlfreak-freeze-protection | freeze-protection/ | Turns equipment on when temperatures drop |
Publishers and subscribers
These live in mqtt-sub-pubs/. A publisher reads hardware and publishes controller/<device>/status. A subscriber listens on controller/<device>/state and drives hardware.
| Hardware | Publisher | Subscriber |
|---|---|---|
| Controller heartbeat | publisher-alive, every 60s on controller/status | |
| RS485 bus | publisher-rs485 | subscriber-rs485 |
| Pumps | publisher-pump | handled by subscriber-rs485 |
| High voltage relays | publisher-hv-relay | subscriber-hv-relay |
| Low voltage relays | publisher-lv-relay | subscriber-lv-relay |
| Valves | publisher-valves | subscriber-valves |
| Thermistors | publisher-thermistor | |
| Flow sensors | publisher-flow-sensor | |
| Pressure sensor | publisher-pressure-sensor |
Sensors are read only, so they have no subscriber. subscriber-rs485 is the only process that opens the RS485 bus. It handles both the controller/rs485_<n> topics and the controller/pump<n> topics, which is why there is no separate pump subscriber.
On demand containers
These are not part of the always running stack. controlfreak-api starts them when needed.
| Container | Source | When it runs |
|---|---|---|
controlfreak-api-bridge | api-bridge/ | Always, once the controller is web enabled. Carries REST over MQTT for remote clients. |
aws-installer | aws-installer/ | Once, when a user web enables the controller from the app. |
controlfreak-docker-updater | docker-updater/ | During a controller update, started by POST /prepare-update. |
os-updater | os-updater/ | During a controller update, for OS level steps. |
mqtt-builder also appears in the compose file. It is the base image the MQTT services build on, runs true and exits.
Shared code
shared-lib/cfp_shared is installed into every Python service. It holds the MQTT helpers, API client calls, device state helpers, the RS485 drivers, and the container configs the updater reads.
Local path
The app talks to the controller directly on the LAN.
| What | Where |
|---|---|
| MQTT | ws://controlfreak.local:8080 |
| REST | http://controlfreak.local:8000 |
Only the mobile app uses the local path. The fleet console is cloud only.
Remote path
Remote clients never reach the Pi directly. Everything goes through AWS IoT Core, and the Pi keeps a single outbound connection to it.
The bridge
On a web enabled controller, mosquitto runs a bridge to AWS IoT Core on port 8883 with mutual TLS, using the controller's own certificate and its thing name as the client id. The bridge maps the local controller/... topics to devices/<thing_name>/controller/... in the cloud. The config is written by aws-installer/scripts/03-mosquitto-bridge.sh to /home/pi/configs/conf.d/bridge.conf.
| Direction | Local topic |
|---|---|
| Pi to AWS | controller/+/status |
| Pi to AWS | controller/status |
| Pi to AWS | controller/api/response/+ |
| AWS to Pi | controller/+/state |
| AWS to Pi | controller/update/start, controller/update/restore |
| AWS to Pi | controller/themes/start, controller/themes/stop |
| AWS to Pi | controller/freeze/pause, controller/freeze/resume |
| AWS to Pi | controller/api/request |
controller/status needs its own line because controller/+/status only matches three level topics. Anything not listed here stays on the Pi.
REST over MQTT
Remote clients use the same REST calls as local ones. In the app, mobile-controller/src/api/MQTTAdapter.ts is an axios adapter that wraps each request in a message with a request id and publishes it to devices/<thing_name>/controller/api/request. On the Pi, api-bridge makes the real HTTP call to controlfreak-api and publishes the result to controller/api/response/<request_id>.
Two limits come with it. api-bridge waits 15 seconds for the API, then answers 504. It refuses responses over 120 KB with a 413, because AWS IoT drops connections past 128 KB.
Identity and access
| Piece | Where | What it does |
|---|---|---|
| Cognito user pool | infrastructure/cognito | Signs users in and issues the tokens everything else checks |
| IoT custom authorizer | infrastructure/authenticator | Runs on every client MQTT connect, builds the topic policy from the pairings table |
| Fleet API | infrastructure/fleet-list | API Gateway with a Cognito JWT authorizer in front of a Lambda. Lists and names controllers, creates and removes pairings. |
controlfreak-pairings | DynamoDB, infrastructure/fleet-list | Which accounts may reach which controller |
controlfreak-pairing-windows | DynamoDB, infrastructure/fleet-list | Which controllers are in pairing mode right now |
| Fleet provisioning | infrastructure/fleet-provisioning | Creates each controller's thing and certificate on first connect |
The Cognito token says who a user is. The pairings table says what they may reach. A controller's thing name is its identity and never changes. Its display name is stored on the controller and on the thing's name attribute.
Key flows
A remote command
- The app publishes to
devices/<thing>/controller/pump1/stateon IoT Core. The authorizer has already limited the connection to controllers the account is paired with. - The bridge delivers it to
controller/pump1/stateon the Pi. subscriber-rs485drives the pump.publisher-pumppublishescontroller/pump1/status, which goes back out through the bridge.
Locally the same topics are used, straight on the Pi's broker.
Web enabling a controller
- The user starts web enablement from the app. The app asks
controlfreak-apito run theaws-installerimage. - The installer uses the claim certificate baked into its image to run fleet provisioning, which creates the controller's thing and its own certificate.
- It writes the bridge config, restarts mosquitto, and hands the cloud connection to the bridge.
aws-iot-device-clientis stopped, since both would connect with the same client id. - It reports the thing name back to
controlfreak-api, which then startsapi-bridge.
Pairing an account
- The user holds the two left buttons until the display shows PAIRING.
controlfreak-displayopens a 120 second window and publishescontroller/pairing/status. - The bridge sends it to AWS, where the IoT rule
controlfreak_pairing_windowwrites it to thecontrolfreak-pairing-windowstable. - The app calls
POST /fleet/pairings. The fleet Lambda checks the window is open and writes the pairing. - On its next connect, the authorizer grants the account that controller's topics.
Being physically at the controller is what allows a claim. A controller can be paired with more than one account.
Updating a controller
- The app calls
POST /prepare-update.controlfreak-apipulls and startscontrolfreak-docker-updater. - The updater backs up the database, compares each image with the registry, snapshots the ones that changed, runs
os-updater, restarts the changed services and prunes old images. - Progress is written to the database and read back through
GET /health, so the app follows it the same way over local and remote.
See Releasing for how new images get published.
Hardware
The Pi drives the equipment through a stack of Sequent Systems boards over I²C, using the cfcrpi library in rpi-controller/cfcrpi/. Pumps and other RS485 devices sit on the boards' RS485 ports. The LCD and the physical buttons are driven by controlfreak-display.
For development without hardware, sequentulator/ emulates the boards and smulator/ emulates the display. Services use the emulator when USE_EMULATOR=1 is set.
Where to look
| To change | Look in |
|---|---|
| Which containers run and how | docker-compose.yaml, mqtt-sub-pubs/mqtt-compose.yaml |
| What the updater manages | shared-lib/cfp_shared/configs/config.prod.json |
| REST endpoints | database-api/app/endpoints/ |
| Topics and payloads | docs/MQTTAPI.md in rpi-controller |
| The AWS bridge | aws-installer/scripts/03-mosquitto-bridge.sh |
| Broker listeners | configs/mosquitto.conf |
| AWS resources | infrastructure/ |