- MQTTDriver::publishConfirmed(): QoS 1 + PUBACK tracking (8 s; heartbeat 20 s). A missed PUBACK forces a reconnect. status/playback relies on the existing republish-current-state-on-connect; control/reports are resent after the next CONNACK (newest 4). Publish + registration under one recursive mutex since the PUBACK can arrive before publish() returns. - AsyncMqttClient vendored into lib/ (upstream 3d93fc7) with one patch: _onPubAck()/_onPubComp() call _handleQueue(), so a QoS 1 message delays the next packet by one RTT instead of RTT + up to ~500 ms. - platformio.ini: platform and every library pinned to the exact versions already being built (git deps by commit). Clean-libdeps rebuild verified to resolve the same AsyncTCP and the vendored AsyncMqttClient. - Removed board_build.cmake_extra_args (timer stack): no effect on Arduino builds, the SDK is precompiled with 2048. Replaced by a warning comment. - mqtt.status gains resent_messages and the new forced-reset reasons. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5.4 KiB
MQTTTransport
Summary
MQTTTransport wraps MQTTDriver and connects it to the CommandBus. It subscribes to control/command, parses incoming JSON messages into CommandEnvelope objects (including the optional req_id), and dispatches them. Command replies are published back to control/ack. Bus-wide unsolicited broadcasts also go through broadcast() to control/ack — but purpose-specific events (playback transitions, alerts, reports, metrics) are published directly by CommunicationRouter to their own dedicated topics instead, so control/ack stays strictly request/response. The underlying MQTTDriver manages the broker connection lifecycle, reconnect backoff, LWT, and heartbeat independently; MQTTTransport only owns the message-routing glue.
See docs/reference/vesper_mqtt_topic_spec_v2.md for the full topic table.
Public API
| Method | Description |
|---|---|
MQTTTransport(MQTTDriver& mqtt, CommandBus& bus) |
Constructor. Does not start the connection. |
void begin() |
Wires the message callback onto MQTTDriver, then calls MQTTDriver::begin() to configure the AsyncMqttClient. Does not connect to the broker — connection is triggered by onNetworkConnected(). |
void loop() |
No-op. AsyncMqttClient is fully event-driven. |
bool isHealthy() const |
Returns MQTTDriver::isConnected(). |
String name() const |
Returns "MQTT". |
void broadcast(const String& msg) |
Publishes msg to control/ack (bus-wide broadcasts only — see summary above). |
void onNetworkConnected() |
Forwards to MQTTDriver::onNetworkConnected(), which schedules the broker connection after a 2-second stabilisation delay. |
void onNetworkDisconnected() |
Forwards to MQTTDriver::onNetworkDisconnected(). |
Initialization
CommunicationRouter::begin() calls _mqttTransport.begin(), then adds the transport to the bus with _bus.addTransport(&_mqttTransport). Network events from Networking arrive via CommunicationRouter::onNetworkConnected() / onNetworkDisconnected(), which forward to this transport.
Message Format
Incoming control messages (on control/command) must be JSON with at least a "cmd" field:
{ "v": 2, "cmd": "relay.set_durations", "req_id": "abc123", "contents": { "bells": { "0": 500 } } }
"v" defaults to 2 if omitted. "v": 1 triggers legacy translation before dispatch. "req_id" is optional — see CommandEnvelope.hpp / docs/reference/vesper_mqtt_topic_spec_v2.md for the correlation contract.
Responses are published to control/ack:
{ "status": "SUCCESS", "type": "relay.set_durations", "req_id": "abc123", "message": "Bell durations updated and saved" }
req_id is only present in the reply if the request included one.
Dependencies
MQTTDriver— broker connection, publish/subscribe, reconnect, heartbeatCommandBus— dispatch and broadcastArduinoJson,Logging
Gotchas / Notes
control/ackis strictly request/response plus bus-wide broadcasts — never purpose-specific events. The broker does not distinguish a command reply from aCommandBus::broadcast()fan-out; clients must inspect"type"and"status"to differentiate. But playback transitions, alerts, reports, and metrics never appear here — those go to their own topics viaCommunicationRouter(publishPlayback(),publishAlert(),publishReport(),publishMetrics()), bypassing this transport entirely.- Broker connection is deferred.
begin()does not connect. The connection is triggered byonNetworkConnected(), which starts a 2-second non-blocking stabilisation timer inMQTTDriver. - MQTT must be enabled in config, and the
commandtopic switch must be on.MQTTDriver::connect()is a no-op ifmqttConfig.enabledis false. Toggle the master switch withmqtt.enable/mqtt.disable. Independently, thecommandtopic switch (viamqtt.set_topics) gates thecontrol/commandsubscription andcontrol/ackreplies — turning it off while MQTT stays connected silently stops all command traffic. - No per-message QoS. Command replies and broadcasts are published at QoS 0 (see F-072/F-073). Heartbeats use QoS 1 with
retain = true. - JSON parse errors and missing
cmdget anERRORreply oncontrol/ack(type: "parse_error"), not a silent drop — aLOG_WARNINGis still emitted too. A parse-error reply necessarily has noreq_id(it can't be read from input that failed to parse); a missing-cmdreply echoesreq_idif the JSON was otherwise valid and included one. - Incoming commands are dropped under critical heap pressure.
onMessage()checksMQTTDriver::hasHeapForPublish()(free heap ≥ 20KB) before even constructing theJsonDocumentused to deserialize the incoming payload. Below that threshold, the message is dropped with no reply and no error response — indistinguishable from a lost network message to the caller. This exists because a rapid burst of commands (each involving deserialize + dispatch + reply, all allocation-heavy, all running inline on AsyncTCP's own task) previously fragmented the heap enough to crash the device with an unrecoverableabort()— seedocs/features/feature-catalog.mdF-055. A full fix (queueing incoming commands to a dedicated task, so this work never runs on AsyncTCP's task at all) is a scoped future improvement, not yet implemented.