Skip to content

Conversation

@flacjacket
Copy link
Contributor

@flacjacket flacjacket commented Jul 12, 2021

What does this implement/fix?

Previously, the ethernet component would try to setup and connect before
esp_eth_enable is called, which can be called right after
esp_eth_init is called. The rest of the logic in start_connect_,
for example setting up the hostname and dhcp information, should wait
until after the ETH_START event is received. This would result in the
first connection attempt always failing (as esp_eth_enable would only
be called on the second run through start_connect_) and once
connected, would fail to set the hostname correctly.

This PR correctly runs esp_eth_enable and esp_eth_init in the
setup for the component. This allows the ethernet component to
connect on the first attempt, saving 15 sec (the connection timeout) on
component startup.

This PR adds some additional state tracking to the ethernet component to
be able to know when the start event has been received and start the
connection once the eth_start event is received, and also track the
connection status through this state machine. This makes the state more
explicit, rather than using last_connected_ and connected_ to track
state.

With this PR, because of the now correct setup, the hostname is
correctly set. This could have been seen earlier by error checking the
return from tcpip_adapter_set_hostname, as this would fail on the
first call, before the esp_eth_enable. Error checking is added to the
call to set the hostname, to catch further problems with this.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Related issue or feature (if applicable):

Fixes esphome/issues#2184

Pull request in esphome-docs with documentation (if applicable): N/A

Test Environment

  • ESP32
  • ESP8266

Example entry for config.yaml:

esphome:
  name: testdevice
  platform: ESP32
  board: wesp32

ethernet:
  type: LAN8720
  mdc_pin: GPIO16
  mdio_pin: GPIO17
  clk_mode: GPIO0_IN
  domain: !secret my_domain

logger:

api:

ota:
  password: !secret ota_password

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

Ran test yaml on wesp32 device, saw that device would connect correctly
on the first attempt without requiring a re-try on the ethernet
connection, and once connected, had the correct hostname. Disconnected
and re-connected the ethernet cable to observe the state correctly
transition between connecting and connected.

If user exposed functionality or configuration variables are added/changed:

Previously, the ethernet component would try to setup and connect before
`esp_eth_enable` is called, which can be called right after
`esp_eth_init` is called.  The rest of the logic in `start_connect_`,
for example setting up the hostname and dhcp information, should wait
until after the ETH_START event is received.  This would result in the
first connection attempt always failing (as `esp_eth_enable` would only
be called on the second run through `start_connect_`) and once
connected, would fail to set the hostname correctly.

This PR correctly runs `esp_eth_enable` and `esp_eth_init` in the
`setup` for the component.  This allows the ethernet component to
connect on the first attempt, saving 15 sec (the connection timeout) on
component startup.

This PR adds some additional state tracking to the ethernet component to
be able to know when the start event has been received and start the
connection once the eth_start event is received, and also track the
connection status through this state machine.  This makes the state more
explicit, rather than using `last_connected_` and `connected_` to track
state.

With this PR, because of the now correct setup, the hostname is
correctly set.  This could have been seen earlier by error checking the
return from `tcpip_adapter_set_hostname`, as this would fail on the
first call, before the esp_eth_enable.  Error checking is added to the
call to set the hostname, to catch further problems with this.

Fixes esphome/issues#2184
flacjacket and others added 2 commits July 13, 2021 07:33
Propagae previous change to all uses of the enum
@jesserockz jesserockz added this to the 1.20.0b3 milestone Jul 15, 2021
@jesserockz jesserockz merged commit 628a94b into esphome:dev Jul 15, 2021
jesserockz pushed a commit that referenced this pull request Jul 18, 2021
This was referenced Jul 18, 2021
This was referenced Jul 21, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Sep 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hostname always "espressif" when using Ethernet on ESP32

3 participants