ESP8266 Door Access Control System
Loading...
Searching...
No Matches
Infrastructure – WiFi & MQTT

Files

file  WiFiMqttClient.cpp
 Implementation of the WifiMqttClient helper class.

Classes

class  WifiMqttClient
 Combined WiFi and MQTT client abstraction. More...

Functions

 WifiMqttClient::WifiMqttClient ()
 Default constructor.
void WifiMqttClient::begin (const char *wifiSsid, const char *wifiPass, const char *mqttHost, uint16_t mqttPort, const char *mqttUser, const char *mqttPass, const char *deviceId, const char *site)
 Initializes WiFi and MQTT configuration.
void WifiMqttClient::loop ()
 Main service loop.
bool WifiMqttClient::connected ()
 Checks whether the MQTT client is currently connected.
bool WifiMqttClient::publishJson (const char *topicSuffix, const JsonDocument &data)
 Publishes a JSON document to an MQTT topic.
void WifiMqttClient::setCallback (MQTT_CALLBACK_SIGNATURE)
 Sets the MQTT message callback.
bool WifiMqttClient::subscribe (const char *topic)
 Subscribes to a topic.
bool WifiMqttClient::unsubscribe (const char *topic)
 Unsubscribes from a topic.
String WifiMqttClient::makeTopic (const char *suffix) const
 Constructs a fully qualified MQTT topic.

Detailed Description

This header defines the WifiMqttClient class, which encapsulates:

  • WiFi connection management
  • MQTT connection and reconnection logic
  • Topic namespace handling (site / device scoping)
  • JSON-based MQTT publish helpers

The class is designed to simplify MQTT usage in distributed embedded systems by providing a consistent base topic structure and robust connection handling.

Function Documentation

◆ begin()

void WifiMqttClient::begin ( const char * wifiSsid,
const char * wifiPass,
const char * mqttHost,
uint16_t mqttPort,
const char * mqttUser,
const char * mqttPass,
const char * deviceId,
const char * site )

Initializes WiFi and MQTT configuration.

Stores credentials and connection parameters, initializes internal clients, and prepares base topic paths.

Parameters
wifiSsidWiFi network SSID.
wifiPassWiFi network password.
mqttHostMQTT broker hostname or IP.
mqttPortMQTT broker port.
mqttUserMQTT username.
mqttPassMQTT password.
deviceIdUnique device identifier (e.g. "door1").
siteSite or location identifier (e.g. "site1").

Stores provided credentials, prepares base topic structure, configures WiFi and MQTT clients, and performs initial connections.

Parameters
wifiSsidWiFi network SSID.
wifiPassWiFi network password.
mqttHostMQTT broker hostname.
mqttPortMQTT broker port.
mqttUserMQTT username.
mqttPassMQTT password.
deviceIdDevice identifier used in topic hierarchy.
siteSite identifier used in topic hierarchy.

Definition at line 43 of file WiFiMqttClient.cpp.

◆ connected()

bool WifiMqttClient::connected ( )

Checks whether the MQTT client is currently connected.

Checks whether the MQTT client is connected.

Returns
true if MQTT connection is active, false otherwise.
true if connected, false otherwise.

Definition at line 108 of file WiFiMqttClient.cpp.

◆ loop()

void WifiMqttClient::loop ( )

Main service loop.

Must be called frequently from the Arduino loop(). Handles:

  • WiFi reconnection
  • MQTT reconnection
  • MQTT client loop processing

Ensures WiFi and MQTT connections remain active and processes incoming MQTT messages.

Definition at line 87 of file WiFiMqttClient.cpp.

◆ makeTopic()

String WifiMqttClient::makeTopic ( const char * suffix) const

Constructs a fully qualified MQTT topic.

Combines the base topic with a suffix: <user>/<site>/<deviceId>/<suffix>

Parameters
suffixTopic suffix (e.g. "access/request").
Returns
Constructed topic as an Arduino String.

Appends a suffix to the base topic: <user>/<site>/<deviceId>/<suffix>

Parameters
suffixTopic suffix.
Returns
Constructed topic as an Arduino String.

Definition at line 197 of file WiFiMqttClient.cpp.

Here is the caller graph for this function:

◆ publishJson()

bool WifiMqttClient::publishJson ( const char * topicSuffix,
const JsonDocument & data )

Publishes a JSON document to an MQTT topic.

Automatically prefixes the topic with the base topic (<user>/<site>/<deviceId>/).

Parameters
topicSuffixTopic suffix appended to the base topic.
dataJSON document to serialize and publish.
Returns
true if publish succeeded, false otherwise.

Wraps the provided JSON data in a standard envelope containing device metadata and a timestamp.

Parameters
topicSuffixTopic suffix appended to the base topic.
dataJSON document containing application payload.
Returns
true if publish succeeded, false otherwise.

Definition at line 211 of file WiFiMqttClient.cpp.

Here is the call graph for this function:

◆ setCallback()

void WifiMqttClient::setCallback ( MQTT_CALLBACK_SIGNATURE )

Sets the MQTT message callback.

The callback is invoked when subscribed messages are received.

Parameters
MQTT_CALLBACK_SIGNATUREFunction pointer matching PubSubClient callback signature.
MQTT_CALLBACK_SIGNATURECallback function pointer.

Definition at line 244 of file WiFiMqttClient.cpp.

Here is the call graph for this function:

◆ subscribe()

bool WifiMqttClient::subscribe ( const char * topic)

Subscribes to a topic.

Subscribes to an MQTT topic.

The provided topic should already be fully constructed (use makeTopic() if needed).

Parameters
topicFull MQTT topic string.
Returns
true if subscription succeeded, false otherwise.
Parameters
topicFull MQTT topic string.
Returns
true if subscription succeeded, false otherwise.

Definition at line 254 of file WiFiMqttClient.cpp.

◆ unsubscribe()

bool WifiMqttClient::unsubscribe ( const char * topic)

Unsubscribes from a topic.

Unsubscribes from an MQTT topic.

Parameters
topicFull MQTT topic string.
Returns
true if unsubscribe succeeded, false otherwise.

Definition at line 265 of file WiFiMqttClient.cpp.

◆ WifiMqttClient()

WifiMqttClient::WifiMqttClient ( )

Default constructor.

Initializes internal state but does not establish any network connections.

Initializes the PubSubClient instance with the internal WiFiClient.

Definition at line 25 of file WiFiMqttClient.cpp.