PlatformIO and WiFi credentials

I am still very new to vscode+PlatformIO but was interested in what I found in the documentation under platformio.ini. In the example that is provided is shows this:

build_flags =

I am assuming that this sets the WiFi credentials. My question is, how do I used these in my script? At the moment I have something like:

const char* essid = "**********";
const char* psks = "***************";

Is these some global variable pair that I need to pick up in my code? Otherwise, how do I connect to my WiFi router? I was hoping to find something in the documentation, perhaps in the build options where the options are located in the example, but so far have not found any info.

BTW, I also recently came across WiFi Manager, a WiFi configuration manager library for Arduino. I haven’t investigated it yet, but wondering whether I actually need it in the vscode+PlatformIO environment?

The -D<macro name>=<value> option is an instruction to the compiler to create a macro with the given name and value. So, -DVAL=123 is the same as writing on top of every file

#define VAL 123

The documentation shows a further example of this. Important is the need to escape values / stringify them, just writing e.g. -DSSID_NAME=HELLO does create


and not:


Since it creates a regular macro, you can either refer to the macro directly, or use it to initialize a variable. Doesn’t make a lot of difference.

So, you could have

build_flags =
   '-DSSID_NAME="My Test"'
   '-DSSID_PASSWORD="Test Password"'

and use it as

#include <Arduino.h>
const char* essid = SSID_NAME;
const char* psks = SSID_PASSWORD;

void setup(){ Serial.begin(9600); }
void loop() { Serial.println(psks); }

Thank you. I did wonder whether that might be creating macros but wasn’t sure. That clarifies things a lot. Its certainly seems a better approach than hard-coding credentials in the code.

I have also today come across an example where someone is doing this:

ssid = mySSID
password = myPassword

Does this also create macros?

Writing that in platformio.ini itself does not do anything that affects the build. There would have to be an extra_script that transports these values into the build system, e.g. as a macro, such as shown here and here. However, for a simple macro addition, that is quite overkill…

Sorry, my bad! I missed in that same example where he had this in the build_flags which were right near the bottom of the rather long config :


So I see now that he is actually assigning the items configured earlier as section parameters to macros in the build flags. The extra script examples you posted are interesting as well. I see that this arrangement quite flexible.

Ah right, so extra scripts are indeed not the only way, since you can reference the value of other environments and variables, you can inject them into the build using build_flags again with ${env_name.variable name}, as documented. Thought only of the more complicated way here.

Indeed, as documented in the platformio.ini example, although without an explanation in the document text, I didn’t initially appreciate what the ${…} constructs were doing. My understanding is gradually improving and I appreciate your help.