Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a5935ce

Browse filesBrowse files
luc-githubme-no-dev
authored andcommitted
Add sample for Time date for FatFS like SPIFFS (espressif#3234)
1 parent 96d6975 commit a5935ce
Copy full SHA for a5935ce

File tree

Expand file treeCollapse file tree

1 file changed

+177
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+177
-0
lines changed
+177Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#include "FS.h"
2+
#include "FFat.h"
3+
#include <time.h>
4+
#include <WiFi.h>
5+
6+
const char* ssid = "your-ssid";
7+
const char* password = "your-password";
8+
9+
long timezone = 1;
10+
byte daysavetime = 1;
11+
12+
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
13+
Serial.printf("Listing directory: %s\n", dirname);
14+
15+
File root = fs.open(dirname);
16+
if(!root){
17+
Serial.println("Failed to open directory");
18+
return;
19+
}
20+
if(!root.isDirectory()){
21+
Serial.println("Not a directory");
22+
return;
23+
}
24+
25+
File file = root.openNextFile();
26+
while(file){
27+
if(file.isDirectory()){
28+
Serial.print(" DIR : ");
29+
Serial.print (file.name());
30+
time_t t= file.getLastWrite();
31+
struct tm * tmstruct = localtime(&t);
32+
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);
33+
if(levels){
34+
listDir(fs, file.name(), levels -1);
35+
}
36+
} else {
37+
Serial.print(" FILE: ");
38+
Serial.print(file.name());
39+
Serial.print(" SIZE: ");
40+
Serial.print(file.size());
41+
time_t t= file.getLastWrite();
42+
struct tm * tmstruct = localtime(&t);
43+
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);
44+
}
45+
file = root.openNextFile();
46+
}
47+
}
48+
49+
void createDir(fs::FS &fs, const char * path){
50+
Serial.printf("Creating Dir: %s\n", path);
51+
if(fs.mkdir(path)){
52+
Serial.println("Dir created");
53+
} else {
54+
Serial.println("mkdir failed");
55+
}
56+
}
57+
58+
void removeDir(fs::FS &fs, const char * path){
59+
Serial.printf("Removing Dir: %s\n", path);
60+
if(fs.rmdir(path)){
61+
Serial.println("Dir removed");
62+
} else {
63+
Serial.println("rmdir failed");
64+
}
65+
}
66+
67+
void readFile(fs::FS &fs, const char * path){
68+
Serial.printf("Reading file: %s\n", path);
69+
70+
File file = fs.open(path);
71+
if(!file){
72+
Serial.println("Failed to open file for reading");
73+
return;
74+
}
75+
76+
Serial.print("Read from file: ");
77+
while(file.available()){
78+
Serial.write(file.read());
79+
}
80+
file.close();
81+
}
82+
83+
void writeFile(fs::FS &fs, const char * path, const char * message){
84+
Serial.printf("Writing file: %s\n", path);
85+
86+
File file = fs.open(path, FILE_WRITE);
87+
if(!file){
88+
Serial.println("Failed to open file for writing");
89+
return;
90+
}
91+
if(file.print(message)){
92+
Serial.println("File written");
93+
} else {
94+
Serial.println("Write failed");
95+
}
96+
file.close();
97+
}
98+
99+
void appendFile(fs::FS &fs, const char * path, const char * message){
100+
Serial.printf("Appending to file: %s\n", path);
101+
102+
File file = fs.open(path, FILE_APPEND);
103+
if(!file){
104+
Serial.println("Failed to open file for appending");
105+
return;
106+
}
107+
if(file.print(message)){
108+
Serial.println("Message appended");
109+
} else {
110+
Serial.println("Append failed");
111+
}
112+
file.close();
113+
}
114+
115+
void renameFile(fs::FS &fs, const char * path1, const char * path2){
116+
Serial.printf("Renaming file %s to %s\n", path1, path2);
117+
if (fs.rename(path1, path2)) {
118+
Serial.println("File renamed");
119+
} else {
120+
Serial.println("Rename failed");
121+
}
122+
}
123+
124+
void deleteFile(fs::FS &fs, const char * path){
125+
Serial.printf("Deleting file: %s\n", path);
126+
if(fs.remove(path)){
127+
Serial.println("File deleted");
128+
} else {
129+
Serial.println("Delete failed");
130+
}
131+
}
132+
133+
void setup(){
134+
Serial.begin(115200);
135+
// We start by connecting to a WiFi network
136+
Serial.println();
137+
Serial.println();
138+
Serial.print("Connecting to ");
139+
Serial.println(ssid);
140+
141+
WiFi.begin(ssid, password);
142+
143+
while (WiFi.status() != WL_CONNECTED) {
144+
delay(500);
145+
Serial.print(".");
146+
}
147+
Serial.println("WiFi connected");
148+
Serial.println("IP address: ");
149+
Serial.println(WiFi.localIP());
150+
Serial.println("Contacting Time Server");
151+
configTime(3600*timezone, daysavetime*3600, "time.nist.gov", "0.pool.ntp.org", "1.pool.ntp.org");
152+
struct tm tmstruct ;
153+
delay(2000);
154+
tmstruct.tm_year = 0;
155+
getLocalTime(&tmstruct, 5000);
156+
Serial.printf("\nNow is : %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);
157+
Serial.println("");
158+
159+
if(!FFat.begin(true)){
160+
Serial.println("FFat Mount Failed");
161+
return;
162+
}
163+
164+
listDir(FFat, "/", 0);
165+
removeDir(FFat, "/mydir");
166+
createDir(FFat, "/mydir");
167+
deleteFile(FFat, "/hello.txt");
168+
writeFile(FFat, "/hello.txt", "Hello ");
169+
appendFile(FFat, "/hello.txt", "World!\n");
170+
listDir(FFat, "/", 0);
171+
}
172+
173+
void loop(){
174+
175+
}
176+
177+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.