Skip to content

MqttModule

Source: MqttModule.h

MqttModule

class MqttModule
src/core/system/MqttModule.h:40

Inherits: MoonModule

Bridges the light's controls to an MQTT broker, so a home-automation hub can drive it.

A network sub-service, wired by code beside the other network children. Every command routes through the shared control primitive, adding only a transport.

MqttModule card

Public Methods

inline void setSystemModule(SystemModule * s) : Adopt the system module, whose name and address the published topics carry.

inline void setControlModule(ControlModule * c) : Publish presets as the hub's effect list, look-only ones alone so nothing rewires hardware.

virtual void setup() override : Prime the status line before the first connect attempt.

virtual void release() override : Free the discovery buffers, which are allocated only while announcing.

virtual void defineControls() override : Declare the broker settings, the discovery opt-in and the status readout.

virtual void onControlChanged(const char * controlName) override : Re-home the socket when the broker, port or credentials change.

virtual void onEnabled(bool enabled) override : Connect on enable, and disconnect cleanly on disable.

virtual void tick1s() override : Drive the connection: connect, keep alive, drain inbound, and publish on change.

void feedForTest(const uint8_t * bytes, size_t len) : Feed inbound bytes as if from the broker, since a test has no live one.

void enableSendCaptureForTest(uint8_t * buf, size_t cap) : Capture every outbound packet, so a test can assert what the module emits.

inline size_t sentCaptureLenForTest() const : How many bytes that capture holds.

Public Static Attributes

constexpr size_t kDiscoveryDynamicBytes = 320 + 448 : The heap floor while discovery announces, so a test asserts against this not a literal.

More info

The client is our own

MQTT 3.1.1 is small and standard, so the wire format lives in a tested header. This module owns only the socket lifecycle. The topic prefix derives from a stable hardware id, so a rename never repoints topics. The friendly name rides its own retained topic instead.

Home Assistant discovery

With the opt-in on, a retained config makes a discovery-aware hub create a wired entity. It defaults off because the compatibility shim already gives that hub a richer light. The same gate publishes an update entity, where a firmware update surfaces.

Lifecycle

Everything runs on the slow tick, off the render path, since MQTT is slow control. It connects lazily, subscribes, keeps alive, and publishes on change and on connect. A dropped socket reconnects with a backoff.