Compile on both ESP32 and ESP8266

what is the best way to

#ifdef ESP32
and
#ifdef ESP8266

is there some variable I can use to ifdef some part of the code in case using esp8266 and some part in case I use esp32?

thanks

ESP8266 and ARDUINO_ARCH_ESP8266 are defined for the ESP8266 using the Arduino framework, so ESP8266 is one define you can look for. ESP32 and ARDUINO_ARCH_ESP32 are defined for the ESP32… so they are the others you can check for. Something like the below would do the necessary selection… now it just puts a compile-time message in saying which branch is being used, or aborts the build if it’s not ESP8266 or ESP32…

I usually just do a verbose build, and see what defines are being thrown around to see if there is anything I can use if I need to do unique code for different platforms in the one project.

#if defined(ESP8266)
#pragma message "ESP8266 stuff happening!"
#elif defined(ESP32)
#pragma message "ESP32 stuff happening!"
#else
#error "This ain't a ESP8266 or ESP32, dumbo!"
#endif
4 Likes

thanks my friend :smiley:

1 Like