ESP32 MQTT TLS: Secure Broker Connections Explained

What is ESP32 MQTT TLS and why does it matter?
MQTT is a lightweight publish/subscribe messaging protocol, and ESP-MQTT is Espressif's client implementation of it, now including MQTT v5.0 support. Plain MQTT is just bytes over TCP. Adding TLS puts an encrypted, authenticated channel underneath that traffic, so broker credentials and payloads are not exposed on the network.
On ESP32 the encryption work belongs to the ESP-TLS component. It provides a simplified API for the common TLS jobs: CA certificate validation, SNI, ALPN negotiation and non-blocking connections. All settings live in an esp_tls_cfg_t structure, and the connection follows a short sequence of calls: esp_tls_init, then esp_tls_conn_new_sync or esp_tls_conn_new_async, then esp_tls_conn_read and esp_tls_conn_write, and finally esp_tls_conn_destroy. Internally ESP-TLS uses MbedTLS by default, or a different stack registered through esp_tls_register_stack.
Because ESP-TLS sits below the application protocol, MQTT simply runs on top of it. That separation is why the same verification settings apply whether you are fetching HTTPS or talking to an MQTT broker.
\2ESP32 dev board, the class of hardware used as an MQTT over TLS client. Image: Edwiyanto, CC BY-SA 4.0, via Wikimedia Commons
Which server verification option should I choose?
The ESP-TLS client can verify the broker by validating its certificate or by using pre-shared keys. You should select exactly one option in esp_tls_cfg_t. If none is selected, the client returns a fatal error by default when the TLS connection is set up.
| Option | What it does | Notes |
|---|---|---|
cacert_buf + cacert_bytes |
CA certificate supplied in a buffer, with its size in bytes | Good when you have one broker and want the CA in your code |
use_global_ca_store |
A global CA store initialised once and reused by any connection that sets the flag to true | Useful when several TLS connections share the same roots |
crt_bundle_attach |
Attaches the ESP x509 Certificate Bundle of root certificates | This is what the ESP-IDF MQTT examples use |
psk_hint_key |
Pre-shared key verification, with a hint and key pointer | Requires CONFIG_ESP_TLS_PSK_VERIFICATION; only used when no other option is set |
| Skip verification | Disables server checks | Requires CONFIG_ESP_TLS_INSECURE and CONFIG_ESP_TLS_SKIP_SERVER_CERT_VERIFY; testing only |
The last row is a warning, not a shortcut. The ESP-TLS documentation states that with verification skipped there is a risk of connecting to a server with a fake identity unless the certificate is supplied by another mechanism.
\2ESP-WROOM-32 Wi-Fi and Bluetooth module found on many ESP32 boards. Image: Brian Krent, CC BY-SA 4.0, via Wikimedia Commons
How do I get started with an ESP32 MQTT TLS example?
Start from the reference example rather than an empty project. Since ESP-IDF v6.0 the ESP-MQTT component lives in a separate repository, so add it to your project first:
idf.py add-dependency espressif/mqtt
The protocols/mqtt example demonstrates MQTT over TLS with certificate bundle validation. Its default broker URI is mqtts://test.mosquitto.org:8886, which uses a Let's Encrypt chain, and validation happens through the ESP-IDF certificate bundle. The example also shows an alternative configuration that pins the Mosquitto CA and points at mqtts://test.mosquitto.org:8883.
| Example | Transport | Default URI |
|---|---|---|
protocols/mqtt |
MQTT over TLS, certificate bundle | mqtts://test.mosquitto.org:8886 |
protocols/mqtt alternative |
MQTT over TLS, pinned Mosquitto CA | mqtts://test.mosquitto.org:8883 |
protocols/mqtt5 |
MQTT 5.0 over TLS, certificate bundle | mqtts://test.mosquitto.org:8886 |
For other transports, including plain TCP, WebSockets, WSS and mutual authentication, the ESP-MQTT component repository carries additional examples. A plain TCP MQTT setup such as the one in the Wi-Fi mesh report uses port 1883 and no TLS at all, which is a useful baseline when you are isolating a TLS problem.
How does SNI fit into an ESP32 MQTT TLS handshake?
SNI is a TLS extension that lets the client name the host it is connecting to during the handshake. It matters when one IP address serves several domains, because the server needs the hostname to pick the right certificate.
The ESP-TLS guidance is straightforward. SNI is enabled by default for HTTPS connections, and you can set the hostname explicitly with the common_name field in esp_tls_cfg_t. That value must match the server certificate's Common Name, and skip_common_name should be set to false so the certificate is validated against the hostname.
esp_tls_cfg_t cfg = {
.cacert_buf = ...,
.cacert_bytes = ...,
.common_name = "example.com",
.skip_common_name = false,
};
If you leave the hostname handling loose, a broker behind a shared IP may present a certificate that does not match what you expect, and verification will fail. Setting common_name explicitly and keeping hostname checking on avoids that class of failure.
What does ESP32 MQTT TLS look like on Arduino?
The sources here do not document an Arduino MQTT client library with TLS configuration, so treat any specific Arduino client API as something to verify against that library's own documentation rather than assume.
What the sources do cover on the Arduino side is the broker role. EmbeddedMqttBroker is described as an asynchronous MQTT broker and a synchronous MQTTS broker for ESP32 and ESP8266, built on the Arduino platform, speaking MQTT 3.1.1 at QoS 0 over TCP, WebSockets, mTLS and secure WebSockets. Its secure modes are implemented synchronously because of the overhead of TLS, while the plain TCP and WebSocket paths stay asynchronous. The project ships sketches such as secure-tcp-MqttBroker.ino and secure-websocket-MqttBroker.ino, and the secure TCP example uses an mqttsPort of 8883 with a server certificate and private key in PEM format.
For a client-side mutual authentication pattern, the only worked example in these sources is Toit rather than Arduino: it supplies a root certificate, a client certificate and a client private key, connects on port 8883, and notes that AWS IoT policies restrict which client IDs and topics each device may use.
What are the common pitfalls with ESP32 MQTT TLS?
| Pitfall | What happens |
|---|---|
| No verification option set | ESP-TLS returns a fatal error during connection setup |
| Skipping verification to "make it work" | The client may accept a server with a fake identity |
| Hostname mismatch | common_name must equal the certificate CN, and skip_common_name should stay false |
| TLS over Wi-Fi mesh | A reported MESH_NODE case connects, receives a SUBACK for the first subscribe, then logs esp-tls-mbedtls: read error :-0x7180 and disconnects, while the MESH_ROOT board works |
| Missing client credentials | Mutual auth needs both the client certificate and the private key, not just the root CA |
| Wrong client ID under a policy | AWS restricts client IDs per device; a disallowed one produces an "invalid packet kind: 0" error |
| Assuming secure broker modes are async | In EmbeddedMqttBroker, mTLS and WSS are synchronous by design |
The mesh case is worth reading carefully because the thread reports the symptom without a confirmed fix: the failing board works once it becomes the root, which points at the mesh path rather than at the certificate configuration alone. Reproduce the same broker over plain TCP on port 1883 first, as the reporter did, to separate MQTT problems from TLS and routing problems.
Frequently asked questions
Does ESP32 MQTT TLS require a CA certificate?
You need one verification method, but not necessarily a CA buffer. ESP-TLS accepts a CA buffer, the global CA store, the ESP x509 certificate bundle, or pre-shared keys. With none selected, the connection fails by default.
Which port should the ESP32 MQTT TLS example use?
The ESP-IDF examples default to mqtts://test.mosquitto.org:8886 with the certificate bundle, and show an alternative on port 8883 with the Mosquitto CA pinned.
What causes esp-tls-mbedtls: read error -0x7180?
The available source is a forum report where a Wi-Fi mesh node hits this after its first SUBACK while the root board works. No confirmed fix is given in that thread.
Can an ESP32 run its own MQTT broker with TLS?
EmbeddedMqttBroker supports MQTTS with mTLS and secure WebSockets on ESP32 under Arduino, with secure modes implemented synchronously.
Does ESP-MQTT support MQTT 5.0 over TLS?
Yes. The protocols/mqtt5 example uses MQTT v5.0 with TLS and certificate bundle validation, defaulting to mqtts://test.mosquitto.org:8886.
ESP32 MQTT TLS is mostly a configuration problem rather than a coding problem: pick one verification method, get the hostname right, and start from the ESP-IDF mqtt example so the certificate bundle path is already working. Once plain MQTT is stable, move the same broker to TLS and compare behaviour, which makes mesh and policy issues much easier to isolate.
Sources
github.comESP-TLS (esp-idf docs)github.comESP-MQTT (esp-idf docs)stackoverflow.comESP32 IDF Problem running MQTT with TLS over WIFI MESHstackoverflow.comAWS IoT MQTT with Toitgithub.comalexCajas/EmbeddedMqttBroker (GitHub README)This guide summarises the public sources listed above in our own words. Check the original documentation before relying on exact figures.
