0Pricing
Arduino & IoT Academy · Lektion

Mit einem Broker verbinden

Verwenden Sie PubSubClient, um einen MQTT-Server zu erreichen.

Mit einem Broker verbinden ist eine kostenlose Arduino & IoT Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Arduino & IoT Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Arduino & IoT Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 🎉

Häufig gestellte Fragen

Ist die Lektion „Mit einem Broker verbinden“ kostenlos?

Ja — der vollständige Text von „Mit einem Broker verbinden“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Arduino & IoT Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Arduino & IoT Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Mit einem Broker verbinden“?

Verwenden Sie PubSubClient, um einen MQTT-Server zu erreichen. Du übst Arduino & IoT Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Arduino & IoT Academy zu starten?

Keine Vorkenntnisse erforderlich. Arduino & IoT Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Mit einem Broker verbinden“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Arduino & IoT Academy-Lektion Code schreiben und ausführen?

Ja. Jede Arduino & IoT Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Warum MQTT zu IoT passt
  2. Mit einem Broker verbinden
  3. Sensorthemen veröffentlichen
  4. Befehle abonnieren und darauf reagieren
← Zurück zu Arduino & IoT Academy