Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NodeMCU SPI Flash File System (#SPIFFS) module example
- // Writing 10 strings to SPIFFS, and reading back afterwards.
- #include "FS.h"
- // The Amount of data to write and read from SPIFFS
- int writenumber = 25;
- void setup() {
- Serial.begin(9600);
- Serial.println("\nNodeMCU SPI Flash File System (#SPIFFS) module example. Writing 10 strings to SPIFFS, and reading back afterwards.");
- SPIFFS.begin();
- // Next lines have to be done ONLY ONCE!!!!!
- // Unquote them for the 1st run !!!!
- // When SPIFFS is formatted ONCE you can comment these lines out!!
- // START unquoting below this line.....
- // Serial.println("Please wait 30 secs for SPIFFS to be formatted");
- // SPIFFS.format();
- // Serial.println("Spiffs formatted");
- // END unquoting below this line.....
- Serial.println("Started...");
- Serial.println();
- }
- void loop() {
- // open file for writing
- File f = SPIFFS.open("/f.txt", "w");
- if (!f) {
- Serial.println("file open failed");
- }
- Serial.println("====== Writing to SPIFFS file =========");
- // write 10 strings to file
- for (int i=1; i<=writenumber; i++){
- f.print("Millis() : ");
- f.println(millis());
- Serial.println(millis());
- }
- f.close();
- // open file for reading
- f = SPIFFS.open("/f.txt", "r");
- if (!f) {
- Serial.println("file open failed");
- } Serial.println("====== Reading from SPIFFS file =======");
- // read 10 strings from file
- for (int i=1; i<=writenumber; i++){
- String s=f.readStringUntil('\n');
- Serial.print(i);
- Serial.print(":");
- Serial.println(s);
- }
- // wait a few seconds before doing it all over again
- delay(10000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement