I am programming on the Arduino platform for ESP8266 IoT devices and I tend to use env sections in the platformio.ini file in order to separate settings between the various variants I make.
But what about settings I want to be global for all env:s?
Is it possible to put them before the first env section and then they will be used by all env:s?
And if I want one env to override a specific setting is that possible by just having the same setting in the env section with a different value?
For example to use a “latest” version of a platform globally but specify a different version on one env?
Say to globally use
platform = espressif8266
but specify a specific version for one env:
[env:esp01_serial-aspo]
platform = espressif8266@2.5.2
Or vice versa, to use the specific version globally unless the platform is specified in the env section without version number?
I am trying to tidy up my ini file to avoid duplication as much as possible.
Right now I have to duplicate these in every single env section:
board = esp01
framework = arduino
board_upload.maximum_size = 1048576
upload_speed = 115200
upload_port = COM8
monitor_speed = 115200
monitor_port = COM8
It seems like a waste of space and is easy to overlook a change for instance of the serial port to use.
Here’s an example of one of my platformio.ini
files where I have extracted the common stuff to the [env]
environment, which means that it is common to every other environment in the file.
There are 5 environments in my file. the [env]
one is common to the other four. They, in turn allow me to program the code for my Uno or my Duemilanove (2009) and in both cases, I can upload using the Arduino bootloader or my USBtiny ICSP device.
HTH
Cheers,
Norm.
; Settings common to *all* environments.
[env]
platform = atmelavr
framework = arduino
; Duemilanove settings using the Arduino bootloader.
[env:2009]
board = diecimilaatmega328
; Uno settings using the Arduino bootloader.
[env:uno]
board = uno
; Duemilanove settings using the USBTiny.
[env:2009_icsp]
board = diecimilaatmega328
upload_protocol = usbtiny
; Uno settings using the USBTiny.
[env:uno_icsp]
board = uno
upload_protocol = usbtiny
Thanks.
before I posted I looked at this documentation page and then I was confused because they use a section [common]
for the settings used in the env sections.
But then in each such env they showed that you had to refer to the common setting like this:
${common.build_flags}
This totally defeats the concept of a common setting that it has to be repeated in each section even though the value is not.
So you suggest to put all common settings into an [env]
section and then it will be used in all actual environments named like [env:env_name]
?
I don’t understand the logic behind this…
But if it works then so be it.
Question:
What happens to settings inside the platformio.ini file without any section name before?
Like if you just enter the various settings at the top before any [env.xxx]
section?
UPDATE:
I now found the proper doc page for env and it explains it all…
So it works like I hoped such that whatever is in [env] gets included into all [env:envname] unless the same setting is present also here in which case the local version “wins”.
Perfect!
Not exactly. You can combine them per Interpolation of Values — PlatformIO latest documentation
That should be the solution you really want to have.
1 Like
I don’t understand the Interpolation part but what I have done is the following:
I have a basic config that contains everything needed for all the versions I deal with. These settings are in section [env]
Then I have 4 different variations:
2 different locations and for each location both an AP and a MainRouter connection separated by their MAC addresses and SSID. Password is the same on all 4.
So I also have 4 sections named say [env:location_x]
where location is the name of the target and x is the name of the AP to use.
Each of these sections just hold the specific data for the connection by WiFi and the MQTT topic to use.
This way all configs entries are in one place only and I have 4 different environments I can build.
This seems to be what I need.
With Interpolation you can inherit settings from another environment so you don’t have to rewrite them. Example for build-flags:
[env]
platform = espressif32
board = esp32dev
framework = arduino
build_flags =
'-DWIFI_SSID="mywifissid"'
'-DWIFI_PASS="mypass"'
[env:server]
build_flags =
${env.build_flags}
'-DMODE="server"'
[env:client]
build_flags =
${env.build_flags}
'-DMODE="client"'
The build_flags
from env
are “inherited” to env:server
and env:client
so the
WIFI_SSID
and WIFI_PASS
will be available to the server and client environment.
2 Likes
Thanks,
that sounds more understandable rather than the strange word “Interpolation”, which indicates some kind of calculated “mean value”…
I have now updated my platformio.ini file according to these findings to reduce its size considerably.