เผยแพร่หัวข้อของเซนเซอร์
ส่งค่าที่อ่านได้ไปยังหัวข้อที่มีชื่อกำกับ
เผยแพร่หัวข้อของเซนเซอร์ เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Sending Data Is Publishing
Once connected, sharing a reading is one call. You publish a value to a topic and the broker delivers it to subscribers.
Choose a Clear Topic
Pick a descriptive topic path like home/garden/soil. Good names make a growing system easy to read and route.
home/garden/soil/moistureThe publish Call
The basic form takes a topic and a message. Both are simple strings, so MQTT does not care what is inside the payload.
client.publish("home/garden/temp", "23.5");Payloads Are Just Text
A payload is a plain byte string. You decide its meaning, whether it is a number, a word, or structured data.
Numbers Need Converting
Sensor readings are often floats. Turn them into a string first with dtostrf or String() before you publish.
char buf[8];
dtostrf(temp, 4, 1, buf);
client.publish("home/temp", buf);Send Structured Data as JSON
To bundle several values, format the payload as JSON. One message can carry temperature and humidity together.
client.publish("home/dht", "{\"t\":23.5,\"h\":55}");Do Not Flood the Broker
Publishing thousands of times a second wastes power and bandwidth. Add a sensible interval so messages stay useful.
Time It with millis
Use millis() instead of delay to publish every few seconds. Your device stays responsive while it waits.
if (millis() - last > 5000) {
last = millis();
publishReading();
}Retain the Latest Reading
Add a retain flag so the broker stores the most recent value. New dashboards then show data the instant they connect.
client.publish("home/temp", buf, true);One Topic Per Quantity
Give each measurement its own topic, like .../temp and .../humidity. Clean separation keeps subscribers simple.
Confirm It Worked
The publish call returns true on success. Log it, or watch with a desktop MQTT client, to be sure data left the board.
Quick Check
You want a new dashboard to immediately show the last temperature, even before the sensor publishes again. What helps?
Recap
You learned to publish sensor data: convert values to strings, pick clear topics, time sends with millis, and retain the latest. 🎉
คำถามที่พบบ่อย
บทเรียน “เผยแพร่หัวข้อของเซนเซอร์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “เผยแพร่หัวข้อของเซนเซอร์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “เผยแพร่หัวข้อของเซนเซอร์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำไม MQTT จึงเหมาะกับ IoT
- เชื่อมต่อกับโบรกเกอร์
- เผยแพร่หัวข้อของเซนเซอร์
- สมัครรับข้อมูลและตอบสนองต่อคำสั่ง