Skip to content

Commit 2b16113

Browse files
authored
Merge pull request #499 from sparkfun/Fix_NTRIP_Server_on_Ethernet
Fix ntrip server on ethernet
2 parents 4c5e886 + c6f9a43 commit 2b16113

File tree

3 files changed

+55
-29
lines changed

3 files changed

+55
-29
lines changed

Firmware/RTK_Surveyor/NtripClient.ino

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,9 @@ bool ntripClientConnectLimitReached()
169169
ntripClientStop(false); // Allocate new wifiClient
170170

171171
// Retry the connection a few times
172-
bool limitReached = false;
173-
if (ntripClientConnectionAttempts++ >= MAX_NTRIP_CLIENT_CONNECTION_ATTEMPTS)
174-
limitReached = true;
172+
bool limitReached = (ntripClientConnectionAttempts >= MAX_NTRIP_CLIENT_CONNECTION_ATTEMPTS);
175173

174+
ntripClientConnectionAttempts++;
176175
ntripClientConnectionAttemptsTotal++;
177176

178177
if (limitReached == false)
@@ -434,6 +433,12 @@ void ntripClientUpdate()
434433
else
435434
{
436435
// Wait for ethernet to connect
436+
static unsigned long lastDebug = millis();
437+
if (millis() > (lastDebug + 5000))
438+
{
439+
lastDebug = millis();
440+
log_d("NTRIP Client: Ethernet not connected. Waiting to retry.");
441+
}
437442
}
438443
}
439444
else

Firmware/RTK_Surveyor/NtripServer.ino

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,24 @@ bool ntripServerConnectLimitReached()
130130
ntripServerStop(false); // Allocate new wifiClient
131131

132132
// Retry the connection a few times
133-
bool limitReached = false;
134-
if (ntripServerConnectionAttempts++ >= MAX_NTRIP_SERVER_CONNECTION_ATTEMPTS)
135-
limitReached = true;
133+
bool limitReached = (ntripServerConnectionAttempts >= MAX_NTRIP_SERVER_CONNECTION_ATTEMPTS);
136134

135+
ntripServerConnectionAttempts++;
137136
ntripServerConnectionAttemptsTotal++;
138137

139138
if (limitReached == false)
140139
{
141-
ntripServerConnectionAttemptTimeout =
142-
ntripServerConnectionAttempts * 5 * 60 * 1000L; // Wait 5, 10, 15, etc minutes between attempts
140+
if (ntripServerConnectionAttempts == 1)
141+
ntripServerConnectionAttemptTimeout = 15 * 1000L; // Wait 15s
142+
else if (ntripServerConnectionAttempts == 2)
143+
ntripServerConnectionAttemptTimeout = 30 * 1000L; // Wait 30s
144+
else if (ntripServerConnectionAttempts == 3)
145+
ntripServerConnectionAttemptTimeout = 1 * 60 * 1000L; // Wait 1 minute
146+
else if (ntripServerConnectionAttempts == 4)
147+
ntripServerConnectionAttemptTimeout = 2 * 60 * 1000L; // Wait 2 minutes
148+
else
149+
ntripServerConnectionAttemptTimeout =
150+
(ntripServerConnectionAttempts - 4) * 5 * 60 * 1000L; // Wait 5, 10, 15, etc minutes between attempts
143151

144152
reportHeapNow();
145153
}
@@ -419,8 +427,18 @@ void ntripServerUpdate()
419427
{
420428
if (online.ethernetStatus == ETH_CONNECTED)
421429
ntripServerSetState(NTRIP_SERVER_WIFI_ETHERNET_CONNECTED);
422-
else
430+
else if (online.ethernetStatus == ETH_CAN_NOT_BEGIN) // Ethernet hardware failure or not available
423431
ntripServerSetState(NTRIP_SERVER_OFF);
432+
else
433+
{
434+
// Wait for ethernet to connect
435+
static unsigned long lastDebug = millis();
436+
if (millis() > (lastDebug + 5000))
437+
{
438+
lastDebug = millis();
439+
log_d("NTRIP Server: Ethernet not connected. Waiting to retry.");
440+
}
441+
}
424442
}
425443
else
426444
{
@@ -456,18 +474,20 @@ void ntripServerUpdate()
456474
// Attempt a connection to the NTRIP caster
457475
if (!ntripServerConnectCaster())
458476
{
459-
if (ntripServerConnectionAttemptTimeout / 1000 < 120)
460-
systemPrintf("NTRIP Server failed to connect to caster. Trying again in %d seconds.\r\n",
461-
ntripServerConnectionAttemptTimeout / 1000);
462-
else
463-
systemPrintf("NTRIP Server failed to connect to caster. Trying again in %d minutes.\r\n",
464-
ntripServerConnectionAttemptTimeout / 1000 / 60);
465-
466477
// Assume service not available
467-
if (ntripServerConnectLimitReached())
478+
if (ntripServerConnectLimitReached()) // Update ntripServerConnectionAttemptTimeout
468479
{
469480
systemPrintln("NTRIP Server failed to connect! Do you have your caster address and port correct?");
470481
}
482+
else
483+
{
484+
if (ntripServerConnectionAttemptTimeout / 1000 < 120)
485+
systemPrintf("NTRIP Server failed to connect to caster. Trying again in %d seconds.\r\n",
486+
ntripServerConnectionAttemptTimeout / 1000);
487+
else
488+
systemPrintf("NTRIP Server failed to connect to caster. Trying again in %d minutes.\r\n",
489+
ntripServerConnectionAttemptTimeout / 1000 / 60);
490+
}
471491
}
472492
else
473493
{

Firmware/RTK_Surveyor/menuEthernet.ino

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ bool ethernetIsNeeded()
1414
)
1515
return true;
1616

17-
// Does Base mode NTRIP Server need Ethernet?
17+
// Does Rover mode NTRIP Client need Ethernet?
1818
if (HAS_ETHERNET && settings.enableNtripClient == true &&
1919
(systemState >= STATE_ROVER_NOT_STARTED && systemState <= STATE_ROVER_RTK_FIX)
2020
//&& !settings.ntripClientUseWiFiNotEthernet //For future expansion
@@ -56,18 +56,20 @@ void beginEthernet()
5656
Ethernet.begin(ethernetMACAddress, settings.ethernetIP, settings.ethernetDNS, settings.ethernetGateway,
5757
settings.ethernetSubnet);
5858

59-
if (Ethernet.hardwareStatus() == EthernetNoHardware)
59+
if (Ethernet.hardwareStatus() == EthernetNoHardware) // Check that a W5n00 has been detected
6060
{
6161
log_d("Ethernet hardware not found");
6262
online.ethernetStatus = ETH_CAN_NOT_BEGIN;
6363
return;
6464
}
6565

6666
online.ethernetStatus = ETH_STARTED_CHECK_CABLE;
67+
lastEthernetCheck = millis(); // Wait a full second before checking the cable
68+
6769
break;
6870

6971
case (ETH_STARTED_CHECK_CABLE):
70-
if (millis() - lastEthernetCheck > 1000) // Don't check for cable but once a second
72+
if (millis() - lastEthernetCheck > 1000) // Check for cable every second
7173
{
7274
lastEthernetCheck = millis();
7375

@@ -94,17 +96,16 @@ void beginEthernet()
9496
break;
9597

9698
case (ETH_STARTED_START_DHCP):
97-
Ethernet.begin(ethernetMACAddress);
98-
99-
if (Ethernet.hardwareStatus() == EthernetNoHardware)
99+
if (millis() - lastEthernetCheck > 1000) // Try DHCP every second
100100
{
101-
log_d("Ethernet hardware not found");
102-
online.ethernetStatus = ETH_CAN_NOT_BEGIN;
103-
return;
104-
}
101+
lastEthernetCheck = millis();
105102

106-
log_d("Ethernet started with DHCP");
107-
online.ethernetStatus = ETH_CONNECTED;
103+
if (Ethernet.begin(ethernetMACAddress, 20000)) // Restart Ethernet with DHCP. Use 20s timeout
104+
{
105+
log_d("Ethernet started with DHCP");
106+
online.ethernetStatus = ETH_CONNECTED;
107+
}
108+
}
108109
break;
109110

110111
case (ETH_CONNECTED):

0 commit comments

Comments
 (0)