Arduino (using Arduino IDE or PlatformIO) and ESP-IDF (using Espressif IoT Development Framework or PlatformIO) compatible library MQTT wrapper for setting up an MQTT connection.
The wrapper was created to reduce boilerplate of common MQTT setup code that I was repeated in various projects.
Given the MQTT host and credentials, it connects to the host and reconnect on connection loss. It provides methods for publishing messages as well as subscribing to topics.
On connection, it publish online
to the client-id/status
topic, and sets up a last will to publish offline
to the same topic on connection loss/device offline. This is a common practice for devices running as Home Assistant nodes.
Add the following to libs_deps
:
Johboh/MQTTRemote
In your existing idf_component.yml
or in a new idf_component.yml
next to your main component:
dependencies:
johboh/mqttremote:
version: ">=4.0.5"
Search for MQTTRemote
by johboh
in the library manager. See note about version above.
Note: Need ESP32 core v3.0.3 until this issue has been fixed. If you get issues with undefined reference to
lwip_hook_ip6_input'`, try a different ESP32 core version. Need at least 3+ for C++17 support.
- ESP32 (tested with PlatformIO espressif32@6.4.0 / arduino-esp32@2.0.11 / ESP-IDF@4.4.6 / ESP-IDF@5.1.2 on ESP32-S2 and ESP32-C3)
- ESP8266 (tested with PlatformIO espressif8266@4.2.1 / ardunio-core@3.1.2)
Newer version most probably work too, but they have not been verified.
- For Arduino: https://github.com/256dpi/arduino-mqtt @^2.5.1