Code samples

ESP32 MQTT example

Connect an ESP32 to an MQTT broker over Wi-Fi with the Arduino PubSubClient library. Start on the free public broker, then point the same code at a secure private broker on ConnectMQ.

Install
arduino-cli lib install PubSubClient
Source code on GitHubmqtt-code-samples/esp32Clone the complete, runnable examples. Stars and pull requests welcome. View on GitHub

1. Try it on the free public broker

No account needed: connect to broker.connectmq.com on port 1883 and start publishing in seconds.

esp32_mqtt.ino
#include <WiFi.h>
#include <PubSubClient.h>

const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
const char* TOPIC = "your-project/sensors/temperature"; // Use a unique topic prefix: everyone can read the public broker

WiFiClient net;
PubSubClient mqtt(net);

// Runs for every message on a subscribed topic
void onMessage(char* topic, byte* payload, unsigned int length) {
  Serial.printf("%s: %.*s\n", topic, length, (char*)payload);
}

void connectMqtt() {
  while (!mqtt.connected()) {
    // Every client needs a unique client ID
    String id = "esp32-" + String((uint32_t)ESP.getEfuseMac(), HEX);
    if (mqtt.connect(id.c_str())) {
      // Subscribe to the temperature topic
      mqtt.subscribe(TOPIC);
    } else {
      delay(2000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  // Connect to Wi-Fi first
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  // Connect to the free public broker (no account needed)
  mqtt.setServer("broker.connectmq.com", 1883);
  mqtt.setCallback(onMessage);
}

void loop() {
  // Reconnect automatically if the connection drops
  if (!mqtt.connected()) connectMqtt();
  mqtt.loop();
  static unsigned long last = 0;
  if (millis() - last > 10000) {
    last = millis();
    // Publish a JSON reading
    mqtt.publish(TOPIC, "{\"temperature\":21.5}");
  }
}

Free public MQTT broker

The public broker is shared: anyone can subscribe to your topics. Never send passwords, personal data or anything confidential through it.

About the free public MQTT broker

2. Go to production with ConnectMQ

For real devices, use a private serverless MQTT broker on ConnectMQ instead of the public one: TLS encryption, a username and password for every device and per-topic access control, fully managed. Start on the Free plan — no card required.

Replace YOUR_BROKER_HOST, YOUR_USERNAME and YOUR_PASSWORD with the values from your ConnectMQ dashboard.

esp32_mqtt.ino
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>

const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
const char* TOPIC = "your-project/sensors/temperature";

// Root CA certificate (PEM) of the broker TLS certificate
const char* ROOT_CA = R"PEM(
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
)PEM";

WiFiClientSecure net;
PubSubClient mqtt(net);

// Runs for every message on a subscribed topic
void onMessage(char* topic, byte* payload, unsigned int length) {
  Serial.printf("%s: %.*s\n", topic, length, (char*)payload);
}

void connectMqtt() {
  while (!mqtt.connected()) {
    // Every client needs a unique client ID
    String id = "esp32-" + String((uint32_t)ESP.getEfuseMac(), HEX);
    // Username and password from your ConnectMQ dashboard
    if (mqtt.connect(id.c_str(), "YOUR_USERNAME", "YOUR_PASSWORD")) {
      // Subscribe to the temperature topic
      mqtt.subscribe(TOPIC);
    } else {
      delay(2000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  // Connect to Wi-Fi first
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  net.setCACert(ROOT_CA);
  // Connect to your private ConnectMQ broker on port 8883
  mqtt.setServer("YOUR_BROKER_HOST", 8883);
  mqtt.setCallback(onMessage);
}

void loop() {
  // Reconnect automatically if the connection drops
  if (!mqtt.connected()) connectMqtt();
  mqtt.loop();
  static unsigned long last = 0;
  if (millis() - last > 10000) {
    last = millis();
    // Publish a JSON reading
    mqtt.publish(TOPIC, "{\"temperature\":21.5}");
  }
}

ESP32 MQTT FAQ

How do I connect ESP32 to an MQTT broker?

Install PubSubClient (Arduino), connect to the broker host on port 1883 (or 8883 with TLS), then subscribe to a topic and publish messages to it. The examples on this page connect to the free public broker broker.connectmq.com and to a private ConnectMQ broker.

Is there a free MQTT broker for testing ESP32?

Yes. broker.connectmq.com is a free public MQTT broker that needs no account. Anyone can read its topics, so use it only for tests and use a unique topic prefix.

How do I secure a ESP32 MQTT connection?

Connect over TLS on port 8883 with a username and password from your ConnectMQ dashboard, and give every device its own credentials and topic permissions instead of using the public broker.

Free public MQTT broker

Test any example on broker.connectmq.com — no sign-up, MQTT 3.1.1 and 5, TCP, TLS and WebSocket.

About the free public MQTT broker

Secure serverless MQTT for production

When it is time for real devices, run the same code on a private, fully managed ConnectMQ broker with TLS and per-device credentials.

Start for free

Your private MQTT broker is a few clicks away

Create an account, get your broker credentials and connect your first device — no servers to install, patch or scale.