PlatformIO/Arduino/ESP-IDF: 'Serial' was not declared in this scope

User avatar
aremmell
Posts: 4
Joined: Tue Dec 12, 2023 10:41 pm

PlatformIO/Arduino/ESP-IDF: 'Serial' was not declared in this scope

Postby aremmell » Tue Dec 12, 2023 11:06 pm

I am writing a library that I'd like to be as portable as possible, and my development boards are all ESP32-based (the board I'm using currently is the Unexpected Maker Feather S3). I had been primarily using PlatformIO, with platform = espressif32 and framework = arduino until yesterday, when I realized I was going to need some of the APIs implemented in ESP-IDF.

I then spent several hours trying to get it compiling in the ESP-IDF VS Code extension, with no luck–I couldn't get all of my Arduino-based dependencies to cooperate. As a result, I kept researching and discovered framework-arduinoespressif32 and framework-espidf which I am now using in PlatformIO as follows:

Code: Select all

platform = espressif32
platform_packages = platformio/framework-arduinoespressif32
board = um_feathers3
framework = arduino, espidf
lib_deps [...]

This *almost* works for me, but I have one compiler error that I cannot figure out: 'Serial' is no longer defined, so I've got errors coming from everywhere. :oops:

I looked into it the best I could, and found HardwareSerial.h, which declares Serial if !ARDUINO_USB_CDC_ON_BOOT && !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL).

I have gone through menuconfig a dozen times, and I cannot come up with a configuration that resolves this situation.

If anyone has some insight into this problem, I would greatly appreciate being pointed in the right direction.

Thanks in advance!
- aremmell

lbernstone
Posts: 797
Joined: Mon Jul 22, 2019 3:20 pm

Re: PlatformIO/Arduino/ESP-IDF: 'Serial' was not declared in this scope

Postby lbernstone » Wed Dec 13, 2023 3:06 am

arduino-esp32 is built on top of esp-idf. This means all the APIs that aren't specifically excluded (due to memory or other constraints) are included in arduino. You simply need to include the appropriate header file, generally in the same location you would reference it in an IDF program. The included functions should then be available. If you get a symbol missing error, then you have hit one of those excluded functions.
It sounds like the missing CONFIG entry is CONFIG_AUTOSTART_ARDUINO, which calls initArduino() via main.cpp. You will get best results if you use the sdkconfig that is in the arduino core.

User avatar
aremmell
Posts: 4
Joined: Tue Dec 12, 2023 10:41 pm

Re: PlatformIO/Arduino/ESP-IDF: 'Serial' was not declared in this scope

Postby aremmell » Wed Dec 13, 2023 4:04 am

Thanks for the reply. I copied the sdkconfig from core into my project, but I'm still getting no Serial symbol defined, even though this time, HardwareSerial.h has an extern HardwareSerial Serial; in it.

Serial is even undefined in the core itself:

Code: Select all

/Users/ryan/.platformio/packages/framework-arduinoespressif32/cores/esp32/main.cpp: In function 'void app_main()':
/Users/ryan/.platformio/packages/framework-arduinoespressif32/cores/esp32/main.cpp:58:5: error: 'Serial' was not declared in this scope
So, I'm basically seemingly stuck between choosing undefined Serial or file not found tusb.h, even though as far as I can tell, it is in my include path.

I must be doing something really wrong. As far as sdkconfig goes:

  • I have one at .pio/sdkconfig
  • I have in the root dir: sdkconfig.feathers3 and sdkconfig.old
So, something moved sdkconfig to the .pio directory upon build, and renamed it to sdkconfig.board_type? Which one of these many files is the one actually used for the compilation process?

I'm really confused at this point, and ready to spend all night banging my head on the desk if I have to!!!
- aremmell

User avatar
aremmell
Posts: 4
Joined: Tue Dec 12, 2023 10:41 pm

Re: PlatformIO/Arduino/ESP-IDF: 'Serial' was not declared in this scope

Postby aremmell » Thu Dec 14, 2023 12:07 am

So, I got past the undefined symbol by adding this to platformio.ini for this board:

Code: Select all

build_flags = -DCORE_DEBUG_LEVEL=3 -DARDUINO_USB_MODE -DARDUINO_USB_CDC_ON_BOOT=1
It builds, but Serial.println does not result in any text coming back over the connection (yes, I did Serial.begin()). Strangely (or perhaps not, I'll leave that up to you) a straight printf or a log_e both result in text output.

Does anyone know why, and how to fix it?
- aremmell

mcknly
Posts: 1
Joined: Mon May 20, 2024 6:12 pm

Re: PlatformIO/Arduino/ESP-IDF: 'Serial' was not declared in this scope

Postby mcknly » Mon May 20, 2024 6:15 pm

Same issue, mine was caused by using 'main.c' instead of 'main.cpp'. Fixed by using cpp extension.
Source: https://community.platformio.org/t/iden ... ared/25114

Who is online

Users browsing this forum: No registered users and 39 guests