PubSubClient3 v3.3.0
Located at <a href='https://github.com/hmueller01/pubsubclient3'>GitHub</a>
 
Loading...
Searching...
No Matches
PubSubClient Class Reference

This class provides a client for doing simple publish and subscribe messaging with a server that supports MQTT. More...

#include <PubSubClient.h>

Inheritance diagram for PubSubClient:

Public Member Functions

 PubSubClient ()
 Creates an uninitialised client instance.
 
 PubSubClient (Client &client)
 Creates a partially initialised client instance.
 
 PubSubClient (IPAddress addr, uint16_t port, Client &client)
 Creates a fully configured client instance.
 
 PubSubClient (IPAddress addr, uint16_t port, Client &client, Stream &stream)
 Creates a fully configured client instance.
 
 PubSubClient (IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client &client)
 Creates a fully configured client instance.
 
 PubSubClient (IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client &client, Stream &stream)
 Creates a fully configured client instance.
 
 PubSubClient (uint8_t *ip, uint16_t port, Client &client)
 Creates a fully configured client instance.
 
 PubSubClient (uint8_t *ip, uint16_t port, Client &client, Stream &stream)
 Creates a fully configured client instance.
 
 PubSubClient (uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client &client)
 Creates a fully configured client instance.
 
 PubSubClient (uint8_t *ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client &client, Stream &stream)
 Creates a fully configured client instance.
 
 PubSubClient (const char *domain, uint16_t port, Client &client)
 Creates a fully configured client instance.
 
 PubSubClient (const char *domain, uint16_t port, Client &client, Stream &stream)
 Creates a fully configured client instance.
 
 PubSubClient (const char *domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client &client)
 Creates a fully configured client instance.
 
 PubSubClient (const char *domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client &client, Stream &stream)
 Creates a fully configured client instance.
 
 ~PubSubClient ()
 Destructor for the PubSubClient class.
 
PubSubClientsetServer (IPAddress ip, uint16_t port)
 Sets the server details.
 
PubSubClientsetServer (uint8_t *ip, uint16_t port)
 Sets the server details.
 
PubSubClientsetServer (const char *domain, uint16_t port)
 Sets the server details.
 
PubSubClientsetCallback (MQTT_CALLBACK_SIGNATURE)
 Sets the message callback function.
 
PubSubClientsetClient (Client &client)
 Sets the network client instance to use.
 
PubSubClientsetStream (Stream &stream)
 Sets the stream to write received messages to.
 
PubSubClientsetKeepAlive (uint16_t keepAlive)
 Sets the keep alive interval used by the client. This value should only be changed when the client is not connected. Set keepAlive to zero (0) to turn off the keep alive mechanism.
 
PubSubClientsetSocketTimeout (uint16_t timeout)
 Sets the socket timeout used by the client. This determines how long the client will wait for incoming data when it expects data to arrive - for example, whilst it is in the middle of reading a MQTT packet.
 
bool setBufferSize (size_t size)
 Sets the size, in bytes, of the internal send and receive buffer. This must be large enough to contain the full MQTT packet. When sending or receiving messages, the packet will contain the full topic string, the payload data, and a small number of header bytes.
 
size_t getBufferSize ()
 Gets the current size of the internal buffer.
 
bool connect (const char *id)
 Connects the client using a clean session without username and password.
 
bool connect (const char *id, const char *user, const char *pass)
 Connects the client using a clean session with username and password.
 
bool connect (const char *id, const char *willTopic, uint8_t willQos, bool willRetain, const char *willMessage)
 Connects the client using a clean session and will.
 
bool connect (const char *id, const char *user, const char *pass, const char *willTopic, uint8_t willQos, bool willRetain, const char *willMessage)
 Connects the client using a clean session with username, password and will.
 
bool connect (const char *id, const char *user, const char *pass, const char *willTopic, uint8_t willQos, bool willRetain, const char *willMessage, bool cleanSession)
 Connects the client with all possible parameters (user, password, will and session).
 
void disconnect ()
 Disconnects the client.
 
bool publish (const char *topic, const char *payload)
 Publishes a non retained message to the specified topic using QoS 0.
 
bool publish (const char *topic, const char *payload, bool retained)
 Publishes a message to the specified topic using QoS 0.
 
bool publish (const char *topic, const char *payload, uint8_t qos, bool retained)
 Publishes a message to the specified topic.
 
bool publish (const __FlashStringHelper *topic, const char *payload, uint8_t qos, bool retained)
 Publishes a message to the specified topic.
 
bool publish (const __FlashStringHelper *topic, const __FlashStringHelper *payload, uint8_t qos, bool retained)
 Publishes a message from __FlashStringHelper to the specified topic from __FlashStringHelper.
 
bool publish (const char *topic, const uint8_t *payload, size_t plength)
 Publishes a non retained message to the specified topic using QoS 0.
 
bool publish (const char *topic, const uint8_t *payload, size_t plength, bool retained)
 Publishes a message to the specified topic using QoS 0.
 
bool publish (const char *topic, const uint8_t *payload, size_t plength, uint8_t qos, bool retained)
 Publishes a message to the specified topic.
 
bool publish (const __FlashStringHelper *topic, const uint8_t *payload, size_t plength, uint8_t qos, bool retained)
 Publishes a message to the specified topic.
 
bool publish_P (const char *topic, PGM_P payload, bool retained)
 Publishes a message stored in PROGMEM to the specified topic using QoS 0.
 
bool publish_P (const char *topic, PGM_P payload, uint8_t qos, bool retained)
 Publishes a message stored in PROGMEM to the specified topic.
 
bool publish_P (const __FlashStringHelper *topic, PGM_P payload, uint8_t qos, bool retained)
 Publishes a message stored in PROGMEM to the specified topic.
 
bool publish_P (const char *topic, const uint8_t *payload, size_t plength, bool retained)
 Publishes a message stored in PROGMEM to the specified topic using QoS 0.
 
bool publish_P (const char *topic, const uint8_t *payload, size_t plength, uint8_t qos, bool retained)
 Publishes a message stored in PROGMEM to the specified topic.
 
bool publish_P (const __FlashStringHelper *topic, const uint8_t *payload, size_t plength, uint8_t qos, bool retained)
 Publishes a message stored in PROGMEM to the specified topic.
 
bool beginPublish (const char *topic, size_t plength, bool retained)
 Start to publish a message using QoS 0. This API: beginPublish(...) one or more calls to write(...) endPublish() Allows for arbitrarily large payloads to be sent without them having to be copied into a new buffer and held in memory at one time.
 
bool beginPublish (const char *topic, size_t plength, uint8_t qos, bool retained)
 Start to publish a message. This API: beginPublish(...) one or more calls to write(...) endPublish() Allows for arbitrarily large payloads to be sent without them having to be copied into a new buffer and held in memory at one time.
 
bool beginPublish (const __FlashStringHelper *topic, size_t plength, uint8_t qos, bool retained)
 Start to publish a message using a topic from __FlashStringHelper F(). This API: beginPublish(...) one or more calls to write(...) endPublish() Allows for arbitrarily large payloads to be sent without them having to be copied into a new buffer and held in memory at one time.
 
bool beginPublish_P (PGM_P topic, size_t plength, uint8_t qos, bool retained)
 Start to publish a message using a topic in PROGMEM. This API: beginPublish_P(...) one or more calls to write(...) endPublish() Allows for arbitrarily large payloads to be sent without them having to be copied into a new buffer and held in memory at one time.
 
bool endPublish ()
 Finish sending a message that was started with a call to beginPublish.
 
virtual size_t write (uint8_t data)
 Writes a single byte as a component of a publish started with a call to beginPublish. For performance reasons, this will be appended to the internal buffer, which will be flushed when full or on a call to endPublish().
 
virtual size_t write (const uint8_t *buf, size_t size)
 Writes an array of bytes as a component of a publish started with a call to beginPublish. For performance reasons, this will be appended to the internal buffer, which will be flushed when full or on a call to endPublish().
 
size_t write_P (PGM_P string)
 Writes a string in PROGMEM as a component of a publish started with a call to beginPublish. For performance reasons, this will be appended to the internal buffer, which will be flushed when full or on a call to endPublish().
 
size_t write_P (const uint8_t *buf, size_t size)
 Writes an array of progmem bytes as a component of a publish started with a call to beginPublish. For performance reasons, this will be appended to the internal buffer, which will be flushed when full or on a call to endPublish().
 
bool subscribe (const char *topic)
 Subscribes to messages published to the specified topic using QoS 0.
 
bool subscribe (const __FlashStringHelper *topic)
 Subscribes to messages published to the specified topic from __FlashStringHelper using QoS 0.
 
bool subscribe_P (PGM_P topic)
 Subscribes to messages published to the specified topic in PROGMEM using QoS 0.
 
bool subscribe (const char *topic, uint8_t qos)
 Subscribes to messages published to the specified topic.
 
bool subscribe (const __FlashStringHelper *topic, uint8_t qos)
 Subscribes to messages published to the specified topic from __FlashStringHelper.
 
bool subscribe_P (PGM_P topic, uint8_t qos)
 Subscribes to messages published to the specified topic in PROGMEM.
 
bool unsubscribe (const char *topic)
 Unsubscribes from the specified topic.
 
bool unsubscribe (const __FlashStringHelper *topic)
 Unsubscribes from the specified topic from __FlashStringHelper.
 
bool unsubscribe_P (PGM_P topic)
 Unsubscribes from the specified topic in PROGMEM.
 
bool loop ()
 This should be called regularly to allow the client to process incoming messages and maintain its connection to the server.
 
bool connected ()
 Checks whether the client is connected to the server.
 
int state ()
 Returns the current state of the client. If a connection attempt fails, this can be used to get more information about the failure.
 

Detailed Description

This class provides a client for doing simple publish and subscribe messaging with a server that supports MQTT.

Definition at line 176 of file PubSubClient.h.

Constructor & Destructor Documentation

◆ PubSubClient() [1/14]

PubSubClient::PubSubClient ( )

Creates an uninitialised client instance.

Note
Before it can be used, it must be configured using the property setters setClient() and setServer().

Definition at line 26 of file PubSubClient.cpp.

◆ PubSubClient() [2/14]

PubSubClient::PubSubClient ( Client & client)

Creates a partially initialised client instance.

Parameters
clientThe network client to use, for example WiFiClient.
Note
Before it can be used, it must be configured with the property setter setServer().

Definition at line 32 of file PubSubClient.cpp.

◆ PubSubClient() [3/14]

PubSubClient::PubSubClient ( IPAddress addr,
uint16_t port,
Client & client )

Creates a fully configured client instance.

Parameters
addrThe address of the server.
portThe port to connect to.
clientThe network client to use, for example WiFiClient.

Definition at line 36 of file PubSubClient.cpp.

◆ PubSubClient() [4/14]

PubSubClient::PubSubClient ( IPAddress addr,
uint16_t port,
Client & client,
Stream & stream )

Creates a fully configured client instance.

Parameters
addrThe address of the server.
portThe port to connect to.
clientThe network client to use, for example WiFiClient.
streamA stream to write received messages to.

Definition at line 41 of file PubSubClient.cpp.

◆ PubSubClient() [5/14]

PubSubClient::PubSubClient ( IPAddress addr,
uint16_t port,
MQTT_CALLBACK_SIGNATURE ,
Client & client )

Creates a fully configured client instance.

Parameters
addrThe address of the server.
portThe port to connect to.
callbackPointer to a message callback function. Called when a message arrives for a subscription created by this client.
clientThe network client to use, for example WiFiClient.

Definition at line 47 of file PubSubClient.cpp.

◆ PubSubClient() [6/14]

PubSubClient::PubSubClient ( IPAddress addr,
uint16_t port,
MQTT_CALLBACK_SIGNATURE ,
Client & client,
Stream & stream )

Creates a fully configured client instance.

Parameters
addrThe address of the server.
portThe port to connect to.
callbackPointer to a message callback function. Called when a message arrives for a subscription created by this client.
clientThe network client to use, for example WiFiClient.
streamA stream to write received messages to.

Definition at line 53 of file PubSubClient.cpp.

◆ PubSubClient() [7/14]

PubSubClient::PubSubClient ( uint8_t * ip,
uint16_t port,
Client & client )

Creates a fully configured client instance.

Parameters
ipThe address of the server.
portThe port to connect to.
clientThe network client to use, for example WiFiClient.

Definition at line 60 of file PubSubClient.cpp.

◆ PubSubClient() [8/14]

PubSubClient::PubSubClient ( uint8_t * ip,
uint16_t port,
Client & client,
Stream & stream )

Creates a fully configured client instance.

Parameters
ipThe address of the server.
portThe port to connect to.
clientThe network client to use, for example WiFiClient.
streamA stream to write received messages to.

Definition at line 65 of file PubSubClient.cpp.

◆ PubSubClient() [9/14]

PubSubClient::PubSubClient ( uint8_t * ip,
uint16_t port,
MQTT_CALLBACK_SIGNATURE ,
Client & client )

Creates a fully configured client instance.

Parameters
ipThe address of the server.
portThe port to connect to.
callbackPointer to a message callback function. Called when a message arrives for a subscription created by this client.
clientThe network client to use, for example WiFiClient.

Definition at line 71 of file PubSubClient.cpp.

◆ PubSubClient() [10/14]

PubSubClient::PubSubClient ( uint8_t * ip,
uint16_t port,
MQTT_CALLBACK_SIGNATURE ,
Client & client,
Stream & stream )

Creates a fully configured client instance.

Parameters
ipThe address of the server.
portThe port to connect to.
callbackPointer to a message callback function. Called when a message arrives for a subscription created by this client.
clientThe network client to use, for example WiFiClient.
streamA stream to write received messages to.

Definition at line 77 of file PubSubClient.cpp.

◆ PubSubClient() [11/14]

PubSubClient::PubSubClient ( const char * domain,
uint16_t port,
Client & client )

Creates a fully configured client instance.

Parameters
domainThe address of the server.
portThe port to connect to.
clientThe network client to use, for example WiFiClient.

Definition at line 84 of file PubSubClient.cpp.

◆ PubSubClient() [12/14]

PubSubClient::PubSubClient ( const char * domain,
uint16_t port,
Client & client,
Stream & stream )

Creates a fully configured client instance.

Parameters
domainThe address of the server.
portThe port to connect to.
clientThe network client to use, for example WiFiClient.
streamA stream to write received messages to.

Definition at line 89 of file PubSubClient.cpp.

◆ PubSubClient() [13/14]

PubSubClient::PubSubClient ( const char * domain,
uint16_t port,
MQTT_CALLBACK_SIGNATURE ,
Client & client )

Creates a fully configured client instance.

Parameters
domainThe address of the server.
portThe port to connect to.
callbackPointer to a message callback function. Called when a message arrives for a subscription created by this client.
clientThe network client to use, for example WiFiClient.

Definition at line 95 of file PubSubClient.cpp.

◆ PubSubClient() [14/14]

PubSubClient::PubSubClient ( const char * domain,
uint16_t port,
MQTT_CALLBACK_SIGNATURE ,
Client & client,
Stream & stream )

Creates a fully configured client instance.

Parameters
domainThe address of the server.
portThe port to connect to.
callbackPointer to a message callback function. Called when a message arrives for a subscription created by this client.
clientThe network client to use, for example WiFiClient.
streamA stream to write received messages to.

Definition at line 101 of file PubSubClient.cpp.

◆ ~PubSubClient()

PubSubClient::~PubSubClient ( )

Destructor for the PubSubClient class.

Definition at line 108 of file PubSubClient.cpp.

Member Function Documentation

◆ beginPublish() [1/3]

bool PubSubClient::beginPublish ( const __FlashStringHelper * topic,
size_t plength,
uint8_t qos,
bool retained )
inline

Start to publish a message using a topic from __FlashStringHelper F(). This API: beginPublish(...) one or more calls to write(...) endPublish() Allows for arbitrarily large payloads to be sent without them having to be copied into a new buffer and held in memory at one time.

Parameters
topicThe topic from __FlashStringHelper to publish to.
plengthThe length of the payload.
qosThe quality of service (QoS levels) to publish at. [0, 1, 2].
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 749 of file PubSubClient.h.

◆ beginPublish() [2/3]

bool PubSubClient::beginPublish ( const char * topic,
size_t plength,
bool retained )
inline

Start to publish a message using QoS 0. This API: beginPublish(...) one or more calls to write(...) endPublish() Allows for arbitrarily large payloads to be sent without them having to be copied into a new buffer and held in memory at one time.

Parameters
topicThe topic to publish to.
plengthThe length of the payload.
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 711 of file PubSubClient.h.

◆ beginPublish() [3/3]

bool PubSubClient::beginPublish ( const char * topic,
size_t plength,
uint8_t qos,
bool retained )
inline

Start to publish a message. This API: beginPublish(...) one or more calls to write(...) endPublish() Allows for arbitrarily large payloads to be sent without them having to be copied into a new buffer and held in memory at one time.

Parameters
topicThe topic to publish to.
plengthThe length of the payload.
qosThe quality of service (QoS levels) to publish at. [0, 1, 2].
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 730 of file PubSubClient.h.

◆ beginPublish_P()

bool PubSubClient::beginPublish_P ( PGM_P topic,
size_t plength,
uint8_t qos,
bool retained )
inline

Start to publish a message using a topic in PROGMEM. This API: beginPublish_P(...) one or more calls to write(...) endPublish() Allows for arbitrarily large payloads to be sent without them having to be copied into a new buffer and held in memory at one time.

Parameters
topicThe topic in PROGMEM to publish to.
plengthThe length of the payload.
qosThe quality of service (QoS levels) to publish at. [0, 1, 2].
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 769 of file PubSubClient.h.

◆ connect() [1/5]

bool PubSubClient::connect ( const char * id)
inline

Connects the client using a clean session without username and password.

Parameters
idThe client ID to use when connecting to the server.
Returns
true If client succeeded in establishing a connection to the broker. false If client failed to establish a connection to the broker.

Definition at line 434 of file PubSubClient.h.

◆ connect() [2/5]

bool PubSubClient::connect ( const char * id,
const char * user,
const char * pass )
inline

Connects the client using a clean session with username and password.

Parameters
idThe client ID to use when connecting to the server.
userThe username to use.
passThe password to use.
Note
If user is NULL, no username or password is used.
If pass is NULL, no password is used.
Returns
true If client succeeded in establishing a connection to the broker. false If client failed to establish a connection to the broker.

Definition at line 448 of file PubSubClient.h.

◆ connect() [3/5]

bool PubSubClient::connect ( const char * id,
const char * user,
const char * pass,
const char * willTopic,
uint8_t willQos,
bool willRetain,
const char * willMessage )
inline

Connects the client using a clean session with username, password and will.

Parameters
idThe client ID to use when connecting to the server.
userThe username to use.
passThe password to use.
willTopicThe topic to be used by the will message.
willQosThe quality of service to be used by the will message. [0, 1, 2].
willRetainPublish the will message with the retain flag.
willMessageThe message to be used by the will message.
Note
If user is NULL, no username or password is used.
If pass is NULL, no password is used.
If willTopic is NULL, no will message is sent.
Returns
true If client succeeded in establishing a connection to the broker. false If client failed to establish a connection to the broker.

Definition at line 482 of file PubSubClient.h.

◆ connect() [4/5]

bool PubSubClient::connect ( const char * id,
const char * user,
const char * pass,
const char * willTopic,
uint8_t willQos,
bool willRetain,
const char * willMessage,
bool cleanSession )

Connects the client with all possible parameters (user, password, will and session).

Parameters
idThe client ID to use when connecting to the server.
userThe username to use.
passThe password to use.
willTopicThe topic to be used by the will message.
willQosThe quality of service to be used by the will message. [0, 1, 2].
willRetainPublish the will message with the retain flag.
willMessageThe message to be used by the will message.
cleanSessionTrue to connect with a clean session.
Note
If user is NULL, no username or password is used.
If pass is NULL, no password is used.
If willTopic is NULL, no will message is sent.
Returns
true If client succeeded in establishing a connection to the broker. false If client failed to establish a connection to the broker.

Definition at line 113 of file PubSubClient.cpp.

◆ connect() [5/5]

bool PubSubClient::connect ( const char * id,
const char * willTopic,
uint8_t willQos,
bool willRetain,
const char * willMessage )
inline

Connects the client using a clean session and will.

Parameters
idThe client ID to use when connecting to the server.
willTopicThe topic to be used by the will message.
willQosThe quality of service to be used by the will message. [0, 1, 2].
willRetainPublish the will message with the retain flag.
willMessageThe message to be used by the will message.
Note
If willTopic is NULL, no will message is sent.
Returns
true If client succeeded in establishing a connection to the broker. false If client failed to establish a connection to the broker.

Definition at line 463 of file PubSubClient.h.

◆ connected()

bool PubSubClient::connected ( )

Checks whether the client is connected to the server.

Returns
true If the client is connected. false If the client is not connected.

Definition at line 217 of file PubSubClient.cpp.

◆ disconnect()

void PubSubClient::disconnect ( )

Disconnects the client.

Definition at line 231 of file PubSubClient.cpp.

◆ endPublish()

bool PubSubClient::endPublish ( )

Finish sending a message that was started with a call to beginPublish.

Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 579 of file PubSubClient.cpp.

◆ getBufferSize()

size_t PubSubClient::getBufferSize ( )

Gets the current size of the internal buffer.

Returns
The size of the internal buffer.

Definition at line 904 of file PubSubClient.cpp.

◆ loop()

bool PubSubClient::loop ( )

This should be called regularly to allow the client to process incoming messages and maintain its connection to the server.

Returns
true If the client is still connected. false If the client is no longer connected.

Definition at line 455 of file PubSubClient.cpp.

◆ publish() [1/9]

bool PubSubClient::publish ( const __FlashStringHelper * topic,
const __FlashStringHelper * payload,
uint8_t qos,
bool retained )
inline

Publishes a message from __FlashStringHelper to the specified topic from __FlashStringHelper.

Parameters
topicThe topic to publish to.
payloadThe message to publish.
qosThe quality of service (QoS levels) to publish at. [0, 1, 2].
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 569 of file PubSubClient.h.

◆ publish() [2/9]

bool PubSubClient::publish ( const __FlashStringHelper * topic,
const char * payload,
uint8_t qos,
bool retained )
inline

Publishes a message to the specified topic.

Parameters
topicThe topic from __FlashStringHelper to publish to.
payloadThe message to publish.
qosThe quality of service (QoS levels) to publish at. [0, 1, 2].
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 556 of file PubSubClient.h.

◆ publish() [3/9]

bool PubSubClient::publish ( const __FlashStringHelper * topic,
const uint8_t * payload,
size_t plength,
uint8_t qos,
bool retained )

Publishes a message to the specified topic.

Parameters
topicThe topic from __FlashStringHelper to publish to.
payloadThe message to publish.
plengthThe length of the payload.
qosThe quality of service (QoS levels) to publish at. [0, 1, 2].
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 511 of file PubSubClient.cpp.

◆ publish() [4/9]

bool PubSubClient::publish ( const char * topic,
const char * payload )
inline

Publishes a non retained message to the specified topic using QoS 0.

Parameters
topicThe topic to publish to.
payloadThe message to publish.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 518 of file PubSubClient.h.

◆ publish() [5/9]

bool PubSubClient::publish ( const char * topic,
const char * payload,
bool retained )
inline

Publishes a message to the specified topic using QoS 0.

Parameters
topicThe topic to publish to.
payloadThe message to publish.
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 530 of file PubSubClient.h.

◆ publish() [6/9]

bool PubSubClient::publish ( const char * topic,
const char * payload,
uint8_t qos,
bool retained )
inline

Publishes a message to the specified topic.

Parameters
topicThe topic to publish to.
payloadThe message to publish.
qosThe quality of service (QoS levels) to publish at. [0, 1, 2].
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 543 of file PubSubClient.h.

◆ publish() [7/9]

bool PubSubClient::publish ( const char * topic,
const uint8_t * payload,
size_t plength )
inline

Publishes a non retained message to the specified topic using QoS 0.

Parameters
topicThe topic to publish to.
payloadThe message to publish.
plengthThe length of the payload.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 581 of file PubSubClient.h.

◆ publish() [8/9]

bool PubSubClient::publish ( const char * topic,
const uint8_t * payload,
size_t plength,
bool retained )
inline

Publishes a message to the specified topic using QoS 0.

Parameters
topicThe topic to publish to.
payloadThe message to publish.
plengthThe length of the payload.
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 594 of file PubSubClient.h.

◆ publish() [9/9]

bool PubSubClient::publish ( const char * topic,
const uint8_t * payload,
size_t plength,
uint8_t qos,
bool retained )

Publishes a message to the specified topic.

Parameters
topicThe topic to publish to.
payloadThe message to publish.
plengthThe length of the payload.
qosThe quality of service (QoS levels) to publish at. [0, 1, 2].
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 503 of file PubSubClient.cpp.

◆ publish_P() [1/6]

bool PubSubClient::publish_P ( const __FlashStringHelper * topic,
const uint8_t * payload,
size_t plength,
uint8_t qos,
bool retained )

Publishes a message stored in PROGMEM to the specified topic.

Parameters
topicThe topic from __FlashStringHelper to publish to.
payloadThe message from PROGMEM to publish.
plengthThe length of the payload.
qosThe quality of service (QoS levels) to publish at. [0, 1, 2].
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 527 of file PubSubClient.cpp.

◆ publish_P() [2/6]

bool PubSubClient::publish_P ( const __FlashStringHelper * topic,
PGM_P payload,
uint8_t qos,
bool retained )
inline

Publishes a message stored in PROGMEM to the specified topic.

Parameters
topicThe topic from __FlashStringHelper to publish to.
payloadThe message to publish.
qosThe quality of service (QoS levels) to publish at. [0, 1, 2].
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 656 of file PubSubClient.h.

◆ publish_P() [3/6]

bool PubSubClient::publish_P ( const char * topic,
const uint8_t * payload,
size_t plength,
bool retained )
inline

Publishes a message stored in PROGMEM to the specified topic using QoS 0.

Parameters
topicThe topic to publish to.
payloadThe message to publish.
plengthThe length of the payload.
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 669 of file PubSubClient.h.

◆ publish_P() [4/6]

bool PubSubClient::publish_P ( const char * topic,
const uint8_t * payload,
size_t plength,
uint8_t qos,
bool retained )

Publishes a message stored in PROGMEM to the specified topic.

Parameters
topicThe topic to publish to.
payloadThe message to publish.
plengthThe length of the payload.
qosThe quality of service (QoS levels) to publish at. [0, 1, 2].
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 519 of file PubSubClient.cpp.

◆ publish_P() [5/6]

bool PubSubClient::publish_P ( const char * topic,
PGM_P payload,
bool retained )
inline

Publishes a message stored in PROGMEM to the specified topic using QoS 0.

Parameters
topicThe topic to publish to.
payloadThe message to publish.
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 630 of file PubSubClient.h.

◆ publish_P() [6/6]

bool PubSubClient::publish_P ( const char * topic,
PGM_P payload,
uint8_t qos,
bool retained )
inline

Publishes a message stored in PROGMEM to the specified topic.

Parameters
topicThe topic to publish to.
payloadThe message to publish.
qosThe quality of service (QoS levels) to publish at. [0, 1, 2].
retainedPublish the message with the retain flag.
Returns
true If the publish succeeded. false If the publish failed, either connection lost or message too large.

Definition at line 643 of file PubSubClient.h.

◆ setBufferSize()

bool PubSubClient::setBufferSize ( size_t size)

Sets the size, in bytes, of the internal send and receive buffer. This must be large enough to contain the full MQTT packet. When sending or receiving messages, the packet will contain the full topic string, the payload data, and a small number of header bytes.

Parameters
sizeThe size, in bytes, for the internal buffer.
Returns
true If the buffer was resized. false If the buffer could not be resized.

Definition at line 885 of file PubSubClient.cpp.

◆ setCallback()

PubSubClient & PubSubClient::setCallback ( MQTT_CALLBACK_SIGNATURE )

Sets the message callback function.

Parameters
callbackPointer to a message callback function. Called when a message arrives for a subscription created by this client.
Returns
The client instance, allowing the function to be chained.

Definition at line 870 of file PubSubClient.cpp.

◆ setClient()

PubSubClient & PubSubClient::setClient ( Client & client)

Sets the network client instance to use.

Parameters
clientThe network client to use, for example WiFiClient.
Returns
The client instance, allowing the function to be chained.

Definition at line 875 of file PubSubClient.cpp.

◆ setKeepAlive()

PubSubClient & PubSubClient::setKeepAlive ( uint16_t keepAlive)

Sets the keep alive interval used by the client. This value should only be changed when the client is not connected. Set keepAlive to zero (0) to turn off the keep alive mechanism.

Parameters
keepAliveThe keep alive interval, in seconds.
Returns
The client instance, allowing the function to be chained.

Definition at line 908 of file PubSubClient.cpp.

◆ setServer() [1/3]

PubSubClient & PubSubClient::setServer ( const char * domain,
uint16_t port )

Sets the server details.

Parameters
domainThe address of the server.
portThe port to connect to.
Returns
The client instance, allowing the function to be chained.

Definition at line 853 of file PubSubClient.cpp.

◆ setServer() [2/3]

PubSubClient & PubSubClient::setServer ( IPAddress ip,
uint16_t port )

Sets the server details.

Parameters
ipThe address of the server.
portThe port to connect to.
Returns
The client instance, allowing the function to be chained.

Definition at line 845 of file PubSubClient.cpp.

◆ setServer() [3/3]

PubSubClient & PubSubClient::setServer ( uint8_t * ip,
uint16_t port )

Sets the server details.

Parameters
ipThe address of the server.
portThe port to connect to.
Returns
The client instance, allowing the function to be chained.

Definition at line 840 of file PubSubClient.cpp.

◆ setSocketTimeout()

PubSubClient & PubSubClient::setSocketTimeout ( uint16_t timeout)

Sets the socket timeout used by the client. This determines how long the client will wait for incoming data when it expects data to arrive - for example, whilst it is in the middle of reading a MQTT packet.

Parameters
timeoutThe socket timeout, in seconds.
Returns
The client instance, allowing the function to be chained.

Definition at line 913 of file PubSubClient.cpp.

◆ setStream()

PubSubClient & PubSubClient::setStream ( Stream & stream)

Sets the stream to write received messages to.

Parameters
streamA stream to write received messages to.
Returns
The client instance, allowing the function to be chained.

Definition at line 880 of file PubSubClient.cpp.

◆ state()

int PubSubClient::state ( )

Returns the current state of the client. If a connection attempt fails, this can be used to get more information about the failure.

Note
All of the values have corresponding constants defined in PubSubClient.h.
Returns
See state() result

Definition at line 918 of file PubSubClient.cpp.

◆ subscribe() [1/4]

bool PubSubClient::subscribe ( const __FlashStringHelper * topic)
inline

Subscribes to messages published to the specified topic from __FlashStringHelper using QoS 0.

Parameters
topicThe topic from __FlashStringHelper to subscribe to.
Returns
true If sending the subscribe succeeded. false If sending the subscribe failed, either connection lost or message too large.

Definition at line 836 of file PubSubClient.h.

◆ subscribe() [2/4]

bool PubSubClient::subscribe ( const __FlashStringHelper * topic,
uint8_t qos )
inline

Subscribes to messages published to the specified topic from __FlashStringHelper.

Parameters
topicThe topic from __FlashStringHelper to subscribe to.
qosThe qos to subscribe at. [0, 1].
Returns
true If sending the subscribe succeeded. false If sending the subscribe failed, either connection lost or message too large.

Definition at line 869 of file PubSubClient.h.

◆ subscribe() [3/4]

bool PubSubClient::subscribe ( const char * topic)
inline

Subscribes to messages published to the specified topic using QoS 0.

Parameters
topicThe topic to subscribe to.
Returns
true If sending the subscribe succeeded. false If sending the subscribe failed, either connection lost or message too large.

Definition at line 826 of file PubSubClient.h.

◆ subscribe() [4/4]

bool PubSubClient::subscribe ( const char * topic,
uint8_t qos )
inline

Subscribes to messages published to the specified topic.

Parameters
topicThe topic to subscribe to.
qosThe qos to subscribe at. [0, 1].
Returns
true If sending the subscribe succeeded. false If sending the subscribe failed, either connection lost or message too large.

Definition at line 858 of file PubSubClient.h.

◆ subscribe_P() [1/2]

bool PubSubClient::subscribe_P ( PGM_P topic)
inline

Subscribes to messages published to the specified topic in PROGMEM using QoS 0.

Parameters
topicThe topic in PROGMEM to subscribe to.
Returns
true If sending the subscribe succeeded. false If sending the subscribe failed, either connection lost or message too large.

Definition at line 847 of file PubSubClient.h.

◆ subscribe_P() [2/2]

bool PubSubClient::subscribe_P ( PGM_P topic,
uint8_t qos )
inline

Subscribes to messages published to the specified topic in PROGMEM.

Parameters
topicThe topic in PROGMEM to subscribe to.
qosThe qos to subscribe at. [0, 1].
Returns
true If sending the subscribe succeeded. false If sending the subscribe failed, either connection lost or message too large.

Definition at line 881 of file PubSubClient.h.

◆ unsubscribe() [1/2]

bool PubSubClient::unsubscribe ( const __FlashStringHelper * topic)
inline

Unsubscribes from the specified topic from __FlashStringHelper.

Parameters
topicThe topic from __FlashStringHelper to unsubscribe from.
Returns
true If sending the unsubscribe succeeded. false If sending the unsubscribe failed, either connection lost or message too large.

Definition at line 901 of file PubSubClient.h.

◆ unsubscribe() [2/2]

bool PubSubClient::unsubscribe ( const char * topic)
inline

Unsubscribes from the specified topic.

Parameters
topicThe topic to unsubscribe from.
Returns
true If sending the unsubscribe succeeded. false If sending the unsubscribe failed, either connection lost or message too large.

Definition at line 891 of file PubSubClient.h.

◆ unsubscribe_P()

bool PubSubClient::unsubscribe_P ( PGM_P topic)
inline

Unsubscribes from the specified topic in PROGMEM.

Parameters
topicThe topic in PROGMEM to unsubscribe from.
Returns
true If sending the unsubscribe succeeded. false If sending the unsubscribe failed, either connection lost or message too large.

Definition at line 912 of file PubSubClient.h.

◆ write() [1/2]

size_t PubSubClient::write ( const uint8_t * buf,
size_t size )
virtual

Writes an array of bytes as a component of a publish started with a call to beginPublish. For performance reasons, this will be appended to the internal buffer, which will be flushed when full or on a call to endPublish().

Parameters
bufThe bytes to write.
sizeThe length of the payload to be sent.
Returns
The number of bytes written. If return value is != size a write error occurred.

Definition at line 627 of file PubSubClient.cpp.

◆ write() [2/2]

size_t PubSubClient::write ( uint8_t data)
virtual

Writes a single byte as a component of a publish started with a call to beginPublish. For performance reasons, this will be appended to the internal buffer, which will be flushed when full or on a call to endPublish().

Parameters
dataA byte to write to the publish payload.
Returns
The number of bytes written (0 or 1). If 0 is returned a write error occurred.

Definition at line 623 of file PubSubClient.cpp.

◆ write_P() [1/2]

size_t PubSubClient::write_P ( const uint8_t * buf,
size_t size )

Writes an array of progmem bytes as a component of a publish started with a call to beginPublish. For performance reasons, this will be appended to the internal buffer, which will be flushed when full or on a call to endPublish().

Parameters
bufThe bytes to write.
sizeThe length of the payload to be sent.
Returns
The number of bytes written. If return value is != size a write error occurred.

Definition at line 634 of file PubSubClient.cpp.

◆ write_P() [2/2]

size_t PubSubClient::write_P ( PGM_P string)
inline

Writes a string in PROGMEM as a component of a publish started with a call to beginPublish. For performance reasons, this will be appended to the internal buffer, which will be flushed when full or on a call to endPublish().

Parameters
stringThe message to write.
Returns
The number of bytes written. If return value is != string length a write error occurred.

Definition at line 806 of file PubSubClient.h.


The documentation for this class was generated from the following files: