PubSubClient3 v3.3.2
Located at <a href='https://github.com/hmueller01/pubsubclient3'>GitHub</a>
 
Loading...
Searching...
No Matches
PubSubClient.h
Go to the documentation of this file.
1
10
11#ifndef PubSubClient_h
12#define PubSubClient_h
13
14#include <Arduino.h>
15
16#include "Client.h"
17#include "IPAddress.h"
18#include "Stream.h"
19
20#define MQTT_VERSION_3_1 3
21#define MQTT_VERSION_3_1_1 4
22
23//< @note The following #define directives can be used to configure the library.
24
29#ifndef MQTT_VERSION
30#define MQTT_VERSION MQTT_VERSION_3_1_1
31#endif
32
36#ifndef MQTT_MAX_POSSIBLE_PACKET_SIZE
37#define MQTT_MAX_POSSIBLE_PACKET_SIZE ((size_t)268435455) // might be limited to 65535 if size_t is 16-bit (unsigned int)
38#endif
39
46#ifndef MQTT_MAX_PACKET_SIZE
47#define MQTT_MAX_PACKET_SIZE 256
48#endif
49
56#ifndef MQTT_KEEPALIVE
57#define MQTT_KEEPALIVE 15
58#endif
59
66#ifndef MQTT_SOCKET_TIMEOUT
67#define MQTT_SOCKET_TIMEOUT 15
68#endif
69
76#ifndef MQTT_MAX_TRANSFER_SIZE // just a hack that it gets shown in Doxygen
77#define MQTT_MAX_TRANSFER_SIZE 80
78#undef MQTT_MAX_TRANSFER_SIZE
79#endif
80
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
97
99#define MQTTRETAINED 1 // Retained flag in the header
100#define MQTTCONNECT 1 << 4 // Client request to connect to Server
101#define MQTTCONNACK 2 << 4 // Connect Acknowledgment
102#define MQTTPUBLISH 3 << 4 // Publish message
103#define MQTTPUBACK 4 << 4 // Publish Acknowledgment
104#define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
105#define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
106#define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
107#define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
108#define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
109#define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
110#define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
111#define MQTTPINGREQ 12 << 4 // PING Request
112#define MQTTPINGRESP 13 << 4 // PING Response
113#define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
114#define MQTTRESERVED 15 << 4 // Reserved
116
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) // Get QoS header bits from QoS value
127#define MQTT_HDR_GET_QOS(header) (((header) & 0x06 ) >> 1) // Get QoS value from MQTT header
129
130
132// Maximum size of fixed header and variable length size header
133#define MQTT_MAX_HEADER_SIZE 5
134// Minimal buffer size that can be handled, used to check if the buffer size is sufficient in setBufferSize().
135// MQTT_MAX_HEADER_SIZE (5) + protocol (9) + flags (1) + keepalive (2) covers a minmal CONNECT message
136#define MQTT_MIN_BUFFER_SIZE (MQTT_MAX_HEADER_SIZE + 9 + 1 + 2)
138
140
146#if defined(__has_include) && __has_include(<functional>) && !defined(NOFUNCTIONAL)
147#include <functional>
148#define MQTT_CALLBACK_SIGNATURE std::function<void(char* topic, uint8_t* payload, size_t plength)> callback
149#else
150#define MQTT_CALLBACK_SIGNATURE void (*callback)(char* topic, uint8_t* payload, size_t plength)
151#endif
152
154#ifdef DEBUG_ESP_PORT
155#ifdef DEBUG_PUBSUBCLIENT
156#define DEBUG_PSC_PRINTF(fmt, ...) DEBUG_ESP_PORT.printf(("PubSubClient: " fmt), ##__VA_ARGS__)
157#else
158#define DEBUG_PSC_PRINTF(...)
159#endif
160
161#define ERROR_PSC_PRINTF(fmt, ...) DEBUG_ESP_PORT.printf(("PubSubClient error: " fmt), ##__VA_ARGS__)
162#define ERROR_PSC_PRINTF_P(fmt, ...) DEBUG_ESP_PORT.printf_P(PSTR("PubSubClient error: " fmt), ##__VA_ARGS__)
163#else // DEBUG_ESP_PORT
164#ifndef DEBUG_PSC_PRINTF
165#define DEBUG_PSC_PRINTF(...)
166#endif
167#ifndef ERROR_PSC_PRINTF
168#define ERROR_PSC_PRINTF(fmt, ...)
169#endif
170#ifndef ERROR_PSC_PRINTF_P
171#define ERROR_PSC_PRINTF_P(fmt, ...)
172#endif
173#endif
175
180class PubSubClient : public Print {
181 private:
182 Client* _client{};
183 uint8_t* _buffer{};
184 size_t _bufferSize{};
185 size_t _bufferWritePos{};
186 unsigned long _keepAliveMillis{};
187 unsigned long _socketTimeoutMillis{};
188 uint16_t _nextMsgId{};
189 unsigned long _lastOutActivity{};
190 unsigned long _lastInActivity{};
191 bool _pingOutstanding{};
192 MQTT_CALLBACK_SIGNATURE{};
193 IPAddress _ip{};
194 char* _domain{};
195 uint16_t _port{};
196 Stream* _stream{};
197 int _state{MQTT_DISCONNECTED};
198
199 size_t readPacket(uint8_t* hdrLen);
200 bool handlePacket(uint8_t hdrLen, size_t len);
201 bool readByte(uint8_t* result);
202 bool readByte(uint8_t* result, size_t* pos);
203 uint8_t buildHeader(uint8_t header, size_t length);
204 bool writeControlPacket(uint8_t header, size_t length);
205 size_t writeBuffer(size_t pos, size_t size);
206 size_t writeStringImpl(bool progmem, const char* string, size_t pos);
207 size_t writeString(const char* string, size_t pos);
208 size_t writeNextMsgId(size_t pos);
209
210 bool beginPublishImpl(bool progmem, const char* topic, size_t plength, uint8_t qos, bool retained);
211 bool subscribeImpl(bool progmem, const char* topic, uint8_t qos);
212 bool unsubscribeImpl(bool progmem, const char* topic);
213
214 // Add to buffer and flush if full (only to be used with beginPublish/endPublish)
215 size_t appendBuffer(uint8_t data);
216 size_t flushBuffer();
217
218 public:
223 PubSubClient();
224
230 PubSubClient(Client& client);
231
238 PubSubClient(IPAddress addr, uint16_t port, Client& client);
239
247 PubSubClient(IPAddress addr, uint16_t port, Client& client, Stream& stream);
248
257 PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client);
258
268 PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream);
269
276 PubSubClient(uint8_t* ip, uint16_t port, Client& client);
277
285 PubSubClient(uint8_t* ip, uint16_t port, Client& client, Stream& stream);
286
295 PubSubClient(uint8_t* ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client);
296
306 PubSubClient(uint8_t* ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream);
307
314 PubSubClient(const char* domain, uint16_t port, Client& client);
315
323 PubSubClient(const char* domain, uint16_t port, Client& client, Stream& stream);
324
333 PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client);
334
344 PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream);
345
350
357 PubSubClient& setServer(IPAddress ip, uint16_t port);
358
365 PubSubClient& setServer(uint8_t* ip, uint16_t port);
366
373 PubSubClient& setServer(const char* domain, uint16_t port);
374
381 PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
382
388 PubSubClient& setClient(Client& client);
389
395 PubSubClient& setStream(Stream& stream);
396
404 PubSubClient& setKeepAlive(uint16_t keepAlive);
405
413 PubSubClient& setSocketTimeout(uint16_t timeout);
414
430 bool setBufferSize(size_t size);
431
436 size_t getBufferSize();
437
444 inline bool connect(const char* id) {
445 return connect(id, nullptr, nullptr, nullptr, MQTT_QOS0, false, nullptr, true);
446 }
447
458 inline bool connect(const char* id, const char* user, const char* pass) {
459 return connect(id, user, pass, nullptr, MQTT_QOS0, false, nullptr, true);
460 }
461
473 inline bool connect(const char* id, const char* willTopic, uint8_t willQos, bool willRetain, const char* willMessage) {
474 return connect(id, nullptr, nullptr, willTopic, willQos, willRetain, willMessage, true);
475 }
476
492 inline bool connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, bool willRetain,
493 const char* willMessage) {
494 return connect(id, user, pass, willTopic, willQos, willRetain, willMessage, true);
495 }
496
513 bool connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, bool willRetain, const char* willMessage,
514 bool cleanSession);
515
519 void disconnect();
520
528 inline bool publish(const char* topic, const char* payload) {
529 return publish(topic, payload, MQTT_QOS0, false);
530 }
531
540 inline bool publish(const char* topic, const char* payload, bool retained) {
541 return publish(topic, payload, MQTT_QOS0, retained);
542 }
543
553 inline bool publish(const char* topic, const char* payload, uint8_t qos, bool retained) {
554 return publish(topic, reinterpret_cast<const uint8_t*>(payload), payload ? strlen(payload) : 0, qos, retained);
555 }
556
566 inline bool publish(const __FlashStringHelper* topic, const char* payload, uint8_t qos, bool retained) {
567 return publish(topic, reinterpret_cast<const uint8_t*>(payload), payload ? strlen(payload) : 0, qos, retained);
568 }
569
579 inline bool publish(const __FlashStringHelper* topic, const __FlashStringHelper* payload, uint8_t qos, bool retained) {
580 return publish_P(topic, reinterpret_cast<const uint8_t*>(payload), payload ? strlen_P(reinterpret_cast<const char*>(payload)) : 0, qos, retained);
581 }
582
591 inline bool publish(const char* topic, const uint8_t* payload, size_t plength) {
592 return publish(topic, payload, plength, MQTT_QOS0, false);
593 }
594
604 inline bool publish(const char* topic, const uint8_t* payload, size_t plength, bool retained) {
605 return publish(topic, payload, plength, MQTT_QOS0, retained);
606 }
607
618 bool publish(const char* topic, const uint8_t* payload, size_t plength, uint8_t qos, bool retained);
619
630 bool publish(const __FlashStringHelper* topic, const uint8_t* payload, size_t plength, uint8_t qos, bool retained);
631
640 inline bool publish_P(const char* topic, PGM_P payload, bool retained) {
641 return publish_P(topic, payload, MQTT_QOS0, retained);
642 }
643
653 inline bool publish_P(const char* topic, PGM_P payload, uint8_t qos, bool retained) {
654 return publish_P(topic, reinterpret_cast<const uint8_t*>(payload), payload ? strlen_P(payload) : 0, qos, retained);
655 }
656
666 bool publish_P(const __FlashStringHelper* topic, PGM_P payload, uint8_t qos, bool retained) {
667 return publish_P(topic, reinterpret_cast<const uint8_t*>(payload), payload ? strlen_P(payload) : 0, qos, retained);
668 }
669
679 inline bool publish_P(const char* topic, const uint8_t* payload, size_t plength, bool retained) {
680 return publish_P(topic, payload, plength, MQTT_QOS0, retained);
681 }
682
693 bool publish_P(const char* topic, const uint8_t* payload, size_t plength, uint8_t qos, bool retained);
694
705 bool publish_P(const __FlashStringHelper* topic, const uint8_t* payload, size_t plength, uint8_t qos, bool retained);
706
721 inline bool beginPublish(const char* topic, size_t plength, bool retained) {
722 return beginPublishImpl(false, topic, plength, MQTT_QOS0, retained);
723 }
724
740 inline bool beginPublish(const char* topic, size_t plength, uint8_t qos, bool retained) {
741 return beginPublishImpl(false, topic, plength, qos, retained);
742 }
743
759 inline bool beginPublish(const __FlashStringHelper* topic, size_t plength, uint8_t qos, bool retained) {
760 // convert FlashStringHelper in PROGMEM-pointer
761 return beginPublishImpl(true, reinterpret_cast<const char*>(topic), plength, qos, retained);
762 }
763
779 inline bool beginPublish_P(PGM_P topic, size_t plength, uint8_t qos, bool retained) {
780 return beginPublishImpl(true, reinterpret_cast<const char*>(topic), plength, qos, retained);
781 }
782
788 bool endPublish();
789
797 virtual size_t write(uint8_t data);
798
807 virtual size_t write(const uint8_t* buf, size_t size);
808
816 inline size_t write_P(PGM_P string) {
817 return write_P(reinterpret_cast<const uint8_t*>(string), strlen_P(string));
818 }
819
828 size_t write_P(const uint8_t* buf, size_t size);
829
836 inline bool subscribe(const char* topic) {
837 return subscribeImpl(false, topic, MQTT_QOS0);
838 }
839
846 inline bool subscribe(const __FlashStringHelper* topic) {
847 // convert FlashStringHelper in PROGMEM-pointer
848 return subscribeImpl(true, reinterpret_cast<const char*>(topic), MQTT_QOS0);
849 }
850
857 inline bool subscribe_P(PGM_P topic) {
858 return subscribeImpl(true, reinterpret_cast<const char*>(topic), MQTT_QOS0);
859 }
860
868 inline bool subscribe(const char* topic, uint8_t qos) {
869 return subscribeImpl(false, topic, qos);
870 }
871
879 inline bool subscribe(const __FlashStringHelper* topic, uint8_t qos) {
880 // convert FlashStringHelper in PROGMEM-pointer
881 return subscribeImpl(true, reinterpret_cast<const char*>(topic), qos);
882 }
883
891 inline bool subscribe_P(PGM_P topic, uint8_t qos) {
892 return subscribeImpl(true, reinterpret_cast<const char*>(topic), qos);
893 }
894
901 inline bool unsubscribe(const char* topic) {
902 return unsubscribeImpl(false, topic);
903 }
904
911 inline bool unsubscribe(const __FlashStringHelper* topic) {
912 // convert FlashStringHelper in PROGMEM-pointer
913 return unsubscribeImpl(true, reinterpret_cast<const char*>(topic));
914 }
915
922 inline bool unsubscribe_P(PGM_P topic) {
923 return unsubscribeImpl(true, reinterpret_cast<const char*>(topic));
924 }
925
931 bool loop();
932
938 bool connected();
939
946 int state();
947};
948
949#endif
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.