Skip to content

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.

  1. 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.
  2. 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.
  3. 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 ​

ContainerSourceRole
mosquittoeclipse-mosquitto:2.0.18Local MQTT broker. TCP on 1515, WebSocket on 8080. Also runs the AWS bridge on web enabled controllers.
controlfreak-apidatabase-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-displaydisplay-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.

ContainerSourceRole
controlfreak-schedulerscheduler/Runs schedules
controlfreak-tethertether/Runs tethering rules, where one device follows another
controlfreak-themerthemer/Runs light themes and show sequences
controlfreak-freeze-protectionfreeze-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.

HardwarePublisherSubscriber
Controller heartbeatpublisher-alive, every 60s on controller/status
RS485 buspublisher-rs485subscriber-rs485
Pumpspublisher-pumphandled by subscriber-rs485
High voltage relayspublisher-hv-relaysubscriber-hv-relay
Low voltage relayspublisher-lv-relaysubscriber-lv-relay
Valvespublisher-valvessubscriber-valves
Thermistorspublisher-thermistor
Flow sensorspublisher-flow-sensor
Pressure sensorpublisher-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.

ContainerSourceWhen it runs
controlfreak-api-bridgeapi-bridge/Always, once the controller is web enabled. Carries REST over MQTT for remote clients.
aws-installeraws-installer/Once, when a user web enables the controller from the app.
controlfreak-docker-updaterdocker-updater/During a controller update, started by POST /prepare-update.
os-updateros-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.

WhatWhere
MQTTws://controlfreak.local:8080
RESThttp://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.

DirectionLocal topic
Pi to AWScontroller/+/status
Pi to AWScontroller/status
Pi to AWScontroller/api/response/+
AWS to Picontroller/+/state
AWS to Picontroller/update/start, controller/update/restore
AWS to Picontroller/themes/start, controller/themes/stop
AWS to Picontroller/freeze/pause, controller/freeze/resume
AWS to Picontroller/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 ​

PieceWhereWhat it does
Cognito user poolinfrastructure/cognitoSigns users in and issues the tokens everything else checks
IoT custom authorizerinfrastructure/authenticatorRuns on every client MQTT connect, builds the topic policy from the pairings table
Fleet APIinfrastructure/fleet-listAPI Gateway with a Cognito JWT authorizer in front of a Lambda. Lists and names controllers, creates and removes pairings.
controlfreak-pairingsDynamoDB, infrastructure/fleet-listWhich accounts may reach which controller
controlfreak-pairing-windowsDynamoDB, infrastructure/fleet-listWhich controllers are in pairing mode right now
Fleet provisioninginfrastructure/fleet-provisioningCreates 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 ​

  1. The app publishes to devices/<thing>/controller/pump1/state on IoT Core. The authorizer has already limited the connection to controllers the account is paired with.
  2. The bridge delivers it to controller/pump1/state on the Pi.
  3. subscriber-rs485 drives the pump.
  4. publisher-pump publishes controller/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 ​

  1. The user starts web enablement from the app. The app asks controlfreak-api to run the aws-installer image.
  2. The installer uses the claim certificate baked into its image to run fleet provisioning, which creates the controller's thing and its own certificate.
  3. It writes the bridge config, restarts mosquitto, and hands the cloud connection to the bridge. aws-iot-device-client is stopped, since both would connect with the same client id.
  4. It reports the thing name back to controlfreak-api, which then starts api-bridge.

Pairing an account ​

  1. The user holds the two left buttons until the display shows PAIRING. controlfreak-display opens a 120 second window and publishes controller/pairing/status.
  2. The bridge sends it to AWS, where the IoT rule controlfreak_pairing_window writes it to the controlfreak-pairing-windows table.
  3. The app calls POST /fleet/pairings. The fleet Lambda checks the window is open and writes the pairing.
  4. 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 ​

  1. The app calls POST /prepare-update. controlfreak-api pulls and starts controlfreak-docker-updater.
  2. 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.
  3. 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 changeLook in
Which containers run and howdocker-compose.yaml, mqtt-sub-pubs/mqtt-compose.yaml
What the updater managesshared-lib/cfp_shared/configs/config.prod.json
REST endpointsdatabase-api/app/endpoints/
Topics and payloadsdocs/MQTTAPI.md in rpi-controller
The AWS bridgeaws-installer/scripts/03-mosquitto-bridge.sh
Broker listenersconfigs/mosquitto.conf
AWS resourcesinfrastructure/