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>
using MqttStringSubscriptionCallback = Delegate<void(String topic, String message)>

Deprecated:

Use MqttDelegate instead

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

Deprecated:

Use MqttDelegate instead

MQTT_REQUEST_POOL_SIZE
MQTT_CLIENT_CONNECTED
MQTT_FLAG_RETAINED
MQTT_MAX_BUFFER_SIZE

Deprecated:

MQTT_MSG_PUBREC

Deprecated:

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

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

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

Deprecated:

: Use setWill(const String& topic, const String& message,uint8_t flags) instead

inline 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.

Deprecated:

: Use publish(const String& topic, const String& message, uint8_t flags = 0) instead. If you want to have a callback that should be triggered on successful delivery of messages then use setEventHandler(MQTT_TYPE_PUBACK, youCallback) instead.

inline void setCallback(MqttStringSubscriptionCallback subscriptionCallback = nullptr)

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

Deprecated:

: Use setEventHandler(MQTT_TYPE_PUBLISH, MqttDelegate handler) instead.

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