Conditional platformio.ini options

Hello all!

I found myself often commenting out some options in platformio.ini file.
I’m developing for esp32 and this is my ini

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[platformio]
build_cache_dir = ./cache

[env:esp-wrover-kit]
check_skip_packages = yes
platform = espressif32
board = esp-wrover-kit
framework = arduino, espidf
board_build.filesystem = littlefs

;upload_protocol = espota
;upload_port     = 192.168.10.107
upload_speed    = 921600

monitor_speed   = 115200
monitor_filters = colorize, esp32_exception_decoder

board_upload.flash_size = 16MB
board_build.flash_mode = qio
board_build.partitions = ./Partitions/hshPartition_APP_3MB.csv
board_build.f_cpu   = 240000000L
board_build.f_flash = 80000000L


build_flags = 
    -DBOARD_HAS_PSRAM
    -mfix-esp32-psram-cache-issue
    -mfix-esp32-psram-cache-strategy=memw
    -DCONFIG_SPIRAM_USE_MALLOC=1
    -DCONFIG_MBEDTLS_DYNAMIC_BUFFER=1
    -DCONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST=1
    -DCONFIG_SPIRAM_CACHE_WORKAROUND=1
    -DCORE_DEBUG_LEVEL=0
    -std=gnu++17

build_unflags =
    -std=gnu++11

;upload_flags =
     ;--auth=admin_test_password

As you can see there are commented out lines like these

upload_protocol = espota
upload_port     = 192.168.10.107
upload_flags =
     --auth=admin_test_password

If my esp crashes and is in a boot loop, I have to comment the remote upload things out so I can upload it via USB. But most of the time I’m uploading with IP address.
Is there a way to create a flag which would toggle these options in/out so I don’t have to comment it every time i want to upload my sketch via usb?

I’m thinking about some preprocessor macro or define in the ini file which would conditionally include these options.

Just create yet another working environment with custom options and extend the original

[env]
check_skip_packages = yes
platform = espressif32
board = esp-wrover-kit
framework = arduino, espidf
board_build.filesystem = littlefs

;upload_protocol = espota
;upload_port     = 192.168.10.107
upload_speed    = 921600

monitor_speed   = 115200
monitor_filters = colorize, esp32_exception_decoder

board_upload.flash_size = 16MB
board_build.flash_mode = qio
board_build.partitions = ./Partitions/hshPartition_APP_3MB.csv
board_build.f_cpu   = 240000000L
board_build.f_flash = 80000000L


build_flags = 
    -DBOARD_HAS_PSRAM
    -mfix-esp32-psram-cache-issue
    -mfix-esp32-psram-cache-strategy=memw
    -DCONFIG_SPIRAM_USE_MALLOC=1
    -DCONFIG_MBEDTLS_DYNAMIC_BUFFER=1
    -DCONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST=1
    -DCONFIG_SPIRAM_CACHE_WORKAROUND=1
    -DCORE_DEBUG_LEVEL=0
    -std=gnu++17

build_unflags =
    -std=gnu++11

[env:esp-wrover-kit-ota]
upload_protocol = espota
upload_port     = 192.168.10.107
upload_flags =
     --auth=admin_test_password
...


[env:esp-wrover-kit-skip-ota]

See extends — PlatformIO latest documentation

1 Like

I see. will give it a try!

How to switch between these environments?

I have only one upload button at the bottom of VSCODE.

See “Project Environment Switcher”:

1 Like

Yeah i have found it but now i got a bunch of compilation errors.
I also had to load all these envs one by one.

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[platformio]
build_cache_dir = ./cache

[env:esp-wrover-kit]
check_skip_packages = yes
platform = espressif32
board = esp-wrover-kit
framework = arduino, espidf
board_build.filesystem = littlefs

upload_speed    = 921600

monitor_speed   = 115200
monitor_filters = colorize, esp32_exception_decoder

board_upload.flash_size = 16MB
board_build.flash_mode = qio
board_build.partitions = ./Partitions/hshPartition_APP_3MB.csv
board_build.f_cpu   = 240000000L
board_build.f_flash = 80000000L


build_flags = 
    -DBOARD_HAS_PSRAM
    -mfix-esp32-psram-cache-issue
    -mfix-esp32-psram-cache-strategy=memw
    -DCONFIG_SPIRAM_USE_MALLOC=1
    -DCONFIG_MBEDTLS_DYNAMIC_BUFFER=1
    -DCONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST=1
    -DCONFIG_SPIRAM_CACHE_WORKAROUND=1
    -DCORE_DEBUG_LEVEL=0
    -std=gnu++17

build_unflags =
    -std=gnu++11

[env:esp-wrover-kit-ota]
extends = env:esp-wrover-kit
upload_protocol = espota
upload_port     = hsh.local
upload_flags =
    --auth=admin_2024_04_23

[env:esp-wrover-kit-skip-ota]
extends = env:esp-wrover-kit

Should I reconfigure the project in menuconfig for all of these environments one by one? It seems like they do not know about my menuconfig settings

Yes, I had to copy my sdkconfig of the working env to the new one. It created a new file for the new env. Thank you very much, it seems to me that it is working!