UWB-Tracking
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
MqttClient Class Reference

A class for managing MQTT communication. More...

#include <mqtt-client.h>

Public Member Functions

 MqttClient (const char *topic, const char *mqtt_server, uint16_t mqtt_port, const char *wifi_ssid, const char *wifi_pwd, String dev_id, uint16_t buffer_size)
 Default constructor for the MQTT client. More...
 
void update ()
 Must be called frequently to allow the callback function to handle incoming messages. Handles reconnection and MQTT event processing. More...
 
void publish (char *topic, char *msg, unsigned int plength)
 Publishes a message on the specified MQTT topic. More...
 
bool is_connected ()
 Checks if the MQTT client is connected. More...
 
String get_mac ()
 Get the MAC address of the WiFi client. More...
 

Private Member Functions

void reconnect ()
 Reconnects the MQTT client to the broker. It retries every 5 seconds until a connection is established. More...
 
void setup_wifi (const char wifi_ssid[], const char wifi_pwd[])
 Connects the ESP to the WiFi network using provided credentials. WiFi settings can be found in the main.cpp file. More...
 

Private Attributes

WiFiClient espClient
 Used to establish the WiFi connection. More...
 
PubSubClient client
 Used to send MQTT commands. More...
 
String dev_id
 
const char * topic
 

Detailed Description

A class for managing MQTT communication.

Constructor & Destructor Documentation

◆ MqttClient()

MqttClient ( const char *  topic,
const char *  mqtt_server,
uint16_t  mqtt_port,
const char *  wifi_ssid,
const char *  wifi_pwd,
String  dev_id,
uint16_t  buffer_size 
)

Default constructor for the MQTT client.

Parameters
topicThe MQTT topic for communication.
mqtt_serverThe MQTT server hostname or IP address.
mqtt_portThe MQTT server port number.
wifi_ssidThe WiFi SSID for network connection.
wifi_pwdThe WiFi password for network connection.
dev_idThe PCB's MQTT Devicename.
buffer_sizeThe outputbuffer used for incoming and outgoing messages.

Member Function Documentation

◆ get_mac()

String get_mac ( )

Get the MAC address of the WiFi client.

Returns
The MAC address as a String.

◆ is_connected()

bool is_connected ( )

Checks if the MQTT client is connected.

Returns
true if connected, false otherwise.

◆ publish()

void publish ( char *  topic,
char *  msg,
unsigned int  plength 
)

Publishes a message on the specified MQTT topic.

Parameters
topicThe MQTT topic to publish to.
msgThe message to publish.
plengthLength of the message to publish.

◆ reconnect()

void reconnect ( )
private

Reconnects the MQTT client to the broker. It retries every 5 seconds until a connection is established.

Warning
Contains a while loop to wait for a connection, which may cause delays.

◆ setup_wifi()

void setup_wifi ( const char  wifi_ssid[],
const char  wifi_pwd[] 
)
private

Connects the ESP to the WiFi network using provided credentials. WiFi settings can be found in the main.cpp file.

Parameters
wifi_ssidThe WiFi SSID.
wifi_pwdThe WiFi password.

◆ update()

void update ( )

Must be called frequently to allow the callback function to handle incoming messages. Handles reconnection and MQTT event processing.

Member Data Documentation

◆ client

PubSubClient client
private

Used to send MQTT commands.

◆ dev_id

String dev_id
private

◆ espClient

WiFiClient espClient
private

Used to establish the WiFi connection.

◆ topic

const char* topic
private

The documentation for this class was generated from the following files: