Click here to Skip to main content
15,881,794 members
Articles / Internet of Things
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
22 Nov 2020MIT1 min read 4.8K   53   2
This is a very specialized tool that generates source code to upload files into SPIFFS on an ESP32
This tool will create a C++ .ino file to flash to an ESP32. The .ino file contains other files embedded as arrays in source code, along with a stub that writes the embedded files to the SPIFFS filesystem, which it creates if necessary. The upshot is it's a guaranteed way to insert files into your flash because the File Uploader tool does not work properly with the 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:

C++
#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

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
United States United States
Just a shiny lil monster. Casts spells in C++. Mostly harmless.

Comments and Discussions

 
Questionproblem and question Pin
thierryppp28-Nov-20 0:46
thierryppp28-Nov-20 0:46 
GeneralMy vote of 5 Pin
Emile van Gerwen26-Nov-20 23:27
Emile van Gerwen26-Nov-20 23:27 
Clever idea and thanks for making it available!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.