Wesp32 ethernet ip and MAC address sensor

Well i finally got off my butt and I got my wesp32 board configured and on my network however I discovered that there are some gaps in support from WiFi to Ethernet like sensors for Ip address and MAC address. I started to build some sensors using Lambda but I cant get them to compile. I have listed the code below, any help would be very much appreciated…

wESP32.com

text_sensor:
  - platform: template
    name: "${name}_MAC_Address"
    lambda: 'return {Ethernet.macAddress().c_str()};'
    icon: mdi:expansion-card-variant
    update_interval: 1d 
  - platform: template
    name: "IP Address"
    lambda: 'return {Ethernet.localIP().c_str()};'
    icon: mdi:ip-network
    update_interval: 1d
    
Compiling /data/wesp32_01/.pioenvs/wesp32_01/src/main.cpp.o
src/main.cpp: In lambda function:
src/main.cpp:244:15: error: 'Ethernet' was not declared in this scope
       return {Ethernet.macAddress().c_str()};
               ^
src/main.cpp:244:44: error: could not convert '{<expression error>}' from '<brace-enclosed initializer list>' to 'esphome::optional<std::__cxx11::basic_string<char> >'
       return {Ethernet.macAddress().c_str()};
                                            ^
src/main.cpp: In lambda function:
src/main.cpp:247:15: error: 'Ethernet' was not declared in this scope
       return {Ethernet.localIP().c_str()};
               ^
src/main.cpp:247:41: error: could not convert '{<expression error>}' from '<brace-enclosed initializer list>' to 'esphome::optional<std::__cxx11::basic_string<char> >'
       return {Ethernet.localIP().c_str()};
                                         ^
*** [/data/wesp32_01/.pioenvs/wesp32_01/src/main.cpp.o] Error 1
========================== [FAILED] Took 7.27 seconds ==========================

I don’t see any errors one your compile logs.

@nickrout, added log entries to the OP…

I’ve just started to play with a wesp32.

Have you resolved this issue?

For those who are also looking for this, it turns out to be fairly trivial. The key is to give your ethernet component an id, and you can then use id(eth) to access this within lambdas.

ethernet:
  id: eth
  type: LAN8720
  mdc_pin: GPIO23
  mdio_pin: GPIO18
  clk_mode: GPIO0_IN
  phy_addr: 1
  power_pin: GPIO16

text_sensor:
  - platform: template
    name: "My Device IP"
    entity_category: diagnostic
    icon: "mdi:ip-network"
    lambda: return { id(eth).get_ip_address().str() };
    update_interval: 60s

2 Likes

awesome, worked like a charm!

Hi.
I have ESP32 TTGO Poe and how i can create sensor to show MAC address ? MAC address i see that probably we can show only for ESP32 wifi radio…?