Inserting and using environmental variables as strings

I’m trying to do a thing which I suppose is pretty basic - I wan’t to insert WIFI and other credentials at build time. I think my problem is a combination of inexperience with PlatformIO and C++ types. Anyhow, thankful for assistance.

In code when running WiFi.begin(WIFI_SSID, WIFI_PASSWORD); I get

src\main.cpp: In function 'void setup()':
src\main.cpp:50:23: error: expected primary-expression before ',' token
   WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

and when running String auth = base64::encode(API_AUTH_STRING); I get

<command-line>:0:17: error: 'weatherstation' was not declared in this scope
src\main.cpp:33:30: note: in expansion of macro 'API_AUTH_STRING'
 String auth = base64::encode(API_AUTH_STRING);

I guess there is some kind of casting / type problem but I don’t know what to do, so here I am, asking for help.

My Configuration file

[platformio]
default_envs = development

[env]
platform = espressif32
board = esp-wrover-kit
framework = arduino
lib_deps =
    SparkFun CCS811 Arduino Library@2.0.1
    SparkFun BME280@2.0.8
    ArduinoJson@6.15.2

[env:development]
build_flags = 
    -D WIFI_SSID=${sysenv.WIFI_SSID}
    -D WIFI_PASSWORD=${sysenv.WIFI_PASSWORD}
    -D API_URL=${sysenv.API_URL}
    -D API_AUTH_STRING="weatherstation:donotuseinproduction"

[env:production]
build_flags = 
    -D API_AUTH_STRING=${sysenv.API_AUTH_STRING}

You might want to try wrapping them in escaped double quotes - I had trouble with strings on Windows if it didn’t have that, and I’m not sure if the space is an issue (also)?

i.e.

-DWIFI_SSID=\"${sysenv.WIFI_SSID}\"

Hmm when I try this the error disappears but the variables seems to just resolve to empty strings "".

But I don’t understand, from which shell is the environmental variables supposed to be taken? I’ve set them with this syntax $env:WIFI_SSID = "blblabla" in the Powershell terminal but it seems as if PlatformIO does not notice this.

…I’m getting a bit annoyed since I’m only doing what the official documentation says… Redirecting...

solved! (sorta kinda…)

Since my objective was to just manage secrets, what I ended up doing instead was creating a secrets.h and add it to .gitignore

#define WIFI_SSID "myssid"
#define WIFI_PASSWORD "mypass"

Good enough for me (hobby project).

Although I still got no clue how to get the environment variables working…

Are you then executing pio in the same Powershell session from where you set the environment variable?

For debugging purposes, you can also use Advanced Scripting to print os.environ to the shell at the start of the compilation process, to check if it’s even in there.

1 Like

As Max is hinting at, it’s possible you’re defining them in one shell, and then running platformio in a different one, which is where the confusion is coming from. If you set your environment variable, and then platformio run from there, it should pick it up. I’m on linux atm, but I just tested setting the WIFI_SSID env variable, and then doing a pio run -v to see if it was being seen by PlatformIO.

When I exported the WIFI_SSID, and then hit ‘verbose build’ in the UI (which opens another shell), it didn’t see the WIFI_SSID environment variable.
image

When I tried pip run -vfrom the shell/tab I defined the environment variable from, it worked:
image

I used your platformio.ini as is from above, so no change to escaping double quotes, etc.

I’ve not tested this, but on the assumption that VSCode + PlatformIO behave like most other programs on this, you should also have the option of setting the environment variables at the system level, and then opening VSCode, which should allow both it and PlatformIO to see those globally set environment variables.

@einarpersson my solution was adding simple quotes. This is because the variable is a Macro and if you want to replace it as String you should be able to add double quote.

build_flags = 
-D API_AUTH_STRING='"weatherstation:donotuseinproduction"'

This is
Simple quote " text here " Simple quote
’ " text here" ’

1 Like

This is definitely also my way to go. Works perfectly. Thanks!