Change name of firmware file

Hi, I would like to have custom firmware name, instead of firmware.bin i would like IotSwitch.bin file to be generated. Is it possible to configure?

http://docs.platformio.org/en/latest/projectconf/advanced_scripting.html#custom-firmware-program-name

Hi there. Thank you for this info, but where in Platform.ini do I put this code?
Do I just copy and paste it just like it is:

[env:featheresp32]
platform = espressif32
board = featheresp32
framework = arduino
lib_deps =
PubSubClient
EspSoftwareSerial
build_flags = -D VERSION=1.1.3
extra_scripts = pre:extra_script.py
monitor_speed = 115200

Or what part of this code do I use in my specific board’s env section:

[env:LPC1769]
platform = ${common_LPC.platform}
extends = common_LPC
board = nxp_lpc1769

The critical part is the definition of the verseion via build_flags and the definition of the extra_script to run. So just add the lines

build_flags = -D VERSION=13
extra_scripts = pre:extra_script.py

to the environment and make sure that the extra_script.py with the content as shown in the docs exists in the project root folder. Refer to the docs for all further documentation on platformio.ini options.

So the script must be exactly this:

And put into the root dirctory like this:

But where in platform.ini do I put what to call that script?

Well through the extra_scripts = pre:extra_script.py line… Please see docs and the therein linked docs…

You mean, like this:

Can you show the contents of your extra_script.py in VSCode? Is the first line Import("env") as it should or Import("projenv"). Not being able to reference projenv is correct for pre: scripts as per docs.

Though there might be secondary error after that: The given example doesn’t work when there defines in the build script that are not defined to some value, but just defined. Because then the CPPDEFINES will e.g. be

['BEARSSL_SSL_BASIC', ['VERSION', '13']

and viewing them as (key, value) pairs as the code

defines = {k: v for (k, v) in my_flags.get("CPPDEFINES")}

does fails since the first item cannot be written in such a way.

But let’s see your file content first. The directives in the platformio.ini are correct.

1 Like

A better, more general script for converting the -D VERSION=14 into the firmware name would be

Import("env")

version_from_buildflags = "not_found"
my_flags = env.ParseFlags(env['BUILD_FLAGS'])
#print(my_flags.get("CPPDEFINES"))
# some flags are just defines without value
# some are key, value pairs
for x in my_flags.get("CPPDEFINES"):
    #print(x)
    if isinstance(x, list):
        # grab as key, value
        k, v = x
        if k == "VERSION":
            version_from_buildflags = v
            # no need to iterate further
            break

env.Replace(PROGNAME="firmware_%s" % str(version_from_buildflags))
1 Like

Thank you.
I’ll try that today.
But how do I call this script and where in platformio.ini file?
Currently I have this:

[env:Replace]
build_flags = -D VERSION=1.1
extra_scripts = pre:extra_script.py

I had somebody else’ script with that projenv name thingi…

So I changed to another script without that.
This example shows that I need to define the platform, board and framework etc to call this script?

[env:env_custom_prog_name]
platform = espressif8266
board = nodemcuv2
framework = arduino
build_flags = -D VERSION=13
extra_scripts = pre:extra_script.py

I am currently running the BTT SKR1.4 turbo board with tmc2209 drivers…

And with this setup, I still only get the firmware.bin file instead of the firmware version number…

I leverage the project name and version within the CMakeLists.txt file to dynamically alter the firmware’s output name. Below are the configurations I’ve set up for this purpose.

rename_script.py (place this file in the root directory of your PlatformIO project)

Import("env")
import os
import re

file_path = 'CMakeLists.txt'  # Adjust the path accordingly

def extract_project_version(file_path):
    # Open the file in read mode ('r')
    with open(file_path, 'r') as file:
        # Read the entire content of the file into a string
        file_content = file.read()

        # Use regular expression to find the value inside double quotes after PROJECT_VER
        match = re.search(r'PROJECT_VER\s+"([^"]+)"', file_content)

        # Check if a match is found
        if match:
            return match.group(1)
        else:
            return None

# Call the function and print the result
project_version = extract_project_version(file_path)
if project_version:
    print(f"The project version is: {project_version}")
else:
    project_version = "unknown"


project_name = os.path.basename(os.path.dirname(env["PROJECT_CONFIG"]))

env.Replace(PROGNAME="%s_%s"% (project_name, project_version))

platformio.ini

[env]
platform =espressif32
framework = espidf
monitor_raw = yes
monitor_speed = 115200
extra_scripts = pre:rename_script.py

[env:esp32s3box]
board = esp32s3box

CMakeLists.txt (this is the one in the root directory of your PlatformIO project)

cmake_minimum_required(VERSION 3.16.0)
set(PROJECT_VER "v1.0.0")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(my_project_name)