Reconfigurable computing
Small Embedded Systems
Unit 5.3
Using a File System with SPIFFS
For more serious web sites, putting the entire HTML code inside a program is unrealistic
The Huzzah board has 4Mbyte of flash
We can form a file system within the flash using SPIFFS
SPI Flash File System
To access SPIFFS, we need the ESP8266FS tool available at https://github.com/esp8266/arduino-esp8266fs-plugin/releases
Download ESP8266FS-0.5.0.zip and unzip it
Copy ESP8266FS into the tools subfolder of the Arduino folder
Using a File System on the Huzzah
Create a file index.html
We will upload this into the file system of the Huzzah
The file will be served out in response to browser requests
A File System Example
A Small Hello
Hello
This is a being read
from a file.
The file index.html needs to be put into a folder called “data” in the root folder of your sketch
Use the Tools menu to upload the contents of the “data” directory into the ESP8266
Uploading the Files
Code Listing for setup() and loop()
#include
#include
#include
#include
#include
ESP8266WebServer server(80);
void setup() {
WiFi.begin(“SSID”, “PASSWORD”);
while (WiFi.status() != WL_CONNECTED) {delay(1000);}
MDNS.begin(“ESP8266”);
SPIFFS.begin();
server.onNotFound(handleNotFound);
server.begin();
}
void loop(void) {
server.handleClient();
}
Your real credentials go here
Handler function for requests
Include the SPIFFS library
We don’t want to hard code the names of the files in our website, so we give no request handler except
server.onNotFound(handleNotFound)
This will match everything not already caught by another handler
It will either find the file and serve it out,
or return a 404 Not Found error page
Websites default to reading a file called index.html if no other file name is given
The Default Handler
Coding the Handler for File Requests
bool handleFileRead(String path) {
if (path.endsWith(“/”)) path += “index.html”;
if (SPIFFS.exists(path)) {
File file = SPIFFS.open(path, “r”);
server.streamFile(file, “text/html”);
file.close();
return true;
}
return false;
}
void handleNotFound(){
if (!handleFileRead(server.uri()))
server.send(404, “text/plain”, “404: Not Found”);
}
Default file
Send the file contents
File not found
Coding the Handler for File Requests
bool handleFileRead(String path) {
if (path.endsWith(“/”)) path += “index.html”;
if (SPIFFS.exists(path)) {
File file = SPIFFS.open(path, “r”);
server.streamFile(file, “text/html”);
file.close();
return true;
}
return false;
}
void handleNotFound(){
if (!handleFileRead(server.uri()))
server.send(404, “text/plain”, “404: Not Found”);
}
Default file
Summary
ESP8266 has large flash memory
It can be used for file storage
Useful for Web pages that are to be served out
/docProps/thumbnail.jpeg