ESP_IOT v2.5
IOT ESP Coding
PubSubClient.h
Go to the documentation of this file.
1#ifdef NOT_FOR_SHOW_ONLY
2/*
3 PubSubClient.h - A simple client for MQTT.
4 Nick O'Leary
5 http://knolleary.net
6*/
7
8#ifndef PubSubClient_h
9#define PubSubClient_h
10
11#include <Arduino.h>
12#include "IPAddress.h"
13#include "Client.h"
14#include "Stream.h"
15
16#define MQTT_VERSION_3_1 3
17#define MQTT_VERSION_3_1_1 4
18
19// MQTT_VERSION : Pick the version
20//#define MQTT_VERSION MQTT_VERSION_3_1
21#ifndef MQTT_VERSION
22#define MQTT_VERSION MQTT_VERSION_3_1_1
23#endif
24
25// MQTT_MAX_PACKET_SIZE : Maximum packet size. Override with setBufferSize().
26#ifndef MQTT_MAX_PACKET_SIZE
27#define MQTT_MAX_PACKET_SIZE 256
28#endif
29
30// MQTT_KEEPALIVE : keepAlive interval in Seconds. Override with setKeepAlive()
31#ifndef MQTT_KEEPALIVE
32#define MQTT_KEEPALIVE 15
33#endif
34
35// MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds. Override with setSocketTimeout()
36#ifndef MQTT_SOCKET_TIMEOUT
37#define MQTT_SOCKET_TIMEOUT 15
38#endif
39
40// MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client
41// in each write call. Needed for the Arduino Wifi Shield. Leave undefined to
42// pass the entire MQTT packet in each write call.
43//#define MQTT_MAX_TRANSFER_SIZE 80
44
45// Possible values for client.state()
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
56
57#define MQTTCONNECT 1 << 4 // Client request to connect to Server
58#define MQTTCONNACK 2 << 4 // Connect Acknowledgment
59#define MQTTPUBLISH 3 << 4 // Publish message
60#define MQTTPUBACK 4 << 4 // Publish Acknowledgment
61#define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
62#define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
63#define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
64#define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
65#define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
66#define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
67#define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
68#define MQTTPINGREQ 12 << 4 // PING Request
69#define MQTTPINGRESP 13 << 4 // PING Response
70#define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
71#define MQTTReserved 15 << 4 // Reserved
72
73#define MQTTQOS0 (0 << 1)
74#define MQTTQOS1 (1 << 1)
75#define MQTTQOS2 (2 << 1)
76
77// Maximum size of fixed header and variable length size header
78#define MQTT_MAX_HEADER_SIZE 5
79
80#if defined(ESP8266) || defined(ESP32)
81#include <functional>
82#define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback
83#else
84#define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
85#endif
86
87#define CHECK_STRING_LENGTH(l,s) if (l+2+strnlen(s, this->bufferSize) > this->bufferSize) {_client->stop();return false;}
88
89class PubSubClient : public Print {
90private:
91 Client* _client;
92 uint8_t* buffer;
93 uint16_t bufferSize;
94 uint16_t keepAlive;
95 uint16_t socketTimeout;
96 uint16_t nextMsgId;
97 unsigned long lastOutActivity;
98 unsigned long lastInActivity;
99 bool pingOutstanding;
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);
106 // Build up the header ready to send
107 // Returns the size of the header
108 // Note: the header is built at the end of the first MQTT_MAX_HEADER_SIZE bytes, so will start
109 // (MQTT_MAX_HEADER_SIZE - <returned size>) bytes into the buffer
110 size_t buildHeader(uint8_t header, uint8_t* buf, uint16_t length);
111 IPAddress ip;
112 const char* domain;
113 uint16_t port;
114 Stream* stream;
115 int _state;
116public:
117 PubSubClient();
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&);
131
132 ~PubSubClient();
133
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);
142
143 boolean setBufferSize(uint16_t size);
144 uint16_t getBufferSize();
145
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);
151 void disconnect();
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);
158 // Start to publish a message.
159 // This API:
160 // beginPublish(...)
161 // one or more calls to write(...)
162 // endPublish()
163 // Allows for arbitrarily large payloads to be sent without them having to be copied into
164 // a new buffer and held in memory at one time
165 // Returns 1 if the message was started successfully, 0 if there was an error
166 boolean beginPublish(const char* topic, unsigned int plength, boolean retained);
167 // Finish off this publish message (started with beginPublish)
168 // Returns 1 if the packet was sent successfully, 0 if there was an error
169 int endPublish();
170 // Write a single byte of payload (only to be used with beginPublish/endPublish)
171 virtual size_t write(uint8_t);
172 // Write size bytes from buffer into the payload (only to be used with beginPublish/endPublish)
173 // Returns the number of bytes written
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);
178 boolean loop();
179 boolean connected();
180 int state();
181
182};
183
184
185#endif
186
187#endif
void loop()
main loop() of the Arduino runtime
Definition: ESP_IOT.ino:272
WiFiClient _client