0Pricing
Arduino & IoT Academy · 课时

连接到代理服务器

使用 PubSubClient 连接 MQTT 服务器

连接到代理服务器 是 CoddyKit 上的免费 Arduino & IoT Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Arduino & IoT Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Arduino & IoT Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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

常见问题解答

「连接到代理服务器」课时是免费的吗?

是的 — 「连接到代理服务器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Arduino & IoT Academy 课程的其余内容,请升级到 CoddyKit PRO。 Arduino & IoT Academy 课程共包含 4 节课。

「连接到代理服务器」这节课中我会学到什么?

使用 PubSubClient 连接 MQTT 服务器 你通过在浏览器中直接运行的动手代码来练习 Arduino & IoT Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Arduino & IoT Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Arduino & IoT Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「连接到代理服务器」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Arduino & IoT Academy 课中编写并运行代码吗?

能。每节 Arduino & IoT Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 为什么 MQTT 适合物联网
  2. 连接到代理服务器
  3. 发布传感器主题
  4. 订阅命令并执行操作
← 返回 Arduino & IoT Academy