| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  | #include "FS.h"
 | 
					
						
							|  |  |  | #include "SPIFFS.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-25 09:44:37 -07:00
										 |  |  | /* You only need to format SPIFFS the first time you run a
 | 
					
						
							|  |  |  |    test or else use the SPIFFS plugin to create a partition | 
					
						
							|  |  |  |    https://github.com/me-no-dev/arduino-esp32fs-plugin */
 | 
					
						
							|  |  |  | #define FORMAT_SPIFFS_IF_FAILED true
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  | void listDir(fs::FS &fs, const char * dirname, uint8_t levels){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |     Serial.printf("Listing directory: %s\r\n", dirname); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     File root = fs.open(dirname); | 
					
						
							|  |  |  |     if(!root){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.println("- failed to open directory"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if(!root.isDirectory()){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.println(" - not a directory"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     File file = root.openNextFile(); | 
					
						
							|  |  |  |     while(file){ | 
					
						
							|  |  |  |         if(file.isDirectory()){ | 
					
						
							|  |  |  |             Serial.print("  DIR : "); | 
					
						
							|  |  |  |             Serial.println(file.name()); | 
					
						
							|  |  |  |             if(levels){ | 
					
						
							| 
									
										
										
										
											2021-04-15 17:25:01 +03:00
										 |  |  |                 listDir(fs, file.path(), levels -1); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             Serial.print("  FILE: "); | 
					
						
							|  |  |  |             Serial.print(file.name()); | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |             Serial.print("\tSIZE: "); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |             Serial.println(file.size()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         file = root.openNextFile(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void readFile(fs::FS &fs, const char * path){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |     Serial.printf("Reading file: %s\r\n", path); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     File file = fs.open(path); | 
					
						
							| 
									
										
										
										
											2017-10-01 10:27:04 +08:00
										 |  |  |     if(!file || file.isDirectory()){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.println("- failed to open file for reading"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |     Serial.println("- read from file:"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     while(file.available()){ | 
					
						
							|  |  |  |         Serial.write(file.read()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-20 07:47:40 -07:00
										 |  |  |     file.close(); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void writeFile(fs::FS &fs, const char * path, const char * message){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |     Serial.printf("Writing file: %s\r\n", path); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     File file = fs.open(path, FILE_WRITE); | 
					
						
							|  |  |  |     if(!file){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.println("- failed to open file for writing"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if(file.print(message)){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.println("- file written"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2019-10-06 16:29:28 +02:00
										 |  |  |         Serial.println("- write failed"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-20 07:47:40 -07:00
										 |  |  |     file.close(); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void appendFile(fs::FS &fs, const char * path, const char * message){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |     Serial.printf("Appending to file: %s\r\n", path); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     File file = fs.open(path, FILE_APPEND); | 
					
						
							|  |  |  |     if(!file){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.println("- failed to open file for appending"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if(file.print(message)){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.println("- message appended"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.println("- append failed"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-20 07:47:40 -07:00
										 |  |  |     file.close(); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void renameFile(fs::FS &fs, const char * path1, const char * path2){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |     Serial.printf("Renaming file %s to %s\r\n", path1, path2); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     if (fs.rename(path1, path2)) { | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.println("- file renamed"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.println("- rename failed"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void deleteFile(fs::FS &fs, const char * path){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |     Serial.printf("Deleting file: %s\r\n", path); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     if(fs.remove(path)){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.println("- file deleted"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.println("- delete failed"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void testFileIO(fs::FS &fs, const char * path){ | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |     Serial.printf("Testing file I/O with %s\r\n", path); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     static uint8_t buf[512]; | 
					
						
							|  |  |  |     size_t len = 0; | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |     File file = fs.open(path, FILE_WRITE); | 
					
						
							|  |  |  |     if(!file){ | 
					
						
							|  |  |  |         Serial.println("- failed to open file for writing"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     size_t i; | 
					
						
							|  |  |  |     Serial.print("- writing" ); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     uint32_t start = millis(); | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |     for(i=0; i<2048; i++){ | 
					
						
							|  |  |  |         if ((i & 0x001F) == 0x001F){ | 
					
						
							|  |  |  |           Serial.print("."); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         file.write(buf, 512); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Serial.println(""); | 
					
						
							|  |  |  |     uint32_t end = millis() - start; | 
					
						
							|  |  |  |     Serial.printf(" - %u bytes written in %u ms\r\n", 2048 * 512, end); | 
					
						
							|  |  |  |     file.close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     file = fs.open(path); | 
					
						
							|  |  |  |     start = millis(); | 
					
						
							|  |  |  |     end = start; | 
					
						
							|  |  |  |     i = 0; | 
					
						
							| 
									
										
										
										
											2017-10-01 10:27:04 +08:00
										 |  |  |     if(file && !file.isDirectory()){ | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |         len = file.size(); | 
					
						
							|  |  |  |         size_t flen = len; | 
					
						
							|  |  |  |         start = millis(); | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.print("- reading" ); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |         while(len){ | 
					
						
							|  |  |  |             size_t toRead = len; | 
					
						
							|  |  |  |             if(toRead > 512){ | 
					
						
							|  |  |  |                 toRead = 512; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             file.read(buf, toRead); | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |             if ((i++ & 0x001F) == 0x001F){ | 
					
						
							|  |  |  |               Serial.print("."); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |             len -= toRead; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.println(""); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |         end = millis() - start; | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.printf("- %u bytes read in %u ms\r\n", flen, end); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |         file.close(); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |         Serial.println("- failed to open file for reading"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void setup(){ | 
					
						
							|  |  |  |     Serial.begin(115200); | 
					
						
							| 
									
										
										
										
											2018-07-25 09:44:37 -07:00
										 |  |  |     if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){ | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |         Serial.println("SPIFFS Mount Failed"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     listDir(SPIFFS, "/", 0); | 
					
						
							|  |  |  |     writeFile(SPIFFS, "/hello.txt", "Hello "); | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |     appendFile(SPIFFS, "/hello.txt", "World!\r\n"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     readFile(SPIFFS, "/hello.txt"); | 
					
						
							|  |  |  |     renameFile(SPIFFS, "/hello.txt", "/foo.txt"); | 
					
						
							|  |  |  |     readFile(SPIFFS, "/foo.txt"); | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |     deleteFile(SPIFFS, "/foo.txt"); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  |     testFileIO(SPIFFS, "/test.txt"); | 
					
						
							| 
									
										
										
										
											2018-03-04 14:57:45 -05:00
										 |  |  |     deleteFile(SPIFFS, "/test.txt"); | 
					
						
							|  |  |  |     Serial.println( "Test complete" ); | 
					
						
							| 
									
										
										
										
											2017-09-12 11:09:59 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void loop(){ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |