Source: https://mokxi.com/libraries/adafruit-mqtt
Updated: 2026-11-22

Libraries / Network

Arduino library · Network

# The Adafruit MQTT Library: feeds as objects

Yes, you can build with the Adafruit MQTT Library online in Mokxi with nothing to install. Its hardware is not simulated, so a sketch says plainly that it cannot reach the device. The example below, "Adafruit MQTT to Adafruit IO, and why it cannot connect here", runs on the ESP32-C3-DevKitM-1.

Run the example in Mokxi See the calls

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

## What Adafruit MQTT Library does

The Adafruit MQTT Library is the MQTT client Adafruit’s own IO examples use, though it talks to any MQTT 3.1.1 broker. Its distinctive idea is that each topic becomes an object: an Adafruit_MQTT_Publish for a feed you write to and an Adafruit_MQTT_Subscribe for a feed you listen to, both tied to one Adafruit_MQTT_Client that holds the broker address, user name and key.

A sketch registers its subscriptions before connecting, then in loop() makes sure it is connected, waits in readSubscription() for up to a set time for anything new, and publishes values. When a subscription has news, its lastread buffer holds the payload. If the sketch publishes rarely, ping() keeps the broker from dropping it. Up to five subscriptions fit by default.

In Mokxi the library compiles, and the example follows the library’s ESP8266 sample on an ESP32-C3, but it cannot reach a broker: MQTT needs a raw TCP connection, which the simulated network does not carry. connect() returns -1 ("Connection failed"), publish() and ping() return false and readSubscription() returns nullptr at once. HTTPClient with http://api.mokxi/ is the way to the cloud here.

Library Manager name | Adafruit MQTT Library

Author | Adafruit

Include | #include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>

Boards | 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, ESP32-S3-CAM, ESP8266 NodeMCU, Raspberry Pi Pico W, Arduino Uno R4 WiFi

In the simulator | Builds 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, ESP32-S3-CAM, ESP8266 NodeMCU, Raspberry Pi Pico W, Arduino Uno R4 WiFi.

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

On a real board, install "Adafruit MQTT Library" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit MQTT Library to lib_deps in PlatformIO. Mokxi follows the calls of release 2.6.6, 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 -1 ("Connection failed") and one Serial line says why; publish() and ping() return false and readSubscription() returns nullptr at once. HTTPClient with http://api.mokxi/ is the simulated way to the cloud.

- 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
- ESP32-S3-CAM
- ESP8266 NodeMCU
- Raspberry Pi Pico W
- Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY)

Creates the MQTT client on a WiFiClient, with the broker, port, user name and key (for Adafruit IO, your AIO key).

### Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell")

An object for one topic you publish to. On Adafruit IO, feeds live under your user name.

### Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff")

An object for one topic you listen to. Its lastread field holds the latest payload.

### mqtt.subscribe(&onoff)

Registers a subscription with the client. Do this in setup(), before the first connect().

### mqtt.connect()

Connects and returns 0 on success, or a code that connectErrorString() turns into words.

### mqtt.readSubscription(5000)

Waits up to 5000 ms for a message on any subscription and returns a pointer to that subscription, or nullptr.

### photocell.publish(value)

Publishes a number or a string to that feed and returns true when it went out.

### mqtt.ping()

Sends a keepalive and returns false when the broker did not answer, a sign to disconnect and reconnect.

## Example: Adafruit MQTT to Adafruit IO, 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

Open it in the editor, wired and ready to run

## Common errors and how to fix them

### Subscribed feed never delivers anything

subscribe() was called after connect(), so the broker was never told. Register every subscription in setup() before the first connect(), and keep within MAXSUBSCRIPTIONS, five by default, or the extra ones are dropped.

### Connection failed, or "Wrong username or password"

For Adafruit IO, the user name is your account name and the password is the AIO key from the site, not your login password. For other brokers, check the port: 1883 for plain MQTT, 8883 only with WiFiClientSecure.

### The connection drops every few minutes

Nothing was sent within the keepalive time, so the broker gave up. Call mqtt.ping() every minute or so when you publish less often, and reconnect when it returns false.

## Questions and answers

### Adafruit MQTT or the Adafruit IO Arduino library?

Adafruit IO Arduino wraps this library together with WiFi setup and feed names, so it is shorter for Adafruit IO alone. Use Adafruit MQTT directly when you want control over the connection or a different broker.

## Keep going

### Learn

- ESP32 Not Connecting to WiFi: Causes and Fixes
- ESP32 IoT Projects for Final Year Students

### More network libraries

- PubSubClient
- MQTT
- Blynk
- ThingSpeak
- WiFiManager
- WiFi, HTTPClient and WebServer (ESP32 core)
- WebSockets
- NTPClient

Every built-in library · Open the editor

## Try Adafruit MQTT Library without installing anything

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

Start building free

Open the editor
