Store data in the flash memory of the ESP

Hello and welcome to our today's post.

 

The ESP modules to be very popular. We have shown in the past how easy it is to connect to the Wi-Fi to view web pages, and to switch the Relay.

A very useful feature to store data in the Flash memory of the ESPs, however. So is it possible to upload files, to the we later with our Sketch can access. Or to store information even after a restart still available.

Files on the ESP to upload

, We need the Tool "ESP8266FS" - an extension in the Arduino IDE integrated. It adds in the "tools" menu the entry "ESP8266 Sketch Data Upload".

First download the Tool from the ESP8266 GitHub page:

https://github.com/esp8266/arduino-esp8266fs-plugin/releases/download/0.1.3/ESP8266FS-0.1.3.zip

We will now create a folder with name "tools" in the same folder as our Arduino sketches. Normally, this is located under "C:\Users\<user name>\Documents\Arduino\".

Unzip the downloaded .zip file and copy the folderESP8266FS in the "tools" directory. At the end we have then "C:\Users\<user name>\Documents\Arduino\tools\ESP8266FS\tool\esp8266fs.jar".

Now we start the Arduino IDE, and make a new Sketch. I call ESP-Data-Test in this example,"". This Sketch, we need to save now.

We now navigate into the newly created folder "C:\Users\<user name>\Documents\Arduino\ESP-Data-Test" and create there the folder "data". All files are stored here, will be uploaded in the Flash memory of the ESPs, as soon as we perform the Upload.

I put a new text file with the name test.txt to write a Few lines of Text, and save this in the new "data" folder.

I am using a ESP8266-01S with a USB Adapter, so I tools the following options:

  • Board: "Generic ESP8266 Module"
  • Flash Size: "1M (64K SPIFFS)
  • Upload Speed: "115200"
  • Port: "COMx" - your COM-Port to select

If the memory size of your module do not know, at the end of the article for a tip on how you can determine.

As a Test, we try the empty Sketch (just void setup() and void loop()) to the ESP to load. For my ESP I have to make sure that the programming mode is activated.

If all of this works, we can try to load the contents of the "data" folder on the ESP. For this we go to "tools" -> "ESP8266 Sketch Data Upload".

In the IDE appears to be "SPIFFS Uploading Image..." ... and after a short time appears to be: "SPIFFS Image Uploaded":

 

If at this point an error comes ("SPIFFS Upload failed!"), then, probably because the ESP is no longer in the programming mode. Just the USB connection disconnect and re-connect, and to make sure this is enabled in the programming mode.

SPIFFS in the Sketch to use

For SPIFFS in the Sketch to use, we integrate the library FS.h:

#include "FS.h"

 

Now we have the following commands available:

file system object (SPIFFS)

SPIFFS.begin()

Mounts the SPIFFS Filesystem. Must always be before any other commands are executed. Returns true, if the Mount has to work, otherwise "false".

SPIFFS.format()

Formats (deletes) the file system. Returns true if formatting was successful.

SPIFFS.open(path, mode)

Opens a file. As a "path" to enter the file name including the absolute path (e.g. "/Ordnername/test.txt"). "mode" specifies the type of access. Can the following options are included:"r", "w", "a", "r+", "w+", "a+".

  • "r" Opens the file for reading. The file must exist
  • "w" Creates a new file with no content. If a file with the same name exists, its contents are deleted, and the file as a new empty file is not seen.
  • "a" appends data to the file ("append"). The data will be appended at the end of the file. If the file does not exists, a new one is created.
  • "r+" Opens a file for read and write. The file must exist.
  • "w+" Create an empty file for reading and writing.
  • "a+" Opens a file for reading, as well as to the annexes.
It is a file object (file object) is returned. In order to check whether a file opened successfully could be, it is best used a Boolean Operator:
File f = SPIFFS.open("/test.txt", "w");
if (!f) {
Serial.println("file open failed");
}

SPIFFS.exists(path)

Returns true if the path exists, otherwise "false".

SPIFFS.openDir(path)

Opens the specified directory. Returns a "dir" object.

SPIFFS.remove(path)

Deletes the specified path. A "true" back if the deletion was successful.

SPIFFS.rename(pathFrom, pathTo)

identifies a file of "pathFrom" to "PathTo". The path must be absolute. A "true" returned if the Rename was successful.

 

Filesystem Info structure

FSInfo fs_info;
SPIFFS.info(fs_info);

Fills the FSInfo structure with information about the file system. A "true" return on success, otherwise a false.

Structure:

struct FSInfo {
size_t total number of bytes;
size_t used bytes;
size_t blockSize;
size_t pageSize;
size_t maxOpenFiles;
size_t maxPathLength;
};

 

directory object (You)

, The "Dir" object allows us to files within a directory address by means of the methods "next()", "filename()" and "openFile(mode)".

You dir = SPIFFS.openDir("/data");
while (you.next()) {
Serial.print(dir.fileName());
File f = dir.openFile("r");
Serial.println(Q. size());
}

dir.next() is true as long as it finds files in the directory. It must be in front of "fileName" or "openFile" be called.

"openFile" takes the "mode" Argument, which is the same as in the case of SPIFFS.open.

 

File object (file)

SPIFFS.the open and you.openFile to return a file object. This is a stream object and supports all of the functions such as "read-bytes", "find until", "parseInt", "println", and all the other Stream methods.

There are also some but are for the file object (file) specific.

file.seek(offset, mode),

functions like the fseek function in C. The current Position is shifted depending on the value of "mode" as follows:

    • SeekSet -> Position is "offset" Bytes from the beginning of set
    • SeekCur -> current Position is to "offset" Bytes moved
    • SeekEnd -> Position is "offset" Bytes from the end of the file desetzt
Returns true if the Position could be.

 

file.position()

Gives the Current Position within the file as the value, in Bytes, of back

file.size()

Returns the file size in Bytes again.

file.name();

Returns the file name as a "const char*" again. Can using "String name = file.name();" in a String will be stored.

file.close()

Closes the file.

 

Practical example

Now we will write a small Sketch of the whole test: we want our text file "test.txt" what we are in the beginning created with a Few lines of Text filled on the serial Monitor output.

 

 

#include "FS.h" // Here we bind the required library

void setup() {   Serial.begin(9600); // Serial output enable   delay(1000); // a Short Pause, so that we have time to the Serial Monitor to open.      SPIFFS.begin(); // file-system mounting   File f = SPIFFS.open( "/test.txt", "r"); // file to read open   if (!f) {     Serial.println("file open failed");   }   String data = f.read string(); // contents of the text file is read...   Serial.println("Contents of the open file:");   Serial.println(data); // ... and spent again   f.close(); // We close the file
}
void loop() {
}

 

After uploading, the output appears on the serial monitor:

 

Bonus tip: How big is the flash memory of my ESP 8266?

Depending on which ESP8266 module you are using, the size of the flash memory may differ. You can search for the data sheet using the label on the chip, but to be on the safe side there is a small sketch that checks the size, speed and mode of your module. There is the sketchhttps://github.com/esp8266/Arduino/tree/master/libraries/esp8266/examples/CheckFlashConfig

Simply upload this sketch and set the serial monitor to 115200 baud, and we will be shown the size of the chip.

 

 

 

I hope you enjoyed our blog post today and stimulated your creativity. You can now use the SPIFFS function of your ESP to e.g. Filing HTML text modules, storing sensor data permanently, and much more.

We are very happy about praise, criticism and suggestions for our blog.

Yours Markus Neumann

Esp-8266Basics software

14 comments

Adele

Adele

https://github.com/esp8266/arduino-esp8266fs-plugin/releases/download/0.5.0/ESP8266FS-0.5.0.zip
löst das SPIFFS Error: esptool not found! – Problem

Bernd Albrecht

Bernd Albrecht

@ Tom: Bitte der Anleitung folgen, die Max dankenswerterweise am 30. März hier “geposted” hat.
(Ein Bild mit den Einstellungen habe ich per Email gesendet.)
@ all: Einer unserer Blogger wird das Thema, die Fragen und Anregungen erneut aufgreifen.
Zieldatum für Veröffentlichung Mitte Juli 2020

Tom

Tom

Hallo Zusammen,
Funktioniert das Flashen von SPIFFS beim “D1 Mini NodeMcu mit ESP8266-12F WLAN Modul für Arduino” nicht.
Ich habe das nun genau nach Anleitung gemacht und bleibe leider bei genau dieser Meldung “SPIFFS Upload failed!” hängen. Den D1 Mini muss man ja nicht extra in den Programmiermodus bringen soweit mir das bekannt ist, oder? Auf jeden Fall finde ich keine Lösung um den Fehler zu beseitigen. Im Anschluss habe ich den gesamten LOG aus dem Arduino IDE kopiert. Vielleicht hat jemand noch eine Idee woran es liegen könnte.
-—————————————————————————————————————————————-
esptool.py v2.8
Serial port COM3
Connecting….
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: ec:fa:bc:59:3f:98
Uploading stub…
Running stub…
Stub running…
Changing baud rate to 460800
Changed.
Configuring flash size…
Auto-detected Flash size: 4MB
Compressed 259824 bytes to 190276…

Writing at 0×00000000... (8 %)
Writing at 0×00004000... (16 %)
Writing at 0×00008000... (25 %)
Writing at 0×0000c000… (33 %)
Writing at 0×00010000... (41 %)
Writing at 0×00014000... (50 %)
Writing at 0×00018000... (58 %)
Writing at 0×0001c000… (66 %)
Writing at 0×00020000... (75 %)
Writing at 0×00024000... (83 %)
Writing at 0×00028000... (91 %)
Writing at 0×0002c000… (100 %)
Wrote 259824 bytes (190276 compressed) at 0×00000000 in 4.3 seconds (effective 482.7 kbit/s)…
Hash of data verified.

Leaving…
Hard resetting via RTS pin…
[SPIFFS] data : C:\Users\Thomas Schmid\Documents\Arduino\D1-Mini-WebserverV4b\data
[SPIFFS] size : 2024
[SPIFFS] page : 256
[SPIFFS] block : 8192
/Test.txt

[SPIFFS] upload : C:\Users\THOMAS~1\AppData\Local\Temp\arduino_build_524988/D1-Mini-WebserverV4b.spiffs.bin
[SPIFFS] address : 0×200000
[SPIFFS] reset : —before default_reset —after hard_reset
[SPIFFS] port : COM3
[SPIFFS] speed : 921600
[SPIFFS] python : python.exe
[SPIFFS] uploader : C:\Users\Thomas Schmid\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\tools\upload.py

SPIFFS Upload failed!

Danke im Voraus für Eure Hilfe

Gruß Tom

steve

steve

Hallo,
danke für die sehr guten Anleitungen.
Aber wie könnte man als nächstes die entstandene SPIFFS-Datei “test.txt” per SFTP zu aaaaa.bplaced.net (user: bbbbb; passwort: ccccc) hochladen. Beim Raspi funktionierte es mit curl…. oder ncftp…. .

steve

Max

Max

Wer Probleme mit SPIFFS Error: esptool not found! hat tauscht sein ESP8266FS aus.
ESP8266FS-0.5.0.zip
https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html#uploading-files-to-file-system

Peter

Peter

Hallo, sehr schönes Tool, beim Kopieren des Codes zeigten sich aber doch viele Tipp-Fehler. Nach Korrektur der (meisten) Fehler sieht es so aus:

/*
ESP8266 CheckFlashConfig von Markus Sattler

Diese Skizze testet, ob die EEPROM-Einstellungen der IDE mit der Hardware übereinstimmen

*/

void setup (void)
{
Serial.begin(115200);
}

void loop ()
{

uint32_t realSize = ESP.getFlashChipRealSize(); uint32_t ideSize = ESP.getFlashChipSize(); FlashMode_t ideMode = ESP.getFlashChipMode(); Serial.printf (“Flash-Real-ID:% 08X \n”, ESP.getFlashChipId ()); Serial.printf (“Flash-Realgröße:% u Bytes \n \n”, realSize); Serial.printf (“Flash-Ide-Größe:% u Bytes \n”, ideSize); Serial.printf (“Flash-Ide-Geschwindigkeit:% u Hz \n”, ESP.getFlashChipSpeed()); Serial.printf (“Flash-Ide-Modus:% s \n”, (ideMode == FM_QIO? “QIO”: ideMode == FM_QOUT? “QOUT”: ideMode == FM_DIO? “DIO”: ideMode == FM_DOUT? "DOUT " : “UNBEKANNT”)); if (ideSize != realSize) { Serial.println (“Flash-Chip-Konfiguration falsch! \ N”); } else { Serial.println (“Flash Chip Konfiguration ok. \ N”); } delay(5000);

}

O.Wenzel

O.Wenzel

“Soweit so gut, funktioniert das.
Nur wie kann ich die Datei jetzt wieder Zeilenweise auslesen?
Bzw. wie kann ich den Inhalt der Datei wieder in Variablen einlesen?”
*************************
das würde mich auch brennend interessieren, alle Versuche mit array’s , f.seek(3, fs::SeekEnd); etc. führen zu keinem Ergebnis !

Äd Franzis

Äd Franzis

Lieben Dank, für die Info.

Mal eine Frage: Kann man nur Text-Dateien speichern (also Type char) oder lassen sich auch Messwerte direkt als Integer ablegen. Char ist halt sehr Daten-hungrig.
Ich konnte leider kein entsprechendes Beispiel finden.

/Äd

Matthias

Matthias

Hallo,
die Fehlermeldung:“SPIFFS Error: esptool” lässt einen Fehler mit dem Setup vermuten und hat nichts mit dem hier zur Verfügung gestellten Code und der Erklärung der Funktionsweise zu tun (danke dafür).
Gruß Matthias

Detlef

Detlef

vom September 20, 2019

Hallo,
gibt es denn jetzt eine Lösung für den Fehler
SPIFFS Error: esptool not found!
Ich habe das gleiche Problem aber noch keine Lösung gefunden.
wo ist die Lösung ????

Klaus

Klaus

Hallo,
gibt es denn jetzt eine Lösung für den Fehler
SPIFFS Error: esptool not found!
Ich habe das gleiche Problem aber noch keine Lösung gefunden.
Danke.

Andreas Engelmann

Andreas Engelmann

Hallo Herr Neumann,
Danke für den Beitrag. Das Thema Flash wird bei den meisten Tutorials garnicht behandelt. Auch die Informationen zur Flashgrösse gehen meist unter.
Grade für HTML-Seiten für einen Server auf dem ESP32/8266 ist das prima.
Lieber Torsten, ich habe neulich ein schönes Zitat bei " Microkontroller.net "gelesen:
“Wer schnelle Lösungen sucht sollte nicht programmieren”.
Jedes Proggramm ist eine Herausforderung . Nim sie an und finde die Lösung.
Dann kannst du sagen: “Das habe ich programmiert”.
Viel Spaß beim Basteln.
Andreas

Torsten

Torsten

Wie üblich funktioniert der ganze Kram nur nach tausenden Suchen….
Ich bekomme immer wieder: SPIFFS Error: esptool not found!
Ist und bleibt schlicht Bastelkram,

Christian

Christian

Hallo!

Soweit so gut, funktioniert das.
Nur wie kann ich die Datei jetzt wieder Zeilenweise auslesen?
Bzw. wie kann ich den Inhalt der Datei wieder in Variablen einlesen?

Danke

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN