0Pricing
Arduino & IoT Academy · Lezione

Connettersi a un broker

Utilizzi PubSubClient per raggiungere un server MQTT.

Connettersi a un broker è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Arduino & IoT Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Arduino & IoT Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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

Domande Frequenti

La lezione «Connettersi a un broker» è gratuita?

Sì — il testo completo di «Connettersi a un broker» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Arduino & IoT Academy, passa a CoddyKit PRO. Il corso Arduino & IoT Academy include 4 lezioni in totale.

Cosa imparerò in «Connettersi a un broker»?

Utilizzi PubSubClient per raggiungere un server MQTT. Eserciti Arduino & IoT Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Arduino & IoT Academy?

Non è richiesta alcuna esperienza precedente. Arduino & IoT Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.

Quanto tempo richiede la lezione «Connettersi a un broker»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Arduino & IoT Academy?

Sì. Ogni lezione Arduino & IoT Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Perché MQTT è adatto all'IoT
  2. Connettersi a un broker
  3. Pubblicare i topic dei sensori
  4. Sottoscriversi ai comandi e agire
← Torna a Arduino & IoT Academy