ESP32-C5 Runtime error on WiFi.mode()

Hello everyone.
I’m currently running a test project with ESP32-C5-DevKitC-1-N8R4,
but I’m encountering a runtime error.
I’ve tried to fix it, but I’m not sure if it’s the right solution.
Can someone tell me the best solution?

The build completed (although the message esp_idf_size: error: unrecognized arguments: --ng appears when linking),
but I’m able to load and run the code on the device.
[Status]

  1. Target device: ESP32-C5-DevKitC-1-N8R4
  2. Environment:
    (1) OS
    Ubuntu 24.04.3 LTS
    (2) Development Tools
    Visual Studio Code 1.107.1 + pioarduino plugin
    (3) platformio.ini
    [env:esp32-c5-devkitc1-n8r4]
    platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip
    board = esp32-c5-devkitc1-n8r4
    framework = arduino
  3. Runtime Error
    When WiFi.mode(WIFI_STA) was executed, the following message was output to the Serial port:
    “E (1117) wifi: can’t get wifi protocol under WiFi band mode WIFI_BAND_MODE_AUTO, please use esp_wifi_get_protocols instead”
    After this message was output, I performed a dual-band network scan, but there didn’t seem to be any problems and no error messages were displayed.
  4. Current Solution
    In (myhome)/.platformio/packages/framework-arduinoespressif32/libraries/WiFi/src/WiFiGeneric.cpp,
    I changed two occurrences of esp_wifi_get_protocol(WIFI_IF_STA,(uint8_t *)&current_protocol)”
    to “esp_wifi_get_protocols(WIFI_IF_STA,(wifi_protocols_t *)&current_protocol)”
    within bool WiFiGenericClass::mode(wifi_mode_t m).
    This fix no longer causes runtime errors, but I’m not sure if it was appropriate.

Thank you in advance.

You problably better ask the Espressif guys at GitHub - espressif/arduino-esp32: Arduino core for the ESP32 or on their “Arduino Core for Espressif” discord server.

1 Like

Thank you so much for your quick response, Boris!
It’s very helpful.
I’ll consider posting a separate comment about your suggested community.
For now, I’ve modified the library source code to allow conditional compilation.

#if SOC_WIFI_SUPPORT_5G
err = esp_wifi_get_protocols(WIFI_IF_STA, (wifi_protocols_t *)&current_protocol);
#else
err = esp_wifi_get_protocol(WIFI_IF_STA, (uint8_t *)&current_protocol);
#endif

This modification should make the code compatible with both the ESP32-C5, which supports the 5GHz band, and other devices that only support the 2.4GHz band, I think.

1 Like

I woudln’t modify a core library.

This kind of “fix” will just work for you, nobody else.
Open an issue at the espressif repository or create a pull request including your fix.

Doing this way, the issue will be fixed for everyone, not just for you :slight_smile:

1 Like

Yes, I agree with your point.
What I’m doing now is just a temporary solution.
Thank you very much for your accurate comment.

You may need to check here. Wi-Fi - ESP32 - — ESP-IDF Programming Guide release-v5.1 documentation

bidrohini
Thank you for your response.
The link you specified is just for “ESP32”, I think.
The issue is on “ESP32-C5” : 2.4GHz/5GHz available.
In addition, no problem on ESP32-C6 (2.4GHz only).
I think the meaning of error message is as “Use esp_wifi_get_protocols() instead of esp_wifi_get_protocol()”.
esp_wifi_get_protocol() is used in “WiFiGeneric.cpp” of core library.

If you just want to avoid this message, set the band (WIFI_BAND_MODE_5G_ONLY or WIFI_BAND_MODE_2G_ONLY) like so:

    WiFi.STA.begin();
    WiFi.setBandMode(WIFI_BAND_MODE_5G_ONLY);
    WiFi.begin(SSID, PASS);
1 Like

Thank you for your suggestion.
I will take it into consideration.