เชื่อมต่อกับโบรกเกอร์
ใช้ PubSubClient เพื่อเชื่อมต่อเซิร์ฟเวอร์ MQTT
เชื่อมต่อกับโบรกเกอร์ เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “เชื่อมต่อกับโบรกเกอร์”
ใช้ PubSubClient เพื่อเชื่อมต่อเซิร์ฟเวอร์ MQTT คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “เชื่อมต่อกับโบรกเกอร์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำไม MQTT จึงเหมาะกับ IoT
- เชื่อมต่อกับโบรกเกอร์
- เผยแพร่หัวข้อของเซนเซอร์
- สมัครรับข้อมูลและตอบสนองต่อคำสั่ง