1#ifdef NOT_FOR_SHOW_ONLY
16#define MQTT_VERSION_3_1 3
17#define MQTT_VERSION_3_1_1 4
22#define MQTT_VERSION MQTT_VERSION_3_1_1
26#ifndef MQTT_MAX_PACKET_SIZE
27#define MQTT_MAX_PACKET_SIZE 256
32#define MQTT_KEEPALIVE 15
36#ifndef MQTT_SOCKET_TIMEOUT
37#define MQTT_SOCKET_TIMEOUT 15
46#define MQTT_CONNECTION_TIMEOUT -4
47#define MQTT_CONNECTION_LOST -3
48#define MQTT_CONNECT_FAILED -2
49#define MQTT_DISCONNECTED -1
50#define MQTT_CONNECTED 0
51#define MQTT_CONNECT_BAD_PROTOCOL 1
52#define MQTT_CONNECT_BAD_CLIENT_ID 2
53#define MQTT_CONNECT_UNAVAILABLE 3
54#define MQTT_CONNECT_BAD_CREDENTIALS 4
55#define MQTT_CONNECT_UNAUTHORIZED 5
57#define MQTTCONNECT 1 << 4
58#define MQTTCONNACK 2 << 4
59#define MQTTPUBLISH 3 << 4
60#define MQTTPUBACK 4 << 4
61#define MQTTPUBREC 5 << 4
62#define MQTTPUBREL 6 << 4
63#define MQTTPUBCOMP 7 << 4
64#define MQTTSUBSCRIBE 8 << 4
65#define MQTTSUBACK 9 << 4
66#define MQTTUNSUBSCRIBE 10 << 4
67#define MQTTUNSUBACK 11 << 4
68#define MQTTPINGREQ 12 << 4
69#define MQTTPINGRESP 13 << 4
70#define MQTTDISCONNECT 14 << 4
71#define MQTTReserved 15 << 4
73#define MQTTQOS0 (0 << 1)
74#define MQTTQOS1 (1 << 1)
75#define MQTTQOS2 (2 << 1)
78#define MQTT_MAX_HEADER_SIZE 5
80#if defined(ESP8266) || defined(ESP32)
82#define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback
84#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
87#define CHECK_STRING_LENGTH(l,s) if (l+2+strnlen(s, this->bufferSize) > this->bufferSize) {_client->stop();return false;}
89class PubSubClient :
public Print {
95 uint16_t socketTimeout;
97 unsigned long lastOutActivity;
98 unsigned long lastInActivity;
100 MQTT_CALLBACK_SIGNATURE;
101 uint32_t readPacket(uint8_t*);
102 boolean readByte(uint8_t * result);
103 boolean readByte(uint8_t * result, uint16_t * index);
104 boolean write(uint8_t header, uint8_t* buf, uint16_t length);
105 uint16_t writeString(
const char*
string, uint8_t* buf, uint16_t pos);
110 size_t buildHeader(uint8_t header, uint8_t* buf, uint16_t length);
118 PubSubClient(Client& client);
119 PubSubClient(IPAddress, uint16_t, Client& client);
120 PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
121 PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
122 PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
123 PubSubClient(uint8_t *, uint16_t, Client& client);
124 PubSubClient(uint8_t *, uint16_t, Client& client, Stream&);
125 PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
126 PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
127 PubSubClient(
const char*, uint16_t, Client& client);
128 PubSubClient(
const char*, uint16_t, Client& client, Stream&);
129 PubSubClient(
const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
130 PubSubClient(
const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
134 PubSubClient& setServer(IPAddress ip, uint16_t port);
135 PubSubClient& setServer(uint8_t * ip, uint16_t port);
136 PubSubClient& setServer(
const char * domain, uint16_t port);
137 PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
138 PubSubClient& setClient(Client& client);
139 PubSubClient& setStream(Stream& stream);
140 PubSubClient& setKeepAlive(uint16_t keepAlive);
141 PubSubClient& setSocketTimeout(uint16_t timeout);
143 boolean setBufferSize(uint16_t size);
144 uint16_t getBufferSize();
146 boolean connect(
const char*
id);
147 boolean connect(
const char*
id,
const char* user,
const char* pass);
148 boolean connect(
const char*
id,
const char* willTopic, uint8_t willQos,
boolean willRetain,
const char* willMessage);
149 boolean connect(
const char*
id,
const char* user,
const char* pass,
const char* willTopic, uint8_t willQos,
boolean willRetain,
const char* willMessage);
150 boolean connect(
const char*
id,
const char* user,
const char* pass,
const char* willTopic, uint8_t willQos,
boolean willRetain,
const char* willMessage,
boolean cleanSession);
152 boolean publish(
const char* topic,
const char* payload);
153 boolean publish(
const char* topic,
const char* payload,
boolean retained);
154 boolean publish(
const char* topic,
const uint8_t * payload,
unsigned int plength);
155 boolean publish(
const char* topic,
const uint8_t * payload,
unsigned int plength,
boolean retained);
156 boolean publish_P(
const char* topic,
const char* payload,
boolean retained);
157 boolean publish_P(
const char* topic,
const uint8_t * payload,
unsigned int plength,
boolean retained);
166 boolean beginPublish(
const char* topic,
unsigned int plength,
boolean retained);
171 virtual size_t write(uint8_t);
174 virtual size_t write(
const uint8_t *buffer,
size_t size);
175 boolean subscribe(
const char* topic);
176 boolean subscribe(
const char* topic, uint8_t qos);
177 boolean unsubscribe(
const char* topic);
void loop()
main loop() of the Arduino runtime