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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions BSB_LAN/BSB_LAN.ino
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,9 @@ public:
bool begin(uint8_t *mac) {
return ETHClass::begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
}
bool connected() {
return WiFiGenericClass::getStatusBits() & ETH_CONNECTED_BIT;
}
};

Eth Ethernet;
Expand Down Expand Up @@ -7264,8 +7267,12 @@ void netEvent(WiFiEvent_t event) {
}
break;
case ARDUINO_EVENT_ETH_GOT_IP:
SerialOutput->print("Ethernet got IP: ");
SerialOutput->println(ETH.localIP());
SerialOutput->print(F("Ethernet got IP: "));
SerialOutput->print(ETH.localIP());
SerialOutput->print(F(" netmask: "));
SerialOutput->print(ETH.subnetMask());
SerialOutput->print(F(" gateway: "));
SerialOutput->println(ETH.gatewayIP());
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
SerialOutput->println("Ethernet disconnected.");
Expand Down Expand Up @@ -7739,10 +7746,10 @@ void setup() {

printToDebug("Starting network connection via ");
switch (network_type) {
case LAN: printlnToDebug("Ethernet/LAN"); break;
case WLAN: printlnToDebug("WiFi/WLAN"); break;
case LAN: printToDebug(PSTR("Ethernet/LAN")); break;
case WLAN: printToDebug(PSTR("WiFi/WLAN")); break;
}
printToDebug("...\r\n");
printlnToDebug(PSTR("..."));
#if defined(ESP32)
WiFi.onEvent(netEvent);
#endif
Expand Down Expand Up @@ -7786,11 +7793,7 @@ void setup() {
dnsserver = IPAddress(ip_addr[0], ip_addr[1], ip_addr[2], 1);
}
if (network_type == LAN) {
#if defined(ESP32)
if (!Ethernet.begin(mac, ip, dnsserver, gateway, subnet)) createTemporaryAP(); //Static IP
#else
Ethernet.begin(mac, ip, dnsserver, gateway, subnet); //Static IP
#endif
} else {
#if defined(ESP32)
WiFi.config(ip, gateway, subnet, dnsserver);
Expand All @@ -7800,23 +7803,30 @@ void setup() {
}
} else {
if (network_type == LAN) {
#if defined(ESP32)
if (!Ethernet.begin(mac)) createTemporaryAP(); // DHCP
#else
Ethernet.begin(mac); // DHCP
#endif
printToDebug("Waiting for DHCP address");
unsigned long timeout = millis();
while (!Ethernet.localIP() && millis() - timeout < 20000) {
printToDebug(".");
delay(100);
if (Ethernet.begin(mac)) { // DHCP
if (!Ethernet.localIP()) {
printToDebug(PSTR("Waiting for DHCP address"));
unsigned long timeout = millis();
while (!Ethernet.localIP() && millis() - timeout < 20000) {
printToDebug(PSTR("."));
delay(100);
}
writelnToDebug();
}
}
writelnToDebug();
}
}

#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.

#endif

if (network_type == LAN) {
SerialOutput->println(Ethernet.localIP());
SerialOutput->println(Ethernet.subnetMask());
SerialOutput->print(F("IP: "));
SerialOutput->print(Ethernet.localIP());
SerialOutput->print(F(" netmask: "));
SerialOutput->print(Ethernet.subnetMask());
SerialOutput->print(F(" gateway: "));
SerialOutput->println(Ethernet.gatewayIP());
} else {
#if defined(ESP32) || defined(WIFISPI)
Expand Down Expand Up @@ -7871,7 +7881,7 @@ void setup() {
}
#endif

printToDebug("Waiting 3 seconds to give Ethernet shield time to get ready...\r\n");
printlnToDebug(PSTR("Waiting 3 seconds to give Ethernet shield time to get ready..."));
// turn the LED on until Ethernet shield is ready and other initial procedures are over
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

Expand Down
Loading