PubSubClient3 v3.1.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 268435455
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 unsigned long keepAliveMillis{};
182 unsigned long socketTimeoutMillis{};
183 uint16_t nextMsgId{};
184 unsigned long lastOutActivity{};
185 unsigned long lastInActivity{};
186 bool pingOutstanding{};
187 MQTT_CALLBACK_SIGNATURE{};
188 IPAddress ip{};
189 char* domain{};
190 uint16_t port{};
191 Stream* stream{};
192 int _state{MQTT_DISCONNECTED};
193 uint8_t _qos{MQTT_QOS0};
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, uint8_t* buf, size_t length);
200 bool write(uint8_t header, uint8_t* buf, size_t length);
201 size_t writeString(const char* string, uint8_t* buf, size_t pos, size_t size);
202 size_t writeNextMsgId(uint8_t* buf, size_t pos, size_t size);
203
204 public:
209 PubSubClient();
210
216 PubSubClient(Client& client);
217
224 PubSubClient(IPAddress addr, uint16_t port, Client& client);
225
233 PubSubClient(IPAddress addr, uint16_t port, Client& client, Stream& stream);
234
243 PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client);
244
254 PubSubClient(IPAddress addr, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream);
255
262 PubSubClient(uint8_t* ip, uint16_t port, Client& client);
263
271 PubSubClient(uint8_t* ip, uint16_t port, Client& client, Stream& stream);
272
281 PubSubClient(uint8_t* ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client);
282
292 PubSubClient(uint8_t* ip, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream);
293
300 PubSubClient(const char* domain, uint16_t port, Client& client);
301
309 PubSubClient(const char* domain, uint16_t port, Client& client, Stream& stream);
310
319 PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client);
320
330 PubSubClient(const char* domain, uint16_t port, MQTT_CALLBACK_SIGNATURE, Client& client, Stream& stream);
331
336
343 PubSubClient& setServer(IPAddress ip, uint16_t port);
344
351 PubSubClient& setServer(uint8_t* ip, uint16_t port);
352
359 PubSubClient& setServer(const char* domain, uint16_t port);
360
367 PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
368
374 PubSubClient& setClient(Client& client);
375
381 PubSubClient& setStream(Stream& stream);
382
390 PubSubClient& setKeepAlive(uint16_t keepAlive);
391
399 PubSubClient& setSocketTimeout(uint16_t timeout);
400
410 bool setBufferSize(size_t size);
411
416 size_t getBufferSize();
417
424 bool connect(const char* id);
425
436 bool connect(const char* id, const char* user, const char* pass);
437
449 bool connect(const char* id, const char* willTopic, uint8_t willQos, bool willRetain, const char* willMessage);
450
466 bool connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, bool willRetain, const char* willMessage);
467
484 bool connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, bool willRetain, const char* willMessage,
485 bool cleanSession);
486
490 void disconnect();
491
499 bool publish(const char* topic, const char* payload);
500
509 bool publish(const char* topic, const char* payload, bool retained);
510
520 bool publish(const char* topic, const char* payload, uint8_t qos, bool retained);
521
530 bool publish(const char* topic, const uint8_t* payload, size_t plength);
531
541 bool publish(const char* topic, const uint8_t* payload, size_t plength, bool retained);
542
553 bool publish(const char* topic, const uint8_t* payload, size_t plength, uint8_t qos, bool retained);
554
563 bool publish_P(const char* topic, const char* payload, bool retained);
564
574 bool publish_P(const char* topic, const char* payload, uint8_t qos, bool retained);
575
585 bool publish_P(const char* topic, const uint8_t* payload, size_t plength, bool retained);
586
597 bool publish_P(const char* topic, const uint8_t* payload, size_t plength, uint8_t qos, bool retained);
598
613 bool beginPublish(const char* topic, size_t plength, bool retained);
614
630 bool beginPublish(const char* topic, size_t plength, uint8_t qos, bool retained);
631
637 bool endPublish();
638
644 virtual size_t write(uint8_t data);
645
652 virtual size_t write(const uint8_t* buffer, size_t size);
653
660 bool subscribe(const char* topic);
661
669 bool subscribe(const char* topic, uint8_t qos);
670
677 bool unsubscribe(const char* topic);
678
684 bool loop();
685
691 bool connected();
692
699 int state();
700};
701
702#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.
PubSubClient & setServer(IPAddress ip, uint16_t port)
Sets the server details.
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.
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(const char *topic, const char *payload)
Publishes a non retained message to the specified topic using QoS 0.
~PubSubClient()
Destructor for the PubSubClient class.
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 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 ...
bool endPublish()
Finish sending a message that was started with a call to beginPublish.
PubSubClient & setClient(Client &client)
Sets the network client instance to use.
void disconnect()
Disconnects the client.
size_t getBufferSize()
Gets the current size of the internal buffer.
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)
Connects the client using a clean session without username and password.
PubSubClient & setStream(Stream &stream)
Sets the stream to write received messages to.
#define MQTT_QOS0
Quality of Service 0: At most once.
#define MQTT_DISCONNECTED
The client is disconnected cleanly.