Advertisement
MrLunk

NodeMCU_ESP8266_SPIFF_file_system_basics

Jan 13th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.66 KB | None | 0 0
  1. // NodeMCU SPI Flash File System (#SPIFFS) module example
  2. // Writing 10 strings to SPIFFS, and reading back afterwards.
  3.  
  4. #include "FS.h"
  5.  
  6. // The Amount of data to write and read from SPIFFS
  7. int writenumber = 25;
  8.  
  9. void setup() {
  10.   Serial.begin(9600);
  11.   Serial.println("\nNodeMCU SPI Flash File System (#SPIFFS) module example. Writing 10 strings to SPIFFS, and reading back afterwards.");
  12.   SPIFFS.begin();
  13.  
  14.   // Next lines have to be done ONLY ONCE!!!!!
  15.   // Unquote them for the 1st run !!!!
  16.   // When SPIFFS is formatted ONCE you can comment these lines out!!
  17.   // START unquoting below this line.....
  18.  
  19.   // Serial.println("Please wait 30 secs for SPIFFS to be formatted");
  20.   // SPIFFS.format();
  21.   // Serial.println("Spiffs formatted");
  22.  
  23.   // END unquoting below this line.....
  24.  
  25.  
  26.   Serial.println("Started...");
  27.   Serial.println();
  28. }
  29.  
  30. void loop() {
  31.  
  32.   // open file for writing
  33.   File f = SPIFFS.open("/f.txt", "w");
  34.   if (!f) {
  35.       Serial.println("file open failed");
  36.   }
  37.   Serial.println("====== Writing to SPIFFS file =========");
  38.   // write 10 strings to file
  39.   for (int i=1; i<=writenumber; i++){
  40.     f.print("Millis() : ");
  41.     f.println(millis());
  42.     Serial.println(millis());
  43.   }
  44.  
  45.   f.close();
  46.  
  47.   // open file for reading
  48.   f = SPIFFS.open("/f.txt", "r");
  49.   if (!f) {
  50.       Serial.println("file open failed");
  51.   }  Serial.println("====== Reading from SPIFFS file =======");
  52.   // read 10 strings from file
  53.   for (int i=1; i<=writenumber; i++){
  54.     String s=f.readStringUntil('\n');
  55.     Serial.print(i);
  56.     Serial.print(":");
  57.     Serial.println(s);
  58.   }
  59.  
  60.   // wait a few seconds before doing it all over again
  61.   delay(10000);
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement