ESP32 - Gegevens Loggen met Tijdstempel naar Micro SD Kaart

In deze gids verkennen we het proces van het loggen van gegevens met tijdstempels naar een Micro SD Kaart met behulp van ESP32. We behandelen specifiek de volgende onderwerpen:

De tijdsinformatie wordt gehaald uit een RTC-module (Real-Time Clock) en wordt vervolgens samen met de verzamelde gegevens op de Micro SD Kaart opgeslagen.

De op de Micro SD Kaart gelogde gegevens kunnen uiteenlopende informatie bevatten, zoals:

Voor de eenvoud zal deze tutorial de procedure demonstreren door waarden te lezen vanaf twee analoge pinnen, die dienen als voorbeeldgegevens. De meegeleverde code kan eenvoudig worden aangepast om verschillende soorten data te verwerken.

ESP32 Log naar Micro SD Kaart

Hardware Benodigd

1×ESP32 ESP-WROOM-32 Ontwikkelingsmodule
1×USB Kabel Type-C
1×Micro SD Kaart
1×Micro SD Kaart Module
1×USB 3.0 SD Kaart Lezer
1×DS3231 Realtime Klok (RTC)-Module
1×CR2032 batterij
1×Jumper Draden
1×(Aanbevolen) Schroefklem Uitbreidingsboard voor ESP32
1×(Aanbevolen) Breakout Expansion Board for ESP32
1×(Aanbevolen) Stromsplitter voor ESP32

Of u kunt de volgende kits kopen:

1×DIYables Sensorkit (30 sensoren/displays)
1×DIYables Sensorkit (18 sensoren/displays)
Openbaarmaking: Sommige van de links in deze sectie zijn Amazon-affiliate links. We kunnen een commissie ontvangen voor aankopen die via deze links worden gedaan, zonder extra kosten voor u. We waarderen uw steun.

Over Micro SD Kaart Module en RTC Module

Bent u niet bekend met de Micro SD Kaart Module en RTC-module, inclusief hun pinout, functionaliteit en programmering? Verken de uitgebreide tutorials over deze onderwerpen hieronder:

Aansluitschema

ESP32 Micro SD Kaart Module Aansluitschema

This image is created using Fritzing. Click to enlarge image

※ Notiz:

Als u een Ethernet shield of een ander shield gebruikt met een Micro SD Kaart houder, hoeft u de Micro SD Kaart Module niet te gebruiken. U hoeft alleen de Micro SD Kaart in de houder van het shield te plaatsen.

ESP32 - Gegevens Loggen met Tijdstempel naar Micro SD Kaart

/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/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 }

Snelle Stappen

  • Als u ESP32 voor het eerst gebruikt, bekijk dan hoe u de omgeving instelt voor ESP32 in Arduino IDE.
  • Maak de verbindingen zoals op bovenstaande afbeelding.
  • Verbind de ESP32 board met uw pc via een micro USB-kabel.
  • Open Arduino IDE op uw pc.
  • Selecteer het juiste ESP32 board (bijvoorbeeld ESP32 Dev Module) en de juiste COM-poort.
  • Zorg ervoor dat de Micro SD Kaart geformatteerd is als FAT16 of FAT32 (zoek dit eventueel op via Google)
  • Kopieer bovenstaande code en open deze met Arduino IDE
  • Klik op de knop Upload in Arduino IDE om de code naar de ESP32 te uploaden
  • Bekijk het resultaat op de 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  
  • Verwijder de Micro SD Kaart uit de Micro SD Kaart module
  • Plaats de Micro SD Kaart in een USB SD Kaart lezer
  • Verbind de USB SD Kaart lezer met uw pc
  • Open het log.txt bestand op uw pc, het ziet eruit zoals hieronder
ESP32 log naar Micro SD Kaart met tijdsinformatie

Als u geen USB SD Kaart lezer heeft, kunt u de inhoud van het logbestand controleren door onderstaande ESP32 code te draaien.

/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/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 - Gegevens Loggen in Meerdere Bestanden

Loggen naar één enkel bestand leidt na verloop van tijd tot een groot bestand en maakt het moeilijk om te controleren. De onderstaande code schrijft logbestanden in meerdere bestanden:

  • Eén bestand per dag
  • De bestandsnaam is de datum in het formaat YYYYMMDD.txt
/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/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 }

Na een lange periode, als u:

  • De Micro SD Kaart uit de Micro SD Kaart module haalt
  • De Micro SD Kaart in een USB SD Kaart lezer plaatst
  • De USB SD Kaart lezer aansluit op de pc
  • Ziet u de bestanden als volgt:
ESP32 log naar Micro SD Kaart meerdere bestanden

Video Tutorial

We overwegen het maken van videotutorials. Als u videotutorials belangrijk vindt, abonneer u dan op ons YouTube-kanaal om ons te motiveren de video's te maken.

※ ONZE BERICHTEN

  • U bent welkom om de link naar deze tutorial te delen. Gebruik onze inhoud echter niet op andere websites. We hebben veel moeite en tijd gestoken in het maken van de inhoud, respecteer alstublieft ons werk!