MQTT / MQTT

An API for using MQTT over multiple transports

Dependencies:   FP MQTTPacket

Dependents:   Cellular_HelloMQTT IoTStarterKit GSwifiInterface_HelloMQTT IBMIoTClientEthernetExample ... more

Embed: (wiki syntax)

« Back to documentation index

Client< Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS > Class Template Reference

Client< Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS > Class Template Reference

blocking, non-threaded MQTT client API More...

#include <MQTTClient.h>

Public Member Functions

 Client (Network &network, unsigned int command_timeout_ms=30000)
 Construct the client.
void setDefaultMessageHandler (messageHandler mh)
 Set the default message handling callback - used for any message which does not match a subscription message handler.
int connect ()
 MQTT Connect - send an MQTT connect packet down the network and wait for a Connack The nework object must be connected to the network endpoint before calling this Default connect options are used.
int connect (MQTTPacket_connectData &options)
 MQTT Connect - send an MQTT connect packet down the network and wait for a Connack The nework object must be connected to the network endpoint before calling this.
int publish (const char *topicName, Message &message)
 MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs.
int publish (const char *topicName, void *payload, size_t payloadlen, enum QoS qos=QOS0, bool retained=false)
 MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs.
int publish (const char *topicName, void *payload, size_t payloadlen, unsigned short &id, enum QoS qos=QOS1, bool retained=false)
 MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs.
int subscribe (const char *topicFilter, enum QoS qos, messageHandler mh)
 MQTT Subscribe - send an MQTT subscribe packet and wait for the suback.
int unsubscribe (const char *topicFilter)
 MQTT Unsubscribe - send an MQTT unsubscribe packet and wait for the unsuback.
int disconnect ()
 MQTT Disconnect - send an MQTT disconnect packet, and clean up any state.
int yield (unsigned long timeout_ms=1000L)
 A call to this API must be made within the keepAlive interval to keep the MQTT connection alive yield can be called if no other MQTT operation is needed.
bool isConnected ()
 Is the client connected?

Detailed Description

template<class Network, class Timer, int MAX_MQTT_PACKET_SIZE = 100, int MAX_MESSAGE_HANDLERS = 5>
class MQTT::Client< Network, Timer, MAX_MQTT_PACKET_SIZE, MAX_MESSAGE_HANDLERS >

blocking, non-threaded MQTT client API

This version of the API blocks on all method calls, until they are complete. This means that only one MQTT request can be in process at any one time.

Parameters:
Networka network class which supports send, receive
Timera timer class with the methods:

Definition at line 96 of file MQTTClient.h.


Constructor & Destructor Documentation

Client ( Network &  network,
unsigned int  command_timeout_ms = 30000 
)

Construct the client.

Parameters:
network- pointer to an instance of the Network class - must be connected to the endpoint before calling MQTT connect
limitsan instance of the Limit class - to alter limits as required

Definition at line 277 of file MQTTClient.h.


Member Function Documentation

int connect (  )

MQTT Connect - send an MQTT connect packet down the network and wait for a Connack The nework object must be connected to the network endpoint before calling this Default connect options are used.

Returns:
success code -

Definition at line 719 of file MQTTClient.h.

int connect ( MQTTPacket_connectData &  options )

MQTT Connect - send an MQTT connect packet down the network and wait for a Connack The nework object must be connected to the network endpoint before calling this.

Parameters:
options- connect options
Returns:
success code -

Definition at line 661 of file MQTTClient.h.

int disconnect (  )

MQTT Disconnect - send an MQTT disconnect packet, and clean up any state.

Returns:
success code -

Definition at line 923 of file MQTTClient.h.

bool isConnected (  )

Is the client connected?

Returns:
flag - is the client connected or not?

Definition at line 190 of file MQTTClient.h.

int publish ( const char *  topicName,
Message &  message 
)

MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs.

Parameters:
topic- the topic to publish to
message- the message to send
Returns:
success code -

Definition at line 916 of file MQTTClient.h.

int publish ( const char *  topicName,
void *  payload,
size_t  payloadlen,
enum QoS  qos = QOS0,
bool  retained = false 
)

MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs.

Parameters:
topic- the topic to publish to
payload- the data to send
payloadlen- the length of the data
qos- the QoS to send the publish at
retained- whether the message should be retained
Returns:
success code -

Definition at line 908 of file MQTTClient.h.

int publish ( const char *  topicName,
void *  payload,
size_t  payloadlen,
unsigned short &  id,
enum QoS  qos = QOS1,
bool  retained = false 
)

MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs.

Parameters:
topic- the topic to publish to
payload- the data to send
payloadlen- the length of the data
id- the packet id used - returned
qos- the QoS to send the publish at
retained- whether the message should be retained
Returns:
success code -

Definition at line 866 of file MQTTClient.h.

void setDefaultMessageHandler ( messageHandler  mh )

Set the default message handling callback - used for any message which does not match a subscription message handler.

Parameters:
mh- pointer to the callback function

Definition at line 113 of file MQTTClient.h.

int subscribe ( const char *  topicFilter,
enum QoS  qos,
messageHandler  mh 
)

MQTT Subscribe - send an MQTT subscribe packet and wait for the suback.

Parameters:
topicFilter- a topic pattern which can include wildcards
qos- the MQTT QoS to subscribe at
mh- the callback function to be invoked when a message is received for this subscription
Returns:
success code -

Definition at line 727 of file MQTTClient.h.

int unsubscribe ( const char *  topicFilter )

MQTT Unsubscribe - send an MQTT unsubscribe packet and wait for the unsuback.

Parameters:
topicFilter- a topic pattern which can include wildcards
Returns:
success code -

Definition at line 774 of file MQTTClient.h.

int yield ( unsigned long  timeout_ms = 1000L )

A call to this API must be made within the keepAlive interval to keep the MQTT connection alive yield can be called if no other MQTT operation is needed.

This will also allow messages to be received.

Parameters:
timeout_msthe time to wait, in milliseconds
Returns:
success code - on failure, this means the client has disconnected

Definition at line 504 of file MQTTClient.h.