Skip to content

ESP32 MQTT Publish Fails Silently

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about ESP32 MQTT Publish Fails Silently. We cover key concepts, practical examples, and best practices.

The Problem

ESP32 publishes MQTT messages but the broker never receives them.

Quick Fix

Wrong

client.publish("sensor/temp", "22.5");  // No return value check
No message received on broker. No error message on ESP32.
if (client.connected()) {
  if (client.publish("sensor/temp", "22.5")) {
    Serial.println("Publish OK");
  } else {
    Serial.println("Publish failed");
  }
} else {
  Serial.println("MQTT not connected");
}
Publish OK
(Broker shows message on topic sensor/temp)

Prevention

Always check client.connected() before publishing. Verify the return value of publish(). Set QoS level appropriately (0 for fire-and-forget, 1 for guaranteed). Keep payloads under 256 KB.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### Why does MQTT publish fail when connected?

The connection may have been dropped between the check and the publish. Always check the return value and implement retry logic.

Can I publish large payloads over MQTT?

Brokers limit payload size (typically 256 KB). For larger data, split into chunks or use HTTP. ESP32 memory also limits max payload.

What QoS should I use for sensor data?

QoS 0 is fine for frequent sensor readings since missing one is acceptable. Use QoS 1 for critical alerts. Avoid QoS 2 unless exactly-once delivery is required.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro