Re: SPIFFS file read and write example
Posted: Mon May 08, 2017 12:02 am
@gbafamily1 thanks for your reply. Yes I should have tested the sample sketch before posting it. The code is part of a much bigger sketch and yesterday I suddenly found the huge 5+ seconds pause for EACH line after 30kb disappeared.
Serial monitor output for my revised sample sketch (find record number 98 of 100 in 100kb database)
And the revised sketch:
Serial monitor output for my revised sample sketch (find record number 98 of 100 in 100kb database)
Code: Select all
Rebooted
Found record in 4568 ms
Found record in 4606 ms
Found record in 4583 ms
Found record in 4619 ms
Found record in 4592 ms
Found record in 4619 ms
Found record in 4602 ms
Found record in 4630 ms
Found record in 4602 ms
Found record in 4629 ms
Found record in 4603 ms
And the revised sketch:
Code: Select all
/* SearchSPIFFS.ino 6 May 2017
SPIFFS plain text file has 100 lines, each line approx 1kb
After reading 30 lines (30kb) in less than 2 seconds it takes several seconds PER line
Now working OK, approx 4.6s for 100kb over 100 lines
*/
#include "FS.h"
void SearchRecord(){
String line;
unsigned int lineNumber = 0;
unsigned int line2Find = 98;
File MyFile = SPIFFS.open("/SomeData.txt", "r");
if (!MyFile) {
Serial.println(F("File not found"));
}
unsigned long StartTime = millis();
while(MyFile.available()) { // we could open the file, so loop through it to find the record we require
lineNumber++;
//Serial.println(lineNumber); // show line number of SPIFFS file
//Serial.println(line); // show actual line of SPIFFS file
line = MyFile.readStringUntil('\n'); // Read line by line from the file
if(lineNumber == line2Find){
Serial.print(F("Found record in "));
Serial.print(millis() - StartTime);
Serial.println(F(" ms"));
break; // exit while loop once record is found
}
}
MyFile.close(); // is this required for read ?
}
void setup() {
Serial.begin(115200);
Serial.println(F("\nRebooted"));
SPIFFS.begin();
delay(50);
SearchRecord(); // find record in plain text SPIFFS file
}
void loop() {
yield();
//ESP.wdtFeed(); // not required, covered by yield()
delay(6000);
SearchRecord();
}