How to build and upload all when multiple environments

I have two boards esp32 and esp32-c3. When I click the upload button in the picture, it only builds and uploads to esp32-c3. And what’s more, it opens the serial monitor and connects to esp32 instead of esp32-c3.


My platformio.ini:

[env]
platform = espressif32@6.4.0
framework = arduino
lib_deps = 
	arduino-libraries/ArduinoMqttClient@^0.1.8
	bblanchon/ArduinoJson@^7.0.3
	256dpi/MQTT@^2.5.2
monitor_speed = 9600
build_unflags = 
	-std=gnu++11
	-std=gnu++14
	-std=gnu++20
build_flags = 
	-D ARDUINO_USB_MODE=1
	-D ARDUINO_USB_CDC_ON_BOOT=1
	-D MQTT_CLIENT_STD_FUNCTION_CALLBACK=1
	-std=gnu++17

[env:esp32-c3-devkitm-1]
board = esp32-c3-devkitm-1
upload_port = COM14

[env:esp32dev]
board = esp32dev
upload_port = COM5

When I change my env to esp32, it keeps complaining:

To upload to all boards at once choose the default environment here and click the upload button:

About Serial monitor “issue”
You have selected "Auto".
PIO can’t distinguish between those boards.

Solution:
Also define monitor_port in platformio.ini:

[env:esp32-c3-devkitm-1]
board = esp32-c3-devkitm-1
upload_port = COM14
monitor_port = COM14

[env:esp32dev]
board = esp32dev
upload_port = COM5
monitor_port = COM5

To open a specific board, choose the specific board’s environment and click the serial monitor button.

I can’t open serial monitor for [env:esp32dev], it always compains


I have no clue which id it’s trying to read.

No issues on my side:

Try: restart VS Code and try again.

Trick to specify the PORT just once per environment

[env]
platform = espressif32
framework = arduino
monitor_speed = 115200

[env:esp32-c3-devkitm-1]
board = esp32-c3-devkitm-1
build_flags =
  '-Dboard_name="esp32-s3"'
upload_port = COM4
monitor_port = ${this.upload_port}

[env:esp32dev]
board = esp32dev
build_flags =
  '-Dboard_name="esp32-s3"'
upload_port = COM3
monitor_port = ${this.upload_port}

Curiously, even this works correctly:

[env]
platform = espressif32
framework = arduino
monitor_speed = 115200
monitor_port = ${this.upload_port}

[env:esp32-c3-devkitm-1]
board = esp32-c3-devkitm-1
build_flags =
  '-Dboard_name="esp32-s3"'
upload_port = COM4

[env:esp32dev]
board = esp32dev
build_flags =
  '-Dboard_name="esp32-s3"'
upload_port = COM3

Wow, but it looks dangerous to me. It highly relies on the executing order of the .ini file parser, which is not documented (at least I haven’t found it) and might be changed in the future.

Is it possible to change the option in the red circle instead of changing the environment?


I found it has no impact on which port the serial monitor listens on. So what’s the use case for this red option?

Yeah, I still wonder HOW this can work. LOL

Sure, but this will affect UPLOAD and SERIAL monitor.

My choice would be to specify the upload_port and monitor_port in each section.
Thus you can leave the port on AUTO.

But that’s a matter of taste, I think :slight_smile:

Strange… I have a different behavior.

What’s your PIO Core Version? (Terminal: pio --version) ?
Mine is PlatformIO Core, version 6.1.14

The same as yours

PlatformIO Core, version 6.1.14

It’s so strange. Just now, when I set the button to COM14, it still used COM5. But now it works correctly. But I’m pretty sure I’ve done nothing. I even haven’t restarted my VSCode.

Maybe you have forgotten to save the changes in platformio.ini ?
In contrast to a build task, it is not saved automatically when you open Serial monitor.

This might be an explanation.