Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MQTTClient Class Reference
MQTT client mbed-os wrapper class. More...
#include <MQTTClientMbedOs.h>
Public Types | |
typedef void(* | messageHandler )(MQTT::MessageData &) |
MQTT message handler. | |
typedef void(* | messageHandlerSN )(MQTTSN::MessageData &) |
MQTT-SN message handler. | |
Public Member Functions | |
MQTTClient (TCPSocket *_socket) | |
Constructor for the TCPSocket-based communication. | |
MQTTClient (TLSSocket *_socket) | |
Constructor for the TLSSocket-based communication. | |
MQTTClient (UDPSocket *_socket) | |
Constructor for the UDPSocket-based communication. | |
MQTTClient (DTLSSocket *_socket) | |
Constructor for the DTLSSocket-based communication. | |
nsapi_error_t | connect (MQTTPacket_connectData &options) |
Connect to the MQTT broker. | |
nsapi_error_t | connect (MQTTSNPacket_connectData &options) |
Connect to the MQTT broker via an MQTT-SN gateway. | |
nsapi_error_t | publish (const char *topicName, MQTT::Message &message) |
Publish message to a topic. | |
nsapi_error_t | publish (MQTTSN_topicid &topicName, MQTTSN::Message &message) |
Publish message to an MQTT-SN topic. | |
nsapi_error_t | subscribe (const char *topicFilter, enum MQTT::QoS qos, messageHandler mh) |
Subscribe to a topic. | |
nsapi_error_t | subscribe (MQTTSN_topicid &topicFilter, enum MQTTSN::QoS qos, messageHandlerSN mh) |
Subscribe to an MQTT-SN topic. | |
nsapi_error_t | unsubscribe (const char *topicFilter) |
Unsubscribe from a topic. | |
nsapi_error_t | unsubscribe (MQTTSN_topicid &topicFilter) |
Unsubscribe from an MQTT-SN topic. | |
nsapi_error_t | yield (unsigned long timeout_ms=1000L) |
Yield current thread execution and handle other events. | |
nsapi_error_t | disconnect () |
Disconnect from a broker, that the client has been connected to. | |
bool | isConnected () |
(MQTT-SN) Check whether client is connected to a broker. | |
void | setDefaultMessageHandler (messageHandler mh) |
Set the default message handling callback - used for any message which does not match a subscription message handler. | |
nsapi_error_t | setMessageHandler (const char *topicFilter, messageHandler mh) |
Set a message handling callback. |
Detailed Description
MQTT client mbed-os wrapper class.
This class wraps around the paho library templated MQTT(-SN)Client. It depends on the type of socket provided whether MQTT or MQTT-SN will be used. MQTTNetworkMbedOs will be used as a Network for the paho MQTTClient.
Definition at line 98 of file MQTTClientMbedOs.h.
Member Typedef Documentation
typedef void(* messageHandler)(MQTT::MessageData &) |
MQTT message handler.
Definition at line 101 of file MQTTClientMbedOs.h.
typedef void(* messageHandlerSN)(MQTTSN::MessageData &) |
MQTT-SN message handler.
Definition at line 103 of file MQTTClientMbedOs.h.
Constructor & Destructor Documentation
MQTTClient | ( | TCPSocket * | _socket ) |
Constructor for the TCPSocket-based communication.
MQTT protocol will be used.
- Parameters:
-
_socket socket to be used for communication
Definition at line 66 of file MQTTClientMbedOs.cpp.
MQTTClient | ( | TLSSocket * | _socket ) |
Constructor for the TLSSocket-based communication.
MQTT protocol will be used over a secure socket.
- Parameters:
-
_socket socket to be used for communication
Definition at line 74 of file MQTTClientMbedOs.cpp.
MQTTClient | ( | UDPSocket * | _socket ) |
Constructor for the UDPSocket-based communication.
MQTT-SN protocol will be used.
- Parameters:
-
_socket socket to be used for communication
Definition at line 82 of file MQTTClientMbedOs.cpp.
MQTTClient | ( | DTLSSocket * | _socket ) |
Constructor for the DTLSSocket-based communication.
MQTT-SN protocol will be used over a secure socket.
- Parameters:
-
_socket socket to be used for communication
Definition at line 90 of file MQTTClientMbedOs.cpp.
Member Function Documentation
nsapi_error_t connect | ( | MQTTPacket_connectData & | options ) |
Connect to the MQTT broker.
This is different from connecting to the server, done in MQTTNetworkMbedOs::connect() and also different from connecting the network interface, that the socket is using.
- Parameters:
-
options options to be used for the connection.
- Return values:
-
NSAPI_ERROR_OK on success, error code on failure
Definition at line 98 of file MQTTClientMbedOs.cpp.
nsapi_error_t connect | ( | MQTTSNPacket_connectData & | options ) |
Connect to the MQTT broker via an MQTT-SN gateway.
This is different from connecting to the server, done in MQTTNetworkMbedOs::connect() and also different from connecting the network interface, that the socket is using.
- Parameters:
-
options options to be used for the connection.
- Return values:
-
NSAPI_ERROR_OK on success, error code on failure
Definition at line 107 of file MQTTClientMbedOs.cpp.
nsapi_error_t disconnect | ( | ) |
Disconnect from a broker, that the client has been connected to.
- Return values:
-
NSAPI_ERROR_OK on success, error code on failure
Definition at line 183 of file MQTTClientMbedOs.cpp.
bool isConnected | ( | ) |
(MQTT-SN) Check whether client is connected to a broker.
- Return values:
-
true if the client is connected, false otherwise
Definition at line 196 of file MQTTClientMbedOs.cpp.
nsapi_error_t publish | ( | MQTTSN_topicid & | topicName, |
MQTTSN::Message & | message | ||
) |
Publish message to an MQTT-SN topic.
- Parameters:
-
topicName MQTTSN_topicid structure specifying the topic. message message to be published
- Return values:
-
NSAPI_ERROR_OK on success, error code on failure
Definition at line 125 of file MQTTClientMbedOs.cpp.
nsapi_error_t publish | ( | const char * | topicName, |
MQTT::Message & | message | ||
) |
Publish message to a topic.
- Parameters:
-
topicName string with a topic name message message to be published
- Return values:
-
NSAPI_ERROR_OK on success, error code on failure
Definition at line 116 of file MQTTClientMbedOs.cpp.
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. Set to 0 to remove.
Definition at line 207 of file MQTTClientMbedOs.cpp.
nsapi_error_t setMessageHandler | ( | const char * | topicFilter, |
messageHandler | mh | ||
) |
Set a message handling callback.
This can be used outside of the the subscribe method.
- Parameters:
-
topicFilter - a topic pattern which can include wildcards mh - pointer to the callback function. If 0, removes the callback if any
Definition at line 216 of file MQTTClientMbedOs.cpp.
nsapi_error_t subscribe | ( | MQTTSN_topicid & | topicFilter, |
enum MQTTSN::QoS | qos, | ||
messageHandlerSN | mh | ||
) |
Subscribe to an MQTT-SN topic.
- Parameters:
-
topicFilter string with a topic filter qos level of qos to be received mh message handler to be called upon message reception
- Return values:
-
NSAPI_ERROR_OK on success, error code on failure
Definition at line 143 of file MQTTClientMbedOs.cpp.
nsapi_error_t subscribe | ( | const char * | topicFilter, |
enum MQTT::QoS | qos, | ||
messageHandler | mh | ||
) |
Subscribe to a topic.
- Parameters:
-
topicFilter string with a topic filter qos level of qos to be received mh message handler to be called upon message reception
- Return values:
-
NSAPI_ERROR_OK on success, error code on failure
Definition at line 134 of file MQTTClientMbedOs.cpp.
nsapi_error_t unsubscribe | ( | const char * | topicFilter ) |
Unsubscribe from a topic.
- Parameters:
-
topicFilter string with a topic filter
- Return values:
-
NSAPI_ERROR_OK on success, error code on failure
Definition at line 152 of file MQTTClientMbedOs.cpp.
nsapi_error_t unsubscribe | ( | MQTTSN_topicid & | topicFilter ) |
Unsubscribe from an MQTT-SN topic.
- Parameters:
-
topicFilter string with a topic filter
- Return values:
-
NSAPI_ERROR_OK on success, error code on failure
Definition at line 161 of file MQTTClientMbedOs.cpp.
nsapi_error_t yield | ( | unsigned long | timeout_ms = 1000L ) |
Yield current thread execution and handle other events.
- Parameters:
-
timeout_ms ms to yield for
- Return values:
-
NSAPI_ERROR_OK on success, error code on failure
Definition at line 170 of file MQTTClientMbedOs.cpp.
Generated on Thu Jul 14 2022 12:58:44 by
