Arduino Sketch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | // Very basic Spiffs example, writing 10 strings to SPIFFS filesystem, and then read them back // For SPIFFS doc see : https://github.com/esp8266/Arduino/blob/master/doc/filesystem.md #include <FS.h> // This one must be on top before all the other includes. File f; void setup() { Serial.begin(115200); Serial.println("\nVery basic Spiffs example, writing 10 lines to SPIFFS filesystem, and then read them back"); Serial.print("Don't run the format (SPIFFS.format()) and write (f.print / f.println) command in a loop "); Serial.println("as the flash memory has limited write cycle!!"); SPIFFS.begin(); // Check to see if the file exists f = SPIFFS.open("/f.txt", "r"); if (!f) { Serial.println("File open failed or file doesn't exist.."); Serial.println("Please wait for SPIFFS disk to be formatted"); SPIFFS.format(); Serial.println("SPIFFS disk formatted."); } // Open file for writing f = SPIFFS.open("/f.txt", "w"); if (!f) { Serial.println("File open failed. Unable to write to SPIFFS disk."); } else { Serial.println("====== Writing to SPIFFS Disk ========="); // write 10 strings to file for (int i = 1; i <= 10; i++) { f.print("Millis() : "); f.println(millis()); Serial.println(millis()); delay(500); } } f.close(); // Open file for reading f = SPIFFS.open("/f.txt", "r"); if (!f) { Serial.println("File open failed."); } else { Serial.println("====== Reading from SPIFFS file ======="); // read 10 strings from file for (int i = 1; i <= 10; i++){ String s = f.readStringUntil('\n'); Serial.print(i); Serial.print(":"); Serial.println(s); } } f.close(); } void loop() { // Do nothing.. } |
The Result
1st run
2nd run
References:
SPIFFS FILE READ AND WRITE EXAMPLE
http://www.esp8266.com/viewtopic.php?f=29&t=8194
ESP8266/Arduino: Playing around with the upcoming filesystem featur
https://blog.squix.org/2015/08/esp8266arduino-playing-around-with.html
No comments:
Post a Comment