这一篇博文讲述ESP32_CAM自带的那个SD卡座的使用方法以及ESP32连接网络后获取目前时间的方法
SD卡座支持一般的SD小卡,而且有大小限制,我记得限制是8GB以下还是多少,不过一般我们也不可能用得到那么大的SD卡给ESP32用,我买的一批2GB的卡才5块钱一张,下面的代码里面罗列了包括例程以及网上的资料在内的SD卡操作方法 (包括 路径的创建删除列出,文件的写入读取重命名以及添加删除)
而时间获取在我们的使用中也相当常见,上一篇博文我说了要更ESP32_CAM拍摄图片后存储到SD卡的操作,而我们将要把这个时间作为这个图片的文件名来存储到SD卡当中。
话不多说上代码大家自己品,wifi用户名以及密码改成自己的
#include "FS.h"
#include "SD_MMC.h"
#include "SPI.h"
#include
#include
#include "cJSON.h"
#define ssid "b123"
#define password "cx123456"
const char* ntpServer = "cn.pool.ntp.org";
const long gmtOffset_sec = 8*60*60;
const int daylightOffset_sec = 0*60*60;
struct tm timeinfo;
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing directory: %s\n", dirname);
File root = fs.open(dirname);
if(!root){
Serial.println("Failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println("Not a directory");
return;
}
File file = root.openNextFile();
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.print (file.name());
time_t t= file.getLastWrite();
struct tm * tmstruct = localtime(&t);
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
if(levels){
listDir(fs, file.name(), levels -1);
}
} else {
Serial.print(" FILE: ");
Serial.print(file.name());
Serial.print(" SIZE: ");
Serial.print(file.size());
time_t t= file.getLastWrite();
struct tm * tmstruct = localtime(&t);
Serial.printf(" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n",(tmstruct->tm_year)+1900,( tmstruct->tm_mon)+1, tmstruct->tm_mday,tmstruct->tm_hour , tmstruct->tm_min, tmstruct->tm_sec);
}
file = root.openNextFile();
}
}
void createDir(fs::FS &fs, const char * path){
Serial.printf("Creating Dir: %s\n", path);
if(fs.mkdir(path)){
Serial.println("Dir created");
} else {
Serial.println("mkdir failed");
}
}
void removeDir(fs::FS &fs, const char * path){
Serial.printf("Removing Dir: %s\n", path);
if(fs.rmdir(path)){
Serial.println("Dir removed");
} else {
Serial.println("rmdir failed");
}
}
void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if(!file){
Serial.println("Failed to open file for reading");
return;
}
Serial.print("Read from file: ");
while(file.available()){
Serial.write(file.read());
}
file.close();
}
void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("Failed to open file for writing");
return;
}
if(file.print(message)){
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}
void appendFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)){
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
void renameFile(fs::FS &fs, const char * path1, const char * path2){
Serial.printf("Renaming file %s to %s\n", path1, path2);
if (fs.rename(path1, path2)) {
Serial.println("File renamed");
} else {
Serial.println("Rename failed");
}
}
void deleteFile(fs::FS &fs, const char * path){
Serial.printf("Deleting file: %s\n", path);
if(fs.remove(path)){
Serial.println("File deleted");
} else {
Serial.println("Delete failed");
}
}
String translateEncryptionType(wifi_auth_mode_t encryptionType) {
switch (encryptionType){
case (WIFI_AUTH_OPEN):
return "Open";
case (WIFI_AUTH_WEP):
return "WEP";
case (WIFI_AUTH_WPA_PSK):
return "WPA_PSK";
case (WIFI_AUTH_WPA2_PSK):
return "WPA2_PSK";
case (WIFI_AUTH_WPA_WPA2_PSK):
return "WPA_WPA2_PSK";
case (WIFI_AUTH_WPA2_ENTERPRISE):
return "WPA2_ENTERPRISE";
}
}
void scanNetworks(){
int numberOfNetworks= WiFi.scanNetworks();
Serial.print("Numberof networks found: ");
Serial.println(numberOfNetworks);
for (int i = 0; i< numberOfNetworks; i++) {
Serial.print("Networkname: ");
Serial.println(WiFi.SSID(i));
Serial.print("Signalstrength: ");
Serial.println(WiFi.RSSI(i));
Serial.print("MACaddress: ");
Serial.println(WiFi.BSSIDstr(i));
Serial.print("Encryptiontype: ");
String encryptionTypeDescription = translateEncryptionType(WiFi.encryptionType(i));
Serial.println(encryptionTypeDescription);
Serial.println("-----------------------");
}
}
void connectToNetwork(){
WiFi.begin(ssid,password);
while (WiFi.status()!= WL_CONNECTED) {
delay(1000);
Serial.println("try to connecting ...");
}
Serial.println("Connectedto network");
}
void wifi_connect(){
scanNetworks();
connectToNetwork();
Serial.println(WiFi.macAddress());
Serial.println(WiFi.localIP());
Serial.println("wifi连接成功");
}
void setup() {
int i = 3;
Serial.begin(115200);
Serial.println("尝试进行WIFI连接!!!");
wifi_connect();
while(i--)
{
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
delay(5000);
if(!getLocalTime(&timeinfo))
Serial.println("Failed to obtain time");
Serial.print("现在时间是:");
Serial.print("年份是:");
Serial.println(timeinfo.tm_year + 1900);
Serial.print("月份是:");
Serial.println(timeinfo.tm_mon + 1);
Serial.print("号数是:");
Serial.println(timeinfo.tm_mday);
Serial.print("小时数是:");
Serial.println(timeinfo.tm_hour );
Serial.print("分钟数是:");
Serial.println(timeinfo.tm_min);
Serial.print("分钟数是:");
Serial.println(timeinfo.tm_sec);
}
if(!SD_MMC.begin()){
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD_MMC.cardType();
if(cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
}
Serial.print("SD Card Type: ");
if(cardType == CARD_MMC){
Serial.println("MMC");
}
else if(cardType == CARD_SD){ Serial.println("SDSC"); }
else if(cardType == CARD_SDHC){ Serial.println("SDHC"); }
else { Serial.println("UNKNOWN"); }
uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024);
Serial.printf("SD 卡容量大小: %lluMB\n", cardSize);
Serial.println("现在列出目前SD卡内部根目录下所有的路径:");
listDir(SD_MMC, "/", 0);
Serial.println("现在在SD卡内部根目录下创建一个名为demo的路径:");
createDir(SD_MMC, "/demo");
Serial.println("现在列出目前SD卡更新后根目录下所有的路径:");
listDir(SD_MMC, "/", 0);
Serial.println("现在删除之前我们创建的名为demo的路径:");
removeDir(SD_MMC, "/demo");
Serial.println("现在列出目前SD卡删除demo路径后根目录下所有的路径:");
listDir(SD_MMC, "/", 0);
createDir(SD_MMC, "/demo");
writeFile(SD_MMC, "/demo/demo.txt", "Hello ");
appendFile(SD_MMC, "/demo/demo.txt", "World!\n");
readFile(SD_MMC, "/demo/demo.txt");
}
void loop() {
}