MQTT: MQ Telemetry Transport

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

Client API

enum mqttclient::MqttClientState

Values:

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

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

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

MqttClient(bool withDefaultPayloadParser = true, bool autoDestruct = false)
~MqttClient()
void setKeepAlive(uint16_t seconds)

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

Parameters
  • seconds:

void setPingRepeatTime(unsigned 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:

bool publish(const String &topic, const String &message, uint8_t flags = 0)
bool publish(const String &topic, IDataSourceStream *stream, uint8_t flags = 0)
bool subscribe(const String &topic)
bool unsubscribe(const String &topic)
void setEventHandler(mqtt_type_t type, MqttDelegate handler)
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.

Protected Functions

bool connect(const String &server, int port, bool useSsl = false)
bool connect(IpAddress addr, uint16_t port, bool useSsl = false)
void close()
void setReceiveDelegate(TcpClientDataDelegate receiveCb = nullptr)

Set or clear the callback for received data.

Parameters
  • receiveCb: callback delegate or nullptr

void setCompleteDelegate(TcpClientCompleteDelegate completeCb = nullptr)

Set or clear the callback for connection close.

Parameters
  • completeCb: callback delegate or nullptr

bool send(const char *data, uint16_t len, bool forceCloseAfterSent = false)
bool sendString(const String &data, bool forceCloseAfterSent = false)
bool isProcessing()
TcpClientState getConnectionState()
void setCloseAfterSent(bool ignoreIncomingData = false)

Schedules the connection to get closed after the data is sent

Parameters
  • ignoreIncomingData: when that flag is set the connection will start ignoring incoming data.

int writeString(const char *data, uint8_t apiflags = TCP_WRITE_FLAG_COPY)
int writeString(const String &data, uint8_t apiflags = TCP_WRITE_FLAG_COPY)
virtual int write(const char *data, int len, uint8_t apiflags = TCP_WRITE_FLAG_COPY)

Base write operation.

Parameters
  • data:
  • len:
  • apiflags: TCP_WRITE_FLAG_COPY, TCP_WRITE_FLAG_MORE
Return Value
  • int: -1 on error

int write(IDataSourceStream *stream)
uint16_t getAvailableWriteSize()
void flush()
void setTimeOut(uint16_t waitTimeOut)
IpAddress getRemoteIp() const
uint16_t getRemotePort() const
void setDestroyedDelegate(TcpConnectionDestroyedDelegate destroyedDelegate)

Sets a callback to be called when the object instance is destroyed.

Parameters
  • destroyedDelegate:

void setSslInitHandler(Ssl::Session::InitDelegate handler)

Set the SSL session initialisation callback.

Parameters
  • handler:

bool setSslConnection(Ssl::Connection *connection)
Ssl::Session *getSsl()

Get a pointer to the current SSL session object.

Note that this is typically used so we can query properties of an established session. If you need to change session parameters this must be done via setSslInitHandler.