0Pricing
Arduino & IoT Academy · Lesson

Connect to a Broker

Use PubSubClient to reach an MQTT server.

Connect to a Broker is a free Arduino & IoT Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

You Need a Broker First

Before any messages flow, your device must reach a broker. It is the server that accepts connections and routes every message.

Public or Private Brokers

You can use a free public broker like test.mosquitto.org for learning, or run your own private one for real projects.

The PubSubClient Library

On ESP boards the popular choice is the PubSubClient library. Install it once, then include it at the top of your sketch.

#include <PubSubClient.h>

It Rides on a WiFi Client

PubSubClient needs a network client to ride on. You hand it a WiFiClient that has already joined your network.

WiFiClient espClient;
PubSubClient client(espClient);

Point It at the Broker

Tell the client where the broker lives with its address and port. Plain MQTT uses port 1883 by default.

client.setServer("test.mosquitto.org", 1883);

Give Each Device a Client ID

Every connection needs a unique client ID. If two devices share one, the broker kicks the older connection off.

Make the Connection

Call connect with your client ID to log in. It returns true when the broker accepts you and false if something is wrong.

client.connect("esp32-sensor-01");

Reconnect When It Drops

Connections die on real networks. Wrap your connect in a loop so the device keeps retrying until it is back online.

Keep the Link Alive

You must call client.loop() often inside loop(). It handles keepalive pings and incoming messages behind the scenes.

void loop() {
  client.loop();
}

Authenticate When Needed

Private brokers usually want a username and password. Pass them to connect so only trusted devices get in.

client.connect("id", "user", "pass");

Watch the Connection State

Print the result of connect or check client.state() to debug. Clear logs save you from guessing why nothing arrives.

Quick Check

Your ESP32 sketch compiles but never receives messages, even though it connected once. What is the likely cause?

Recap

You connected to a broker with PubSubClient: set the server and port, used a unique client ID, and kept the link alive. 🎉

Frequently asked questions

Is the “Connect to a Broker” lesson free?

Yes — the full text of “Connect to a Broker” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.

What will I learn in “Connect to a Broker”?

Use PubSubClient to reach an MQTT server. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Arduino & IoT Academy?

No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Connect to a Broker” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Arduino & IoT Academy lesson?

Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why MQTT Fits IoT
  2. Connect to a Broker
  3. Publish Sensor Topics
  4. Subscribe & Act on Commands
← Back to Arduino & IoT Academy