Advertisement
RuiViana

Lista_SPIFFS

May 5th, 2018
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. /*
  2.   Lista arquivos do SPISSF
  3. */
  4. #include "FS.h"
  5. String buf;
  6. //----------------------------------------------------
  7. void formatFS()
  8. {
  9.   SPIFFS.format();
  10. }
  11. //----------------------------------------------------
  12. void createFile()
  13. {
  14.   File wFile;
  15.   if (SPIFFS.exists("/index.html"))   // Cria o arquivo se ele não existir
  16.   {
  17.     Serial.println("Arquivo ja existe!");
  18.   }
  19.   else
  20.   {
  21.     Serial.println("Criando o arquivo...");
  22.     wFile = SPIFFS.open("/index.html", "w+");
  23.  
  24.     //Verifica a criação do arquivo
  25.     if (!wFile)
  26.     {
  27.       Serial.println("Erro ao criar arquivo!");
  28.     }
  29.     else
  30.     {
  31.       Serial.println("Arquivo criado com sucesso!");
  32.     }
  33.   }
  34.   wFile.close();
  35. }
  36. //----------------------------------------------------
  37. void deleteFile()         // Remove o arquivo
  38. {
  39.   if (SPIFFS.remove("/index.html"))
  40.   {
  41.     Serial.println("Erro ao remover arquivo!");
  42.   }
  43.   else
  44.   {
  45.     Serial.println("Arquivo removido com sucesso!");
  46.   }
  47. }
  48. //----------------------------------------------------
  49. void writeFile(String msg)      // Abre o arquivo para adição (append)
  50. {
  51.   //Inclue sempre a escrita na ultima linha do arquivo
  52.   File rFile = SPIFFS.open("/index.html", "a+");
  53.  
  54.   if (!rFile)
  55.   {
  56.     Serial.println("Erro ao abrir arquivo!");
  57.   }
  58.   else
  59.   {
  60.     rFile.println("Log: " + msg);
  61.     Serial.println(msg);
  62.   }
  63.   rFile.close();
  64. }
  65. //----------------------------------------------------
  66. void readFile()       // Faz a leitura do arquivo
  67. {
  68.   File rFile = SPIFFS.open("/index.html", "r");
  69.   Serial.println("Reading file...");
  70.   while (rFile.available())
  71.   {
  72.     String line = rFile.readStringUntil('\n');
  73.     buf += line;
  74.     buf += "<br />";
  75.   }
  76.   rFile.close();
  77. }
  78. //----------------------------------------------------
  79. void closeFS()
  80. {
  81.   SPIFFS.end();
  82. }
  83. //----------------------------------------------------
  84. void openFS()       // Abre o sistema de arquivos
  85. {
  86.   if (!SPIFFS.begin())
  87.   {
  88.     Serial.println("Erro ao abrir o sistema de arquivos");
  89.   }
  90.   else
  91.   {
  92.     Serial.println("Sistema de arquivos aberto com sucesso!");
  93.   }
  94. }
  95. //----------------------------------------------------
  96. void setup()
  97. {
  98.   Serial.begin(115200);                       // Configura a porta serial para 115200bps
  99.   openFS();                                   // Abre o sistema de arquivos (mount)
  100.   Dir dir = SPIFFS.openDir("");               // Lista arquivos
  101.   Serial.println(" filename ext  size ");
  102.  
  103.   while (dir.next())
  104.   {
  105.     Serial.print(dir.fileName());
  106.     Serial.print("     ");
  107.     File f = dir.openFile("r");
  108.     Serial.println(f.size());
  109.   }
  110.   closeFS();
  111.  
  112.   // formatFS()               // Formata o SPIFFS
  113.  
  114.   // createFile()             // Cria o arquivo caso o mesmo não exista
  115.  
  116.   // deleteFile()             // Deletar aquivo
  117.  
  118.   // writeFile(String msg)    // Gravar no arquivo
  119.  
  120.   // readFile()               // Ler arquivo
  121. }
  122. //----------------------------------------------------
  123. void loop() {
  124.   ;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement