My Project
 All Classes Functions Variables Pages
PubSubClient.h
1 /*
2  PubSubClient.h - A simple client for MQTT.
3  Nick O'Leary
4  http://knolleary.net
5 
6  edit by marcel.seerig
7 */
8 
9 #ifndef PubSubClient_h
10 #define PubSubClient_h
11 
12 #include <string>
13 #include "Socket.h"
14 #include "FreeRTOSTimer.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
26 #ifndef MQTT_MAX_PACKET_SIZE
27 #define MQTT_MAX_PACKET_SIZE 128
28 #endif
29 
30 // MQTT_KEEPALIVE : keepAlive interval in Seconds
31 #ifndef MQTT_KEEPALIVE
32 #define MQTT_KEEPALIVE 15
33 #endif
34 
35 // MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds
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 
48  std::string ip;
49  uint16_t port;
50 
51  const char* user;
52  const char* pass;
53 
54  const char* id;
55  const char* willTopic;
56  uint8_t willQos;
57  bool willRetain;
58  const char* willMessage;
59 };
60 
61 typedef enum {
62  CONNECTION_TIMEOUT = -4,
63  CONNECTION_LOST = -3,
64  CONNECT_FAILED = -2,
65  DISCONNECTED = -1,
66  CONNECTED = 0,
67  CONNECT_BAD_PROTOCOL = 1,
68  CONNECT_BAD_CLIENT_ID = 2,
69  CONNECT_UNAVAILABLE = 3,
70  CONNECT_BAD_CREDENTIALS = 4,
71  CONNECT_UNAUTHORIZED = 5,
72 } mqtt_state;
73 
74 typedef enum {
75  CONNECT = 1 << 4, // Client request to connect to Server
76  CONNACK = 2 << 4, // Connect Acknowledgment
77  PUBLISH = 3 << 4, // Publish message
78  PUBACK = 4 << 4, // Publish Acknowledgment
79  PUBREC = 5 << 4, // Publish Received (assured delivery part 1)
80  PUBREL = 6 << 4, // Publish Release (assured delivery part 2)
81  PUBCOMP = 7 << 4, // Publish Complete (assured delivery part 3)
82  SUBSCRIBE = 8 << 4, // Client Subscribe request
83  SUBACK = 9 << 4, // Subscribe Acknowledgment
84  UNSUBSCRIBE = 10 << 4, // Client Unsubscribe request
85  UNSUBACK = 11 << 4, // Unsubscribe Acknowledgment
86  PINGREQ = 12 << 4, // PING Request
87  PINGRESP = 13 << 4, // PING Response
88  DISCONNECT = 14 << 4, // Client is Disconnecting
89  Reserved = 15 << 4, // Reserved
90 } mqtt_message_type;
91 
92 typedef enum {
93  QOS0 = (0 << 1),
94  QOS1 = (1 << 1),
95  QOS2 = (2 << 1),
96 } mqtt_qos;
97 
98 struct mqtt_message {
99  uint8_t type;
100  uint8_t qos;
101  bool retained;
102  bool dup;
103  std::string topic;
104  std::string payload;
105  uint16_t msgId;
106 };
107 
108 #define MQTT_CALLBACK_SIGNATURE void (*callback) (std::string, std::string)
109 
110 class PubSubClientTask;
111 
113 public:
114  PubSubClient();
115  PubSubClient(Socket& client);
116  PubSubClient(std::string ip, uint16_t port);
117  PubSubClient(std::string ip, uint16_t port, Socket& client);
118  PubSubClient(std::string ip, uint16_t port, MQTT_CALLBACK_SIGNATURE,Socket& client);
119  ~PubSubClient();
120 
121  PubSubClient& setServer(std::string ip, uint16_t port);
122  PubSubClient& setCallback(MQTT_CALLBACK_SIGNATURE);
123  PubSubClient& setClient(Socket& client);
124 
125  bool connect(const char* id);
126  bool connect(const char* id, const char* user, const char* pass);
127  bool connect(const char* id, const char* willTopic, uint8_t willQos, bool willRetain, const char* willMessage);
128  bool connect(const char* id, const char* user, const char* pass, const char* willTopic, uint8_t willQos, bool willRetain, const char* willMessage);
129  bool connect();
130  void disconnect();
131  bool publish(const char* topic, const char* payload);
132  bool publish(const char* topic, const char* payload, bool retained);
133  bool publish(const char* topic, const uint8_t* payload, unsigned int plength);
134  bool publish(const char* topic, const uint8_t* payload, unsigned int plength, bool retained);
135  //bool publish_P(const char* topic, const uint8_t * payload, unsigned int plength, bool retained);
136 
137  bool subscribe(const char* topic, bool ack = false);
138  bool unsubscribe(const char* topic, bool ack = false);
139  bool isSubscribeDone();
140  bool isUnsubscribeDone();
141 
142  bool connected();
143  int state();
144  void keepAliveChecker();
145  void timeoutChecker();
146 
147 private:
148  friend class PubSubClientTask;
149  PubSubClientTask* m_task;
150  Socket* _client;
151  mqtt_InitTypeDef _config;
152  mqtt_state _state;
153  uint8_t buffer[MQTT_MAX_PACKET_SIZE];
154  uint16_t nextMsgId;
155  bool PING_outstanding;
156  bool SUBACK_outstanding;
157  bool UNSUBACK_Outstanding;
158  FreeRTOSTimer* keepAliveTimer;
159  FreeRTOSTimer* timeoutTimer;
160 
161  MQTT_CALLBACK_SIGNATURE;
162  void setup();
163  size_t readPacket();
164  bool write(uint8_t header, uint8_t* buf, uint16_t length);
165  uint16_t writeString(const char* string, uint8_t* buf, uint16_t pos);
166  void parseData(mqtt_message* msg, uint16_t len);
167  void dumpData(mqtt_message* msg);
168  std::string messageType_toString(uint8_t type);
169 
170 };
171 
172 #endif
Definition: PubSubClient.h:112
Encapsulate a socket.
Definition: Socket.h:62
bool connect()
Connect to a MQTT server with the with the previous settings. Note: do not call this function without...
Definition: PubSubClient.cpp:311
void timeoutChecker()
This is a Timer called routine, which is called, when we reach the timeout. Used is this function for...
Definition: PubSubClient.cpp:233
PubSubClient & setServer(std::string ip, uint16_t port)
Set server ip and Port of my MQTT server.
Definition: PubSubClient.cpp:719
Definition: PubSubClient.h:98
bool publish(const char *topic, const char *payload)
Publish a MQTT message.
Definition: PubSubClient.cpp:419
bool connected()
Check the connection to the MQTT server.
Definition: PubSubClient.cpp:694
bool unsubscribe(const char *topic, bool ack=false)
Unsubscribe a MQTT topic.
Definition: PubSubClient.cpp:625
bool subscribe(const char *topic, bool ack=false)
Subscribe a MQTT topic.
Definition: PubSubClient.cpp:584
Definition: PubSubClient.h:47
int state()
Get the current MYTT state form the instance.
Definition: PubSubClient.cpp:753
PubSubClient & setCallback(MQTT_CALLBACK_SIGNATURE)
Set the callback function for incoming data.
Definition: PubSubClient.cpp:731
void disconnect()
Disconnect form MQTT server and close the socket.
Definition: PubSubClient.cpp:662
PubSubClient & setClient(Socket &client)
Set the socket, which we want to use for our MQTT communication.
Definition: PubSubClient.cpp:742
bool isSubscribeDone()
Check the state of subscription. If there was received a subscription ACK, we return a true here...
Definition: PubSubClient.cpp:614
bool isUnsubscribeDone()
Check the state of unsubscription. If there was received a unsubscription ACK, we return a true here...
Definition: PubSubClient.cpp:653
A task that will handle the PubSubClient.
Definition: PubSubClient.cpp:27
Wrapper around the FreeRTOS timer functions.
Definition: FreeRTOSTimer.h:16
void keepAliveChecker()
This is a Timer called routine, which checks the PING_outstanding flag. This flag is set in this func...
Definition: PubSubClient.cpp:210