If condition on different boards?

Is it possible to define IF conditions for different boards?

I wanna share the same code for different boards (esp32 & esp8266). These boards need different WiFi libraries. How can I SWITCH them by an IF condition? Is this possible?

I saw, that I can add build params like -D ESP32.

But is there something which is set already by PlatformIO?

Yes, as you can see in e.g. here each board has specific macros. But PIO also defines macros like

-DPLATFORMIO=40000 -DARDUINO_Heltec_WIFI_Kit_32 -DESP32 -DESP_PLATFORM -DF_CPU=240000000L -DHAVE_CONFIG_H -DMBEDTLS_CONFIG_FILE=\"mbedtls/esp_config.h\" -DARDUINO=10805 -DARDUINO_ARCH_ESP32 -DARDUINO_VARIANT=\"heltec_wifi_kit_32\" "-DARDUINO_BOARD=\"Heltec WIFI Kit 32\""

(for a heltec esp32 board)

So you could e.g. use

#ifdef ARDUINO_ARCH_ESP32
//include ESP32 specific libs
#elif defined(ARDUINO_ARCH_ESP8266) 
//include esp8266 specific libs
#else  
//...
#endif

You can do a verbose compilation run with pio run -v.

3 Likes

Also take a look at Redirecting...

2 Likes