All the ESP8266 boards I've seen come with on-board flash, which is persistant storage you can access for both reading and writing.
Although the flash-memory isn't rated for a huge number of writes it's enormously useful, especially as a way of serving content over HTTP.
We'll cover programatically serving content in the next section, but the most typical use-case is reading content from flash and sending it to clients over HTTP.
Install the arduino-esp8266fs-plugin, according to the instructions.
Once you've done that you'll find that you can create a
data/
directory inside your sketch folder. The contents of that folder will be uploaded to your flash-storage when you choose the menu item:
Tools | ESP8266 Sketch Data Upload
A sample tree might look like this
d1-http-server/ ├── d1-http-server.ino └── data ├── index.html └── rss.jpg
Include the appropriate header in your sketch, and initialize the library in your setup method:
#include <FS.h> void setup() { Serial.begin(115200); SPIFFS.begin(); .. }Once you've done that you can open files as you'd expect:
File f = SPIFFS.open("/f.txt", "w"); if (f) { f.println("Here is some content" ); f.close(); }Or for reading:
// this opens the file "test.txt" in read-mode File f = SPIFFS.open("/test.txt", "r"); while(f.available()) { //Lets read line by line from the file String line = f.readStringUntil('\n'); Serial.println(line); } f.close();You can also read the names of files which are present:
Dir dir = SPIFFS.openDir("/data"); while (dir.next()) { Serial.print(dir.fileName()); File f = dir.openFile("r"); Serial.println(f.size()); }
Assuming you've added some content, as per the description above, you can now serve that content via a simple program such as this one: