ThingsBoard Client SDK 0.16.0
Client SDK to connect with ThingsBoard IoT Platform from IoT devices (Arduino, Espressif, etc.)
Loading...
Searching...
No Matches
Arduino_MQTT_Client.h
Go to the documentation of this file.
1#ifndef Arduino_MQTT_Client_h
2#define Arduino_MQTT_Client_h
3
4#ifdef ARDUINO
5
6// Local includes.
7#include "IMQTT_Client.h"
8
9// Library include
10#include <PubSubClient.h>
11
12
21class Arduino_MQTT_Client : public IMQTT_Client {
22 public:
24 Arduino_MQTT_Client() = default;
25
29 Arduino_MQTT_Client(Client & transport_client);
30
31 ~Arduino_MQTT_Client() override = default;
32
36 void set_client(Client & transport_client);
37
39
40 void set_connect_callback(Callback<void>::function callback) override;
41
42 bool set_buffer_size(uint16_t receive_buffer_size, uint16_t send_buffer_size) override;
43
44 uint16_t get_receive_buffer_size() override;
45
46 uint16_t get_send_buffer_size() override;
47
48 void set_server(char const * domain, uint16_t port) override;
49
50 bool connect(char const * client_id, char const * user_name, char const * password) override;
51
52 void disconnect() override;
53
54 bool loop() override;
55
56 bool publish(char const * topic, uint8_t const * payload, size_t const & length) override;
57
58 bool subscribe(char const * topic) override;
59
60 bool unsubscribe(char const * topic) override;
61
62 bool connected() override;
63
65
67
69
70#if THINGSBOARD_ENABLE_STREAM_UTILS
71
72 bool begin_publish(char const * topic, size_t const & length) override;
73
74 bool end_publish() override;
75
76 //----------------------------------------------------------------------------
77 // Print interface
78 //----------------------------------------------------------------------------
79
80 size_t write(uint8_t payload_byte) override;
81
82 size_t write(uint8_t const * buffer, size_t const & size) override;
83
84#endif // THINGSBOARD_ENABLE_STREAM_UTILS
85
86 private:
87 MQTT_Connection_Error connect_mqtt_client(char const * client_id, char const * user_name, char const * password);
88
91 void update_connection_state(MQTT_Connection_State new_state);
92
93 MQTT_Connection_State m_connection_state = {}; // Current connection state to the MQTT broker
94 MQTT_Connection_Error m_last_connection_error = {}; // Last error that occured while trying to establish a connection to the MQTT broker
95 Callback<void, MQTT_Connection_State, MQTT_Connection_Error> m_connection_state_changed_callback = {}; // Callback that will be called as soon as the mqtt client connection changes
96 Callback<void> m_connected_callback = {}; // Callback that will be called as soon as the mqtt client has connected
97 PubSubClient m_mqtt_client = {}; // Underlying MQTT client instance used to send data
98};
99
100#endif // ARDUINO
101
102#endif // Arduino_MQTT_Client_h
MQTT_Connection_Error
Possible error states the MQTT broker connection can be in, if connecting the device to the MQTT brok...
Definition: MQTT_Connection_Error.h:9
MQTT_Connection_State
Possible states the MQTT broker connection can be in.
Definition: MQTT_Connection_State.h:11
General purpose safe callback wrapper. Expects either c-style or c++ style function pointer,...
Definition: Callback.h:30
std::function< return_type(argument_types... arguments)> function
Callback signature.
Definition: Callback.h:34
MQTT Client interface that contains the method that a class that can be used to send and receive data...
Definition: IMQTT_Client.h:36
virtual void disconnect()=0
Force disconnects from the previously connected server and should release all used resources.
virtual bool loop()=0
Receives / sends any outstanding messages from and to the MQTT broker.
virtual bool connect(char const *client_id, char const *user_name, char const *password)=0
Connects to the previously with set_server configured server instance and port with the given credent...
virtual void subscribe_connection_state_changed_callback(Callback< void, MQTT_Connection_State, MQTT_Connection_Error >::function callback)=0
Sets the callback that is called, whenever the underlying state of our connection with the MQTT broke...
virtual MQTT_Connection_Error get_last_connection_error() const =0
Allows to deciper the reason for a failure, while attempting to establish a connection to the MQTT br...
virtual uint16_t get_receive_buffer_size()=0
Gets the previously set size of the internal buffer size meant for incoming MQTT data.
virtual void set_data_callback(Callback< void, char *, uint8_t *, unsigned int >::function callback)=0
Sets the callback that is called, if any message is received by the MQTT broker.
virtual bool unsubscribe(char const *topic)=0
Unsubscribes to previously subscribed MQTT messages of the given topic.
virtual bool set_buffer_size(uint16_t receive_buffer_size, uint16_t send_buffer_size)=0
Changes the size of the buffer for sent and received MQTT messages.
virtual MQTT_Connection_State get_connection_state() const =0
Get the current connection state to the server includes possible intermediate states between connecti...
virtual void set_server(char const *domain, uint16_t port)=0
Configures the server and port that the client should connect with over MQTT.
virtual bool publish(char const *topic, uint8_t const *payload, size_t const &length)=0
Sends the given payload over the previously established connection.
virtual uint16_t get_send_buffer_size()=0
Gets the previously set size of the internal buffer size meant for outgoing MQTT data.
virtual bool connected()=0
Returns our current connection status to MQTT, true meaning we are connected, false meaning we have b...
virtual bool subscribe(char const *topic)=0
Subscribes to MQTT message on the given topic, which will cause an internal callback to be called for...
virtual void set_connect_callback(Callback< void >::function callback)=0
Sets the callback that is called, if we have successfully established a connection with the MQTT brok...