How to include two header file in code using platfom ini file

Hello everyone,

I am working on the project. where we have different sensors and switches. but we upgrade hardware where we change pin connection due to some reason, Now we have two boards, one is working on according to old hardware pins and new one is working on new hardware pins.

The code is working on both the boards but we have to change pins variable iaccording to the board.((new board)ultrasonic sensor is connected on 13 pin of esp32,(old board)ultrasonic sensor is connected on 27 pin of esp32).

so if i add pin config header file(where the pin define) of both boards into the code and then try to control including of config file according board before the compilation process.

so my question was, is there any way where i define some flag in platformio.ini to control lncluding process of header file of different boards (Pinconfig.h).

or
i make different the enviorment for old and new boards in platfform.ini then make flag to control the including process.

please suggest me best way .

i make basic code to memic the bheviour of my original code.so now i defines the led pins in different header files.i want to add config file base on build flag ,which is define in platform.ini but i do not know how to define flag and base flag on include config header file.

Here us some images.
NewConfig
OldConfig

Thanks in advance.

The documentation on build_flags is clear, -I<folder> adds an include path to the project so that the code can e.g. find the NewCONFIG.h or OldCONFIG.h files.

However, I’m seeing multiple problems with the way you do it above:

  • you should never define variables like that in a header file. If two different .cpp files include that header, they will both try to define the global variable LED. If you want to create global variables, then only declare them as extern in the header and add a .cpp file defining the variable (this cannot be done in the include/ folder since .cpp files in it are not compiled – refactor to src/ or as library)
  • You call your header files different names, so the code will have to choose to either #include <NewCONFIG.h> or #include <OldCONFIG.h>

I’d suggest the following: Always name the file e.g. BoardConfig.h, but depending on the platformio.ini environment, include different folders so that different versions of the file are found. Instead of using global variables, just use macro definitions.

With platformio.ini

[env:New_Board]
platform = espressif32
board = esp32dev
framework = arduino
build_flags = -I include/New_Board

[env:Old_Board]
platform = espressif32
board = esp32dev
framework = arduino
build_flags = -I include/Old_Board

Hello @maxgerhardt

Thank you for your quick reply. I am recently shifted from Arduino ide to Platformio for my projects, but I am still learning about platformio.
now i make changes according to your code and run on platform io , but all three environments are running after one another. but I want to run the code like this first default env run and then base on board_flag(if true then new board config.h is selected and if false then old_board config.h is selected) in the platform.ini the corresponding env is selected.

Output:
After uploading the code , the Old_board’s BoardConfig.h is included in the main.cpp and led connected on pin 12 is ligthup.

Platformio.ini file

[env:esp32dev]

platform =espressif32

board = esp32dev

framework = arduino

[env:New_Board]

platform = espressif32

board = esp32dev

framework = arduino

build_flags = -I include/Old_Board

[env:Old_Board]

platform = espressif32

board = esp32dev

framework = arduino

build_flags = -I include/New_Board

Thanks

Since you have not added build flags for the first esp32dev environment, no include folder is added in which the BoardConfig.h file can be found – hence you get the error. As expected.

@maxgerhardt Thank you for the answer but can you help with the led code how I control the envs?

…What do you want the build to do when you don’t give it the include path where the BoardConfig.h file can be found? All it can do is error out. Add a build_flags = -I<..> to the platformio.ini for the [env:esp32dev] or say how you want it to behave.

Hello @maxgerhardt

[env:esp32dev] is my base environment and other two board’s environments. i want behave like this

[env:esp32]
platform =espressif32
board = esp32dev
framework = arduino
build_flags = -DBoardFlag = true

[env:New_Board]
build_flags = -I include/New_Board

[env:Old_Board]
build_flags = -I include/Old_Board

so depend on this BoardFlag I want to include header files in my code. if it is true then the New Board header file is included in the code or if it’s false then the Old Header file is included in the code.
.
so basically I want to say that using BoardFlag i want to select the environment in platform.ini which included different header files.
I want to automate the environment selection process in PlatformIO IDE

Thank you for your help sir

Carefully read through the documentation. The correct form to do that would be

; settings that apply to all environments
[env]
platform = espressif32
board = esp32dev
framework = arduino

; stores common build flags -- 
; can't be put in [env] because 
; the sub-environments would overwrite it,
; needs to be additive.
[common_env_data]
build_flags = -DBoardFlag=true

[env:New_Board]
build_flags = 
   ${common_env_data.build_flags}
   -I include/New_Board

[env:Old_Board]
build_flags = 
   ${common_env_data.build_flags}
   -I include/Old_Board

Thanks for the help @maxgerhardt