How could the main.cpp read the parameters at platformio.ini?

I would like to read/receive the platformio.ini in the main.cpp to make some tests about the values in the PlatformIO.ini.

For example: the serial port configuration parameters marked in Bold below

Could you help me?

[env:wiscore_rak4631]
platform = nordicnrf52
board = wiscore_rak4631
framework = arduino
lib_deps =
ArduinoModbus
closedcube/ClosedCube OPT3001@^1.1.2
boschsensortec/BSEC Software Library @ ^1.6.1480
beegee-tokyo/SX126x-Arduino@^1.2.1
build_flags = -L".pio/libdeps/wiscore_rak4631/BSEC Software Library/src/cortex-m4/fpv4-sp-d16-hard"
-DREGION_AU915
upload_port = COM15
upload_speed = 115200
monitor_speed = 9600

1 Like

This topic might be of some use maybe?

Cheers,
Norm.

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}

code

#include <Arduino.h>
void setup() {
    Serial.begin(9600);
    Serial.println("This firmware was uploaded from " + String(UPLOAD_PORT));
    Serial.println("At " + String(UPLOAD_SPEED) + " baud.");
    Serial.println("Monitor speed was " + String(MONITOR_SPEED) + " baud.");

}
void loop() {}

prints

This firmware was uploaded from COM14
At 115200 baud.
Monitor speed was 9600 baud.

For your concrete platformio.ini you can e.g. try…

[upload_settings]
upload_port = COM15
upload_speed = 115200
monitor_speed = 9600

[env:wiscore_rak4631]
platform = nordicnrf52
board = wiscore_rak4631
framework = arduino
lib_deps = 
    ArduinoModbus
    closedcube/ClosedCube OPT3001@^1.1.2
    boschsensortec/BSEC Software Library @ ^1.6.1480
    beegee-tokyo/SX126x-Arduino@^1.2.1
build_flags =
   -L".pio/libdeps/wiscore_rak4631/BSEC Software Library/src/cortex-m4/fpv4-sp-d16-hard"
   -DREGION_AU915
   -D UPLOAD_PORT=\"${upload_settings.upload_port}\"
   -D UPLOAD_SPEED=${upload_settings.upload_speed}
   -D MONITOR_SPEED=${upload_settings.monitor_speed}
extends = upload_settings
3 Likes

Cool! I will test them tomorrow. Now, I need to sleep.

Have I nice night!

It is working fine :smile:

Thank you very much!

main.cpp

#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++;
        }
}
platformio.ini

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[upload_settings]
upload_port = COM15
upload_speed = 115200
monitor_speed = 9600
[env:wiscore_rak4631]
platform = nordicnrf52
board = wiscore_rak4631
framework = arduino
; lib_deps = 
;    ArduinoModbus
;    closedcube/ClosedCube OPT3001@^1.1.2
;    boschsensortec/BSEC Software Library @ ^1.6.1480
;    beegee-tokyo/SX126x-Arduino@^1.2.1
build_flags =
   -L".pio/libdeps/wiscore_rak4631/BSEC Software Library/src/cortex-m4/fpv4-sp-d16-hard"
;   -DREGION_AU915
   -D UPLOAD_PORT=\"${upload_settings.upload_port}\"
   -D UPLOAD_SPEED=${upload_settings.upload_speed}
   -D MONITOR_SPEED=${upload_settings.monitor_speed}
extends = upload_settings

1 Like

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 )

Max,

You are right!

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 :slight_smile:

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.

Thank you by your support.

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 :slight_smile:

Cláudio