I plan to program a batch of Arduino MKR NB 1500 (let’s say 20 devices). They shall all be identical with the exception of a macro (NAME) containing a unique name for each device (e.g. “Device1”, “Device2”, …, “Device19”, “Device20”). This shall be achieved automatically, rather than manually editing
#define NAME "Device1"
My current solution is a Python script that reads a file with all the names, then iterating over all names, running first
pio project init -e to manipulate the
build_flags entry in
platformio.ini to fit the current device to program, and only after that calling
pio run -e.
It does work, but I was wondering if there already exists a more straight forward solution for this (common) problem. Just adding an environement for each device is not really a solution as one still needs to insert the corresponding build flags. As far as I understood, a single
pio run -e will upload only a single project (no multiple seqeuntial runs with small changes possible). So neither
dynamic build flags nor
advanced scripting would help me out here, as they all are bound to a single call of
If someone can help me out on this, I would very much appreciate it.
Yes, but, if you allow that “Upload” is pressed multiple times, for each device once, you can execute Python logic that reads the current device name (or a counter) from a file and injects that as a macro in the build process. (See first example script).
You can also use dynamic variables to reference an environment variable. A batch or shell script can then, in a loop, set the environment variable to some value and call
pio run -t upload normally.
plus some batch scripting.
All of this however requires a recompilation of the project. If that is unacceptable, one can also do a faster hack: Build the executable once with an arbitrary
"DEVICE1", and you get a
firmware.bin that contains that string.
You can then find the location in the binary where that string is stored and overwrite it with something else of the same length. That doesn’t break the binary. Once the
.bin file is modified you can either use PlatformIO to upload it (Upload Latest build without a Compile/Link - #4 by ivankravets) or a manual upload invocation with the modified binary as PlatformIO would do (that can be inspected with
pio run -t uplaod -v).
thanks again for your feedback!
Avoiding recompilation would be great but it is not a nogo. Since the ASCII field length would need to be equal for all devices, it might put too much of a restriction to the user.
Upload multiple times and having some script in the background would work, but the user would need to double check how far the counter already is.
So, to me the solution with the environment variable seems to be the most elegant. I do not need to manipulate
platformio.ini directly but only the environment variable.