20#define MQTT_VERSION_3_1 3
21#define MQTT_VERSION_3_1_1 4
30#define MQTT_VERSION MQTT_VERSION_3_1_1
36#ifndef MQTT_MAX_POSSIBLE_PACKET_SIZE
37#define MQTT_MAX_POSSIBLE_PACKET_SIZE ((size_t)268435455)
46#ifndef MQTT_MAX_PACKET_SIZE
47#define MQTT_MAX_PACKET_SIZE 256
57#define MQTT_KEEPALIVE 15
66#ifndef MQTT_SOCKET_TIMEOUT
67#define MQTT_SOCKET_TIMEOUT 15
76#ifndef MQTT_MAX_TRANSFER_SIZE
77#define MQTT_MAX_TRANSFER_SIZE 80
78#undef MQTT_MAX_TRANSFER_SIZE
86#define MQTT_CONNECTION_TIMEOUT -4
87#define MQTT_CONNECTION_LOST -3
88#define MQTT_CONNECT_FAILED -2
89#define MQTT_DISCONNECTED -1
90#define MQTT_CONNECTED 0
91#define MQTT_CONNECT_BAD_PROTOCOL 1
92#define MQTT_CONNECT_BAD_CLIENT_ID 2
93#define MQTT_CONNECT_UNAVAILABLE 3
94#define MQTT_CONNECT_BAD_CREDENTIALS 4
95#define MQTT_CONNECT_UNAUTHORIZED 5
100#define MQTTCONNECT 1 << 4
101#define MQTTCONNACK 2 << 4
102#define MQTTPUBLISH 3 << 4
103#define MQTTPUBACK 4 << 4
104#define MQTTPUBREC 5 << 4
105#define MQTTPUBREL 6 << 4
106#define MQTTPUBCOMP 7 << 4
107#define MQTTSUBSCRIBE 8 << 4
108#define MQTTSUBACK 9 << 4
109#define MQTTUNSUBSCRIBE 10 << 4
110#define MQTTUNSUBACK 11 << 4
111#define MQTTPINGREQ 12 << 4
112#define MQTTPINGRESP 13 << 4
113#define MQTTDISCONNECT 14 << 4
114#define MQTTRESERVED 15 << 4
122#define MQTT_QOS0 ((uint8_t)0)
123#define MQTT_QOS1 ((uint8_t)1)
124#define MQTT_QOS2 ((uint8_t)2)
126#define MQTT_QOS_GET_HDR(qos) (((qos) & 0x03) << 1)
127#define MQTT_HDR_GET_QOS(header) (((header) & 0x06 ) >> 1)
132#define MQTT_MAX_HEADER_SIZE 5
142#if defined(__has_include) && __has_include(<functional>) && !defined(NOFUNCTIONAL)
144#define MQTT_CALLBACK_SIGNATURE std::function<void(char* topic, uint8_t* payload, size_t plength)> callback
146#define MQTT_CALLBACK_SIGNATURE void (*callback)(char* topic, uint8_t* payload, size_t plength)
151#ifdef DEBUG_PUBSUBCLIENT
152#define DEBUG_PSC_PRINTF(fmt, ...) DEBUG_ESP_PORT.printf(("PubSubClient: " fmt), ##__VA_ARGS__)
154#define DEBUG_PSC_PRINTF(...)
157#define ERROR_PSC_PRINTF(fmt, ...) DEBUG_ESP_PORT.printf(("PubSubClient error: " fmt), ##__VA_ARGS__)
158#define ERROR_PSC_PRINTF_P(fmt, ...) DEBUG_ESP_PORT.printf_P(PSTR("PubSubClient error: " fmt), ##__VA_ARGS__)
160#ifndef DEBUG_PSC_PRINTF
161#define DEBUG_PSC_PRINTF(...)
163#ifndef ERROR_PSC_PRINTF
164#define ERROR_PSC_PRINTF(fmt, ...)
166#ifndef ERROR_PSC_PRINTF_P
167#define ERROR_PSC_PRINTF_P(fmt, ...)
180 size_t _bufferSize{};
181 size_t _bufferWritePos{};
182 unsigned long _keepAliveMillis{};
183 unsigned long _socketTimeoutMillis{};
184 uint16_t _nextMsgId{};
185 unsigned long _lastOutActivity{};
186 unsigned long _lastInActivity{};
187 bool _pingOutstanding{};
188 MQTT_CALLBACK_SIGNATURE{};
195 size_t readPacket(uint8_t* hdrLen);
196 bool handlePacket(uint8_t hdrLen,
size_t len);
197 bool readByte(uint8_t* result);
198 bool readByte(uint8_t* result,
size_t* pos);
199 uint8_t buildHeader(uint8_t header,
size_t length);
200 bool writeControlPacket(uint8_t header,
size_t length);
201 size_t writeBuffer(
size_t pos,
size_t size);
202 size_t writeStringImpl(
bool progmem,
const char*
string,
size_t pos);
203 size_t writeString(
const char*
string,
size_t pos);
204 size_t writeNextMsgId(
size_t pos);
206 bool beginPublishImpl(
bool progmem,
const char* topic,
size_t plength, uint8_t qos,
bool retained);
207 bool subscribeImpl(
bool progmem,
const char* topic, uint8_t qos);
208 bool unsubscribeImpl(
bool progmem,
const char* topic);
211 size_t appendBuffer(uint8_t data);
212 size_t flushBuffer();
234 PubSubClient(IPAddress addr, uint16_t port, Client& client);
243 PubSubClient(IPAddress addr, uint16_t port, Client& client, Stream& stream);
253 PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client);
264 PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream);
272 PubSubClient(uint8_t* ip, uint16_t port, Client& client);
281 PubSubClient(uint8_t* ip, uint16_t port, Client& client, Stream& stream);
291 PubSubClient(uint8_t* ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client);
302 PubSubClient(uint8_t* ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream);
310 PubSubClient(
const char* domain, uint16_t port, Client& client);
319 PubSubClient(
const char* domain, uint16_t port, Client& client, Stream& stream);
329 PubSubClient(
const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client);
340 PubSubClient(
const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream);
435 return connect(
id,
nullptr,
nullptr,
nullptr,
MQTT_QOS0,
false,
nullptr,
true);
448 inline bool connect(
const char*
id,
const char* user,
const char* pass) {
463 inline bool connect(
const char*
id,
const char* willTopic, uint8_t willQos,
bool willRetain,
const char* willMessage) {
464 return connect(
id,
nullptr,
nullptr, willTopic, willQos, willRetain, willMessage,
true);
482 inline bool connect(
const char*
id,
const char* user,
const char* pass,
const char* willTopic, uint8_t willQos,
bool willRetain,
483 const char* willMessage) {
484 return connect(
id, user, pass, willTopic, willQos, willRetain, willMessage,
true);
503 bool connect(
const char*
id,
const char* user,
const char* pass,
const char* willTopic, uint8_t willQos,
bool willRetain,
const char* willMessage,
518 inline bool publish(
const char* topic,
const char* payload) {
530 inline bool publish(
const char* topic,
const char* payload,
bool retained) {
543 inline bool publish(
const char* topic,
const char* payload, uint8_t qos,
bool retained) {
544 return publish(topic,
reinterpret_cast<const uint8_t*
>(payload), payload ? strlen(payload) : 0, qos, retained);
556 inline bool publish(
const __FlashStringHelper* topic,
const char* payload, uint8_t qos,
bool retained) {
557 return publish(topic,
reinterpret_cast<const uint8_t*
>(payload), payload ? strlen(payload) : 0, qos, retained);
569 inline bool publish(
const __FlashStringHelper* topic,
const __FlashStringHelper* payload, uint8_t qos,
bool retained) {
570 return publish_P(topic,
reinterpret_cast<const uint8_t*
>(payload), payload ? strlen_P(
reinterpret_cast<const char*
>(payload)) : 0, qos, retained);
581 inline bool publish(
const char* topic,
const uint8_t* payload,
size_t plength) {
594 inline bool publish(
const char* topic,
const uint8_t* payload,
size_t plength,
bool retained) {
608 bool publish(
const char* topic,
const uint8_t* payload,
size_t plength, uint8_t qos,
bool retained);
620 bool publish(
const __FlashStringHelper* topic,
const uint8_t* payload,
size_t plength, uint8_t qos,
bool retained);
630 inline bool publish_P(
const char* topic, PGM_P payload,
bool retained) {
643 inline bool publish_P(
const char* topic, PGM_P payload, uint8_t qos,
bool retained) {
644 return publish_P(topic,
reinterpret_cast<const uint8_t*
>(payload), payload ? strlen_P(payload) : 0, qos, retained);
656 bool publish_P(
const __FlashStringHelper* topic, PGM_P payload, uint8_t qos,
bool retained) {
657 return publish_P(topic,
reinterpret_cast<const uint8_t*
>(payload), payload ? strlen_P(payload) : 0, qos, retained);
669 inline bool publish_P(
const char* topic,
const uint8_t* payload,
size_t plength,
bool retained) {
683 bool publish_P(
const char* topic,
const uint8_t* payload,
size_t plength, uint8_t qos,
bool retained);
695 bool publish_P(
const __FlashStringHelper* topic,
const uint8_t* payload,
size_t plength, uint8_t qos,
bool retained);
711 inline bool beginPublish(
const char* topic,
size_t plength,
bool retained) {
712 return beginPublishImpl(
false, topic, plength,
MQTT_QOS0, retained);
730 inline bool beginPublish(
const char* topic,
size_t plength, uint8_t qos,
bool retained) {
731 return beginPublishImpl(
false, topic, plength, qos, retained);
749 inline bool beginPublish(
const __FlashStringHelper* topic,
size_t plength, uint8_t qos,
bool retained) {
751 return beginPublishImpl(
true,
reinterpret_cast<const char*
>(topic), plength, qos, retained);
769 inline bool beginPublish_P(PGM_P topic,
size_t plength, uint8_t qos,
bool retained) {
770 return beginPublishImpl(
true,
reinterpret_cast<const char*
>(topic), plength, qos, retained);
787 virtual size_t write(uint8_t data);
797 virtual size_t write(
const uint8_t* buf,
size_t size);
807 return write_P(
reinterpret_cast<const uint8_t*
>(
string), strlen_P(
string));
818 size_t write_P(
const uint8_t* buf,
size_t size);
827 return subscribeImpl(
false, topic,
MQTT_QOS0);
836 inline bool subscribe(
const __FlashStringHelper* topic) {
838 return subscribeImpl(
true,
reinterpret_cast<const char*
>(topic),
MQTT_QOS0);
848 return subscribeImpl(
true,
reinterpret_cast<const char*
>(topic),
MQTT_QOS0);
859 return subscribeImpl(
false, topic, qos);
869 inline bool subscribe(
const __FlashStringHelper* topic, uint8_t qos) {
871 return subscribeImpl(
true,
reinterpret_cast<const char*
>(topic), qos);
882 return subscribeImpl(
true,
reinterpret_cast<const char*
>(topic), qos);
892 return unsubscribeImpl(
false, topic);
903 return unsubscribeImpl(
true,
reinterpret_cast<const char*
>(topic));
913 return unsubscribeImpl(
true,
reinterpret_cast<const char*
>(topic));
bool loop()
This should be called regularly to allow the client to process incoming messages and maintain its con...
bool subscribe(const char *topic)
Subscribes to messages published to the specified topic using QoS 0.
PubSubClient & setCallback(MQTT_CALLBACK_SIGNATURE)
Sets the message callback function.
bool unsubscribe(const __FlashStringHelper *topic)
Unsubscribes from the specified topic from __FlashStringHelper.
bool publish_P(const char *topic, PGM_P payload, uint8_t qos, bool retained)
Publishes a message stored in PROGMEM to the specified topic.
PubSubClient & setServer(IPAddress ip, uint16_t port)
Sets the server details.
bool unsubscribe_P(PGM_P topic)
Unsubscribes from the specified topic in PROGMEM.
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 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(....
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 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(....
bool unsubscribe(const char *topic)
Unsubscribes from the specified topic.
virtual size_t write(uint8_t data)
Writes a single byte as a component of a publish started with a call to beginPublish....
bool publish(const char *topic, const char *payload)
Publishes a non retained message to the specified topic using QoS 0.
~PubSubClient()
Destructor for the PubSubClient class.
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 ...
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 __FlashStringHelper *payload, uint8_t qos, bool retained)
Publishes a message from __FlashStringHelper to the specified topic from __FlashStringHelper.
PubSubClient & setSocketTimeout(uint16_t timeout)
Sets the socket timeout used by the client. This determines how long the client will wait for incomin...
PubSubClient()
Creates an uninitialised client instance.
PubSubClient & setKeepAlive(uint16_t keepAlive)
Sets the keep alive interval used by the client. This value should only be changed when the client is...
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 subscribe_P(PGM_P topic)
Subscribes to messages published to the specified topic in PROGMEM 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 char *payload, bool retained)
Publishes a message to the specified topic using QoS 0.
bool connected()
Checks whether the client is connected to the server.
bool connect(const char *id, const char *user, const char *pass)
Connects the client using a clean session with username and password.
int state()
Returns the current state of the client. If a connection attempt fails, this can be used to get more ...
bool endPublish()
Finish sending a message that was started with a call to beginPublish.
size_t write_P(PGM_P string)
Writes a string in PROGMEM as a component of a publish started with a call to beginPublish....
PubSubClient & setClient(Client &client)
Sets the network client instance to use.
void disconnect()
Disconnects the client.
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(....
size_t getBufferSize()
Gets the current size of the internal buffer.
bool subscribe(const __FlashStringHelper *topic)
Subscribes to messages published to the specified topic from __FlashStringHelper using QoS 0.
bool subscribe(const char *topic, uint8_t qos)
Subscribes to messages published to the specified topic.
bool subscribe_P(PGM_P topic, uint8_t qos)
Subscribes to messages published to the specified topic in PROGMEM.
bool subscribe(const __FlashStringHelper *topic, uint8_t qos)
Subscribes to messages published to the specified topic from __FlashStringHelper.
bool setBufferSize(size_t size)
Sets the size, in bytes, of the internal send and receive buffer. This must be large enough to contai...
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 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 connect(const char *id)
Connects the client using a clean session without username and password.
bool publish(const __FlashStringHelper *topic, const char *payload, uint8_t qos, bool retained)
Publishes a message to the specified topic.
PubSubClient & setStream(Stream &stream)
Sets the stream to write received messages to.
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.
#define MQTT_QOS0
Quality of Service 0: At most once.
#define MQTT_DISCONNECTED
The client is disconnected cleanly.