Access upload_port from platformio.ini from extra_script.py

Is there a way to get the current upload_port of my environment passed to the extra_script? I am trying to access this to run python logic against it at compile time.

Also, is there a way to pass variables to the extra_script from the platform.ini environment entry ?

I’ve noticed that print sys.argv[19] in my extra_script.py yields

UPLOAD_PORT=MTkyLjE2OC4xLjExNQ==

It seems obfuscated and I’m unsure if I can use this to get the information I need.

I figured it out based off Redirecting...

I can access this with env.Dump("UPLOAD_PORT") in case anyone from the future wonders this.

You can access to current upload port via env.get("UPLOAD_PORT"). To print all available variables, please use print env.Dump().

1 Like

Thanks Ivan much appreciated. I was also having problems passing strings as #define in SCons.

It may save someone else the trouble of scratching a hole in their head to include it in this thread.

Pass a python variable to SCons env.Append(CPPDEFINES)

aVariable = "Test123"
env.Append(CPPDEFINES={'DEFINED_STRING' : aVariable})

Causes the compiler to return:

‘Test123’ was not declared in this scope

This is the method that I have found that allows a define to be created from a python variable with SCons:

aVariable = "Test123"
fixVariable = '\\"' + aVariable + '\\"'
env.Append(CPPDEFINES={'DEFINED_STRING' : fixVariable})

It should be

env.Append(CPPDEFINES=[
   ("DEFINED_STRING", fixVariable)
])

See examples how to use build API

1 Like

Do you know how to insert some CPPDEFINE into all files at build time with my extra_script.py ?

Do you mean to pass them to frameworks, libraries (before)?

I’m currently using
env.AddPreAction("$BUILD_DIR/src/password.o", setPassword)

I’m having problems with those defines set in setPassword like this:
env.Append(CPPDEFINES=[('DEFAULT_WIFI_PASS', defaultWifi)])

Instead of setting them in just password.h is there a way to set them globally? I did it once by manipulating the BUILD_FLAGS. I thought there might be a clean way to do this.

How about Environment variables — PlatformIO latest documentation ?

By my understanding those are set in the platformio.ini file?

My password is generated in the extra_script.py programmatically at build time.

I tried :

oldBuildFlags = env.get("BUILD_FLAGS")
print oldBuildFlags
test = '"' + "32423test" + '"'
env.Replace(
    BUILD_FLAGS= oldBuildFlags + ' -DPASSWORD=' + test)
print env.Dump("BUILD_FLAGS"); 

at the top of my extra_script but When I try to Serial.print(PASSWORD) I get

src/server.cpp:54:19: error: ‘PASSWORD’ was not declared in this scope

EDIT
Your reply where you directed me to Dynamic build_flags in platformio.ini / UNIX_TIME seemed to vanish. But it solved my problem. Thanks for spelling it out for me Ivan, you’re a legend! <3

1 Like

Here is the one problem. PIO calls extra script after main scripts are processed. Would be good to have extra_script = pre:/path/to/extra_script.py. Current behaviour is post:/path...

Please open feature request for that Issues · platformio/platformio-core · GitHub