ESP8266 Door Access Control System
Loading...
Searching...
No Matches
WiFiMqttClient.h
Go to the documentation of this file.
1/**
2 * @file WiFiMqttClient.h
3 * @brief Lightweight WiFi + MQTT helper wrapper for ESP-based Arduino systems.
4 *
5 * @defgroup infrastructure Infrastructure – WiFi & MQTT
6 * @{
7 *
8 * @details
9 * This header defines the WifiMqttClient class, which encapsulates:
10 * - WiFi connection management
11 * - MQTT connection and reconnection logic
12 * - Topic namespace handling (site / device scoping)
13 * - JSON-based MQTT publish helpers
14 *
15 * The class is designed to simplify MQTT usage in distributed embedded systems
16 * by providing a consistent base topic structure and robust connection handling.
17 */
18
19
20#pragma once
21
22#include <Arduino.h>
23#include <PubSubClient.h>
24#include <ArduinoJson.h>
25#include <WiFiClient.h>
26
27/**
28 * @class WifiMqttClient
29 * @brief Combined WiFi and MQTT client abstraction.
30 *
31 * This class wraps an Arduino WiFiClient and PubSubClient to provide:
32 * - Automatic WiFi and MQTT reconnection
33 * - Topic construction based on site and device identifiers
34 * - JSON publishing convenience functions
35 *
36 * Typical base topic structure:
37 * <mqttUser>/<site>/<deviceId>/<suffix>
38 */
40public:
41 /**
42 * @brief Default constructor.
43 *
44 * Initializes internal state but does not establish
45 * any network connections.
46 */
48
49 /**
50 * @brief Initializes WiFi and MQTT configuration.
51 *
52 * Stores credentials and connection parameters, initializes
53 * internal clients, and prepares base topic paths.
54 *
55 * @param wifiSsid WiFi network SSID.
56 * @param wifiPass WiFi network password.
57 * @param mqttHost MQTT broker hostname or IP.
58 * @param mqttPort MQTT broker port.
59 * @param mqttUser MQTT username.
60 * @param mqttPass MQTT password.
61 * @param deviceId Unique device identifier (e.g. "door1").
62 * @param site Site or location identifier (e.g. "site1").
63 */
64 void begin(
65 const char* wifiSsid,
66 const char* wifiPass,
67 const char* mqttHost,
68 uint16_t mqttPort,
69 const char* mqttUser,
70 const char* mqttPass,
71 const char* deviceId,
72 const char* site
73 );
74
75 /**
76 * @brief Main service loop.
77 *
78 * Must be called frequently from the Arduino loop().
79 * Handles:
80 * - WiFi reconnection
81 * - MQTT reconnection
82 * - MQTT client loop processing
83 */
84 void loop();
85
86 /**
87 * @brief Checks whether the MQTT client is currently connected.
88 *
89 * @return true if MQTT connection is active, false otherwise.
90 */
91 bool connected();
92
93 /**
94 * @brief Publishes a JSON document to an MQTT topic.
95 *
96 * Automatically prefixes the topic with the base topic
97 * (<user>/<site>/<deviceId>/).
98 *
99 * @param topicSuffix Topic suffix appended to the base topic.
100 * @param data JSON document to serialize and publish.
101 * @return true if publish succeeded, false otherwise.
102 */
103 bool publishJson(
104 const char* topicSuffix,
105 const JsonDocument& data
106 );
107
108 /**
109 * @brief Sets the MQTT message callback.
110 *
111 * The callback is invoked when subscribed messages are received.
112 *
113 * @param MQTT_CALLBACK_SIGNATURE Function pointer matching PubSubClient callback signature.
114 */
115 void setCallback(MQTT_CALLBACK_SIGNATURE);
116
117 /**
118 * @brief Subscribes to a topic.
119 *
120 * The provided topic should already be fully constructed
121 * (use makeTopic() if needed).
122 *
123 * @param topic Full MQTT topic string.
124 * @return true if subscription succeeded, false otherwise.
125 */
126 bool subscribe(const char* topic);
127
128 /**
129 * @brief Unsubscribes from a topic.
130 *
131 * @param topic Full MQTT topic string.
132 * @return true if unsubscribe succeeded, false otherwise.
133 */
134 bool unsubscribe(const char* topic);
135
136 /**
137 * @brief Constructs a fully qualified MQTT topic.
138 *
139 * Combines the base topic with a suffix:
140 * <user>/<site>/<deviceId>/<suffix>
141 *
142 * @param suffix Topic suffix (e.g. "access/request").
143 * @return Constructed topic as an Arduino String.
144 */
145 String makeTopic(const char* suffix) const;
146
147private:
148 /**
149 * @brief Establishes a WiFi connection if not already connected.
150 *
151 * Handles blocking connection attempts and retries.
152 */
153 void connectWifi();
154
155 /**
156 * @brief Establishes an MQTT connection if not already connected.
157 *
158 * Uses stored credentials and base topic information.
159 */
160 void connectMqtt();
161
162 // ---------------------------------------------------------------------------
163 // Internal clients
164 // ---------------------------------------------------------------------------
165
166 /** @brief Underlying WiFi client used by the MQTT client. */
167 WiFiClient wifiClient;
168
169 /** @brief PubSubClient instance handling MQTT protocol. */
170 PubSubClient mqtt;
171
172 // ---------------------------------------------------------------------------
173 // Stored configuration parameters
174 // ---------------------------------------------------------------------------
175
176 /** @brief WiFi SSID. */
177 const char* wifiSsid;
178
179 /** @brief WiFi password. */
180 const char* wifiPass;
181
182 /** @brief MQTT broker hostname or IP address. */
183 const char* mqttHost;
184
185 /** @brief MQTT username. */
186 const char* mqttUser;
187
188 /** @brief MQTT password. */
189 const char* mqttPass;
190
191 /** @brief Device identifier used in topic hierarchy. */
192 const char* deviceId;
193
194 /** @brief Site identifier used in topic hierarchy. */
195 const char* site;
196
197 /** @brief MQTT broker port number. */
198 uint16_t mqttPort;
199
200 // ---------------------------------------------------------------------------
201 // Derived and runtime state
202 // ---------------------------------------------------------------------------
203
204 /**
205 * @brief Base MQTT topic prefix.
206 *
207 * Format:
208 * <mqttUser>/<site>/<deviceId>
209 */
210 String baseTopic;
211
212 /**
213 * @brief Unique chip identifier.
214 *
215 * Used to generate unique MQTT client IDs.
216 */
217 uint64_t chipId;
218};
void loop()
Main service loop.
bool subscribe(const char *topic)
Subscribes to a topic.
bool connected()
Checks whether the MQTT client is currently connected.
WifiMqttClient()
Default constructor.
String makeTopic(const char *suffix) const
Constructs a fully qualified MQTT topic.
bool unsubscribe(const char *topic)
Unsubscribes from a topic.
void setCallback(MQTT_CALLBACK_SIGNATURE)
Sets the MQTT message callback.
bool publishJson(const char *topicSuffix, const JsonDocument &data)
Publishes a JSON document to an MQTT topic.
void 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.