ESP32 및 다른 구성 요소에 전원을 공급하는 방법에 대해 잘 알지 못하는 경우, 다음 튜토리얼에서 안내를 찾을 수 있습니다: ESP32 전원 공급 방법.
※ 주의:
이더넷 실드나 마이크로 SD 카드 홀더가 있는 어떤 실드를 사용한다면, 마이크로 SD 카드 모듈을 사용할 필요가 없습니다. 실드에 있는 마이크로 SD 카드 홀더에 마이크로 SD 카드를 삽입하기만 하면 됩니다.
ESP32 - 타임스탬프가 포함된 데이터를 Micro SD 카드에 기록하기
/* * 이 ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/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;voidsetup() {Serial.begin(9600);// SETUP RTC MODULEif (!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("--------------------"));}voidloop() {// open file for writing myFile = SD.open(FILE_NAME, FILE_WRITE);if (myFile) {Serial.println(F("Writing log to SD Card"));// write timestampDateTimenow = 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 dataint 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}
Micro SD 카드가 FAT16 또는 FAT32로 포맷되었는지 확인하세요(Google에서 검색)
위의 코드를 복사하여 Arduino IDE로 엽니다.
Arduino IDE에서 Upload 버튼을 클릭하여 ESP32에 코드를 업로드하세요.
시리얼 모니터에서 결과를 확인하세요.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
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
Ln 11, Col 1
ESP32 Dev Module on COM15
2
마이크로 SD 카드 모듈에서 마이크로 SD 카드를 분리하십시오
마이크로 SD 카드를 USB SD 카드 리더기에 삽입하십시오
USB SD 카드 리더기를 PC에 연결하십시오
PC에서 log.txt 파일을 열면 아래와 같습니다
USB SD 카드 리더기가 없는 경우, 아래의 ESP32 코드를 실행하여 로그 파일의 내용을 확인할 수 있습니다.
/* * 이 ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/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;voidsetup() {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 CardSerial.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); }}voidloop() {}
ESP32 - 여러 파일에 데이터 로깅
로그를 단일 파일에 기록하면 시간이 지남에 따라 파일 크기가 커져서 확인하기 어렵습니다. 아래 코드는 로그 파일을 여러 개로 기록할 것입니다:
하루에 한 파일
파일 이름은 날짜 정보입니다: YYYYMMDD.txt
/* * 이 ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp32/esp32-log-data-with-timestamp-to-sd-card */#include <SD.h>#include <RTClib.h>#define PIN_SPI_CS 5 // The ESP32 pin GPIO5RTC_DS3231 rtc;File myFile;char filename[] = "/yyyymmdd.txt"; // filename (without extension) should not exceed 8 charsvoidsetup() {Serial.begin(9600);// SETUP RTC MODULEif (!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("--------------------"));}voidloop() {DateTimenow = rtc.now();intyear = now.year();intmonth = now.month();intday = 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 dataint 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}
긴 달리기 후에, 만약 당신이:
마이크로 SD 카드를 마이크로 SD 카드 모듈에서 분리하세요.
마이크로 SD 카드를 USB SD 카드 리더기에 삽입하세요.
USB SD 카드 리더기를 PC에 연결하세요.
다음과 같은 파일들을 볼 수 있습니다:
동영상
비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.