Skip to content

Commit f486223

Browse files
committed
Add connection throttling to NTRIP Server
Emlid and other casters do not allow immediate re-connect that is often seen when the connection is lost, or a user makes a setting modification (where WiFi is closed/reconnected). This adds a 5s delay between the first re-attempt at WiFi or NTRIP Caster connection and increases += 5 minutes every failed delay after that.
1 parent 2ebf3d3 commit f486223

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

Firmware/RTK_Surveyor/Form.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void startWebServer()
3030
Serial.println(sdUsedSpaceMB);
3131
}
3232

33-
ntripClientStop(true);
33+
ntripClientStop(true); //Do not allocate new wifiClient
3434
wifiStartAP();
3535

3636
incomingSettings = (char*)malloc(AP_CONFIG_SETTING_SIZE);

Firmware/RTK_Surveyor/NtripServer.ino

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
#ifdef COMPILE_WIFI
5454

5555
//Give up connecting after this number of attempts
56-
static const int MAX_NTRIP_SERVER_CONNECTION_ATTEMPTS = 3;
56+
//Connection attempts are throttled to increase the time between attempts
57+
//30 attempts with 5 minute increases will take over 38 hours
58+
static const int MAX_NTRIP_SERVER_CONNECTION_ATTEMPTS = 30;
5759

5860
//----------------------------------------
5961
// Locals - compiled out
@@ -68,6 +70,10 @@ uint32_t ntripServerBytesSent = 0;
6870
//Count the number of connection attempts
6971
static int ntripServerConnectionAttempts;
7072

73+
//Throttle the time between connection attempts
74+
static int ntripServerConnectionAttemptTimeout = 0;
75+
static uint32_t ntripServerLastConnectionAttempt = 0;
76+
7177
//Last time the NTRIP server state was displayed
7278
static uint32_t ntripServerStateLastDisplayed = 0;
7379

@@ -77,11 +83,6 @@ static uint32_t ntripServerStateLastDisplayed = 0;
7783
// * Monitor last RTCM byte received for frame counting
7884
static uint32_t ntripServerTimer;
7985

80-
//Record last connection attempt
81-
//After 6 hours, reset the connectionAttempts to enable
82-
//multi week/month base installations
83-
static uint32_t lastConnectionAttempt = 0;
84-
8586
//----------------------------------------
8687
// NTRIP Server Routines - compiled out
8788
//----------------------------------------
@@ -128,9 +129,14 @@ bool ntripServerConnectLimitReached()
128129
bool limitReached = false;
129130
if (ntripServerConnectionAttempts++ >= MAX_NTRIP_SERVER_CONNECTION_ATTEMPTS) limitReached = true;
130131

131-
if (!limitReached)
132-
//Display the heap state
132+
if (limitReached == false)
133+
{
134+
ntripServerConnectionAttemptTimeout = ntripServerConnectionAttempts * 5 * 60 * 1000L; //Wait 5, 10, 15, etc minutes between attempts
135+
136+
log_d("ntripServerConnectionAttemptTimeout increased to %d minutes", ntripServerConnectionAttemptTimeout / (60 * 1000L));
137+
133138
reportHeapNow();
139+
}
134140
else
135141
{
136142
//No more connection attempts
@@ -299,10 +305,16 @@ void ntripServerStop(bool wifiClientAllocated)
299305

300306
//Stop WiFi if in use
301307
if (ntripServerState > NTRIP_SERVER_ON)
308+
{
302309
wifiStop();
303310

311+
ntripServerLastConnectionAttempt = millis(); //Mark the Server stop so that we don't immediately attempt re-connect to Caster
312+
ntripServerConnectionAttemptTimeout = 5 * 1000L; //Wait 5s between stopping and the first re-connection attempt
313+
}
314+
304315
//Determine the next NTRIP server state
305316
ntripServerSetState((ntripServer && (wifiClientAllocated == false)) ? NTRIP_SERVER_ON : NTRIP_SERVER_OFF);
317+
306318
online.ntripServer = false;
307319
#endif //COMPILE_WIFI
308320
}
@@ -330,8 +342,24 @@ void ntripServerUpdate()
330342

331343
//Start WiFi
332344
case NTRIP_SERVER_ON:
333-
wifiStart(settings.ntripServer_wifiSSID, settings.ntripServer_wifiPW);
334-
ntripServerSetState(NTRIP_SERVER_WIFI_CONNECTING);
345+
//Pause until connection timeout has passed
346+
if (millis() - ntripServerLastConnectionAttempt > ntripServerConnectionAttemptTimeout)
347+
{
348+
ntripServerLastConnectionAttempt = millis();
349+
wifiStart(settings.ntripServer_wifiSSID, settings.ntripServer_wifiPW);
350+
ntripServerSetState(NTRIP_SERVER_WIFI_CONNECTING);
351+
}
352+
else
353+
{
354+
if (millis() - startTime > 1000)
355+
{
356+
startTime = millis();
357+
log_d("NTRIP Server connection timeout wait: %d of %d \n\r",
358+
(millis() - ntripServerLastConnectionAttempt) / 1000,
359+
ntripServerConnectionAttemptTimeout / 1000
360+
);
361+
}
362+
}
335363
break;
336364

337365
//Wait for connection to an access point
@@ -343,6 +371,8 @@ void ntripServerUpdate()
343371
//Assume AP weak signal, the AP is unable to respond successfully
344372
if (ntripServerConnectLimitReached())
345373
{
374+
Serial.println("NTRIP Server failed to get WiFi. Are your WiFi credentials correct?");
375+
346376
//Display the WiFi failure
347377
paintNtripWiFiFail(4000, false);
348378
}
@@ -354,7 +384,7 @@ void ntripServerUpdate()
354384
ntripServerSetState(NTRIP_SERVER_WIFI_CONNECTED);
355385

356386
// Start the SD card server
357-
// sdCardServerBegin(&server, true, true);
387+
// sdCardServerBegin(&server, true, true);
358388
}
359389
break;
360390

@@ -382,11 +412,11 @@ void ntripServerUpdate()
382412
{
383413
log_d("NTRIP Server caster failed to connect. Trying again.");
384414

385-
lastConnectionAttempt = millis();
386-
387415
//Assume service not available
388416
if (ntripServerConnectLimitReached())
417+
{
389418
Serial.println("NTRIP Server failed to connect! Do you have your caster address and port correct?");
419+
}
390420
}
391421
else
392422
{
@@ -406,7 +436,9 @@ void ntripServerUpdate()
406436
{
407437
//NTRIP web service did not respone
408438
if (ntripServerConnectLimitReached())
439+
{
409440
Serial.println("Caster failed to respond. Do you have your caster address and port correct?");
441+
}
410442
}
411443
}
412444
else
@@ -421,7 +453,7 @@ void ntripServerUpdate()
421453
//Look for '401 Unauthorized'
422454
Serial.printf("NTRIP Server caster responded with bad news: %s. Are you sure your caster credentials are correct?\n\r", response);
423455

424-
ntripServerStop(true); //Don't allocate new wifiClient
456+
ntripServerStop(true); //Don't allocate new wifiClient
425457
}
426458
else
427459
{
@@ -460,14 +492,6 @@ void ntripServerUpdate()
460492
{
461493
//All is well
462494
cyclePositionLEDs();
463-
464-
//There may be intermintant disconnects over days or weeks
465-
//Reset the reconnect attempts every 6 hours
466-
if (ntripServerConnectionAttempts > 0 && (millis() - lastConnectionAttempt) > (1000L * 60 * 60 * 6))
467-
{
468-
ntripServerConnectionAttempts = 0;
469-
log_d("Resetting connection attempts");
470-
}
471495
}
472496

473497
break;

0 commit comments

Comments
 (0)