//Based on example from
// https://circuits4you.com/2018/01/31/example-of-esp8266-flash-file-system-spiffs/
// FLASH MODE: DOUT
/*
* ESP8266 SPIFFS Basic Reading and Writing File
*
*/
#include <ESP8266WiFi.h>
#include <FS.h> //Include File System Headers
#define FILE_SIZE 100000 // change to desired value
#define FILES_NUM 200 // change to desired value
char filename[16] = "/file";
char last_filename[16];
char fileext[5] = ".txt";
char str[11];
int i;
int start;
int sec0;
int sec1;
void setup()
{
delay(1000);
Serial.begin(115200);
delay(1000);
Serial.println();
Serial.println("***************************"); // returns a String containing the core version
Serial.println("*** ESP8266 SPIFFS TEST ***"); // returns a String containing the core version
Serial.println("***************************"); // returns a String containing the core version
Serial.printf("\nESP CHIP ID %08X\n", ESP.getChipId());
Serial.printf("\nFLASH CHIP ID %08X\n\n", ESP.getFlashChipId());
Serial.print("ESP.getCoreVersion= ");
Serial.println(ESP.getCoreVersion()); // returns a String containing the core version
Serial.print("ESP.getSdkVersion= ");
Serial.println(ESP.getSdkVersion()); // returns the SDK version as a char.
Serial.print("ESP.getFreeHeap()="); // returns the free heap size.
Serial.println(ESP.getFreeHeap()); // returns the free heap size.
Serial.printf("ESP.getCpuFreqMHz()="); // returns the CPU frequency in MHz as an unsigned 8-bit integer.
Serial.println(ESP.getCpuFreqMHz()); // returns the CPU frequency in MHz as an unsigned 8-bit integer.
Serial.print("ESP.getSketchSize()="); // returns the size of the current sketch as an unsigned 32-bit integer.
Serial.println(ESP.getSketchSize()); // returns the size of the current sketch as an unsigned 32-bit integer.
Serial.print("ESP.getFreeSketchSpace()="); // returns the free sketch space as an unsigned 32-bit integer.
Serial.println(ESP.getFreeSketchSpace()); // returns the free sketch space as an unsigned 32-bit integer.
Serial.print("ESP.getSketchMD5()="); //returns a lowercase String containing the MD5 of the current sketch.
Serial.println(ESP.getSketchMD5()); //returns a lowercase String containing the MD5 of the current sketch.
Serial.print("ESP.getFlashChipId()="); //returns the flash chip ID as a 32-bit integer.
Serial.print("ESP.getFlashChipSize()="); //returns the flash chip size, in bytes, as seen by the SDK (may be less than actual size).
Serial.println(ESP.getFlashChipSize()); //returns the flash chip size, in bytes, as seen by the SDK (may be less than actual size).
Serial.print("ESP.getFlashChipRealSize()="); //returns the real chip size, in bytes, based on the flash chip ID.
Serial.println(ESP.getFlashChipRealSize()); //returns the real chip size, in bytes, based on the flash chip ID.
Serial.print("ESP.getFlashChipSpeed()="); // returns the flash chip frequency, in Hz.
Serial.println(ESP.getFlashChipSpeed()); // returns the flash chip frequency, in Hz.
Serial.print("ESP.getCycleCount()="); //returns the cpu instruction cycle count since start as an unsigned 32-bit. This is
Serial.println(ESP.getCycleCount()); //returns the cpu instruction cycle count since start as an unsigned 32-bit. This is
//Initialize File System
Serial.println("\n***** Init (Can be up to 30 sec or more...)");
sec0 = millis() / 1000;
if(SPIFFS.begin())
{
Serial.printf("*SPIFFS Initialize....ok. %03d sec\n", (millis() / 1000) - sec0);
}
else
{
Serial.printf("*SPIFFS Initialization...failed. %03d sec\n", (millis() / 1000) - sec0);
}
//Format File System
Serial.println("\n***** Format (Can be long time too...)");
sec0 = millis() / 1000;
if(SPIFFS.format())
{
Serial.printf("*File System Formatted. %03d sec\n", (millis() / 1000) - sec0);
}
else
{
Serial.printf("*File System Formatting Error. %03d sec\n", (millis() / 1000) - sec0);
}
//Create New File And Write Data to It
//w=Write Open file for writing
Serial.println("\n***** Write");
for(int i1 = 1; i1 <= FILES_NUM; i1++)
{
sec0 = millis() / 1000;
snprintf(filename, 10, "/FILE_%03d%s", i1, fileext);
File f = SPIFFS.open(filename, "w");
if (!f) {
Serial.printf("%s open failed. %03d sec\n", filename, (millis() / 1000) - sec0);
}
else
{
//Write data to file
Serial.printf("Writing Data to %s\n", filename);
for(int i2=1; i2 <= (FILE_SIZE / 10); i2++)
{
snprintf(str, 11, "%03d_%05d ", i1, i2);
f.print(str);
}
f.close(); //Close file
Serial.printf("%s. %03d sec\n", str, (millis() / 1000) - sec0);
strcpy(last_filename, filename); }
}
//Read File data
Serial.println("\n***** Read last written file");
File f = SPIFFS.open(last_filename, "r");
if (!f) {
Serial.printf("%s open failed\n", last_filename);
}
else
{
Serial.printf("*Reading Data from %s\n", last_filename);
//Data from file
sec0 = millis() / 1000;
for(i=0;i<f.size();i++) //Read upto complete file size
{
Serial.print((char)f.read());
}
f.close(); //Close file
Serial.printf("\n*%s Closed. %03d sec\n", last_filename, (millis() / 1000) - sec0);
}
Serial.printf("\n*****Finished.\nTotal time: %03d sec\n", (millis() / 1000) - start);
delay(5000);
}
void loop()
{
}