Overview
The MQTT Manager module provides a simple interface for publishing and subscribing to MQTT topics. It handles connection management, automatic reconnection, and message callbacks.Basic MQTT Connection
This example demonstrates connecting to an MQTT broker and publishing/subscribing to topics.Complete Basic Example
Structured Topics
Organize MQTT topics hierarchically for complex systems.Topic Structure
Use a consistent naming scheme:Publishing to Structured Topics
Use thepublishToTopic helper method:
Subscribing to Multiple Topics
Handling Different Topics
Smart Home Example
Complete IoT sensor and control system:Advanced Features
Last Will and Testament (LWT)
Set a message to publish when device disconnects:mqtt-manager_mqtt-lwt-example.ino for details.
QoS Levels
Control message delivery guarantees:Connection Status
Check and monitor connection state:Key Methods
| Method | Description |
|---|---|
beginMQTT(broker, port) | Initialize MQTT connection |
setClientId(id) | Set unique client identifier |
setCallback(function) | Set message handler |
connect() | Connect to broker |
loop() | Maintain connection (call in loop) |
publish(topic, message) | Publish message |
publishToTopic(base, subtopic, msg) | Structured topic publish |
subscribe(topic) | Subscribe to topic |
isConnected() | Check connection status |
getStateString() | Get connection state |
Hardware Requirements
- ESP32 or ESP8266
- WiFi connection
- MQTT broker (local or cloud)
Popular MQTT Brokers
- Public: broker.hivemq.com, test.mosquitto.org
- Self-hosted: Mosquitto, HiveMQ
- Cloud: AWS IoT, Azure IoT Hub, Google Cloud IoT
Source Files
Example files located at:- Basic:
example/modules/wifi/EXAMPLE-mqtt-manager/mqtt-manager_mqtt-basic-example/ - Topics:
example/modules/wifi/EXAMPLE-mqtt-manager/mqtt-manager_mqtt-topics-example/