This class provides a client for doing simple publish and subscribe messaging with a server that supports MQTT. More...
#include <PubSubClient.h>
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. | |
PubSubClient & | setServer (IPAddress ip, uint16_t port) |
Sets the server details. | |
PubSubClient & | setServer (uint8_t *ip, uint16_t port) |
Sets the server details. | |
PubSubClient & | setServer (const char *domain, uint16_t port) |
Sets the server details. | |
PubSubClient & | setCallback (MQTT_CALLBACK_SIGNATURE) |
Sets the message callback function. | |
PubSubClient & | setClient (Client &client) |
Sets the network client instance to use. | |
PubSubClient & | setStream (Stream &stream) |
Sets the stream to write received messages to. | |
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. | |
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. | |
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 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_P (const char *topic, const char *payload, bool retained) |
Publishes a message stored in PROGMEM to the specified topic using QoS 0. | |
bool | publish_P (const char *topic, const char *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 | 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 | 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. | |
virtual size_t | write (const uint8_t *buffer, size_t size) |
Writes an array of bytes as a component of a publish started with a call to beginPublish. | |
bool | subscribe (const char *topic) |
Subscribes to messages published to the specified topic using QoS 0. | |
bool | subscribe (const char *topic, uint8_t qos) |
Subscribes to messages published to the specified topic. | |
bool | unsubscribe (const char *topic) |
Unsubscribes from the specified topic. | |
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. | |
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.
PubSubClient::PubSubClient | ( | ) |
Creates an uninitialised client instance.
Definition at line 26 of file PubSubClient.cpp.
PubSubClient::PubSubClient | ( | Client & | client | ) |
Creates a partially initialised client instance.
client | The network client to use, for example WiFiClient. |
Definition at line 32 of file PubSubClient.cpp.
PubSubClient::PubSubClient | ( | IPAddress | addr, |
uint16_t | port, | ||
Client & | client ) |
Creates a fully configured client instance.
addr | The address of the server. |
port | The port to connect to. |
client | The network client to use, for example WiFiClient. |
Definition at line 36 of file PubSubClient.cpp.
PubSubClient::PubSubClient | ( | IPAddress | addr, |
uint16_t | port, | ||
Client & | client, | ||
Stream & | stream ) |
Creates a fully configured client instance.
addr | The address of the server. |
port | The port to connect to. |
client | The network client to use, for example WiFiClient. |
stream | A stream to write received messages to. |
Definition at line 41 of file PubSubClient.cpp.
PubSubClient::PubSubClient | ( | IPAddress | addr, |
uint16_t | port, | ||
MQTT_CALLBACK_SIGNATURE | , | ||
Client & | client ) |
Creates a fully configured client instance.
addr | The address of the server. |
port | The port to connect to. |
callback | Pointer to a message callback function. Called when a message arrives for a subscription created by this client. |
client | The network client to use, for example WiFiClient. |
Definition at line 47 of file PubSubClient.cpp.
PubSubClient::PubSubClient | ( | IPAddress | addr, |
uint16_t | port, | ||
MQTT_CALLBACK_SIGNATURE | , | ||
Client & | client, | ||
Stream & | stream ) |
Creates a fully configured client instance.
addr | The address of the server. |
port | The port to connect to. |
callback | Pointer to a message callback function. Called when a message arrives for a subscription created by this client. |
client | The network client to use, for example WiFiClient. |
stream | A stream to write received messages to. |
Definition at line 53 of file PubSubClient.cpp.
PubSubClient::PubSubClient | ( | uint8_t * | ip, |
uint16_t | port, | ||
Client & | client ) |
Creates a fully configured client instance.
ip | The address of the server. |
port | The port to connect to. |
client | The network client to use, for example WiFiClient. |
Definition at line 60 of file PubSubClient.cpp.
PubSubClient::PubSubClient | ( | uint8_t * | ip, |
uint16_t | port, | ||
Client & | client, | ||
Stream & | stream ) |
Creates a fully configured client instance.
ip | The address of the server. |
port | The port to connect to. |
client | The network client to use, for example WiFiClient. |
stream | A stream to write received messages to. |
Definition at line 65 of file PubSubClient.cpp.
PubSubClient::PubSubClient | ( | uint8_t * | ip, |
uint16_t | port, | ||
MQTT_CALLBACK_SIGNATURE | , | ||
Client & | client ) |
Creates a fully configured client instance.
ip | The address of the server. |
port | The port to connect to. |
callback | Pointer to a message callback function. Called when a message arrives for a subscription created by this client. |
client | The network client to use, for example WiFiClient. |
Definition at line 71 of file PubSubClient.cpp.
PubSubClient::PubSubClient | ( | uint8_t * | ip, |
uint16_t | port, | ||
MQTT_CALLBACK_SIGNATURE | , | ||
Client & | client, | ||
Stream & | stream ) |
Creates a fully configured client instance.
ip | The address of the server. |
port | The port to connect to. |
callback | Pointer to a message callback function. Called when a message arrives for a subscription created by this client. |
client | The network client to use, for example WiFiClient. |
stream | A stream to write received messages to. |
Definition at line 77 of file PubSubClient.cpp.
PubSubClient::PubSubClient | ( | const char * | domain, |
uint16_t | port, | ||
Client & | client ) |
Creates a fully configured client instance.
domain | The address of the server. |
port | The port to connect to. |
client | The network client to use, for example WiFiClient. |
Definition at line 84 of file PubSubClient.cpp.
PubSubClient::PubSubClient | ( | const char * | domain, |
uint16_t | port, | ||
Client & | client, | ||
Stream & | stream ) |
Creates a fully configured client instance.
domain | The address of the server. |
port | The port to connect to. |
client | The network client to use, for example WiFiClient. |
stream | A stream to write received messages to. |
Definition at line 89 of file PubSubClient.cpp.
PubSubClient::PubSubClient | ( | const char * | domain, |
uint16_t | port, | ||
MQTT_CALLBACK_SIGNATURE | , | ||
Client & | client ) |
Creates a fully configured client instance.
domain | The address of the server. |
port | The port to connect to. |
callback | Pointer to a message callback function. Called when a message arrives for a subscription created by this client. |
client | The network client to use, for example WiFiClient. |
Definition at line 95 of file PubSubClient.cpp.
PubSubClient::PubSubClient | ( | const char * | domain, |
uint16_t | port, | ||
MQTT_CALLBACK_SIGNATURE | , | ||
Client & | client, | ||
Stream & | stream ) |
Creates a fully configured client instance.
domain | The address of the server. |
port | The port to connect to. |
callback | Pointer to a message callback function. Called when a message arrives for a subscription created by this client. |
client | The network client to use, for example WiFiClient. |
stream | A stream to write received messages to. |
Definition at line 101 of file PubSubClient.cpp.
PubSubClient::~PubSubClient | ( | ) |
Destructor for the PubSubClient class.
Definition at line 108 of file PubSubClient.cpp.
bool PubSubClient::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.
topic | The topic to publish to. |
plength | The length of the payload. |
retained | Publish the message with the retain flag. |
Definition at line 557 of file PubSubClient.cpp.
bool PubSubClient::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.
topic | The topic to publish to. |
plength | The length of the payload. |
qos | The quality of service (QoS levels) to publish at. [0, 1, 2]. |
retained | Publish the message with the retain flag. |
Definition at line 561 of file PubSubClient.cpp.
bool PubSubClient::connect | ( | const char * | id | ) |
Connects the client using a clean session without username and password.
id | The client ID to use when connecting to the server. |
Definition at line 113 of file PubSubClient.cpp.
bool PubSubClient::connect | ( | const char * | id, |
const char * | user, | ||
const char * | pass ) |
Connects the client using a clean session with username and password.
id | The client ID to use when connecting to the server. |
user | The username to use. |
pass | The password to use. |
Definition at line 117 of file PubSubClient.cpp.
bool PubSubClient::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.
id | The client ID to use when connecting to the server. |
user | The username to use. |
pass | The password to use. |
willTopic | The topic to be used by the will message. |
willQos | The quality of service to be used by the will message. [0, 1, 2]. |
willRetain | Publish the will message with the retain flag. |
willMessage | The message to be used by the will message. |
Definition at line 125 of file PubSubClient.cpp.
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).
id | The client ID to use when connecting to the server. |
user | The username to use. |
pass | The password to use. |
willTopic | The topic to be used by the will message. |
willQos | The quality of service to be used by the will message. [0, 1, 2]. |
willRetain | Publish the will message with the retain flag. |
willMessage | The message to be used by the will message. |
cleanSession | True to connect with a clean session. |
Definition at line 130 of file PubSubClient.cpp.
bool PubSubClient::connect | ( | const char * | id, |
const char * | willTopic, | ||
uint8_t | willQos, | ||
bool | willRetain, | ||
const char * | willMessage ) |
Connects the client using a clean session and will.
id | The client ID to use when connecting to the server. |
willTopic | The topic to be used by the will message. |
willQos | The quality of service to be used by the will message. [0, 1, 2]. |
willRetain | Publish the will message with the retain flag. |
willMessage | The message to be used by the will message. |
Definition at line 121 of file PubSubClient.cpp.
bool PubSubClient::connected | ( | ) |
Checks whether the client is connected to the server.
Definition at line 230 of file PubSubClient.cpp.
void PubSubClient::disconnect | ( | ) |
Disconnects the client.
Definition at line 244 of file PubSubClient.cpp.
bool PubSubClient::endPublish | ( | ) |
Finish sending a message that was started with a call to beginPublish.
Definition at line 587 of file PubSubClient.cpp.
size_t PubSubClient::getBufferSize | ( | ) |
Gets the current size of the internal buffer.
Definition at line 838 of file PubSubClient.cpp.
bool PubSubClient::loop | ( | ) |
This should be called regularly to allow the client to process incoming messages and maintain its connection to the server.
Definition at line 464 of file PubSubClient.cpp.
bool PubSubClient::publish | ( | const char * | topic, |
const char * | payload ) |
Publishes a non retained message to the specified topic using QoS 0.
topic | The topic to publish to. |
payload | The message to publish. |
Definition at line 504 of file PubSubClient.cpp.
bool PubSubClient::publish | ( | const char * | topic, |
const char * | payload, | ||
bool | retained ) |
Publishes a message to the specified topic using QoS 0.
topic | The topic to publish to. |
payload | The message to publish. |
retained | Publish the message with the retain flag. |
Definition at line 508 of file PubSubClient.cpp.
bool PubSubClient::publish | ( | const char * | topic, |
const char * | payload, | ||
uint8_t | qos, | ||
bool | retained ) |
Publishes a message to the specified topic.
topic | The topic to publish to. |
payload | The message to publish. |
qos | The quality of service (QoS levels) to publish at. [0, 1, 2]. |
retained | Publish the message with the retain flag. |
Definition at line 512 of file PubSubClient.cpp.
bool PubSubClient::publish | ( | const char * | topic, |
const uint8_t * | payload, | ||
size_t | plength ) |
Publishes a non retained message to the specified topic using QoS 0.
topic | The topic to publish to. |
payload | The message to publish. |
plength | The length of the payload. |
Definition at line 516 of file PubSubClient.cpp.
bool PubSubClient::publish | ( | const char * | topic, |
const uint8_t * | payload, | ||
size_t | plength, | ||
bool | retained ) |
Publishes a message to the specified topic using QoS 0.
topic | The topic to publish to. |
payload | The message to publish. |
plength | The length of the payload. |
retained | Publish the message with the retain flag. |
Definition at line 520 of file PubSubClient.cpp.
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.
topic | The topic to publish to. |
payload | The message to publish. |
plength | The length of the payload. |
qos | The quality of service (QoS levels) to publish at. [0, 1, 2]. |
retained | Publish the message with the retain flag. |
Definition at line 524 of file PubSubClient.cpp.
bool PubSubClient::publish_P | ( | const char * | topic, |
const char * | payload, | ||
bool | retained ) |
Publishes a message stored in PROGMEM to the specified topic using QoS 0.
topic | The topic to publish to. |
payload | The message to publish. |
retained | Publish the message with the retain flag. |
Definition at line 533 of file PubSubClient.cpp.
bool PubSubClient::publish_P | ( | const char * | topic, |
const char * | payload, | ||
uint8_t | qos, | ||
bool | retained ) |
Publishes a message stored in PROGMEM to the specified topic.
topic | The topic to publish to. |
payload | The message to publish. |
qos | The quality of service (QoS levels) to publish at. [0, 1, 2]. |
retained | Publish the message with the retain flag. |
Definition at line 537 of file PubSubClient.cpp.
bool PubSubClient::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.
topic | The topic to publish to. |
payload | The message to publish. |
plength | The length of the payload. |
retained | Publish the message with the retain flag. |
Definition at line 541 of file PubSubClient.cpp.
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.
topic | The topic to publish to. |
payload | The message to publish. |
plength | The length of the payload. |
qos | The quality of service (QoS levels) to publish at. [0, 1, 2]. |
retained | Publish the message with the retain flag. |
Definition at line 545 of file PubSubClient.cpp.
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.
size | The size, in bytes, for the internal buffer. |
Definition at line 819 of file PubSubClient.cpp.
PubSubClient & PubSubClient::setCallback | ( | MQTT_CALLBACK_SIGNATURE | ) |
Sets the message callback function.
callback | Pointer to a message callback function. Called when a message arrives for a subscription created by this client. |
Definition at line 804 of file PubSubClient.cpp.
PubSubClient & PubSubClient::setClient | ( | Client & | client | ) |
Sets the network client instance to use.
client | The network client to use, for example WiFiClient. |
Definition at line 809 of file PubSubClient.cpp.
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.
keepAlive | The keep alive interval, in seconds. |
Definition at line 842 of file PubSubClient.cpp.
PubSubClient & PubSubClient::setServer | ( | const char * | domain, |
uint16_t | port ) |
Sets the server details.
domain | The address of the server. |
port | The port to connect to. |
Definition at line 787 of file PubSubClient.cpp.
PubSubClient & PubSubClient::setServer | ( | IPAddress | ip, |
uint16_t | port ) |
Sets the server details.
ip | The address of the server. |
port | The port to connect to. |
Definition at line 779 of file PubSubClient.cpp.
PubSubClient & PubSubClient::setServer | ( | uint8_t * | ip, |
uint16_t | port ) |
Sets the server details.
ip | The address of the server. |
port | The port to connect to. |
Definition at line 774 of file PubSubClient.cpp.
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.
timeout | The socket timeout, in seconds. |
Definition at line 847 of file PubSubClient.cpp.
PubSubClient & PubSubClient::setStream | ( | Stream & | stream | ) |
Sets the stream to write received messages to.
stream | A stream to write received messages to. |
Definition at line 814 of file PubSubClient.cpp.
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.
Definition at line 852 of file PubSubClient.cpp.
bool PubSubClient::subscribe | ( | const char * | topic | ) |
Subscribes to messages published to the specified topic using QoS 0.
topic | The topic to subscribe to. |
Definition at line 733 of file PubSubClient.cpp.
bool PubSubClient::subscribe | ( | const char * | topic, |
uint8_t | qos ) |
Subscribes to messages published to the specified topic.
topic | The topic to subscribe to. |
qos | The qos to subscribe at. [0, 1]. |
Definition at line 737 of file PubSubClient.cpp.
bool PubSubClient::unsubscribe | ( | const char * | topic | ) |
Unsubscribes from the specified topic.
topic | The topic to unsubscribe from. |
Definition at line 757 of file PubSubClient.cpp.
|
virtual |
Writes an array of bytes as a component of a publish started with a call to beginPublish.
buffer | The bytes to write. |
size | The length of the payload to be sent. |
Definition at line 642 of file PubSubClient.cpp.
|
virtual |
Writes a single byte as a component of a publish started with a call to beginPublish.
data | A byte to write to the publish payload. |
Definition at line 637 of file PubSubClient.cpp.