รับเวลาจริงด้วย NTP/RTC
ประทับข้อมูลด้วยวันที่และเวลาที่แม่นยำ
รับเวลาจริงด้วย NTP/RTC เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Time Matters
A reading like "24.3 C" is half a story. To make sense of logged data, every sample needs a timestamp telling you exactly when it happened. ⏰
Arduino Has No Clock
Your Arduino forgets the date the moment power drops. It only counts milliseconds since boot, so it has no idea what real-world wall-clock time it is.
Two Ways to Get Time
You can fetch the time from the internet with NTP, or keep it locally with a battery-backed RTC chip. Each fits a different situation.
What NTP Does
NTP (Network Time Protocol) asks a time server online for the current moment. It is accurate and free, but it needs an active WiFi connection to work.
Asking an NTP Server
On an ESP32 one line of setup points your board at a public time server and your timezone offset. Then the board syncs automatically.
configTime(0, 0, "pool.ntp.org");Reading the Synced Time
Once synced, you pull the current time into a struct and read its fields. This gives you year, month, day, hour, and minute to label data.
struct tm now;
getLocalTime(&now);What an RTC Does
An RTC module like the DS3231 keeps ticking on a tiny coin battery, even with the Arduino fully off. No network needed at all.
Wiring an RTC
Most RTC modules talk over I2C, so you connect just four wires: power, ground, SDA, and SCL. That keeps your build wonderfully simple.
Reading from an RTC
With the RTClib library you grab the current moment as a DateTime object, then read its fields just like the NTP version.
DateTime now = rtc.now();
int h = now.hour();NTP or RTC?
Pick NTP when your device is always online and you want zero extra parts. Pick an RTC for offline or battery nodes that still need the right time.
Best of Both
Many strong designs use both: an RTC keeps time offline, and NTP gently corrects its small drift whenever WiFi is available. 🌐
Quick Check
Which time source still works with no internet connection?
Recap
Arduino has no real clock, so add time with NTP online or an RTC offline. Now every reading you log can carry an accurate timestamp. ✅
คำถามที่พบบ่อย
บทเรียน “รับเวลาจริงด้วย NTP/RTC” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “รับเวลาจริงด้วย NTP/RTC” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “รับเวลาจริงด้วย NTP/RTC”
ประทับข้อมูลด้วยวันที่และเวลาที่แม่นยำ คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “รับเวลาจริงด้วย NTP/RTC” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- รับเวลาจริงด้วย NTP/RTC
- เขียน CSV ลงการ์ด SD
- บัฟเฟอร์และอัปโหลดเป็นชุด
- แสดงภาพข้อมูลที่บันทึกไว้