How to use SPIFFS for ESP32 with Platform.IO

One of my pet projects is a controllable power supply based on the ESP32 with a web interface (GitHub repository: https://github.com/vshcryabets/Esp32AdjPsu). Previously, I created simple web pages directly in the code using C++. However, in this project, I realized that working this way is not usable — I should separate the firmware code from the web application code. Most often, the SPI Flash File System (SPIFFS) is used for this (official documentation here - https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/storage/spiffs.html). SPIFFS allows to store data as separate files that can be both read and written.

What is the size of SPIFFS? Let’s take a look at the typical flash memory layout in the case of a 4 MB flash (https://github.com/espressif/arduino-esp32/blob/master/tools/partitions/default.csv):
# Name,   Type, SubType, Offset,  Size, Flags
nvs,      data, nvs,     0x9000,  0x5000,
otadata,  data, ota,     0xe000,  0x2000,
app0,     app,  ota_0,   0x10000, 0x140000,
app1,     app,  ota_1,   0x150000,0x140000,
spiffs,   data, spiffs,  0x290000,0x160000,
coredump, data, coredump,0x3F0000,0x10000,

About a third of the memory is allocated for SPIFFS, specifically 0x160000 = 1408 KB.

Since I’m using PlatformIO, the instructions will be for this environment. In the project root, create a data folder: 


In the data folder, we will create an index.html file with the following content:

<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
</head>
<body>
   <h1>Test page ESP32 plus SPIFFS</h1>
</body>
</html>

Test scatch code:

#include 
#include 
#include 

WebServer server(80); // out http server

void handle_OnIndex()
{
  // Let’s check if such a file exists:
  if (SPIFFS.exists("/index.html")) {
    // Open the file and pass its contents as a response.
    File file = SPIFFS.open("/index.html", "r");
    server.streamFile(file, "text/html");
    file.close();
  } else {
    server.send(404, "text/plain", "File Not Found");
  }
}

void setup() {
  Serial.begin(9600);
  
  if(!SPIFFS.begin(true)){
    Serial.println("SPIFFS failed");
    return;
  }
  // Let’s connect to the WiFi network.
  WiFi.begin("wifiname", "wifipassword");

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.print("Connected ");
  Serial.println(WiFi.localIP());
  server.on("/index.html", handle_OnIndex);
  server.begin();
}
 
void loop() {
  server.handleClient();
}
We upload the sketch to the ESP32 module. Next, let’s create the SPIFFS image. In PlatformIO, special commands are used for this:
pio run --target buildfs

Or use the PlatformIO Menu to simplify the process:



After the image creation is complete, the console should display SUCCESS. This means the SPIFFS image is ready for uploading.

To upload it, either use the command:
pio run --target uploadfs

Or, once again, this can be done through the PlatformIO Menu: 



Important: 

- You can rebuild and upload the SPIFFS image as many times as needed. It does not affect the main firmware. 

- Make sure that the image size does not exceed the allocated space in the partition table (in this case, 1408 KB).

Comments

Popular posts from this blog

Orange Pi Zero 3 - exploring GPIO

Configuring LED Indicators on Orange Pi Zero 3