C files listed under “esphome: / includes:” get compiled and linked automatically. They can #include esp-idf framework header files. Also, header files listed under “esphome: / includes:” get included by the generated main.cpp and can declare functions so that they can be called from lambdas. For example, this can be placed in the esphome: section:
includes:
- idfWifi.h
- idfWifi.c
and when idfWifi.h contains this:
extern "C"
{
int idfWifiGetChannelNum (void);
}
and idfWifi.c contains this:
#include "esp_wifi.h"
int idfWifiGetChannelNum (void)
{
wifi_ap_record_t ap_info;
if (esp_wifi_sta_get_ap_info (&ap_info) != ESP_OK)
return (-1);
return (ap_info.primary);
}
then esphome yaml code can define a text_sensor using code like this:
text_sensor:
- platform: template
id: wifi_channel
name: "Net WiFi Channel"
lambda: |-
static char str[3]; // two digits (or -1) plus NULL
sprintf(str, "%2d", idfWifiGetChannelNum());
return (std::string) str;
I tested this with idf 5.2.1 and platformio 6.7.0 and it worked. YMMV, Maarten