Spiffsgen: A Code Generation Based Workaround for Lack of ESP32 SPIFFS Uploader Tool

Updated on 2020-11-22

This is a very specialized tool that generates source code to upload files into SPIFFS on an ESP32

Introduction

This article presents a code generator that works around the lack of a working ESP32 File Uploader tool. Without that tool, there is no way to get files into your flash memory. This code generator works around that problem, albeit in a somewhat roundabout way.

What it does is it takes a list of input files, and for each one, it creates a byte array in source code filled with the file contents. Finally, in the setup() routine, it takes each of those byte arrays and writes them to the SPIFFS filesystem. If there is no filesystem, one will be created and formatted.

This article might not be useful to most people but I'm providing it here so that if someone needs it, they can find it.

Using this Mess

Using this tool is simple. It is a command line tool which takes a list of input files and an optional output file.

Here is the using screen:

spiffsgen <file1> { <fileN> } [/output <outputfile>]
  Generates an .ino uploader to insert the specified file(s).

  <file1...N>   The input files to insert into SPIFFS
  <outputfile>  The output .ino file to generate

If no output file is specified, the generated code will be written to STDOUT. Note that currently, this tool does not support storing files under subdirectories.

The .ino file that is generated should be uploaded and run. After that, the files should be available in SPIFFS.

Here's an example of some generated code:

#include <SPIFFS.h>

uint8_t index_html[] = PROGMEM {
33, 68, 79, 67, 84, 89, 80, 69, 32, 104, 116, 109, 108, 62, 10, 60, 104
, 116, 109, 108, 32, 108, 97, 110, 103, 61, 34, 101, 110, 34, 32, 120
, 109, 108, 110, 115, 61, 34, 104, 116, 116, 112, 58, 47, 47, 119, 119
, 119, 46, 119, 51, 46, 111, 114, 103, 47, 49, 57, 57, 57, 47, 120, 104, 116
, 109, 108, 34, 62, 10, 60, 104, 101, 97, 100, 62, 10, 32, 32, 32, 32
, 60, 109, 101, 116, 97, 32, 99, 104, 97, 114, 115, 101, 116, 61, 34, 117
, 116, 102, 45, 56, 34, 32, 47, 62, 10, 32, 32, 32, 32, 60, 109, 101
, 116, 97, 32, 104, 116, 116, 112, 45, 101, 113, 117, 105, 118, 61, 34, 114
, 101, 102, 114, 101, 115, 104, 34, 32, 99, 111, 110, 116, 101, 110, 116
, 61, 34, 49, 34, 32, 47, 62, 10, 32, 32, 32, 32, 60, 116, 105, 116, 108
, 101, 62, 80, 117, 109, 112, 32, 77, 111, 110, 105, 116, 111, 114, 60
, 47, 116, 105, 116, 108, 101, 62, 10, 60, 47, 104, 101, 97, 100, 62, 10, 60
, 98, 111, 100, 121, 62, 10, 32, 32, 32, 32, 60, 112, 62, 83, 116, 97
, 116, 117, 115, 58, 38, 110, 98, 115, 112, 59, 37, 80, 85, 77, 80, 95
, 83, 84, 65, 84, 85, 83, 37, 60, 47, 112, 62, 10, 60, 47, 98, 111, 100
, 121, 62, 10, 60, 47, 104, 116, 109, 108, 62, 10 };
void setup() {
  Serial.begin(115200);
  if(!SPIFFS.begin(true))
  {
    Serial.println("Error mounting filesystem.");
    while(true);
  }

  File file;
  file = SPIFFS.open("/index.html","w");
  if(file) {
    file.write(index_html,252);
    file.close();
  } else {
    Serial.println("Error opening file");
    while(true);
  }
  Serial.println("Successfully uploaded.");
}

void loop() {}

As you can see this is all very straightforward. The array is just the bytes that make up the file.

History

  • 22nd November, 2020 - Initial submission