WIFI channel sensor for ESP-IDF framework

Hello I have had a WIFI channel sensor running under the arduino framework for about 6 months (I use this to monitor if ESP devices are swapping WAPs). The code for the sensor using the arduino framework is:

  - platform: template
    name: Wifi Channel
    id: wifi_channel
    icon: "mdi:wifi-marker"
    lambda: return WiFi.channel();
    accuracy_decimals: 0
    update_interval: 60s

However, I have just switched to the ESP-IDF framework to use the bluetooth proxy, but when compiling it says “WiFi is out of scope” and fails.

I have tried several other ideas from google on how to read the wifi channel but none seem to work/compile with the ESP-IDF framework.

Does anyone know a working solution to get the WIFI channel when using the ESP-IDF framework?

Might be easier to use the Wifi Info Text Sensor

# Example configuration entry
  - platform: wifi_info
      name: ESP IP Address
        name: ESP IP Address 0
        name: ESP IP Address 1
        name: ESP IP Address 2
        name: ESP IP Address 3
        name: ESP IP Address 4
      name: ESP Connected SSID
      name: ESP Connected BSSID
      name: ESP Mac Wifi Address
      name: ESP Latest Scan Results
      name: ESP DNS Address

The BSSID will tell you which AP you’re connected to.

By WiFi “channel”, I read OP as meaning like this?

I feel like the answer may be in here somewhere, possibly around line 314.


Yes I would like the actual WIFI channel. I do have the BSSID but I don’t find it very user friendly. I could convert the BSSID to the channel but I would prefer not to do that as if I the channels change on the routers I would need to remember to do it in the conversion.

Yesterday I spent quite sometime looking at Mahko_Mahko’s above reference from line 314 and this would be an ideal solution that would be framework independent. Through the use of get_channel() on the WiFiAP object for a client connection.

Unfortunately it seems it is impossible to obtain the required WiFiAP object it operates on as it is protected and no function of WIFIComponent allows you to get the correct WiFiAP object. WIFIComponent does have a function get_ap() but this only returns the WiFiAP object when the controller is operating in access point mode rather than client mode. There is a second WiFiAP object in WIFIComponent that is inaccessible which is used for the client connection.

I can see only two solutions add a function to WIFIComponent that allows you to get the protected WiFiAP object, or somehow override the C++ protection (I dont know how as I am a lot happier with C than I am C++)

I think it is in principle possible to go directly to the underlying ESP-IDF framework to extract the channel, with code something like the following. but this fails to compile as I am pretty sure I need to include the framework headers and I cant work it out how to do this.

    lambda: |-
      wifi_ap_record_t wifidata;
      return wifidata.primary;
1 Like

I would also post your question and findings so far on the ESPHome Discord.

Quite a few developers hang out over there and they may have more of a clue about the lower level stuff.

Mahko thanks I will look into discord as its not something I have used very much although I do have an account.

1 Like