I'm coding a library which handles sleep procedures.

Since I'm using both ESP826 and ESP32, which uses different sleep commands ( and WiFi ), I'm looking for a way to detect the MCU, while NOT using a boolean flag that indicates so.


  • 1
    #if defined(ESP8266), #if defined(ESP32)
    – Juraj
    Nov 15 '20 at 9:33
  • 1
    how could you use a boolean flag? the code for esp32 would not compile on esp8266 and vice versa
    – Juraj
    Nov 15 '20 at 9:37
  • @Juraj the part that code differs, I need the flag for.
    – guyd
    Nov 16 '20 at 19:06
  • you have in the question "while NOT using a boolean flag that indicates so." so you exclude a not existing option
    – Juraj
    Nov 16 '20 at 20:51

I take this to mean that you do not want to have to ask the user of the code to configure a boolean flag.

The ESP8266 Arduino package provides -DARDUINO_ARCH_ESP8266 -DESP8266 command-line options these while calling g++. These effectively #define feature test macros as though they were at the beginning of the source code.

For ESP32 it does likewise with -DARDUINO_ARCH_ESP32 -DESP32

You could perform tests in your code such as:

#if defined (ARDUINO_ARCH_ESP8266)
// do one thing
#elif defined(ESP32)
// do another
#error Architecture unrecognized by this code.

There are likely also macros like these that are predefined by the xtensa g++ compiler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.