Using esp-idf library within the Arduino Framework (ESP32)

The base example for ESP-IDF + Arduino as a component is at platform-espressif32/examples/espidf-arduino-blink at develop · platformio/platform-espressif32 · GitHub. Maybe you can start from that and then add esp-homekit-sdk as a component, just as you say you haved done previously which worked.

Well if it’s a ESP-IDF library (/component) and you must absolutely use it, the best way would be to keep using the ESP-IDF build system. This also enables the intended configuration way of that component via menuconfig / KConfig files. Of course you can copy all the code files into a new library but then the configuration via the CONFIG_.. macros also has to be re-written somehow, generating additional work for you.

Indeed, ESP-IDF is based on the CMake build system as you’ve already said. The way the Arduino IDE builds the core can be considered its own build-system and is implemented in Java in the IDE, so it’s not using CMake. PlatformIO builds ESP-IDF projects by also calling in the exact same CMake build system (that’s why you still have to create CMakeLists.txt files etc although we have a platformio.ini config file). Arduino-ESP32 projects are built by using (Python) SCons build system, setup in such a way that the compilation process and option matches that of what the Arduino IDE would do.

However, the most important fact about the Arduino-ESP32 core in that regard is that “Arduino as a component of ESP-IDF” and “just the Arduino-ESP32 core” are the same thing semantically, just built a little bit differently. The Arduino-ESP32 core uses a precompiled ESP-IDF version as its entire base. They don’t re-implement the same peripheral drivers etc. from scratch, they just use ESP-IDF and their APIs. You can e.g. see the precompiled libraries in https://github.com/espressif/arduino-esp32/tree/master/tools/sdk/esp32/lib where each .a file is a static library which maps to one precompiled component from the ESP-IDF version the core is based on. Then the additional Arduino API layer is compiled on top of that, together with your sketch, and linked together with those precompiled libs. Any Arduino-ESP32 sketch can also call the APIs which stem from the used ESP-IDF core directly and this will work.

Of course it still makes sense to do what PIO does with the framework = espidf, arduino option, that is: Compiling ESP-IDF from source gives the user the ability to change the configuration of the ESP-IDF core. When using pure Arduino-ESP32, users are stuck with what the precompiled version has been configured for – alas this may be sufficient for most, but not in some special cases.

You just said that your library, esp-homekit-sdk, is an ESP-IDF library but you want to use the Arduino framework, did you switch the words here? Either way, as I’ve explained above, since the Arduino core is an extension of ESP-IDF, and Arduino libraries are an extension of Arduino, this will work. In that example project we’re demonstrating how to use the Arduino WiFi library with ESP-IDF as base and Arduino as a component.

Also, have you seen GitHub - Mixiaoxiao/Arduino-HomeKit-ESP32: [Deprecated] Native Apple HomeKit accessory implementation for the ESP32 Arduino core.? An Arduino library for the ESP32 claiming to implement HomeKit functionality. If you want to 100% stay in Arduino, you may want to try and use their APIs. Yes, the github says they’re “deprecated” because GitHub - espressif/esp-homekit-sdk exists, but still this an Arduino library and the other is a ESP-IDF component, so I think it has a right to exist separately.

2 Likes