MQTT transport adapter for PubSubClient.
More...
#include <PubSubClientTransport.h>
|
| | PubSubClientTransport (PubSubClient &client) |
| | Construct a PubSubClient transport adapter.
|
| void | setServer (const char *host, uint16_t port, const char *user=nullptr, const char *pass=nullptr) override |
| | Set MQTT server and credentials.
|
| void | setServer (const std::string &host, uint16_t port, const std::string &user="", const std::string &pass="") override |
| | Set MQTT server and credentials (std::string version).
|
| bool | connected () const override |
| bool | publish (const char *topic, const uint8_t *payload, size_t len, bool retained, uint8_t) override |
| void | setOnConnect (void(*cb_)(void *), void *ctx_) override |
| void | tick () override |
| | Detect MQTT connection transitions.
|
| void | setLogger (JBLogger *logger) |
| | Set the logger for this transport.
|
MQTT transport adapter for PubSubClient.
PubSubClient is a synchronous, polling-based MQTT client.
Limitations:
- The host firmware MUST call mqtt.loop() frequently.
- QoS handling is best-effort.
- Connection events are detected via rising-edge logic in tick().
◆ PubSubClientTransport()
| PubSubClientTransport::PubSubClientTransport |
( |
PubSubClient & | client | ) |
|
|
inlineexplicit |
Construct a PubSubClient transport adapter.
- Parameters
-
| client | Reference to an already configured PubSubClient instance |
◆ connected()
| bool PubSubClientTransport::connected |
( |
| ) |
const |
|
inlineoverridevirtual |
Check whether the MQTT client is currently connected.
- Returns
- true if connected, false otherwise
Implements MqttTransport.
◆ publish()
| bool PubSubClientTransport::publish |
( |
const char * | topic, |
|
|
const uint8_t * | payload, |
|
|
size_t | len, |
|
|
bool | retained, |
|
|
uint8_t | ) |
|
inlineoverridevirtual |
Publish an MQTT message.
- Parameters
-
| topic | MQTT topic (null-terminated string) |
| payload | Pointer to payload bytes (may be nullptr) |
| len | Payload length in bytes |
| retained | Whether the message should be retained |
| qos | Requested QoS level (best-effort for some clients) |
- Returns
- true if the publish request was accepted, false otherwise
- Note
- Passing payload == nullptr publishes an empty payload, which is required for Home Assistant entity removal.
Implements MqttTransport.
◆ setOnConnect()
| void PubSubClientTransport::setOnConnect |
( |
void(* | cb_ )(void *), |
|
|
void * | ctx_ ) |
|
inlineoverridevirtual |
Register a callback invoked when the MQTT connection is established.For asynchronous transports, this is wired directly to the MQTT client's onConnect handler.For synchronous transports, this callback is invoked when a rising-edge connection transition is detected via tick().
- Parameters
-
| cb | Callback function |
| ctx | User context pointer passed back to the callback |
Implements MqttTransport.
◆ setServer() [1/2]
| void PubSubClientTransport::setServer |
( |
const char * | host, |
|
|
uint16_t | port, |
|
|
const char * | user = nullptr, |
|
|
const char * | pass = nullptr ) |
|
inlineoverridevirtual |
Set MQTT server and credentials.
- Parameters
-
| host | MQTT host/IP |
| port | MQTT port |
| user | MQTT username |
| pass | MQTT password |
Implements MqttTransport.
◆ setServer() [2/2]
| void PubSubClientTransport::setServer |
( |
const std::string & | host, |
|
|
uint16_t | port, |
|
|
const std::string & | user = "", |
|
|
const std::string & | pass = "" ) |
|
inlineoverridevirtual |
Set MQTT server and credentials (std::string version).
- Parameters
-
| host | MQTT host/IP |
| port | MQTT port |
| user | MQTT username |
| pass | MQTT password |
Implements MqttTransport.
◆ tick()
| void PubSubClientTransport::tick |
( |
| ) |
|
|
inlineoverridevirtual |
Detect MQTT connection transitions.
This method must be called periodically by the host firmware.
It detects a rising-edge transition (disconnected -> connected) and invokes the registered onConnect callback if present.
- Note
- This does NOT replace PubSubClient::loop().
Reimplemented from MqttTransport.
The documentation for this class was generated from the following file: