How to configure correct upload for board

I ported my Arduino project from the Arduino IDE to vscode+PlatformIO. My project is designed to run on ESP8266 and ESP32. In the code I use #ifdef tags to conditionally include the correct libraries as well as to compile the appropriate code snippet for a specific platform e.g.:

#ifdef ESP8266
  #include <ESP8266WiFi.h>
  #include <WiFiClientSecure.h>
  #include <LittleFS.h>
  #define WIFIMCU "ESP8266"
#endif

#ifdef ESP32
  #include <WiFi.h>
  #include <WiFiClientSecure.h>
  #define WIFIMCU "ESP32"
#endif

Vscode seems to support the idea of one board per project when I imported the project into vscode+PlatformIO, I specified the ESP32 board as this is what I was currently working on.

In due course I found that one can add additional env: tags to the platformio.ini file so I did this:

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200

[env:esp8266dev]
platform = espressif8266
board = esp12e
framework = arduino
monitor_speed = 115200

When I hit the compile button, the compiler will now link and compile the code for both boards. The problem is that when I hit upload, it then tries to upload both binaries. Due to the safeguards on the board (fuses etc) the upload obviously fails for the binary that does not match the hardware. I am currently working around this by commenting out the code block for the board that I am not working with.

Do I have to split my “project” into two separate folders, one for each board, or is it possible to configure platformio.ini so that the uploader will only attempt to upload the code for the board is currently plugged in, or at least provide a means for me to manually select which platform version I want to upload?

Please take a look at the project tasks and the project environment switcher at the bottom of the taskbar described therein.

Well, I re-instated the ESP8266 environment in platformio.ini, but still only had the ESP32 option visible in the switcher. However, after I ran a compile, the ESP8266 option appeared and I can now switch between the two and compile/upload individually for each which is what I was looking for.

I also had a look at ‘project tasks’ as you suggested and can see how clicking on options in there executes specific tasks for a given environment.

BTW, I also discovered earlier that I can open individual “projects” in the Arduino IDE sense using Open Folder. This closes the previously open project folder and loads the currently selected one and names the workspace after the folder name. Previously when I opened anonther Ardiuno project, it would just get added to the global “untitled workspace”. It was getting rather cluttered and confusing in there. I can see how workspaces might become useful at some future point, but for now, Open Folder and the things you pointed out here are probably all I need to get me started in vscode+PlatformIO.

Thank you for pointing me in the right direction.