ESP32 Ghi Log Dữ Liệu với Timestamp vào Thẻ SD

Trong hướng dẫn này, chúng ta sẽ khám phá quy trình ghi log dữ liệu với timestamp vào thẻ Micro SD Card sử dụng ESP32. Cụ thể, chúng ta sẽ tìm hiểu các chủ đề sau:

Thông tin thời gian được trích xuất từ module RTC và sau đó được ghi vào thẻ Micro SD Card cùng với dữ liệu thu thập được.

Dữ liệu được ghi vào thẻ Micro SD Card có thể bao gồm nhiều loại thông tin khác nhau, chẳng hạn như:

Để đơn giản, hướng dẫn này sẽ minh họa quy trình bằng cách đọc giá trị từ hai chân analog, như một bộ dữ liệu mẫu. Code được cung cấp có thể dễ dàng được điều chỉnh để phù hợp với các loại dữ liệu khác nhau theo nhu cầu.

ESP32 log to micro thẻ SD

Linh Kiện Cần Thiết

1×mô-đun phát triển ESP-WROOM-32
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×Cáp USB Type-C
1×Thẻ Micro SD Card
1×Module Thẻ Micro SD Card
1×(Tùy chọn) MicroSD to SD Memory Card Adapter
1×Module Real-Time Clock DS3231
1×Pin CR2032
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Expansion Board for ESP32
1×(Khuyến nghị) Breakout Expansion Board for ESP32
1×(Khuyến nghị) Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Module Thẻ Micro SD Card và Module RTC

Chưa quen thuộc với Module thẻ Micro SD Card và module RTC, bao gồm pinout, chức năng và cách lập trình? Hãy khám phá các hướng dẫn toàn diện về những chủ đề này dưới đây:

Sơ Đồ Đấu Nối

ESP32 micro thẻ SD module sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Nếu bạn chưa rõ cách cấp nguồn cho ESP32 và các linh kiện khác, xem: Cách Cung Cấp Nguồn Điện Cho ESP32.

※ Lưu ý:

Nếu bạn sử dụng Ethernet shield hoặc bất kỳ shield nào có Micro SD Card Holder, bạn không cần sử dụng Module thẻ Micro SD Card. Bạn chỉ cần chèn thẻ Micro SD Card vào Micro SD Card Holder trên shield.

ESP32 - Ghi Log Dữ Liệu với Timestamp vào Thẻ Micro SD Card

/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp32/esp32-log-data-with-timestamp-to-sd-card */ #include <SD.h> #include <RTClib.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 #define FILE_NAME "/log.txt" RTC_DS3231 rtc; File myFile; void setup() { Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); // SETUP RTC MODULE if (!rtc.begin()) { while (1) { Serial.println(F("RTC module is NOT found")); delay(1000); } } if (!SD.begin(PIN_SPI_CS)) { while (1) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); delay(1000); } } Serial.println(F("SD CARD INITIALIZED.")); Serial.println(F("--------------------")); } void loop() { // open file for writing myFile = SD.open(FILE_NAME, FILE_WRITE); if (myFile) { Serial.println(F("Writing log to SD Card")); // write timestamp DateTime now = rtc.now(); myFile.print(now.year(), DEC); myFile.print('-'); myFile.print(now.month(), DEC); myFile.print('-'); myFile.print(now.day(), DEC); myFile.print(' '); myFile.print(now.hour(), DEC); myFile.print(':'); myFile.print(now.minute(), DEC); myFile.print(':'); myFile.print(now.second(), DEC); myFile.print(" "); // delimiter between timestamp and data // read data int analog_1 = analogRead(A0); int analog_2 = analogRead(A1); // write data myFile.print("analog_1 = "); myFile.print(analog_1); myFile.print(", "); // delimiter between data myFile.print("analog_2 = "); myFile.print(analog_2); myFile.write("\n"); // new line myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the file ")); Serial.println(FILE_NAME); } delay(2000); // delay 2 seconds }

Các Bước Thực Hiện

  • Nếu đây là lần đầu tiên bạn sử dụng ESP32, hãy xem ESP32 - Cài Đặt Phần Mềm.
  • Thực hiện đấu nối theo hình ảnh trên.
  • Kết nối bo mạch ESP32 với PC của bạn qua cáp micro USB
  • Mở Arduino IDE trên PC của bạn.
  • Chọn đúng bo mạch ESP32 (ví dụ: ESP32 Dev Module) và cổng COM.
  • Đảm bảo rằng thẻ Micro SD Card được định dạng FAT16 hoặc FAT32 (tìm kiếm trên Google để biết cách)
  • Sao chép code trên và mở bằng Arduino IDE
  • Nhấp vào nút Upload trên Arduino IDE để upload code lên ESP32
  • Xem kết quả trên Serial Monitor.
COM6
Send
SD CARD INITIALIZED. -------------------- Writing log to SD Card Writing log to SD Card Writing log to SD Card Writing log to SD Card Writing log to SD Card Writing log to SD Card Writing log to SD Card
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  
  • Tháo thẻ Micro SD Card khỏi module thẻ Micro SD Card
  • Chèn thẻ Micro SD Card vào USB SD Card reader
  • Kết nối USB SD Card reader với PC
  • Mở file log.txt trên PC của bạn, nó sẽ trông như bên dưới
ESP32 log to micro thẻ SD with time information

Nếu bạn không có USB SD Card reader, bạn có thể kiểm tra nội dung của file log bằng cách chạy Code ESP32 bên dưới.

/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp32/esp32-log-data-with-timestamp-to-sd-card */ #include <SD.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 #define FILE_NAME "/log.txt" File myFile; void setup() { Serial.begin(9600); if (!SD.begin(PIN_SPI_CS)) { while (1) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); delay(1000); } } Serial.println(F("SD CARD INITIALIZED.")); // open file for reading myFile = SD.open(FILE_NAME, FILE_READ); if (myFile) { while (myFile.available()) { char ch = myFile.read(); // read characters one by one from Micro SD Card Serial.print(ch); // print the character to Serial Monitor } myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the file ")); Serial.println(FILE_NAME); } } void loop() { }

ESP32 - Ghi Log Dữ Liệu vào Nhiều File

Việc ghi log vào một file duy nhất sẽ dẫn đến kích thước file lớn theo thời gian và khiến việc kiểm tra trở nên khó khăn. Code bên dưới sẽ ghi log file theo nhiều cách:

  • Một file mỗi ngày
  • Tên file là thông tin ngày: YYYYMMDD.txt
/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp32/esp32-log-data-with-timestamp-to-sd-card */ #include <SD.h> #include <RTClib.h> #define PIN_SPI_CS 5 // The ESP32 pin GPIO5 RTC_DS3231 rtc; File myFile; char filename[] = "/yyyymmdd.txt"; // filename (without extension) should not exceed 8 chars void setup() { Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); // SETUP RTC MODULE if (!rtc.begin()) { while (1) { Serial.println(F("RTC module is NOT found")); delay(1000); } } if (!SD.begin(PIN_SPI_CS)) { while (1) { Serial.println(F("SD CARD FAILED, OR NOT PRESENT!")); delay(1000); } } Serial.println(F("SD CARD INITIALIZED.")); Serial.println(F("--------------------")); } void loop() { DateTime now = rtc.now(); int year = now.year(); int month = now.month(); int day = now.day(); // update filename filename[1] = (year / 1000) + '0'; filename[2] = ((year % 1000) / 100) + '0'; filename[3] = ((year % 100) / 10) + '0'; filename[4] = (year % 10) + '0'; filename[5] = (month / 10) + '0'; filename[6] = (month % 10) + '0'; filename[7] = (day / 10) + '0'; filename[8] = (day % 10) + '0'; // open file for writing myFile = SD.open(filename, FILE_WRITE); if (myFile) { Serial.println(F("Writing log to SD Card")); // write timestamp myFile.print(now.year(), DEC); myFile.print('-'); myFile.print(now.month(), DEC); myFile.print('-'); myFile.print(now.day(), DEC); myFile.print(' '); myFile.print(now.hour(), DEC); myFile.print(':'); myFile.print(now.minute(), DEC); myFile.print(':'); myFile.print(now.second(), DEC); myFile.print(" "); // delimiter between timestamp and data // read data int analog_1 = analogRead(A0); int analog_2 = analogRead(A1); // write data myFile.print("analog_1 = "); myFile.print(analog_1); myFile.print(", "); // delimiter between data myFile.print("analog_2 = "); myFile.print(analog_2); myFile.write("\n"); // new line myFile.close(); } else { Serial.print(F("SD Card: Issue encountered while attempting to open the file ")); Serial.println(filename); } delay(2000); // delay 2 seconds }

Sau khi chạy trong thời gian dài, nếu bạn:

  • Tháo thẻ Micro SD Card khỏi module thẻ Micro SD Card
  • Chèn thẻ Micro SD Card vào USB SD Card reader
  • Kết nối USB SD Card reader với PC
  • Bạn sẽ thấy các file như sau:
ESP32 log to micro thẻ SD multiple files

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.

Bình Luận