Problem accessing environment variable in code

olney_one
Posts: 4
Joined: Mon Sep 16, 2024 9:28 pm

Problem accessing environment variable in code

Postby olney_one » Thu Sep 19, 2024 9:06 am

I have read a few threads here and elsewhere on how to do this. This is what I have come up with so far:

1. Define the environment variable in my shell start-up. I can confirm this is working by doing:

# echo $SECRET
too secret to be in the repo
#

2. Create a build-flag in platformio.ini:
...
build_flags =
-DENV_SECRET='"${sysenv.SECRET}"'
...

3. If I do `pio run --target envdump | more` then I can see it in both BUILD_FLAGS and CPPDEFINES:

...
'BUILD_FLAGS': [ '-DENV_SECRET=\'"too secret to be in the repo"\'',
...
'CPPDEFINES': deque([ ('PLATFORMIO', 60115),
'ARDUINO_ESP32C3_DEV',
('ENV_SECRET', '\\"too secret to be in the repo\\"'),
...

4. However, if I do this in main.cpp, then I get it resolving to "":

static const char *SECRET = ENV_SECRET;

I have also tried accessing the environment variable directly using `getenv` but that crashes and burns!

Can anyone suggest what I still need to do to make this work, please?

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 52 guests