MySensors Library & Examples  2.3.2-62-ge298769
PubSubClient.h
1 /*
2  PubSubClient.h - A simple client for MQTT.
3  Nick O'Leary
4  http://knolleary.net
5 */
6 
7 #ifndef PubSubClient_h
8 #define PubSubClient_h
9 
10 #include <Arduino.h>
11 #include "IPAddress.h"
12 #include "Client.h"
13 #include "Stream.h"
14 
15 #define MQTT_VERSION_3_1 3
16 #define MQTT_VERSION_3_1_1 4
17 
18 // MQTT_VERSION : Pick the version
19 //#define MQTT_VERSION MQTT_VERSION_3_1
20 #ifndef MQTT_VERSION
21 #define MQTT_VERSION MQTT_VERSION_3_1_1
22 #endif
23 
24 // MQTT_MAX_PACKET_SIZE : Maximum packet size. Override with setBufferSize().
25 #ifndef MQTT_MAX_PACKET_SIZE
26 #define MQTT_MAX_PACKET_SIZE 256
27 #endif
28 
29 // MQTT_KEEPALIVE : keepAlive interval in Seconds. Override with setKeepAlive()
30 #ifndef MQTT_KEEPALIVE
31 #define MQTT_KEEPALIVE 15
32 #endif
33 
34 // MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds. Override with setSocketTimeout()
35 #ifndef MQTT_SOCKET_TIMEOUT
36 #define MQTT_SOCKET_TIMEOUT 15
37 #endif
38 
39 // MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client
40 // in each write call. Needed for the Arduino Wifi Shield. Leave undefined to
41 // pass the entire MQTT packet in each write call.
42 //#define MQTT_MAX_TRANSFER_SIZE 80
43 
44 // Possible values for client.state()
45 #define MQTT_CONNECTION_TIMEOUT -4
46 #define MQTT_CONNECTION_LOST -3
47 #define MQTT_CONNECT_FAILED -2
48 #define MQTT_DISCONNECTED -1
49 #define MQTT_CONNECTED 0
50 #define MQTT_CONNECT_BAD_PROTOCOL 1
51 #define MQTT_CONNECT_BAD_CLIENT_ID 2
52 #define MQTT_CONNECT_UNAVAILABLE 3
53 #define MQTT_CONNECT_BAD_CREDENTIALS 4
54 #define MQTT_CONNECT_UNAUTHORIZED 5
55 
56 #define MQTTCONNECT 1 << 4 // Client request to connect to Server
57 #define MQTTCONNACK 2 << 4 // Connect Acknowledgment
58 #define MQTTPUBLISH 3 << 4 // Publish message
59 #define MQTTPUBACK 4 << 4 // Publish Acknowledgment
60 #define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
61 #define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
62 #define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
63 #define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
64 #define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
65 #define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
66 #define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
67 #define MQTTPINGREQ 12 << 4 // PING Request
68 #define MQTTPINGRESP 13 << 4 // PING Response
69 #define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
70 #define MQTTReserved 15 << 4 // Reserved
71 
72 #define MQTTQOS0 (0 << 1)
73 #define MQTTQOS1 (1 << 1)
74 #define MQTTQOS2 (2 << 1)
75 
76 // Maximum size of fixed header and variable length size header
77 #define MQTT_MAX_HEADER_SIZE 5
78 
79 #if defined(ESP8266) || defined(ESP32)
80 #include <functional>
81 #define MQTT_CALLBACK_SIGNATURE std::function<void(char*, uint8_t*, unsigned int)> callback
82 #else
83 #define MQTT_CALLBACK_SIGNATURE void (*callback)(char*, uint8_t*, unsigned int)
84 #endif
85 
86 #define CHECK_STRING_LENGTH(l,s) if (l+2+strnlen(s, this->bufferSize) > this->bufferSize) {_client->stop();return false;}
87 
89 class PubSubClient : public Print
90 {
91 private:
92  Client* _client;
93  uint8_t* buffer;
94  uint16_t bufferSize;
95  uint16_t keepAlive;
96  uint16_t socketTimeout;
97  uint16_t nextMsgId;
98  unsigned long lastOutActivity;
99  unsigned long lastInActivity;
100  bool pingOutstanding;
101  MQTT_CALLBACK_SIGNATURE;
102  uint32_t readPacket(uint8_t*);
103  bool readByte(uint8_t * result);
104  bool readByte(uint8_t * result, uint16_t * index);
105  bool write(uint8_t header, uint8_t* buf, uint16_t length);
106  uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
107  // Build up the header ready to send
108  // Returns the size of the header
109  // Note: the header is built at the end of the first MQTT_MAX_HEADER_SIZE bytes, so will start
110  // (MQTT_MAX_HEADER_SIZE - <returned size>) bytes into the buffer
111  size_t buildHeader(uint8_t header, uint8_t* buf, uint16_t length);
112  IPAddress ip;
113  const char* domain;
114  uint16_t port;
115  Stream* stream;
116  int _state;
117 public:
118  PubSubClient();
119  PubSubClient(Client& client);
120  PubSubClient(IPAddress, uint16_t, Client& client);
121  PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
122  PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
123  PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client,
124  Stream&);
125  PubSubClient(uint8_t *, uint16_t, Client& client);
126  PubSubClient(uint8_t *, uint16_t, Client& client, Stream&);
127  PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
128  PubSubClient(uint8_t *, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client,
129  Stream&);
130  PubSubClient(const char*, uint16_t, Client& client);
131  PubSubClient(const char*, uint16_t, Client& client, Stream&);
132  PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
133  PubSubClient(const char*, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client,
134  Stream&);
135 
136  ~PubSubClient();
137 
138  PubSubClient& setServer(IPAddress ip, uint16_t port);
139  PubSubClient& setServer(uint8_t * ip, uint16_t port);
140  PubSubClient& setServer(const char * domain, uint16_t port);
141  PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
142  PubSubClient& setClient(Client& client);
143  PubSubClient& setStream(Stream& stream);
144  PubSubClient& setKeepAlive(uint16_t keepAlive);
145  PubSubClient& setSocketTimeout(uint16_t timeout);
146 
147  bool setBufferSize(uint16_t size);
148  uint16_t getBufferSize();
149 
150  bool connect(const char* id);
151  bool connect(const char* id, const char* user, const char* pass);
152  bool connect(const char* id, const char* willTopic, uint8_t willQos, bool willRetain,
153  const char* willMessage);
154  bool connect(const char* id, const char* user, const char* pass, const char* willTopic,
155  uint8_t willQos, bool willRetain, const char* willMessage);
156  bool connect(const char* id, const char* user, const char* pass, const char* willTopic,
157  uint8_t willQos, bool willRetain, const char* willMessage, bool cleanSession);
158  void disconnect();
159  bool publish(const char* topic, const char* payload);
160  bool publish(const char* topic, const char* payload, bool retained);
161  bool publish(const char* topic, const uint8_t * payload, unsigned int plength);
162  bool publish(const char* topic, const uint8_t * payload, unsigned int plength,
163  bool retained);
164  bool publish_P(const char* topic, const char* payload, bool retained);
165  bool publish_P(const char* topic, const uint8_t * payload, unsigned int plength,
166  bool retained);
167  // Start to publish a message.
168  // This API:
169  // beginPublish(...)
170  // one or more calls to write(...)
171  // endPublish()
172  // Allows for arbitrarily large payloads to be sent without them having to be copied into
173  // a new buffer and held in memory at one time
174  // Returns 1 if the message was started successfully, 0 if there was an error
175  bool beginPublish(const char* topic, unsigned int plength, bool retained);
176  // Finish off this publish message (started with beginPublish)
177  // Returns 1 if the packet was sent successfully, 0 if there was an error
178  int endPublish();
179  // Write a single byte of payload (only to be used with beginPublish/endPublish)
180  virtual size_t write(uint8_t);
181  // Write size bytes from buffer into the payload (only to be used with beginPublish/endPublish)
182  // Returns the number of bytes written
183  virtual size_t write(const uint8_t *buffer, size_t size);
184  bool subscribe(const char* topic);
185  bool subscribe(const char* topic, uint8_t qos);
186  bool unsubscribe(const char* topic);
187  bool loop();
188  bool connected();
189  int state();
190 
191 };
192 
193 
194 #endif
PubSubClient::beginPublish
bool beginPublish(const char *topic, unsigned int plength, bool retained)
beginPublish
PubSubClient::connect
bool connect(const char *id)
connect
PubSubClient::setServer
PubSubClient & setServer(IPAddress ip, uint16_t port)
setServer
PubSubClient::PubSubClient
PubSubClient()
PubSubClient.
PubSubClient::endPublish
int endPublish()
endPublish
PubSubClient::setBufferSize
bool setBufferSize(uint16_t size)
setBufferSize
PubSubClient::setClient
PubSubClient & setClient(Client &client)
setClient
PubSubClient::setStream
PubSubClient & setStream(Stream &stream)
setStream
PubSubClient::publish
bool publish(const char *topic, const char *payload)
publish
PubSubClient::disconnect
void disconnect()
disconnect
PubSubClient::subscribe
bool subscribe(const char *topic)
subscribe
PubSubClient::unsubscribe
bool unsubscribe(const char *topic)
unsubscribe
PubSubClient::getBufferSize
uint16_t getBufferSize()
getBufferSize
PubSubClient::setSocketTimeout
PubSubClient & setSocketTimeout(uint16_t timeout)
setSocketTimeout
PubSubClient::publish_P
bool publish_P(const char *topic, const char *payload, bool retained)
publish
IPAddress
A class to make it easier to handle and pass around IP addresses.
Definition: IPAddress.h:32
PubSubClient::loop
bool loop()
loop
PubSubClient::state
int state()
state
PubSubClient
Definition: PubSubClient.h:89
PubSubClient::setKeepAlive
PubSubClient & setKeepAlive(uint16_t keepAlive)
setKeepAlive
PubSubClient::connected
bool connected()
connected
PubSubClient::setCallback
PubSubClient & setCallback(MQTT_CALLBACK_SIGNATURE)
setCallback