Skip to content

ESP32 / Arduino ​

An ESP32 connects to ConnectMQ over TLS with WiFiClientSecure and the PubSubClient library.

What you need ​

  • An ESP32 board and the esp32 board package in the Arduino IDE (or PlatformIO).
  • The PubSubClient library (Library Manager → search PubSubClient by Nick O'Leary).
  • Your client ID, username and password from Credentials & endpoints.

Root certificate ​

The broker's certificate is issued by Let's Encrypt and chains to ISRG Root X1. Microcontrollers do not have a CA store, so paste that root certificate into the sketch. Download it from letsencrypt.org/certs/isrgrootx1.pem. Root certificates are valid for years; the broker's own certificate renews automatically without firmware changes.

Sketch ​

cpp
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>

const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";

const char* MQTT_HOST = "broker.connectmq.com";
const int   MQTT_PORT = 8883;
const char* CLIENT_ID_PREFIX = "connectmq_client_123456";  // issued client ID
const char* MQTT_USER = "YOUR_USERNAME";
const char* MQTT_PASS = "YOUR_PASSWORD";
const char* TOPIC = "acme/istanbul/assembly/line1/esp32-01/telemetry";

// ISRG Root X1 (https://letsencrypt.org/certs/isrgrootx1.pem)
const char* ROOT_CA = R"PEM(
-----BEGIN CERTIFICATE-----
...paste the certificate here...
-----END CERTIFICATE-----
)PEM";

WiFiClientSecure net;
PubSubClient mqtt(net);

void connectMqtt() {
  while (!mqtt.connected()) {
    // Unique client ID: issued client ID + "_" + chip ID
    String id = String(CLIENT_ID_PREFIX) + "_esp32-" + String((uint32_t)ESP.getEfuseMac(), HEX);
    if (mqtt.connect(id.c_str(), MQTT_USER, MQTT_PASS)) {
      mqtt.subscribe("acme/istanbul/assembly/line1/esp32-01/cmd");
    } else {
      Serial.printf("MQTT connect failed, state=%d\n", mqtt.state());
      delay(2000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  net.setCACert(ROOT_CA);
  mqtt.setServer(MQTT_HOST, MQTT_PORT);
  mqtt.setBufferSize(1024);  // default is 256 bytes, too small for most JSON
  mqtt.setCallback([](char* topic, byte* payload, unsigned int len) {
    Serial.printf("%s: %.*s\n", topic, len, (char*)payload);
  });
}

void loop() {
  if (!mqtt.connected()) connectMqtt();
  mqtt.loop();
  static unsigned long last = 0;
  if (millis() - last > 10000) {
    last = millis();
    char payload[64];
    snprintf(payload, sizeof(payload), "{\"temperature\":%.1f}", temperatureRead());
    mqtt.publish(TOPIC, payload);
  }
}

Tips ​

  • Testing only: net.setInsecure() skips certificate checks so you can rule out a certificate problem. Never ship firmware with it.
  • The device clock does not need to be set for TLS with setCACert, but if you validate dates yourself, sync time with NTP first.
  • PubSubClient publishes with QoS 0 and supports MQTT 3.1.1. For QoS 1/2 or MQTT 5 on ESP-IDF, use the esp-mqtt component with the same host, port and certificate.
  • mqtt.state() returns 5 (not authorized) for a wrong username/password and 2 (identifier rejected) for an invalid client ID.
  • Plain samples for the public test broker are in connectmq/mqtt-code-samples/esp32.

Fully managed MQTT broker for connected devices.