Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve setup LAN DHCP connection #636

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jbaudoux
Copy link
Contributor

@jbaudoux jbaudoux commented Mar 23, 2024

You can do whatever you want with this PR. Besides improving logged message, nothing is changed

Before

Starting network connection via Ethernet/LAN
...
Ethernet Started.
Ethernet connected.
Ethernet got IP: 172.19.3.5
Waiting for DHCP address
172.19.3.5
255.255.255.0
172.19.3.1
Waiting 3 seconds to give Ethernet shield time to get ready...
Trying to get NTP time...

Issue

  • ... : wrong line return on first line
  • It says it waits for DHCP while it's already connected
  • IPs are logged without Label

After

Starting network connection via Ethernet/LAN...
Ethernet Started.
Ethernet connected.
Ethernet got IP: 172.19.3.5 netmask: 255.255.255.0 gateway: 172.19.3.1
Waiting 3 seconds to give Ethernet shield time to get ready...
Trying to get NTP time...

BSB_LAN/BSB_LAN.ino Outdated Show resolved Hide resolved
BSB_LAN/BSB_LAN.ino Outdated Show resolved Hide resolved
Comment on lines 7723 to 7821
if (network_type == LAN) {
SerialOutput->println(Ethernet.localIP());
SerialOutput->println(Ethernet.subnetMask());
SerialOutput->println(Ethernet.gatewayIP());
} else {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was already logged by ARDUINO_EVENT_ETH_GOT_IP and so redundant

@fredlcore
Copy link
Owner

I appreciate you working on our code, but I would very much appreciate it if you let me know first what you are planning to do and discuss this with me first. That saves at least me and most likely both of us a lot of time if I don't have to go through several pull requests and changed files and have to do a lot of guesswork...

@jbaudoux
Copy link
Contributor Author

I appreciate you working on our code, but I would very much appreciate it if you let me know first what you are planning to do and discuss this with me first. That saves at least me and most likely both of us a lot of time if I don't have to go through several pull requests and changed files and have to do a lot of guesswork...

This was in draft and I wasn't expecting you to react so quickly. I still have to provide you the complete description so that you can easily understand the difference (before / after).

I have TCP socket issue that I try to understand. As I was looking at the setup debug message, I improved a bit readability. Besides this, I don't want to change anything here

@jbaudoux jbaudoux marked this pull request as ready for review March 24, 2024 13:37
@fredlcore
Copy link
Owner

The event messages are still new and will most likely only be enabled when developer debug mode is active. This may lead to improper formatting etc., but since this will be only used in rare circumstances, I don't think it is an issue. In any case, it should not lead to a situation where output for "normal" mode is removed (as it seems to be the lines 7723++ you mentioned above.

@fredlcore
Copy link
Owner

Also, you removed this line:
if (!Ethernet.begin(mac)) createTemporaryAP(); // DHCP
Why? Do you understand what it does?

@jbaudoux
Copy link
Contributor Author

Also, you removed this line: if (!Ethernet.begin(mac)) createTemporaryAP(); // DHCP Why? Do you understand what it does?

It is there https://github.com/fredlcore/BSB-LAN/pull/636/files#diff-228b1d315444a8461515ac5c0f97909b36aee0b7e5849a717da1c7348fbad5bcR7724

I just moved down this part to not repeat all the conditions in static or dhcp

@jbaudoux
Copy link
Contributor Author

jbaudoux commented Mar 24, 2024

The event messages are still new and will most likely only be enabled when developer debug mode is active. This may lead to improper formatting etc., but since this will be only used in rare circumstances, I don't think it is an issue. In any case, it should not lead to a situation where output for "normal" mode is removed (as it seems to be the lines 7723++ you mentioned above.

I wasn't in debug mode, but to be safe, I put it back but with labels.

Ethernet connected.
Ethernet got IP: 172.19.3.5 netmask: 255.255.255.0 gateway: 172.19.3.1
IP: 172.19.3.5 netmask: 255.255.255.0 gateway: 172.19.3.1
Waiting 3 seconds to give Ethernet shield time to get ready...

@fredlcore
Copy link
Owner

Also, you removed this line: if (!Ethernet.begin(mac)) createTemporaryAP(); // DHCP Why? Do you understand what it does?

It is there https://github.com/fredlcore/BSB-LAN/pull/636/files#diff-228b1d315444a8461515ac5c0f97909b36aee0b7e5849a717da1c7348fbad5bcR7724

I just moved down this part to not repeat all the conditions in static or dhcp

Ok, I didn't see that. One of the reasons why I prefer opening a bug report or feature request before submitting PRs where the intention can be discussed first as this makes code review or PRs a lot easier.
In this case: Have you checked that a failed Ethernet.begin() is based on the same conditions as a failed Ethernet.localIP()? Will for example a failed DHCP request lead to a "false" localIP()? Or is 0.0.0.0 considered "true"?

@jbaudoux
Copy link
Contributor Author

jbaudoux commented Mar 24, 2024

You're right. I was initially using Ethernet.connected() but this is not in stable. I put it back and added in Eth class.

Result without lan cable:

Ethernet Started.
Waiting for DHCP address........................................................................................................................................................................................................
 Setting up AP 'BSB-LAN'
IP address of BSB-LAN: 192.168.4.1
Connect to access point 'BSB-LAN' with password 'BSB-LPB-PPS-LAN' and open the IP address.
IP: 0.0.0.0 netmask: 0.0.0.0 gateway: 0.0.0.0

With cable:

Ethernet Started.
Ethernet connected.
Ethernet got IP: 172.19.3.5 netmask: 255.255.255.0 gateway: 172.19.3.1
IP: 172.19.3.5 netmask: 255.255.255.0 gateway: 172.19.3.1

}
}

#if defined(ESP32)
if (network_type == LAN && !Ethernet.connected()) createTemporaryAP();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, a parameter inside createTemporaryAP to disable such backdoor on network issue would be recommended. But that's another topic...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean with "such backdoor on network issue"? Please be more specific if you raise issues.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like if you have a LAN issue (for example a power outage that will cause esp to restart faster than the lan), you are creating an open AP that anyone can discover and connect to.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That may be the case, yes. But that person would need to know the password for the temporary WiFi network and also all the other security related aspects (such as passcode, HTTP credentials) in order to do anything.

@fredlcore
Copy link
Owner

Under what condition is Ethernet.connected() returning true? Hardware link? IP obtained? What happens when LAN is selected and cable is connected and fixed IP address is used but there is a mis-configuration in the IP settings that prevent the network from working properly? What happens if you're on an ESP32 system that has WiFi only but is set to LAN in the configuration?

@fredlcore
Copy link
Owner

Under what condition is Ethernet.connected() returning true?
Can you elaborate on this, please, @jbaudoux? I'd like to merge this if this function fails on the same (or more) conditions as Ethernet.begin(). But especially the last two mentioned cases need to be covered here.

@jbaudoux
Copy link
Contributor Author

Under what condition is Ethernet.connected() returning true?
Can you elaborate on this, please, @jbaudoux? I'd like to merge this if this function fails on the same (or more) conditions as Ethernet.begin(). But especially the last two mentioned cases need to be covered here.

I'll run more test soon and let you know

@fredlcore
Copy link
Owner

I checked the sources and this specific bit is set/cleared upon the event ARDUINO_EVENT_ETH_CONNECTED or ARDUINO_EVENT_ETH_DISCONNECTED respectively. So this is what the event handler already covers. However, I think that I observed that if there is no link in the first place, there is no "disconnected" event, and the same goes IIRC for cases where there is no Ethernet adapter in the first place (i.e. on a NodeMCU). This means that if a user has set the network type to LAN on a NodeMCU, this condition won't start the AP.

@jbaudoux
Copy link
Contributor Author

jbaudoux commented Mar 31, 2024

fixed IP address is used but there is a mis-configuration in the IP settings that prevent the network from working properly

Not sure I understand properly what you mean by mis-configuration.
Whatever local IP I set, it get's the IP I configured and continue.

What happens if you're on an ESP32 system that has WiFi only but is set to LAN in the configuration?

LAN DHCP or static, there is no change, it's resetting on begin as the brownout detector triggers

Starting network connection via Ethernet/LAN
...
E (1241) esp.emac: emac_esp32_init(348): reset timeout
E (1242) esp_eth: esp_eth_driver_install(214): init mac failed
[  1248][E][ETH.cpp:324] begin(): esp_eth_driver_install failed
 Setting up AP 'BSB-LAN'

Brownout detector was triggered

ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)

@fredlcore
Copy link
Owner

Huh? Where is that brownout coming from? That's usually a hardware issue.
The default configuration is for network type to be set to LAN. If you flash that onto a ESP32-NodeMCU, you would be locked out from the system, but the temporary AP allows you to access and change the configuration so that you don't have to re-flash.
If your approach leads to either brown-outs or does not detect this scenario properly, I cannot accept it.

@fredlcore
Copy link
Owner

This is what I get on an ESP32-NodeMCU when the default network_type value is not changed:

21:31:32.657 > Starting network connection via Ethernet/LAN
21:31:32.660 > ...
21:31:33.662 > E (1260) esp.emac: emac_esp32_init(348): reset timeout
21:31:33.665 > E (1261) esp_eth: esp_eth_driver_install(214): init mac failed
21:31:33.670 > [  1278][E][ETH.cpp:324] begin(): esp_eth_driver_install failed
21:31:33.676 >  Setting up AP 'BSB-LAN'

Up to this point, no network event fires, but at least ETH_CONNECTED_BIT is initially zero, so setting up the temporary AP works in this case. What I can't test here is if the same also works if
a) an invalid fixed IP is configured which results to an inaccessible network configuration
and
b) if DHCP is configured, but no IP address could be obtained.

@jbaudoux
Copy link
Contributor Author

jbaudoux commented Apr 2, 2024

Huh? Where is that brownout coming from? That's usually a hardware issue.

Indeed. I tried with a different esp32 NodeMCU board and this is what I get:

With Static IP

Starting network connection via Ethernet/LAN...
E (1235) esp.emac: emac_esp32_init(348): reset timeout
E (1236) esp_eth: esp_eth_driver_install(214): init mac failed
[  1260][E][ETH.cpp:324] begin(): esp_eth_driver_install failed
 Setting up AP 'BSB-LAN'
IP address of BSB-LAN: 192.168.4.1
Connect to access point 'BSB-LAN' with password 'BSB-LPB-PPS-LAN' and open the IP address.
IP: 192.168.178.88 netmask: 255.255.255.0 gateway: 192.168.178.1
Waiting 3 seconds to give Ethernet shield time to get ready...
Trying to get NTP time...
Acquisition failed, trying again in one minute...

With DHCP

We see it's not waiting to acquire an IP

Starting network connection via Ethernet/LAN...
E (1235) esp.emac: emac_esp32_init(348): reset timeout
E (1236) esp_eth: esp_eth_driver_install(214): init mac failed
[  1260][E][ETH.cpp:324] begin(): esp_eth_driver_install failed
 Setting up AP 'BSB-LAN'
IP address of BSB-LAN: 192.168.4.1
Connect to access point 'BSB-LAN' with password 'BSB-LPB-PPS-LAN' and open the IP address.
IP: 0.0.0.0 netmask: 0.0.0.0 gateway: 0.0.0.0
Waiting 3 seconds to give Ethernet shield time to get ready...
Trying to get NTP time...
Acquisition failed, trying again in one minute...

Before

...
E (1235) esp.emac: emac_esp32_init(348): reset timeout
E (1236) esp_eth: esp_eth_driver_install(214): init mac failed
[  1260][E][ETH.cpp:324] begin(): esp_eth_driver_install failed
 Setting up AP 'BSB-LAN'
IP address of BSB-LAN: 192.168.4.1
Connect to access point 'BSB-LAN' with password 'BSB-LPB-PPS-LAN' and open the IP address.
Waiting for DHCP address........................................................................................................................................................................................................
0.0.0.0
0.0.0.0
0.0.0.0
Waiting 3 seconds to give Ethernet shield time to get ready...
Trying to get NTP time...
Acquisition failed, trying again in one minute...

@fredlcore
Copy link
Owner

Have your tests been done with a plugged-in network cable or not? My questions before were based on the assumption that a network cable is conencted and the conditions a) and b) are met.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants