ESP32 + Zephyr HTTPS/TLS Connection Fails (Error -116)

2026-08-17

Stack Overflow: View Question

Tags: https, embedded, esp32, zephyr-rtos

Score: 0 | Views: 31

The asker has an ESP32 DevKitC running Zephyr RTOS. Wi-Fi association works, DNS resolves, and a TCP socket to jsonplaceholder.typicode.com:443 is opened — but when the TLS handshake begins, connect() returns error -116, which is ETIMEDOUT in Zephyr's errno mapping. No TLS alert, no certificate error — just silence, then a timeout.

Why this is interesting: -116 on a TLS socket in Zephyr almost never means "the server didn't answer." The TCP layer clearly worked (they "reach the HTTPS connection stage"), so the handshake bytes are getting somewhere. The timeout is the mbedTLS state machine giving up because it never received a satisfactory ServerHello — or, more commonly, because it never sent a valid ClientHello in the first place. On resource-constrained ESP32 builds with Zephyr's mbedTLS port, several silent misconfigurations produce exactly this symptom.

Likely root causes, in order of probability:

Approach: Enable CONFIG_MBEDTLS_DEBUG=y with CONFIG_MBEDTLS_DEBUG_LEVEL=4 and register a debug callback via mbedtls_ssl_conf_dbg(). The log will show exactly where the handshake dies — "ssl_write_client_hello" without a "parse server hello" points at network/SNI; "certificate verify" errors point at the CA. Also bump CONFIG_MAIN_STACK_SIZE and CONFIG_MBEDTLS_HEAP_SIZE to at least 32 KB and 48 KB respectively before assuming a config bug.

Gotchas: Zephyr's error codes are positive at the socket API but the underlying mbedTLS returns are negative (e.g., -0x7780 for cert verify). Don't confuse them. Also, IPPROTO_TLS_1_2 means "at most 1.2" — if the server insists on TLS 1.3, you need IPPROTO_TLS_1_3 and the corresponding Kconfig.

The challenge: A generic "connect timeout" on a TLS socket in Zephyr collapses four completely different failure modes into one opaque errno, and the only way to disambiguate is to turn on mbedTLS's internal debug logging.

All newsletters