LITTLEFS_time.ino 6.3 KB

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