How to set app version for ESP32

Is there any way to set the app version when using PlatformIO with an ESP32 chip?

I am developing a project that uses OTA firmware upgrades. When pulling a firmware.bin file from an upgrade server, I want to check the app version which is included in the header of the .bin file.

According to Espressif’s documentation for the ESP32 chip here, there are a few ways to do it using Espressif’s ESP-IDF toolchain:

  • create a PROJECT_VER variable in the project makefile and assign the version to it
  • use ESP-IDF’s project configurator (MenuConfig) to set the version
  • create a file called version.txt in the project’s root directory and put the version in it
  • put the project in a git repo and pull the version using ‘git describe’

However, I can’t find any way to do this using PlatformIO. The user doesn’t typically interact with a makefile or ESP-IDF’s MenuConfig when using PlatformIO. I tried creating a version.txt file in the project’s root, but the string that I put in this file didn’t end up in the .bin file. Instead, the app version seems to be getting set to a git commit hash (6b2a602-dirty), but this hash doesn’t even exist in my repo. I think it’s getting this from the esp32-arduino-lib-builder repo.

Here are some details about the environment that I’m using:
IDE: Visual Studio Code with PlatformIO plugin
Board: Espressif ESP32 Dev Module
Framework: Arduino

I have used the upstream version:
platform = GitHub - platformio/platform-espressif32: Espressif 32: development platform for PlatformIO

And run “pio run -t menuconfig”

Thanks Jens. Unfortunately when I switch to the upstream version, I encounter 2 new problems. Note that I was previously using the #feature/stage branch, which seems to no longer exist. I’ve included the Platformio.ini settings at the bottom of the post.

Problem #1
After switching to the upstream version, the compiler can no longer find the following items that my project depends on:

  • esp_app_desc_t
  • esp_ota_get_partition_description

It looks like these existed in the version of the espressif arduino framework that was used by the feature/stage branch of the espressif32 platform, but not in the upstream version. To work around this, I added the following to force Platformio to use the latest version of the arduino framework:
platform_packages = framework-arduinoespressif32 @ GitHub - espressif/arduino-esp32: Arduino core for the ESP32

Problem #2
After adding the platform_packages statement shown above, the compiler can now find the items that were missing before. However, now the linker starts throwing the following error several hundred or thousand times:
dangerous relocation: windowed longcall crosses 1GB boundary; return may fail

To confirm that it’s not a problem with my project, I created a brand new project from the PlatformIO Home tab, and entered the PlatformIO.ini settings shown at the very bottom of this post (Problem #2 settings). The linker error still occurs with a completely blank project.

Does anybody know what could be causing this linker error? I haven’t seen it before and am having trouble figuring out what I’m doing wrong even after a lot of searching the web.

Original PlatformIO.ini settings - compiles with no error
platform = GitHub - platformio/platform-espressif32: Espressif 32: development platform for PlatformIO
board = esp32dev
framework = arduino

Problem #1 settings - can’t find esp_app_desc_t and esp_ota_get_partition_description
platform = GitHub - platformio/platform-espressif32: Espressif 32: development platform for PlatformIO
board = esp32dev
framework = arduino

Problem #2 settings - dangerous relocation error even with empty project
platform = GitHub - platformio/platform-espressif32: Espressif 32: development platform for PlatformIO
board = esp32dev
framework = arduino
platform_packages = framework-arduinoespressif32 @ GitHub - espressif/arduino-esp32: Arduino core for the ESP32

I added these lines to root CMakeList.txt file before the project directive and it worked:

file (STRINGS “BuildNumber” BUILD_NUMBER) // read build number from file
set(PROJECT_VER “0.0.${BUILD_NUMBER}”)

1 Like

The suggested answers would not work for arduino framework, since it does not support CMakeList.txt files neither the pio run -t menuconfig” command.

I’m also looking for this feature, for the exact same reason (discerning OTA images on runtime),
and have not find a solution form the last 3 months.

Hi @ankostis

Exactly for this use case I am working on a library (SimpleHTTPUpdater)

The library automatically generates a build number and is able to download and install new firmware versions from a web server.

I believe that the ESP_IDF behavior can be replicated with pio Advanced Scripting. Will describe my journey in this post.

My solution has landed as promised, it uses an extra_script to patch x4 fields (app-name/version, build-date/time) into the image, after it has been compiled, and recalculates checksum & sha256.

1 Like