You can push the upload settings in its environment, then the build environment which uses it extends it, and then you can generate build_flags additionally. See docs regarding e.g. common_env_data.
I tested this technique with a minimal platformio.ini and an Uno
[upload_settings]
upload_port = COM14
upload_speed = 115200
monitor_speed = 9600
[env:uno]
platform = atmelavr
board = uno
framework = arduino
; automatically inherit attributes from upload_settings
extends = upload_settings
; make some settings visible as macros
; string-escape string values, integers are passed as normal
build_flags =
-D UPLOAD_PORT=\"${upload_settings.upload_port}\"
-D UPLOAD_SPEED=${upload_settings.upload_speed}
-D MONITOR_SPEED=${upload_settings.monitor_speed}
#include <Arduino.h>
int i;
void setup() {
/* Serial.begin(UPLOAD_SPEED,SERIAL_8N1);*/
Serial.begin(115200);
delay(1000);
while (!Serial)
{
delay(10);
Serial.println("================================");
Serial.println("= Error to initialize USB port =");
};
Serial.println("=========================================");
Serial.println("= USB port initialize with " + String(UPLOAD_SPEED) + " baud.");
Serial.println("= This firmware was uploaded from " + String(UPLOAD_PORT));
Serial.println("= Monitor speed was " + String(MONITOR_SPEED) + " baud.");
Serial.println("=========================================");
Serial.println("======= work fine in setup section ================");
i = 1;
}
void loop()
{
while (i <= 10)
{
Serial.println("======= work fine in loop section i = " + String(i));
i++;
}
}
Note that UPLOAD_SPEED and UPLOAD_PORT and MONITOR_SPEED are compile-time constants. They were generated when the firmware was compiled once on some PC with regards to the settings that were in the platformio.ini at that time.
They won’t change when you change the e.g. monitor speed or any other settings in the platformio.ini without recompilation and reupload, the firmware will always output the same. So if you e.g. take it to a different PC and ther it’s not connected to COM15 or whatever, the firmware will still always show the hardcoded value generated at compile-time. The firmware can impossibly detect what COM port number it is registered at on the Windows side – it just exposes itself as a USB device and how the OS handles it is kept to itself. (Unless you dynamically communicate that to the board somehow )
The reason do work with serial it is just to test the exchange information between platformio.ini and main.cpp.
I would like to let the user config the code, as we have with AT command, without to touch in the main code.
In fact, I was trying to develop a easy way personalize the individual informations for each endnode.
Bernd already wrote the RAKWireless is developming a Bluetooth interface. I already had a Laird Temperature/Humidity Sensor that it had this BLE interface and it is very confortable/easy/fast to make the individual configurations.
Below I did a code fix, because I forget that to remove the comment in the Serial.begin. Now it is fine.
main.cpp fixed
#include <Arduino.h>
int i;
void setup() {
Serial.begin(UPLOAD_SPEED);
while (!Serial)
{
delay(10);
Serial.println("================================");
Serial.println("= Error to initialize USB port =");
};
Serial.println("=========================================");
Serial.println("= USB port initialize with " + String(UPLOAD_SPEED) + " baud.");
Serial.println("= This firmware was uploaded from " + String(UPLOAD_PORT));
Serial.println("= Monitor speed was " + String(MONITOR_SPEED) + " baud.");
Serial.println("=========================================");
Serial.println("======= work fine in setup section ================");
i = 1;
}
void loop()
{
while (i <= 10)
{
Serial.println("======= work fine in loop section i = " + String(i));
i++;
}
}
Technically you should use MONITOR_SPEED here since that is the baud rate at which PlatformIO will open the serial monitor to look at the output; upload speed is just for uploading / flashing
But since this seems to be working when you specify monitor_speed = 9600 while doing Serial.begin(115200);, you must be using some USB-CDC serial port where baud rate is completely ignored anyways.
Well, about the operations, in fact in the serial port I will do new firmware upload at 115200 and after that, I will see the output in Monitor in 9600. Am I wrong?
Now I will create some initialization libraries, like serial port, LoRa, sensors, etc… to reduce the codes in the main section and to make it more clean