Advertisement
KenFalco

Read-Write-SPIFFS

Aug 17th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. /*
  2.   Test di scrittura e lettura file tramite SPIFFS su ESP8266
  3. */
  4.  
  5. #include <FS.h>
  6.  
  7. void writeFile();
  8. void readFile();
  9.  
  10. void setup() {
  11.   Serial.begin(115200);
  12.  
  13.   SPIFFS.begin();
  14.   SPIFFS.format();
  15.  
  16.   Serial.print("Formattazione completata.\n");
  17.  
  18.   writeFile();
  19.  
  20.   readFile();
  21.  
  22. }
  23.  
  24. void loop() {
  25.   delay(5000);
  26. }
  27.  
  28. void writeFile() {
  29.   File f = SPIFFS.open("/text.txt", "w");
  30.  
  31.   if (!f) {
  32.     Serial.print("Impossibile aprire il file.\n");
  33.     return;
  34.   }
  35.  
  36.   for (uint8_t i = 0; i < 20; ++i) {
  37.     f.print("millis value = ");
  38.     f.print(millis());
  39.     f.print("\n");
  40.     delay(1);
  41.   }
  42.  
  43.   f.close();
  44. }
  45.  
  46. void readFile() {
  47.   File f = SPIFFS.open("/text.txt", "r");
  48.  
  49.   if (!f) {
  50.     Serial.print("Impossibile aprire il file.\n");
  51.     return;
  52.   }
  53.  
  54.   String str = "";
  55.   for (uint8_t i = 0; i < 20; ++i) {
  56.     str += f.readString();
  57.   }
  58.  
  59.   Serial.print("\n\n\n-----Lettura file-----\n\n\n");
  60.  
  61.   Serial.print("Contenuto del file:\n\n");
  62.   Serial.print(str);
  63.  
  64.   f.close();
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement