Code: Select all
/*
* Connect the SD card to the following pins:
*
* SD Card | ESP32
* D2 -
* D3 SS
* CMD MOSI
* VSS GND
* VDD 3.3V
* CLK SCK
* VSS GND
* D0 MISO
* D1 -
*/
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
SerialBT.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if(!root){
SerialBT.println("Failed to open directory");
return;
}
if(!root.isDirectory()){
SerialBT.println("Not a directory");
return;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
SerialBT.print(" DIR : ");
SerialBT.println(file.name());
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
SerialBT.print(" FILE: ");
SerialBT.print(file.name());
SerialBT.print(" SIZE: ");
SerialBT.println(file.size());
}
file = root.openNextFile();
}
}
void setup() {
SerialBT.begin("ESP32SD");
pinMode(22, INPUT_PULLUP);
}
void loop() {
if (digitalRead(22) == LOW) {
if(!SD.begin()){
SerialBT.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE){
SerialBT.println("No SD card attached");
return;
}
SerialBT.print("SD Card Type: ");
if(cardType == CARD_MMC){
SerialBT.println("MMC");
} else if(cardType == CARD_SD){
SerialBT.println("SDSC");
} else if(cardType == CARD_SDHC){
SerialBT.println("SDHC");
} else {
SerialBT.println("UNKNOWN");
}
uint64_t cardSize = SD.cardSize() / (1024 * 1024);
SerialBT.printf("SD Card Size: %lluMB\n", cardSize);
listDir(SD, "/", 0);
SerialBT.printf("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
SerialBT.printf("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));
}
}
Thanks!