How to pass extra #define variables to esp32 (under Arduino framework)

Context:

I want to pass a some #define type values to the src, while compiling and uploading to my esp32 from:

  1. from platformio.ini
  2. command line.

Say wifi credentials, for example.

How do I do that?

Current platformio.ini content:

[env:lolin_d32]
platform = espressif32
board = lolin_d32
framework = arduino
lib_deps = PubSubClient

; --------------
; mac OS configs
; --------------
upload_port = /dev/tty.usbserial-130
upload_speed = 460800           ; More than that, doesn't not working
board_build.f_cpu = 240000000L  ; sets CPU's frequency to 240 MHz
board_build.f_flash = 80000000L ; sets flash's RW frequency to 80MHz

monitor_port = /dev/tty.usbserial-130
monitor_speed = 115200
build_flags = 
    -DCORE_DEBUG_LEVEL=0  ; None
    ; -DCORE_DEBUG_LEVEL=1  ; Error
    ; -DCORE_DEBUG_LEVEL=2  ; Warn
    ; -DCORE_DEBUG_LEVEL=3  ; Info
    ; -DCORE_DEBUG_LEVEL=4  ; Debug
    ; -DCORE_DEBUG_LEVEL=5  ; Verbose

Current main.cpp content:

// **JUST Test code

#include <Arduino.h>

// #WIFI_SSID "xyz" // want to define this in ini file, how to?

void setup()
{
  Serial.begin(115200);
  String ssid = WIFI_SSID;
  Serial.println(ssid);

}

void loop()
{
}

How do we pass a value form .ini file to the main.cpp for to be included during compilation?

Duplicate of PlatformIO and WiFi credentials.

1 Like

Thanks, I will take it there!

But,
I was talking a look there and followed the guide.
It now compiles, but shows red underline on the macros. I call macros in another file in src directory and not main.ccp (but even in main.cpp, it shows red underline

platformio.ini
[env:lolin_d32]
platform = espressif32
board = lolin_d32
framework = arduino
lib_deps = PubSubClient
board_build.f_cpu = 240000000L  ; sets CPU's frequency to 240 MHz
board_build.f_flash = 80000000L ; sets flash's RW frequency to 80MHz
upload_speed = 460800           ; More than that, doesn't not working
monitor_speed = 115200

; --------------
; mac OS configs
; --------------
upload_port = /dev/tty.usbserial-130
monitor_port = /dev/tty.usbserial-130

; ----------------------
; Ubuntu (Linux) configs
; ----------------------
; upload_port = /dev/ttyUSB0
; monitor_port = /dev/ttyUSB0

build_flags = 
    ; --------------------------
    ; Verbose levels for deubgs
    ; --------------------------
    -DCORE_DEBUG_LEVEL=0  ; None
    ; -DCORE_DEBUG_LEVEL=1  ; Error
    ; -DCORE_DEBUG_LEVEL=2  ; Warn
    ; -DCORE_DEBUG_LEVEL=3  ; Info
    ; -DCORE_DEBUG_LEVEL=4  ; Debug
    ; -DCORE_DEBUG_LEVEL=5  ; Verbose

    ; ------------------------------------------------------------
    ; External Flags to be passed to the script during compilation
    ; ------------------------------------------------------------
    '-DWIFI_SSID="SDPAP"'
    '-DWIFI_PWD="Arpita_1491"'
    -DSET_STATIC_IP=. 
    ; Question: Does this mean an empty SET_STATIC_IP will be declared,
    ; so that the below can happen (in one of the the src director\'s files, say in main.cpp)?
    ; #ifdef SET_STATIC_IP
    ; your stuff here
    ; #endif

I have a config.h
As you can see in the config.h file the macros are still red (But it compiles)

On highlighting one of them, it says: identifier "WIFI_SSID" is undefinedC/C++(20)

And the config.h is called in main.cpp like this:

#include <Arduino.h>
#include "config.h"

Is the above red lining is a linker error for VSCode?
if so, how to solve?

1 Like

Just VS code needed a reload :sweat_smile:
It works now

1 Like