How to use EDP-IDF examples with VS Code and PlatformIO

Hi all,
i just started to work with ESP32 and PlatformIO, but i struggle with the use of ESP-IDF examples. I use VS Code + PlatformIO Extension 3.4.0 and ESP-IDF 4.3.
If i follow PlatformIO get started i can get the “WiFi softAP Example” to work. Then i tried to the same procedure with the ESP-IDF example Modbus Serial Slave and i receice the following outpur, if i build it. I tried several times and searched in this forum and google. Is there any easy way to use the ESP-IDF examples with Platform IO?

    > Executing task in folder esp-modbus-test: C:\.platformio\penv\Scripts\platformio.exe run <

    Processing esp-wrover-kit (platform: espressif32; board: esp-wrover-kit; framework: espidf)
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
    CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp-wrover-kit.html
    PLATFORM: Espressif 32 (3.4.0) > Espressif ESP-WROVER-KIT
    HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
    DEBUG: Current (ftdi) On-board (ftdi) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
    PACKAGES:
     - framework-espidf 3.40301.0 (4.3.1)
     - tool-cmake 3.16.4
     - tool-esptoolpy 1.30100.210531 (3.1.0)
     - tool-idf 1.0.1
     - tool-mconf 1.4060000.20190628 (406.0.0)
     - tool-ninja 1.9.0
     - toolchain-esp32ulp 1.22851.191205 (2.28.51)
     - toolchain-riscv32-esp 8.4.0+2021r1
     - toolchain-xtensa-esp32 8.4.0+2021r1
     - toolchain-xtensa-esp32s2 8.4.0+2021r1
    Reading CMake configuration...
    **Error: Couldn't find the main target of the project!**

Is the folder where all the application source files are stored called main orsrc/ in your project? Try either renaming the folder to src or add

[platformio]
src_dir = main

in your platformio.ini if it’s called main.

it is called src.

To clarify what i have done befor: I created a new project with PlatformIO on ESP-IDF framework. Then i copied the the code from the “ESP-IDF Modbus Serial Slave” Example into the “main.c” file in the src folder. The i added idf_component_register(SRCS "main.c") to the platformio.ini file in the src folder. Additionally i added set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/protocols/modbus/mb_example_common) as include path (via C/C++ extension), since the modbus_params.h was not found. Now this path is also listed in the CMakeLists.txt in the project folder.

Is this way to use the ESP-IDF examples with PlatformIO correct?

Does it compile and run? If yes, yes.

That can’t be right, that’s a CMakeLists.txt instruction.

Otherwise you always need to take care of the basics: Use the same ESP-IDF version that PlatformIO uses (per releases, 4.3.1), never the master branch and copy the CMakeLists.txt files and sdkconfig.h files too. Most exactly also need some common components that you need to add as per docs.

So i updated ESP-IDF to 4.3.1. I created a completly new project with platformIO. Than i tried the following steps:

  1. I copied the the whole ESP-IDF example into the platformIO project folder
  2. i added
    [platformio]src_dir=main
    to the platformio.ini file and deletet the src folder
  3. I changed the project name in the CMake file in the root folder to the name of the platformio project (same as name of root folder)
  4. I changed the project name in the CMake file in the main folder to the name of the platformio project (same as name of root folder)

If I built this project with the ESP-IDF CMD it compiles it succesfull.
If I try to built it with the PlatformIO built in VS Code i receive the following output:

>     Executing task in folder esp-modbus-test: C:\.platformio\penv\Scripts\platformio.exe run <

Processing esp32dev (platform: espressif32; board: esp32dev; framework: espidf)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 (3.4.0) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-espidf 3.40301.0 (4.3.1)
 - tool-cmake 3.16.4
 - tool-esptoolpy 1.30100.210531 (3.1.0)
 - tool-idf 1.0.1
 - tool-mconf 1.4060000.20190628 (406.0.0)
 - tool-ninja 1.9.0
 - toolchain-esp32ulp 1.22851.191205 (2.28.51)
 - toolchain-riscv32-esp 8.4.0+2021r1
 - toolchain-xtensa-esp32 8.4.0+2021r1
 - toolchain-xtensa-esp32s2 8.4.0+2021r1
Reading CMake configuration...
KeyError: 'compileGroups':
  File "C:\.platformio\penv\Lib\site-packages\platformio\builder\main.py", line 180:
    env.SConscript("$BUILD_SCRIPT")
  File "C:\.platformio\packages\tool-scons\scons-local-4.2.0\SCons\Script\SConscript.py", line 597:
    return _SConscript(self.fs, *files, **subst_kw)
  File "C:\.platformio\packages\tool-scons\scons-local-4.2.0\SCons\Script\SConscript.py", line 285:
    exec(compile(scriptdata, scriptname, 'exec'), call_stack[-1].globals)
  File "C:\.platformio\platforms\espressif32\builder\main.py", line 233:
    target_elf = env.BuildProgram()
  File "C:\.platformio\packages\tool-scons\scons-local-4.2.0\SCons\Util.py", line 748:
    return self.method(*nargs, **kwargs)
  File "C:\.platformio\penv\Lib\site-packages\platformio\builder\tools\platformio.py", line 61:
    env.ProcessProgramDeps()
  File "C:\.platformio\packages\tool-scons\scons-local-4.2.0\SCons\Util.py", line 748:
    return self.method(*nargs, **kwargs)
  File "C:\.platformio\penv\Lib\site-packages\platformio\builder\tools\platformio.py", line 128:
    env.BuildFrameworks(env.get("PIOFRAMEWORK"))
  File "C:\.platformio\packages\tool-scons\scons-local-4.2.0\SCons\Util.py", line 748:
    return self.method(*nargs, **kwargs)
  File "C:\.platformio\penv\Lib\site-packages\platformio\builder\tools\platformio.py", line 343:
    SConscript(env.GetFrameworkScript(f), exports="env")
  File "C:\.platformio\packages\tool-scons\scons-local-4.2.0\SCons\Script\SConscript.py", line 660:
    return method(*args, **kw)
  File "C:\.platformio\packages\tool-scons\scons-local-4.2.0\SCons\Script\SConscript.py", line 597:
    return _SConscript(self.fs, *files, **subst_kw)
  File "C:\.platformio\packages\tool-scons\scons-local-4.2.0\SCons\Script\SConscript.py", line 285:
    exec(compile(scriptdata, scriptname, 'exec'), call_stack[-1].globals)
  File "C:\.platformio\platforms\espressif32\builder\frameworks\espidf.py", line 1227:
    project_flags = get_app_flags(project_config, default_config)
  File "C:\.platformio\platforms\espressif32\builder\frameworks\espidf.py", line 406:
    default_flags = _extract_flags(default_config)
  File "C:\.platformio\platforms\espressif32\builder\frameworks\espidf.py", line 393:
    for cg in config["compileGroups"]:
===================================================================================== [FAILED] Took 9.52 seconds =====================================================================================
The terminal process "C:\.platformio\penv\Scripts\platformio.exe 'run'" terminated with exit code: 1.

What do i have to to that i can built it with the platformio built command?

I really would appreciate further help :slight_smile:

I tried more ESP-IDF examples, but still receive
"Error: Couldn't find the main target of the project!"

So it seems to be a problem with the cmake in combination with platformIO. If i use a example which uses “external components” (like the ethernet iperf), which have a additional cmake file, platformIO/ Cmake does not build and the “could not find the main target of the project” error pops up.
The ESP-IDF has no problem with that and compiles the project and all external components.

If I use platformIO, i have to put the external files as a library in the lib folder and delete the set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples in the cmake file in the root directory. No error occurs.

Can you point to which example exactly is failing and how you converted it to a PlatformIO project?

Example: Ethernet/Ipferf (ESP-IDF).
The way i wanted to start the example was the following:

  1. Open PlatformIO Example: espidf-blink
  2. Copied the files “cmd_ethernet.c”, “cmd_ethernet.h”, “ethernet_example_main.c” into the directory of the espidf-blink
  3. Changed the name of the function “app_main” in the “ethernet_example_main.c” into “ethernet_app_main” (to avoid a conflict)
  4. Added a “ethernet_example_main.h” with the declaration of the function “ethernet_app_main”
  5. deleted the code regarding the blink example in the “main.c”. Included all header files.
  6. Added " cmd_ethernet.c" “ethernet_example_main.c” to the CMakeList.txt in the src folder
  7. copied the “Kconfig.projbuild” from the iperf example main folder to the blink example src folder
  8. added
    “set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/system/console/components
    $ENV{IDF_PATH}/examples/wifi/iperf/components)”
    to the cMakelist.txt in the blink example root folder. This comes from the cmakelist.txt in the iperf example root folder.

Then i tried to build:

“Reading CMake configuration…
Error: Couldn’t find the main target of the project!”

-If i undo step 8: There is no “Couldn’t find the main target of the project!” Error. But the link to these components are missing.

-This way is working:
a) Open PlatformIO Example: espidf-blink (create a new one. do not use the one from the steps above)
b) add a folder “iperf” to the “lib” directory
c) Put the “iperf component” and the “console components” (cmd_nvs and cmd_system) into seperate libraries (seperate folders) and copy the 3 libraries to the lib directory from b).
(\esp-idf\examples\wifi\iperf\components)
(esp-idf\examples\system\console\components)
d) put the “cmd_ethernet.c” and “cmd_ethernet.h” (esp-idf\examples\ethernet\iperf\main) into a library.
d) Copy the source code from the “ethernet_example_main.c” (examples\ethernet\iperf\main) to the “blink.c” file of the new project from step a)
e) include the header files of all libraries we copied
f) Add the following to the platformio.ini

lib_extra_dirs =
lib
ib_deps =
cmd_nvs
cmd_system
cmd_ethernet