MQTT: MQ Telemetry Transport

https://en.m.wikipedia.org/wiki/MQTT

Client API

enum mqttclient::MqttClientState

Values:

eMCS_Ready = 0
eMCS_SendingData
using MqttDelegate = Delegate<int(MqttClient &client, mqtt_message_t *message)>
using MqttRequestQueue = ObjectQueue<mqtt_message_t, MQTT_REQUEST_POOL_SIZE>
using MqttStringSubscriptionCallback = Delegate<void(String topic, String message)>

using MqttMessageDeliveredCallback = Delegate<void(uint16_t msgId, int type)>

MQTT_REQUEST_POOL_SIZE
MQTT_CLIENT_CONNECTED
MQTT_FLAG_RETAINED
MQTT_MAX_BUFFER_SIZE

MQTT_MSG_PUBREC

class MqttClient : protected TcpClient
#include <MqttClient.h>

Public Functions

void setKeepAlive(uint16_t seconds)

Sets keep-alive time. That information is sent during connection to the server.

Parameters
  • seconds:

void setPingRepeatTime(uint16_t seconds)

Sets the interval in which to ping the remote server if there was no activity

Parameters
  • seconds:

bool setWill(const String &topic, const String &message, uint8_t flags = 0)

Sets last will and testament

Parameters
  • topic:

  • message:

  • flags: QoS, retain, etc flags

Return Value
  • bool:

bool connect(const Url &url, const String &uniqueClientName)

Connect to a MQTT server.

Parameters
  • url: URL in the form “mqtt://user:password@server:port” or “mqtts://user:password@server:port”

  • uniqueClientName:

Return Value
  • bool:

void setPayloadParser(MqttPayloadParser payloadParser = nullptr)

Sets or clears a payload parser (for PUBLISH messages from the server to us)

Note

We no longer have size limitation for incoming or outgoing messages but in order to prevent running out of memory we have a “sane” payload parser that will read up to 1K of payload

void setConnectedHandler(MqttDelegate handler)

Sets a handler to be called after successful MQTT connection.

Parameters
  • handler:

void setPublishedHandler(MqttDelegate handler)

Sets a handler to be called after receiving confirmation from the server for a published message from the client.

Parameters
  • handler:

void setMessageHandler(MqttDelegate handler)

Sets a handler to be called after receiving a PUBLISH message from the server.

Parameters
  • handler:

void setDisconnectHandler(TcpClientCompleteDelegate handler)

Sets a handler to be called on disconnect from the server.

Parameters
  • handler:

bool setWill(const String &topic, const String &message, int QoS, bool retained = false)

bool publishWithQoS(const String &topic, const String &message, int QoS, bool retained = false, MqttMessageDeliveredCallback onDelivery = nullptr)

bool publish(String& topic, String& message, bool retained = false) Use publish(const String& topic, const String& message, uint8_t flags = 0) instead.

void setCallback(MqttStringSubscriptionCallback subscriptionCallback = nullptr)

Provide a function to be called when a message is received from the broker.