Change firmware name based on main.cpp

Hi,
I read this, but could not figure out https://docs.platformio.org/en/latest/scripting/examples/custom_program_name.html

The top of my main.cpp looks like:

/****************************************      Board Definition        ****************************************/
//#define BOARD NODEMCU_DHT11
#define BOARD NODEMCU_DHT22
//#define BOARD ESP01

/****************************************          DHT Config           ****************************************/
#if BOARD == NODEMCU_DHT11
  #define DHTPIN D6     // Digital pin connected to the DHT sensor
  #define DHTTYPE DHT11 // Thermo sensor
#elif BOARD == NODEMCU_DHT22
  #define DHTPIN 12     // Digital pin connected to the DHT sensor
  #define DHTTYPE DHT22 // Thermo sensor
#elif BOARD == ESP01
  #define DHTPIN 2      // Digital pin connected to the DHT sensor
  #define DHTTYPE DHT11 // Thermo sensor
#endif

Is it possible to have Platform.io generate my firmware name using the value stored in BOARD as part of it, e.g. firmware_NODEMCU_DHT22.bin?

Thanks,
RG

Yes, run the g++ preprocessor on the main.cpp file to get a fully expanded source, then look for the value of BOARD and change env["PROGNAME"] accordingly. Marlin does it like that.

Also check Trouble with Custom Firmware Naming - #7 by lbussy for something that does not use GCC but more simple python parsing (which is not always correct).

Remember that env['BUILD_FLAGS'] only stored global build and env["CPPDEFINES"] global macros, not the ones you put in source code files.

Note that invoking the preprocessor and parsing that output is considerably more complicated than restructuring a your project a bit. For example, you could have the board type injected via the build_flags of the platformio.ini in three separate environments

[env:nodemcuv2_dht11]
framework = espressif8266
board = nodemcuv2
framework = arduino
build_flags = -DBOARD=NODEMCU_DHT1

[env:nodemcuv2_dht22]
framework = espressif8266
board = nodemcuv2
framework = arduino
build_flags = -DBOARD=NODEMCU_DHT22

[env:esp01]
framework = espressif8266
board = esp01
framework = arduino
build_flags = -DBOARD=ESP01

which would enable you to make use of the method originally showcased in the documentation.

1 Like

Thanks so much @maxgerhardt. Your approach is much better. As opposed to changing main.cpp depending upon the board I want to build an image for, I can just select an environment to be built, and that injects the right code in main.cpp