Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By ESP4eva
#65691 @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)

Code: Select allRebooted
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();
}
User avatar
By Bhashmi
#68910 // Very basic Spiffs example, writing 10 strings to SPIFFS filesystem, and then read them back
// For SPIFFS doc see : https://github.com/esp8266/Arduino/blob ... esystem.md
// Compiled in Arduino 1.6.7. Runs OK on Wemos D1 ESP8266 board.

#include "FS.h"

void setup() {
Serial.begin(9600);
Serial.println("\nVery basic Spiffs example, writing 10 lines to SPIFFS filesystem, and then read them back");
SPIFFS.begin();
// Next lines have to be done ONLY ONCE!!!!!When SPIFFS is formatted ONCE you can comment these lines out!!
Serial.println("Please wait 30 secs for SPIFFS to be formatted");
SPIFFS.format();
Serial.println("Spiffs formatted");
}

void loop() {

// open file for writing
File f = SPIFFS.open("/f.txt", "w");
if (!f) {
Serial.println("file open failed");
}
Serial.println("====== Writing to SPIFFS file =========");
// write 10 strings to file
for (int i=1; i<=10; i++){
f.print("Millis() : ");
f.println(millis());
Serial.println(millis());
}

f.close();

// open file for reading
f = SPIFFS.open("/f.txt", "r");
if (!f) {
Serial.println("file open failed");
} Serial.println("====== Reading from SPIFFS file =======");
// write 10 strings to file
for (int i=1; i<=10; i++){
String s=f.readStringUntil('\n');
Serial.print(i);
Serial.print(":");
Serial.println(s);
}

// wait a few seconds before doing it all over again
delay(3000);

}

where the file is getting saved? not showing in sketch>show sketch folder>data
help needed