PubSubClient3 v3.3.0
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#define MQTT_MAX_HEADER_SIZE 5
134
136
142#if defined(__has_include) && __has_include(<functional>) && !defined(NOFUNCTIONAL)
143#include <functional>
144#define MQTT_CALLBACK_SIGNATURE std::function<void(char* topic, uint8_t* payload, size_t plength)> callback
145#else
146#define MQTT_CALLBACK_SIGNATURE void (*callback)(char* topic, uint8_t* payload, size_t plength)
147#endif
148
150#ifdef DEBUG_ESP_PORT
151#ifdef DEBUG_PUBSUBCLIENT
152#define DEBUG_PSC_PRINTF(fmt, ...) DEBUG_ESP_PORT.printf(("PubSubClient: " fmt), ##__VA_ARGS__)
153#else
154#define DEBUG_PSC_PRINTF(...)
155#endif
156
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__)
159#else // DEBUG_ESP_PORT
160#ifndef DEBUG_PSC_PRINTF
161#define DEBUG_PSC_PRINTF(...)
162#endif
163#ifndef ERROR_PSC_PRINTF
164#define ERROR_PSC_PRINTF(fmt, ...)
165#endif
166#ifndef ERROR_PSC_PRINTF_P
167#define ERROR_PSC_PRINTF_P(fmt, ...)
168#endif
169#endif
171
176class PubSubClient : public Print {
177 private:
178 Client* _client{};
179 uint8_t* _buffer{};
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{};
189 IPAddress _ip{};
190 char* _domain{};
191 uint16_t _port{};
192 Stream* _stream{};
193 int _state{MQTT_DISCONNECTED};
194
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);
205
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);
209
210 // Add to buffer and flush if full (only to be used with beginPublish/endPublish)
211 size_t appendBuffer(uint8_t data);
212 size_t flushBuffer();
213
214 public:
219 PubSubClient();
220
226 PubSubClient(Client& client);
227
234 PubSubClient(IPAddress addr, uint16_t port, Client& client);
235
243 PubSubClient(IPAddress addr, uint16_t port, Client& client, Stream& stream);
244
253 PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client);
254
264 PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream);
265
272 PubSubClient(uint8_t* ip, uint16_t port, Client& client);
273
281 PubSubClient(uint8_t* ip, uint16_t port, Client& client, Stream& stream);
282
291 PubSubClient(uint8_t* ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client);
292
302 PubSubClient(uint8_t* ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream);
303
310 PubSubClient(const char* domain, uint16_t port, Client& client);
311
319 PubSubClient(const char* domain, uint16_t port, Client& client, Stream& stream);
320
329 PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client);
330
340 PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream);
341
346
353 PubSubClient& setServer(IPAddress ip, uint16_t port);
354
361 PubSubClient& setServer(uint8_t* ip, uint16_t port);
362
369 PubSubClient& setServer(const char* domain, uint16_t port);
370
377 PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
378
384 PubSubClient& setClient(Client& client);
385
391 PubSubClient& setStream(Stream& stream);
392
400 PubSubClient& setKeepAlive(uint16_t keepAlive);
401
409 PubSubClient& setSocketTimeout(uint16_t timeout);
410
420 bool setBufferSize(size_t size);
421
426 size_t getBufferSize();
427
434 inline bool connect(const char* id) {
435 return connect(id, nullptr, nullptr, nullptr, MQTT_QOS0, false, nullptr, true);
436 }
437
448 inline bool connect(const char* id, const char* user, const char* pass) {
449 return connect(id, user, pass, nullptr, MQTT_QOS0, false, nullptr, true);
450 }
451
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);
465 }
466
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);
485 }
486
503 bool connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, bool willRetain, const char* willMessage,
504 bool cleanSession);
505
509 void disconnect();
510
518 inline bool publish(const char* topic, const char* payload) {
519 return publish(topic, payload, MQTT_QOS0, false);
520 }
521
530 inline bool publish(const char* topic, const char* payload, bool retained) {
531 return publish(topic, payload, MQTT_QOS0, retained);
532 }
533
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);
545 }
546
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);
558 }
559
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);
571 }
572
581 inline bool publish(const char* topic, const uint8_t* payload, size_t plength) {
582 return publish(topic, payload, plength, MQTT_QOS0, false);
583 }
584
594 inline bool publish(const char* topic, const uint8_t* payload, size_t plength, bool retained) {
595 return publish(topic, payload, plength, MQTT_QOS0, retained);
596 }
597
608 bool publish(const char* topic, const uint8_t* payload, size_t plength, uint8_t qos, bool retained);
609
620 bool publish(const __FlashStringHelper* topic, const uint8_t* payload, size_t plength, uint8_t qos, bool retained);
621
630 inline bool publish_P(const char* topic, PGM_P payload, bool retained) {
631 return publish_P(topic, payload, MQTT_QOS0, retained);
632 }
633
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);
645 }
646
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);
658 }
659
669 inline bool publish_P(const char* topic, const uint8_t* payload, size_t plength, bool retained) {
670 return publish_P(topic, payload, plength, MQTT_QOS0, retained);
671 }
672
683 bool publish_P(const char* topic, const uint8_t* payload, size_t plength, uint8_t qos, bool retained);
684
695 bool publish_P(const __FlashStringHelper* topic, const uint8_t* payload, size_t plength, uint8_t qos, bool retained);
696
711 inline bool beginPublish(const char* topic, size_t plength, bool retained) {
712 return beginPublishImpl(false, topic, plength, MQTT_QOS0, retained);
713 }
714
730 inline bool beginPublish(const char* topic, size_t plength, uint8_t qos, bool retained) {
731 return beginPublishImpl(false, topic, plength, qos, retained);
732 }
733
749 inline bool beginPublish(const __FlashStringHelper* topic, size_t plength, uint8_t qos, bool retained) {
750 // convert FlashStringHelper in PROGMEM-pointer
751 return beginPublishImpl(true, reinterpret_cast<const char*>(topic), plength, qos, retained);
752 }
753
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);
771 }
772
778 bool endPublish();
779
787 virtual size_t write(uint8_t data);
788
797 virtual size_t write(const uint8_t* buf, size_t size);
798
806 inline size_t write_P(PGM_P string) {
807 return write_P(reinterpret_cast<const uint8_t*>(string), strlen_P(string));
808 }
809
818 size_t write_P(const uint8_t* buf, size_t size);
819
826 inline bool subscribe(const char* topic) {
827 return subscribeImpl(false, topic, MQTT_QOS0);
828 }
829
836 inline bool subscribe(const __FlashStringHelper* topic) {
837 // convert FlashStringHelper in PROGMEM-pointer
838 return subscribeImpl(true, reinterpret_cast<const char*>(topic), MQTT_QOS0);
839 }
840
847 inline bool subscribe_P(PGM_P topic) {
848 return subscribeImpl(true, reinterpret_cast<const char*>(topic), MQTT_QOS0);
849 }
850
858 inline bool subscribe(const char* topic, uint8_t qos) {
859 return subscribeImpl(false, topic, qos);
860 }
861
869 inline bool subscribe(const __FlashStringHelper* topic, uint8_t qos) {
870 // convert FlashStringHelper in PROGMEM-pointer
871 return subscribeImpl(true, reinterpret_cast<const char*>(topic), qos);
872 }
873
881 inline bool subscribe_P(PGM_P topic, uint8_t qos) {
882 return subscribeImpl(true, reinterpret_cast<const char*>(topic), qos);
883 }
884
891 inline bool unsubscribe(const char* topic) {
892 return unsubscribeImpl(false, topic);
893 }
894
901 inline bool unsubscribe(const __FlashStringHelper* topic) {
902 // convert FlashStringHelper in PROGMEM-pointer
903 return unsubscribeImpl(true, reinterpret_cast<const char*>(topic));
904 }
905
912 inline bool unsubscribe_P(PGM_P topic) {
913 return unsubscribeImpl(true, reinterpret_cast<const char*>(topic));
914 }
915
921 bool loop();
922
928 bool connected();
929
936 int state();
937};
938
939#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.