Can i pass variables from main.ino to for advanced scripting?

I am trying to get build to rename firmware.bin to Myunitname.bin I have managed to hardcode a name in the file

build_tag = "V22"
env.Replace(PROGNAME="test%s" % build_tag)
print env['PROGNAME']

but can I pull a variable “boardID” from my main.ino file so I just need to keep the main file updated.

I tried

build_flags = -D UNIT_NAME=LUX_86 -D VERSION=22

platform =
board = d1_mini
framework = arduino
extra_scripts =
build_flags = ${common.build_flags}

in my platformio.ini file but there seems to be no documentation on how to access the env flags and variables you have added within my main.ino file.

My ignorance is Legion.

print env['CPPDEFINES']

or better

print env.Dump()

Thanks for the reply, I had print env.Dump() in my .py script but took it out to post here, I ploughed through it but couldn’t find any of the arduino file variables to use within the script. Am I missing a step? The Build panel is not a lot of use without a way to copy the text out to search through.

OK, had another look through the dump text and found the BUILD_FLAGS text I added in the platformio.ini file. However there is lots left unexplained, what is the -D for? How can I access them in the arduino file? do I #include something, or are they just available using some naming convention? I can’t find any instructions for this. You must be able to import from the ini file or why else include the SSID and PASSWORD variables in the example.

Sorry to be a pest.

You can’t use C/C++ variables from source code in extra Python script :slight_smile:

You can define common “variables” (macros) in platformio.ini via build_flags option and later use the in C/C++ and Python code.

In C

#ifdef VERSION

/* or as replacement*/

See updated example Redirecting...

I now have it working, many thanks Ivan.

I ended up setting up a pre upload script to copy the file and just let it fail when it cant find the board, it copies the renamed firmware.bin file to my ota folder fine before stopping.

Out of interest, is there a way to run a post build but pre upload script? Using the it executed after the tree view but before actually building the firmware file.

See Redirecting...

Cheers Ivan, I have it mostly working now, the only thing I have been unable to do is use build_flags that are strings in my main.ino. I can use int values but trying to set a string gives an error. Here is some of my code


build_flags =
  -D OTA_DIR=//diskstation2/web/fota/

platform =
board = d1_mini
framework = arduino
build_flags =
  -D SSID_NAME=name
  -D SSID_PASSWORD=password
extra_scripts =

some lines from
main.ino although it is actually called OTA_mqtt_esp8266_deepsleep_light_battery.ino does the name matter?

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>

// For OTA Updates
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>

//Import build flags from platformio.ini
const int domoticzID = DOMOTICZ_ID;

//const String BOARD_ID = "LUX86";
const String BOARD_ID = UNIT_NAME;

The FW_VERSION and domoticzID lines work fine but trying to set the BOARD_ID to UNIT_NAME gives this error on build

command-line:0:11: error: ‘LUX86’ was not declared in this scope
note: in expansion of macro 'UNIT_NAME’
const String BOARD_ID = UNIT_NAME;
*** [.pioenvs\d1_mini\src\OTA_mqtt_esp8266_deepsleep_light_battery.ino.o] Error 1

OK After much googling I got it all working I needed to ad some #defines of my own to get a string

#define xstr(a) str(a)
#define str(a) #a

//Import build flags from platformio.ini
const int domoticzID = DOMOTICZ_ID;

const String BOARD_ID = xstr(UNIT_NAME);


1 Like

See docs for Stringification.

Amazing stuff man! such a powerful solution, thanks for sharing.