สมัครรับข้อมูลและตอบสนองต่อคำสั่ง
สลับรีเลย์เมื่อมีข้อความเข้ามา
สมัครรับข้อมูลและตอบสนองต่อคำสั่ง เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Listening for Messages
Publishing sends data out; subscribing lets your device receive it. This is how the cloud sends commands back to hardware.
Subscribe to a Topic
Tell the broker which topic you want with subscribe. From then on, matching messages are pushed straight to your device.
client.subscribe("home/livingroom/light");Messages Arrive in a Callback
Incoming messages trigger a callback function you write. The broker calls it every time a subscribed topic gets a message.
The Callback Signature
Your callback receives the topic, the payload bytes, and a length. You register it once during setup.
void onMessage(char* topic, byte* payload, unsigned int len) {
}Register the Callback
Hook your function up with setCallback before connecting. The library then routes every message to it.
client.setCallback(onMessage);Read the Payload Safely
The payload is raw bytes, not a finished string. Build a String by looping over its length so you never read past the end.
String msg;
for (int i = 0; i < len; i++)
msg += (char)payload[i];Decide What It Means
Compare the message to known commands. A simple if check on the text is enough to map ON and OFF to actions.
if (msg == "ON") digitalWrite(RELAY, HIGH);Act on the Command
Now do something real: flip a relay, move a servo, or change a light. This closes the loop from cloud to physical world.
Check the Topic Too
If you subscribe to several topics, inspect the topic argument first. That tells you which device the command targets.
Re-Subscribe After Reconnect
A dropped link forgets your subscriptions. After every reconnect, call subscribe again or you will hear silence.
Keep Callbacks Short
Do quick work inside the callback and return fast. Long jobs there can stall the MQTT loop and miss new messages.
Quick Check
After a WiFi drop your device reconnects but stops responding to commands. What did the reconnect break?
Recap
You learned to subscribe, handle messages in a callback, read the payload, act on commands, and re-subscribe after reconnects. 🎉
คำถามที่พบบ่อย
บทเรียน “สมัครรับข้อมูลและตอบสนองต่อคำสั่ง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “สมัครรับข้อมูลและตอบสนองต่อคำสั่ง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “สมัครรับข้อมูลและตอบสนองต่อคำสั่ง”
สลับรีเลย์เมื่อมีข้อความเข้ามา คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “สมัครรับข้อมูลและตอบสนองต่อคำสั่ง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำไม MQTT จึงเหมาะกับ IoT
- เชื่อมต่อกับโบรกเกอร์
- เผยแพร่หัวข้อของเซนเซอร์
- สมัครรับข้อมูลและตอบสนองต่อคำสั่ง