espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.31k stars 7.36k forks source link

Supporth for linkStatus() to detect Ethernet cable when using w5500 #9079

Open kapyaar opened 8 months ago

kapyaar commented 8 months ago

Related area

ESP32 + ETH

Hardware specification

ESP32 Dev Module, Generic with W5500

Is your feature request related to a problem?

In the Ethernet library for W5500, there is option to detect if ethernet cable is plugged in.

https://github.com/arduino-libraries/Ethernet/blob/39103da0e1bc569023625ee4693272773397dbb6/src/Ethernet.cpp#L98

Would be nice to have this available when using ETH.h

Describe the solution you'd like

Just like linkUP(), a linkStatus() function would suffice.

Describe alternatives you've considered

No response

Additional context

The purpose is to decide if cable is present, and if not, ditch Ethernet and switch to WiFi.

I have checked existing list of Feature requests and the Contribution Guide

me-no-dev commented 8 months ago

@kapyaar since we are using ESP-IDF's ETH driver, that request should be best posted there.

cc @kostaond

kostaond commented 8 months ago

@me-no-dev, link up is already indicated by the Ethernet driver by raising ETHERNET_EVENT_CONNECTED event. Here is example how to use it. Therefore, I think there is no need for ESP-IDF's Ethernet driver update.

kapyaar commented 8 months ago

@me-no-dev ok, got it :) I was not sure if this feature was there, but not exposed in code. Closing this.

@kostaond On another post, you had mentioned I try esp32.com, but that page does not load for any searches. Tried many times, different browsers, w and w/o vpn, etc. See screenshots for Brave and firefox.

forum-search-no-load

kapyaar commented 8 months ago

@kostaond ETHERNET_EVENT_CONNECTED (if same as ARDUINO_EVENT_ETH_CONNECTED) probably only checks if w5500 is there or not? I tested without the cable plugged in, and it still prints "ETH Started".

On the otherhand, In the Ethernet.h library

if (Ethernet.linkStatus() == LinkOFF) { Serial.println("Ethernet cable is not connected."); }

is specifcally looking for cable.

me-no-dev commented 8 months ago

@kapyaar

kapyaar commented 8 months ago

@me-no-dev

Understood. That is all fine. But consider the following situations.

  1. Ethernet cable is plugged in, Driver has started, but the DHCP server is taking some time (or fails) to assign an IP.
  2. Ethernet cable is unplugged. Driver starts, but never reaches "Connected" or further.

In both cases, We are waiting at "STARTED". Never goes beyond that. And Can't say conclusively if it is 1 or 2. If only Ethernet is available, alright, let's leave it there. But Since WiFi is an option here, resolving the above can be helpful, to switch to wifi as and when. One could use a timeout, but given the functionality is present, making it available would be helpful. That's all.

kostaond commented 8 months ago

Ad 1) You can isolate such situation by receiving ETHERNET_EVENT_START and ETHERNET_EVENT_CONNECTED but you don't get IP_EVENT_ETH_GOT_IP. Ad 2) You just get ETHERNET_EVENT_START.

me-no-dev commented 8 months ago

@kapyaar we will add a way to ask if link is up, but it will be done the same exact way: on CONNECTED set link UP and on DISCONNECTED set link DOWN. You can do this yourself by listening to the events. This is true for STA also. GOT_IP is sent only when static IP was not set and DHCP server responded. This has nothing to do with the physical layer (CONNECTED/DISCONNECTED)