Search code examples
esp32platformioarduino-esp32mcu

ESP32 Retrieve embedded binary files from the firmware .bin file


I'm using the ESP32 aWOT library to setup a webserver and I'd like to embed a bunch of binary files into the firmware .bin image (for OTA update purposes). Using PlatformIO we can use the: board_build.embed_txtfiles = src/file.ext command to embed the files to the .bin file. However I didn't figured out how to retrieve the files (either using a file system like SPIFFS or using C/assembly language). The Espressif documentation mentions the extern const uint8_t file_ext_start[] asm("_binary_src_file_ext_start"); command to access the file content, but I didn't understand how to use it.

Supose a file.html is embedded into the .bin and then I wish to route it like 192.168.0.XX/file.html - the aWOT library provides the app.get("/page", &handler); in which the *handler handles the response, but how to reference the embedded html file inside the handler function?


Solution

  • With the following contents in your platformio.ini file:

    [env:...]
    
    board_build.embed_txtfiles =
        data/file1.ext
        src/file2.ext
    

    You can (and must) define the following variables in your C/C++ code to access the data. The asm(...) part is from ESP-IDF's documentation and it gets the address of the data. Each one will be null-terminated because you used embed_txtfiles rather than embed_files.

    extern const uint8_t file_data_file1_start[] asm("_binary_data_file1_ext_start");
    extern const uint8_t file_src_file2_start[] asm("_binary_src_file2_ext_start");
    

    The names file_data_file1_start and file_src_file2_start can be any names, ESP-IDF's documentation just sets forth the convention that they're similar to the file paths.