Skip to content

Commit 236fe4e

Browse files
committed
Add throttling to NTRIP Client
1 parent 5657cad commit 236fe4e

File tree

1 file changed

+50
-22
lines changed

1 file changed

+50
-22
lines changed

Firmware/RTK_Surveyor/NtripClient.ino

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
static const int CREDENTIALS_BUFFER_SIZE = 512;
4242

4343
//Give up connecting after this number of attempts
44-
static const int MAX_NTRIP_CLIENT_CONNECTION_ATTEMPTS = 3;
44+
//Connection attempts are throttled to increase the time between attempts
45+
//30 attempts with 5 minute increases will take over 38 hours
46+
static const int MAX_NTRIP_CLIENT_CONNECTION_ATTEMPTS = 30;
4547

4648
//NTRIP caster response timeout
4749
static const uint32_t NTRIP_CLIENT_RESPONSE_TIMEOUT = 10 * 1000; //Milliseconds
@@ -64,6 +66,11 @@ static const int NTRIPCLIENT_MS_BETWEEN_GGA = 5000; //5s between transmission of
6466
//The WiFi connection to the NTRIP caster to obtain RTCM data.
6567
static WiFiClient * ntripClient;
6668

69+
//Throttle the time between connection attempts
70+
static int ntripClientConnectionAttemptTimeout = 0;
71+
static uint32_t ntripClientLastConnectionAttempt = 0;
72+
static uint32_t ntripClientTimeoutPrint = 0;
73+
6774
//Last time the NTRIP client state was displayed
6875
static uint32_t lastNtripClientState = 0;
6976

@@ -160,10 +167,17 @@ bool ntripClientConnectLimitReached()
160167
ntripClientStop(false); //Allocate new wifiClient
161168

162169
//Retry the connection a few times
163-
bool limitReached = (ntripClientConnectionAttempts++ >= MAX_NTRIP_CLIENT_CONNECTION_ATTEMPTS);
164-
if (!limitReached)
165-
//Display the heap state
170+
bool limitReached = false;
171+
if (ntripClientConnectionAttempts++ >= MAX_NTRIP_CLIENT_CONNECTION_ATTEMPTS);
172+
173+
if (limitReached == false)
174+
{
175+
ntripClientConnectionAttemptTimeout = ntripClientConnectionAttempts * 5 * 60 * 1000L; //Wait 5, 10, 15, etc minutes between attempts
176+
177+
log_d("ntripClientConnectionAttemptTimeout increased to %d minutes", ntripClientConnectionAttemptTimeout / (60 * 1000L));
178+
166179
reportHeapNow();
180+
}
167181
else
168182
{
169183
//No more connection attempts, switching to Bluetooth
@@ -263,7 +277,7 @@ void ntripClientStart()
263277
}
264278

265279
//Stop the NTRIP client
266-
void ntripClientStop(bool done)
280+
void ntripClientStop(bool wifiClientAllocated)
267281
{
268282
#ifdef COMPILE_WIFI
269283
if (ntripClient)
@@ -277,20 +291,25 @@ void ntripClientStop(bool done)
277291
ntripClient = NULL;
278292

279293
//Allocate the NTRIP client structure if not done
280-
if (!done)
294+
if (wifiClientAllocated == false)
281295
ntripClient = new WiFiClient();
282296
}
283297

284298
//Stop WiFi if in use
285299
if (ntripClientState > NTRIP_CLIENT_ON)
300+
{
286301
wifiStop();
287302

303+
ntripClientLastConnectionAttempt = millis(); //Mark the Client stop so that we don't immediately attempt re-connect to Caster
304+
ntripClientConnectionAttemptTimeout = 15 * 1000L; //Wait 15s between stopping and the first re-connection attempt.
305+
}
306+
288307
// Return the Main Talker ID to "GN".
289308
i2cGNSS.setMainTalkerID(SFE_UBLOX_MAIN_TALKER_ID_GN);
290309
i2cGNSS.setNMEAGPGGAcallbackPtr(NULL); // Remove callback
291310

292311
//Determine the next NTRIP client state
293-
ntripClientSetState((ntripClient && (!done)) ? NTRIP_CLIENT_ON : NTRIP_CLIENT_OFF);
312+
ntripClientSetState((ntripClient && (wifiClientAllocated == false)) ? NTRIP_CLIENT_ON : NTRIP_CLIENT_OFF);
294313
online.ntripClient = false;
295314
wifiIncomingRTCM = false;
296315
#endif //COMPILE_WIFI
@@ -323,29 +342,38 @@ void ntripClientUpdate()
323342
}
324343
else
325344
{
326-
wifiStart(settings.ntripClient_wifiSSID, settings.ntripClient_wifiPW);
327-
ntripClientSetState(NTRIP_CLIENT_WIFI_CONNECTING);
345+
//Pause until connection timeout has passed
346+
if (millis() - ntripClientLastConnectionAttempt > ntripClientConnectionAttemptTimeout)
347+
{
348+
ntripClientLastConnectionAttempt = millis();
349+
wifiStart(settings.ntripClient_wifiSSID, settings.ntripClient_wifiPW);
350+
ntripClientSetState(NTRIP_CLIENT_WIFI_CONNECTING);
351+
}
352+
else
353+
{
354+
if (millis() - ntripClientTimeoutPrint > 1000)
355+
{
356+
ntripClientTimeoutPrint = millis();
357+
Serial.printf("NTRIP Client connection timeout wait: %d of %d seconds \n\r",
358+
(millis() - ntripClientLastConnectionAttempt) / 1000,
359+
ntripClientConnectionAttemptTimeout / 1000
360+
);
361+
}
362+
}
363+
328364
}
329365
break;
330366

331367
case NTRIP_CLIENT_WIFI_CONNECTING:
332368
if (!wifiIsConnected())
333369
{
334-
//Stop if SSID is not detected
335-
if (wifiGetStatus() == WL_NO_SSID_AVAIL)
370+
//Throttle if SSID is not detected
371+
if (wifiConnectionTimeout() || wifiGetStatus() == WL_NO_SSID_AVAIL)
336372
{
337-
Serial.printf("WiFi network '%s' not found\n\r", settings.ntripClient_wifiSSID);
373+
if (wifiGetStatus() == WL_NO_SSID_AVAIL)
374+
Serial.printf("WiFi network '%s' not found\n\r", settings.ntripClient_wifiSSID);
338375

339-
wifiStop();
340-
341-
paintNtripWiFiFail(4000, true); //True = 'Client', False = 'Server'
342-
343-
ntripClientStop(true); //Do not allocate new wifiClient
344-
}
345-
else if (wifiConnectionTimeout())
346-
{
347-
//Assume AP weak signal, the AP is unable to respond successfully
348-
if (ntripClientConnectLimitReached())
376+
if (ntripClientConnectLimitReached()) //Stop WiFi, give up
349377
paintNtripWiFiFail(4000, true);
350378
}
351379
}

0 commit comments

Comments
 (0)