RadioLib
Universal wireless communication library for Arduino
MQTT.h
1 #if !defined(_RADIOLIB_MQTT_H)
2 #define _RADIOLIB_MQTT_H
3 
4 #include "../../TypeDef.h"
5 
6 #if !defined(RADIOLIB_EXCLUDE_MQTT)
7 
8 #include "../TransportLayer/TransportLayer.h"
9 
10 // MQTT packet types
11 #define MQTT_CONNECT 0x01
12 #define MQTT_CONNACK 0x02
13 #define MQTT_PUBLISH 0x03
14 #define MQTT_PUBACK 0x04
15 #define MQTT_PUBREC 0x05
16 #define MQTT_PUBREL 0x06
17 #define MQTT_PUBCOMP 0x07
18 #define MQTT_SUBSCRIBE 0x08
19 #define MQTT_SUBACK 0x09
20 #define MQTT_UNSUBSCRIBE 0x0A
21 #define MQTT_UNSUBACK 0x0B
22 #define MQTT_PINGREQ 0x0C
23 #define MQTT_PINGRESP 0x0D
24 #define MQTT_DISCONNECT 0x0E
25 
26 // MQTT CONNECT flags
27 #define MQTT_CONNECT_USER_NAME_FLAG 0b10000000
28 #define MQTT_CONNECT_PASSWORD_FLAG 0b01000000
29 #define MQTT_CONNECT_WILL_RETAIN 0b00100000
30 #define MQTT_CONNECT_WILL_FLAG 0b00000100
31 #define MQTT_CONNECT_CLEAN_SESSION 0b00000010
32 
38 class MQTTClient {
39  public:
45  explicit MQTTClient(TransportLayer* tl, uint16_t port = 1883);
46 
47  // basic methods
48 
70  int16_t connect(const char* host, const char* clientId, const char* userName = "", const char* password = "", uint16_t keepAlive = 60, bool cleanSession = true, const char* willTopic = "", const char* willMessage = "");
71 
77  int16_t disconnect();
78 
88  int16_t publish(String& topic, String& message);
89 
99  int16_t publish(const char* topic, const char* message);
100 
108  int16_t subscribe(const char* topicFilter);
109 
117  int16_t unsubscribe(const char* topicFilter);
118 
124  int16_t ping();
125 
131  int16_t check(void (*func)(const char*, const char*));
132 
133 #ifndef RADIOLIB_GODMODE
134  private:
135 #endif
136  TransportLayer* _tl;
137 
138  uint16_t _port;
139  uint16_t _packetId;
140 
141  static size_t encodeLength(uint32_t len, uint8_t* encoded);
142  static uint32_t decodeLength(uint8_t* encoded, uint8_t& numBytes);
143 };
144 
145 #endif
146 
147 #endif
int16_t subscribe(const char *topicFilter)
Subscribe to MQTT topic.
Definition: MQTT.cpp:211
Provides common interface for protocols that run on modules with Internet connectivity, such as HTTP or MQTT. Because this class is used mainly as interface, all of its virtual members must be implemented in the module class.
Definition: TransportLayer.h:12
int16_t ping()
Ping MQTT broker. This method can be used to keep connection open.
Definition: MQTT.cpp:356
int16_t disconnect()
Disconnect from MQTT broker.
Definition: MQTT.cpp:146
MQTTClient(TransportLayer *tl, uint16_t port=1883)
Default constructor.
Definition: MQTT.cpp:4
int16_t connect(const char *host, const char *clientId, const char *userName="", const char *password="", uint16_t keepAlive=60, bool cleanSession=true, const char *willTopic="", const char *willMessage="")
Connects to MQTT broker (/server).
Definition: MQTT.cpp:10
int16_t publish(String &topic, String &message)
Publish MQTT message.
Definition: MQTT.cpp:164
int16_t check(void(*func)(const char *, const char *))
Set function to be called when checking new messages in subscribed topics.
Definition: MQTT.cpp:396
int16_t unsubscribe(const char *topicFilter)
Unsubscribe from MQTT topic.
Definition: MQTT.cpp:285
Client for simple MQTT communication.
Definition: MQTT.h:38