PubSubClient
MQTT client library for Arduino
PubSubClient.h
1 /*
2  PubSubClient.h - A simple client for MQTT.
3  Nicholas O'Leary
4  http://knolleary.net
5 */
6 
7 #ifndef PubSubClient_h
8 #define PubSubClient_h
9 
10 #ifdef ESP8266
11 #include <functional>
12 #include <pgmspace.h>
13 #endif
14 
15 #include <Arduino.h>
16 
17 #include "MQTT.h"
18 
20 class PubSubClient {
21 public:
22 #ifdef _GLIBCXX_FUNCTIONAL
23  typedef std::function<void(const MQTT::Publish&)> callback_t;
24 #else
25  typedef void(*callback_t)(const MQTT::Publish&);
26 #endif
27 
28 private:
29  IPAddress server_ip;
30  String server_hostname;
31  uint16_t server_port;
32  callback_t _callback;
33 
34  Client *_client;
35  uint16_t nextMsgId, keepalive;
36  uint8_t _max_retries;
37  unsigned long lastOutActivity;
38  unsigned long lastInActivity;
39  bool pingOutstanding;
40 
42 
45  MQTT::Message* _recv_message(void);
46 
48 
52  bool _send_message(MQTT::Message& msg, bool need_reply = false);
53 
55 
61  void _process_message(MQTT::Message* msg);
62 
64 
70  bool _wait_for(MQTT::message_type wait_type, uint16_t wait_pid = 0);
71 
73  uint16_t _next_packet_id(void) {
74  nextMsgId++;
75  if (nextMsgId == 0) nextMsgId = 1;
76  return nextMsgId;
77  }
78 
79 public:
81 
84  PubSubClient(Client& c);
85 
87  PubSubClient(Client& c, IPAddress &ip, uint16_t port = 1883);
89  PubSubClient(Client& c, String hostname, uint16_t port = 1883);
90 
92  PubSubClient& set_server(IPAddress &ip, uint16_t port = 1883);
94  PubSubClient& set_server(String hostname, uint16_t port = 1883);
95 
97  callback_t callback(void) const { return _callback; }
99  PubSubClient& set_callback(callback_t cb) { _callback = cb; return *this; }
101  PubSubClient& unset_callback(void) { _callback = NULL; return * this; }
102 
104  PubSubClient& set_max_retries(uint8_t mr) { _max_retries = mr; return *this; }
105 
107 
110  bool connect(String id);
111 
113 
122  bool connect(String id, String willTopic, uint8_t willQos, bool willRetain, String willMessage);
123 
125  void disconnect(void);
126 
128 
132  bool publish(String topic, String payload);
133 
135 
143  bool publish(String topic, const uint8_t *payload, uint32_t plength, bool retained = false);
144 
146 
151  bool publish(String topic, MQTT::payload_callback_t pcb, uint32_t length, bool retained = false);
152 
154 
162  bool publish_P(String topic, PGM_P payload, uint32_t plength, bool retained = false);
163 
165 
169  bool subscribe(String topic, uint8_t qos = 0);
170 
172  bool unsubscribe(String topic);
173 
175 
178  bool loop();
179 
181  bool connected();
182 
184  bool connect(MQTT::Connect &conn);
186  bool publish(MQTT::Publish &pub);
188  bool subscribe(MQTT::Subscribe &sub);
190  bool unsubscribe(MQTT::Unsubscribe &unsub);
191 };
192 
193 
194 #endif
Main do-everything class that sketches will use.
Definition: PubSubClient.h:20
PubSubClient & set_callback(callback_t cb)
Set the callback function.
Definition: PubSubClient.h:99
bool publish_P(String topic, PGM_P payload, uint32_t plength, bool retained=false)
Publish an arbitrary data payload stored in "program" memory.
Definition: PubSubClient.cpp:250
bool subscribe(String topic, uint8_t qos=0)
Subscribe to a topic.
Definition: PubSubClient.cpp:282
bool publish(String topic, String payload)
Publish a string payload.
Definition: PubSubClient.cpp:222
Subscribe to one or more topics.
Definition: MQTT.h:398
PubSubClient & set_server(IPAddress &ip, uint16_t port=1883)
Set the server ip address.
Definition: PubSubClient.cpp:32
PubSubClient & unset_callback(void)
Unset the callback function.
Definition: PubSubClient.h:101
callback_t callback(void) const
Get the callback function.
Definition: PubSubClient.h:97
Message sent when connecting to a broker.
Definition: MQTT.h:157
Unsubscribe from one or more topics.
Definition: MQTT.h:455
bool connected()
Are we connected?
Definition: PubSubClient.cpp:325
bool connect(String id)
Connect to the server with a client id.
Definition: PubSubClient.cpp:153
Publish a payload to a topic.
Definition: MQTT.h:221
bool loop()
Wait for packets to come in, processing them.
Definition: PubSubClient.cpp:193
PubSubClient(Client &c)
Simple constructor.
Definition: PubSubClient.cpp:10
void disconnect(void)
Disconnect from the server.
Definition: PubSubClient.cpp:315
bool unsubscribe(String topic)
Unsubscribe from a topic.
Definition: PubSubClient.cpp:300
PubSubClient & set_max_retries(uint8_t mr)
Set the maximum number of retries when waiting for response packets.
Definition: PubSubClient.h:104
Abstract base class.
Definition: MQTT.h:63