Passing (a lot of) variable(s) values at build time

Dear Community,

I’m researching quite a while now for getting into the secrets of passing some variable values at build time. Here was a thread that I walked through but I’m not satisfied yet. These values are not for public sharing i.e. if you want to share your code online on github or similar services (secrets, ips, URLs, etc.pp.)

Thread I studied:

There are still some questions i.E:

  • I have tons of variables in my main.cpp file like (IPAdress, subnet, gateway, secrets like passwords, URLs, etc. pp.) - shall I really add these all to my platformio.ini file (as suggested as dynamic build flags)? In my opinion, it would be great if I could put them into one separate file in my project. Is this feasible? Is there a better solution to this problem I’m not aware of? Is there a better best practice?

  • Anyhow - if I go with the proposed solution of the mentioned thread I can’t share my platfomio.ini file anymore on github (if add it to the gitignore file) - isn’t that bad practice?

Could one of you spend a little bit of time to answer my questions?

Thanks and best regards

So, just a .h header file then that is git-ignored or only filled with example values? Like in WifiMQTTManager/examples/Basic at master · dreed47/WifiMQTTManager · GitHub? That works.

In the special case that the platformio.ini just uses build flags with the reference to the environment variable names, it wouldn’t contain any secrets.

1 Like

@maxgerhardt: Many thanks for bringing light into this. Pretty easy solution. Thanks!

Ok, then I don’t understand where the values of these are stored. Can you clarify that?

Best regards

In the environment variables themselves. This is a very special and you should probably do the .h header file way.

When your platformio.ini is setup e.g. like

build_flags = -D WIFI_PASSWORD=${sysenv.ENV_WIFI_PW}

all this does is to say "fetch the current value of the system environment variable ENV_WIFI_PW and put it in the C/C++ macro WIFI_PASSWORD. A user would then still have to execute

export ENV_WIFI_PW="abcdefgh"

(command depending on the OS used)

to fill it with a concrete value.

Thus you can also see that an editable file is much easier to work with.

@maxgerhardt: Ah ok, from system vars - no you’re absolutely right, that’s not what I want. But many thanks for your clarifications. Very nice explanations!

Have a nice time!