|
ESP8266 Door Access Control System
|
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. | |
This header defines the WifiMqttClient class, which encapsulates:
The class is designed to simplify MQTT usage in distributed embedded systems by providing a consistent base topic structure and robust connection handling.
| 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.
| wifiSsid | WiFi network SSID. |
| wifiPass | WiFi network password. |
| mqttHost | MQTT broker hostname or IP. |
| mqttPort | MQTT broker port. |
| mqttUser | MQTT username. |
| mqttPass | MQTT password. |
| deviceId | Unique device identifier (e.g. "door1"). |
| site | Site or location identifier (e.g. "site1"). |
Stores provided credentials, prepares base topic structure, configures WiFi and MQTT clients, and performs initial connections.
| wifiSsid | WiFi network SSID. |
| wifiPass | WiFi network password. |
| mqttHost | MQTT broker hostname. |
| mqttPort | MQTT broker port. |
| mqttUser | MQTT username. |
| mqttPass | MQTT password. |
| deviceId | Device identifier used in topic hierarchy. |
| site | Site identifier used in topic hierarchy. |
Definition at line 43 of file WiFiMqttClient.cpp.
| bool WifiMqttClient::connected | ( | ) |
Checks whether the MQTT client is currently connected.
Checks whether the MQTT client is connected.
Definition at line 108 of file WiFiMqttClient.cpp.
| void WifiMqttClient::loop | ( | ) |
Main service loop.
Must be called frequently from the Arduino loop(). Handles:
Ensures WiFi and MQTT connections remain active and processes incoming MQTT messages.
Definition at line 87 of file WiFiMqttClient.cpp.
| String WifiMqttClient::makeTopic | ( | const char * | suffix | ) | const |
Constructs a fully qualified MQTT topic.
Combines the base topic with a suffix: <user>/<site>/<deviceId>/<suffix>
| suffix | Topic suffix (e.g. "access/request"). |
Appends a suffix to the base topic: <user>/<site>/<deviceId>/<suffix>
| suffix | Topic suffix. |
Definition at line 197 of file WiFiMqttClient.cpp.
| 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>/).
| topicSuffix | Topic suffix appended to the base topic. |
| data | JSON document to serialize and publish. |
Wraps the provided JSON data in a standard envelope containing device metadata and a timestamp.
| topicSuffix | Topic suffix appended to the base topic. |
| data | JSON document containing application payload. |
Definition at line 211 of file WiFiMqttClient.cpp.
| void WifiMqttClient::setCallback | ( | MQTT_CALLBACK_SIGNATURE | ) |
Sets the MQTT message callback.
The callback is invoked when subscribed messages are received.
| MQTT_CALLBACK_SIGNATURE | Function pointer matching PubSubClient callback signature. |
| MQTT_CALLBACK_SIGNATURE | Callback function pointer. |
Definition at line 244 of file WiFiMqttClient.cpp.
| 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).
| topic | Full MQTT topic string. |
| topic | Full MQTT topic string. |
Definition at line 254 of file WiFiMqttClient.cpp.
| bool WifiMqttClient::unsubscribe | ( | const char * | topic | ) |
Unsubscribes from a topic.
Unsubscribes from an MQTT topic.
| topic | Full MQTT topic string. |
Definition at line 265 of file WiFiMqttClient.cpp.
| 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.