Skip to content

Commit c1cab51

Browse files
committed
Improve Ethernet initialization and diagnostics
Added hardware presence and link status checks to Ethernet initialization. Enhanced error messages for failed configurations and included gateway and subnet information in successful initialization logs. Also added Ethernet.maintain() in loop to support DHCP lease maintenance and link status monitoring.
1 parent d7f2510 commit c1cab51

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

TankAlarm-112025-Server-BluesOpta/TankAlarm-112025-Server-BluesOpta.ino

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
#include <Wire.h>
2222
#include <ArduinoJson.h>
2323
#include <Notecard.h>
24-
#include <Ethernet.h>
24+
#if defined(ARDUINO_OPTA) || defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4)
25+
#include <PortentaEthernet.h>
26+
#include <Ethernet.h>
27+
#else
28+
#include <Ethernet.h>
29+
#endif
2530
#include <math.h>
2631
#include <string.h>
2732
#include <ctype.h>
@@ -2578,6 +2583,9 @@ void loop() {
25782583
IWatchdog.reload();
25792584
#endif
25802585

2586+
// Maintain DHCP lease and check link status
2587+
Ethernet.maintain();
2588+
25812589
handleWebRequests();
25822590

25832591
unsigned long now = millis();
@@ -2879,6 +2887,16 @@ static void scheduleNextViewerSummary() {
28792887

28802888
static void initializeEthernet() {
28812889
Serial.print(F("Initializing Ethernet..."));
2890+
2891+
// Check if Ethernet hardware is present
2892+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
2893+
Serial.println(F(" FAILED - No Ethernet hardware detected!"));
2894+
Serial.println(F("ERROR: Cannot continue without Ethernet. Please check hardware."));
2895+
while (true) {
2896+
delay(1000);
2897+
}
2898+
}
2899+
28822900
int status;
28832901
if (gConfig.useStaticIp) {
28842902
status = Ethernet.begin(gMacAddress, gStaticIp, gStaticDns, gStaticGateway, gStaticSubnet);
@@ -2887,11 +2905,29 @@ static void initializeEthernet() {
28872905
}
28882906

28892907
if (status == 0) {
2890-
Serial.println(F(" failed"));
2908+
Serial.println(F(" FAILED - Could not configure Ethernet!"));
2909+
if (!gConfig.useStaticIp) {
2910+
Serial.println(F("ERROR: DHCP failed. Check network cable and DHCP server."));
2911+
} else {
2912+
Serial.println(F("ERROR: Static IP configuration failed."));
2913+
}
2914+
while (true) {
2915+
delay(1000);
2916+
}
2917+
}
2918+
2919+
// Check link status
2920+
if (Ethernet.linkStatus() == LinkOFF) {
2921+
Serial.println(F(" WARNING - No network cable connected!"));
2922+
Serial.println(F("Continuing, but web server will not be accessible."));
28912923
} else {
28922924
Serial.println(F(" ok"));
28932925
Serial.print(F("IP Address: "));
28942926
Serial.println(Ethernet.localIP());
2927+
Serial.print(F("Gateway: "));
2928+
Serial.println(Ethernet.gatewayIP());
2929+
Serial.print(F("Subnet: "));
2930+
Serial.println(Ethernet.subnetMask());
28952931
}
28962932
}
28972933

0 commit comments

Comments
 (0)