MQTT: MQ Telemetry Transport

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

Client API

enum MqttClientState

Values:

enumerator eMCS_Ready
enumerator eMCS_SendingData
using MqttDelegate = Delegate<int(MqttClient &client, mqtt_message_t *message)>
using MqttRequestQueue = ObjectQueue<mqtt_message_t, MQTT_REQUEST_POOL_SIZE>
MQTT_REQUEST_POOL_SIZE
MQTT_CLIENT_CONNECTED
MQTT_FLAG_RETAINED
class MqttClient : protected TcpClient
#include <MqttClient.h>

Public Functions

inline void setKeepAlive(uint16_t seconds)

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

Parameters

seconds

inline 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

Returns

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

Returns

bool

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

Publish a message.

Parameters
  • topic

  • message – Message content as String

  • flags – Optional flags

Returns

bool

bool publish(const String &topic, IDataSourceStream *stream, uint8_t flags = 0)

Publish a message.

Parameters
  • topic

  • message – Message content as read-only stream

  • flags – Optional flags

Returns

bool

bool subscribe(const String &topic)

Subscribe to a topic.

Parameters

topic

Returns

bool

bool unsubscribe(const String &topic)

Unsubscribe from a topic.

Parameters

topic

Returns

bool

inline void setEventHandler(mqtt_type_t type, MqttDelegate handler)

Register a callback function to be invoked on incoming event notification.

Parameters
  • type – Type of event to be notified of

  • handler – The callback. Pass nullptr to cancel callback.

inline 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

inline void setConnectedHandler(MqttDelegate handler)

Sets a handler to be called after successful MQTT connection.

Parameters

handler

inline 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

inline void setMessageHandler(MqttDelegate handler)

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

Parameters

handler

inline void setDisconnectHandler(TcpClientCompleteDelegate handler)

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

Parameters

handler

Public Static Functions

static inline uint8_t getFlags(mqtt_qos_t QoS, mqtt_retain_t retain = MQTT_RETAIN_FALSE, mqtt_dup_t dup = MQTT_DUP_FALSE)

Compute the flags value.

Parameters
  • QoS – - Quality of Service

  • retain – - Retain flag

  • dup – - Duplicate delivery

Returns

uint8_t – calculated flags value