Arduino library · Network

ArduinoMqttClient: Arduino’s own MQTT client

ArduinoMqttClient is the MQTT library from Arduino itself, used in the examples for the Uno R4 WiFi, Nano 33 IoT, MKR boards and Portenta. Like PubSubClient it rides on a network Client you pass in, so the same code works over WiFi, Ethernet or a cellular modem, and it speaks MQTT 3.1.1 to brokers such as Mosquitto, HiveMQ or EMQX.

Built in on Mokxi: #include <ArduinoMqttClient.h> and it builds, with nothing to install. Free, and no account needed.

What ArduinoMqttClient does

Its style is different, though. A message is written like a print to a stream: beginMessage(topic), then print() the payload in as many pieces as you like, then endMessage(). Incoming messages arrive either through an onMessage() callback or by checking parseMessage(), and in both cases the payload is read byte by byte with read(). poll() must run often to receive and to keep the connection alive.

In Mokxi this library compiles but cannot connect: MQTT needs a TCP connection to a broker, and the simulated network carries only HTTP. connect() returns 0, connectError() gives -2, and the serial monitor explains why. For a working cloud example in the simulator, use HTTPClient with http://api.mokxi/.

Library Manager nameArduinoMqttClient
AuthorArduino
Include#include <ArduinoMqttClient.h>
BoardsESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP8266 NodeMCU, Raspberry Pi Pico W, Arduino Uno R4 WiFi
In the simulatorBuilds and runs, and says plainly that the hardware behind it is not simulated.

Install it

It builds on ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP8266 NodeMCU, Raspberry Pi Pico W, Arduino Uno R4 WiFi.

In Mokxi it is built in: #include <ArduinoMqttClient.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "ArduinoMqttClient" by Arduino from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add ArduinoMqttClient to lib_deps in PlatformIO. Mokxi follows the calls of release 0.1.8, so a sketch written here builds there too.

What to know in the simulator: MQTT needs a TCP connection to a broker, which the simulated network does not carry, so connect() returns 0 with connectError() -2 and says why on Serial. HTTPClient with http://api.mokxi/ is the simulated way to the cloud.

The calls you will use

In the order a sketch meets them.

MqttClient mqttClient(wifiClient)

Creates the MQTT client on top of a network Client you declared first, usually WiFiClient wifiClient.

mqttClient.setId("greenhouse-1")

Sets the client ID the broker sees. Each board needs its own, or they knock each other off.

mqttClient.setUsernamePassword("user", "pass")

Credentials for brokers that require them. Call it before connect().

mqttClient.connect("test.mosquitto.org", 1883)

Connects to the broker and returns 1 on success, 0 on failure.

mqttClient.connectError()

Why connect() failed: -2 connection refused at the network level, -1 timeout, 4 bad user name or password, 5 not authorized.

mqttClient.beginMessage("home/temp")

Starts a message to a topic. Print the payload into the client, then call endMessage() to send it.

mqttClient.beginMessage("home/temp");
mqttClient.print(21.5);
mqttClient.endMessage();

mqttClient.subscribe("home/led")

Asks the broker for messages on a topic, with + and # wildcards allowed. Subscribe again after every reconnect.

mqttClient.onMessage(onMqttMessage)

Registers a function called with the message size when something arrives during poll(). Read the topic with messageTopic().

mqttClient.poll()

Reads incoming packets and sends keepalives. Call it on every pass of loop().

Example: ArduinoMqttClient, and why it cannot connect here

On the ESP32-C3-DevKitM-1. It runs in the browser exactly as printed.

sketch.ino · ESP32-C3-DevKitM-1
#include <WiFi.h>
#include <ArduinoMqttClient.h>

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

void setup() {
  Serial.begin(115200);
  WiFi.begin("Mokxi-Lab", "breadboard");
  if (!mqttClient.connect("test.mosquitto.org", 1883)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());
  }
}

void loop() {}

Open it in the editor, wired and ready to run

Common errors and how to fix them

MQTT connection failed! Error code = -2

The TCP connection to the broker never opened: wrong host or port, no WiFi, or a network that blocks port 1883. Test the broker from a computer with an MQTT client first. In Mokxi this is always the result, because no broker is reachable.

Messages arrive while the board is idle but not while it works

poll() only runs when loop() comes back to it, so a long delay() or blocking sensor code stops reception and lets the keepalive lapse. Use millis() timers and keep each pass of loop() short.

The message on the broker is empty

Nothing was printed between beginMessage() and endMessage(), or endMessage() was never called. Write the payload with mqttClient.print() inside that pair; calling Serial.print() there by mistake is a common slip.

Questions and answers

ArduinoMqttClient or PubSubClient?

PubSubClient has more ESP32 tutorials and a single publish(topic, payload) call. ArduinoMqttClient fits Arduino’s own boards and examples, and its stream style makes long or built-up payloads easy. Both run over any Client.

Try ArduinoMqttClient without installing anything

Open the example, change the sketch, and keep your version in a free account.