- MQTT needs a working Station IP and a reachable broker. If DNS or the port is blocked, the client retries forever with little in the log.
- Keep-alive, client ID clashes, and a broker that drops idle sockets look like ‘random disconnects’.
- TLS time is wrong without SNTP (or an RTC). Certificates fail first; the MQTT library then looks guilty.
- For a product, topics, last will, and back-off belong in firmware design — the official TCP example only proves the pipe.
Short answer
ESP32 MQTT client never stays connected. What usually breaks?
中文
短答
ESP32 MQTT 总连不上或掉线,常见是什么?
- 先要有 Station IP,broker 还要达得到。DNS 或端口被拦时,客户端会一直重试,日志里信息很少。
- keepalive、client ID 冲突、broker 踢空闲连接,看起来都像随机掉线。
- 没有 SNTP(或 RTC)时 TLS 时间是错的。先是证书失败,再被怪到 MQTT 库。
- 产品里主题、遗愿和退避要写进固件设计。官方 TCP 例程只证明这条管道能通。