Skip to content

Commit 7aa7e6d

Browse files
authored
Merge pull request #517 from sparkfun/LL_NTRIP_Server_Fix
Add LL's NTRIP Server fix - WiFi & Ethernet retry
2 parents 93a93a6 + cf5eeab commit 7aa7e6d

File tree

3 files changed

+159
-119
lines changed

3 files changed

+159
-119
lines changed

Firmware/RTK_Surveyor/NtripClient.ino

Lines changed: 67 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ void ntripClientUpdate()
392392
{
393393
ntripClientLastConnectionAttempt = millis();
394394
log_d("NTRIP Client starting on Ethernet");
395+
ntripClientTimer = millis();
395396
ntripClientSetState(NTRIP_CLIENT_WIFI_ETHERNET_STARTED);
396397
}
397398
}
@@ -416,6 +417,7 @@ void ntripClientUpdate()
416417
ntripClientLastConnectionAttempt = millis();
417418
log_d("NTRIP Client starting WiFi");
418419
wifiStart();
420+
ntripClientTimer = millis();
419421
ntripClientSetState(NTRIP_CLIENT_WIFI_ETHERNET_STARTED);
420422
}
421423
}
@@ -424,32 +426,18 @@ void ntripClientUpdate()
424426
break;
425427

426428
case NTRIP_CLIENT_WIFI_ETHERNET_STARTED:
427-
if (HAS_ETHERNET) // && !settings.ntripClientUseWiFiNotEthernet) //For future expansion
429+
if ((millis() - ntripClientTimer) > (1 * 60 * 1000))
430+
// Failed to connect to to the network, attempt to restart the network
431+
ntripClientStop(false);
432+
else if (HAS_ETHERNET) // && !settings.ntripClientUseWiFiNotEthernet) //For future expansion
428433
{
429434
if (online.ethernetStatus == ETH_CONNECTED)
430435
ntripClientSetState(NTRIP_CLIENT_WIFI_ETHERNET_CONNECTED);
431-
else if (online.ethernetStatus == ETH_CAN_NOT_BEGIN) // Ethernet hardware failure or not available
432-
ntripClientSetState(NTRIP_CLIENT_OFF);
433-
else
434-
{
435-
// 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-
}
442-
}
443436
}
444437
else
445438
{
446439
if (wifiIsConnected())
447440
ntripClientSetState(NTRIP_CLIENT_WIFI_ETHERNET_CONNECTED);
448-
else if (wifiState == WIFI_OFF)
449-
{
450-
// WiFi failed to connect. Restart Client which will restart WiFi.
451-
ntripClientSetState(NTRIP_CLIENT_ON);
452-
}
453441
}
454442
break;
455443

@@ -492,7 +480,12 @@ void ntripClientUpdate()
492480
{
493481
// NTRIP web service did not respond
494482
if (ntripClientConnectLimitReached()) // Updates ntripClientConnectionAttemptTimeout
483+
{
495484
systemPrintln("NTRIP Caster failed to respond. Do you have your caster address and port correct?");
485+
486+
// Stop WiFi operations
487+
ntripClientStop(true); // Do not allocate new wifiClient
488+
}
496489
else
497490
{
498491
if (ntripClientConnectionAttemptTimeout / 1000 < 120)
@@ -501,6 +494,9 @@ void ntripClientUpdate()
501494
else
502495
systemPrintf("NTRIP Client failed to connect to caster. Trying again in %d minutes.\r\n",
503496
ntripClientConnectionAttemptTimeout / 1000 / 60);
497+
498+
// Restart network operation after delay
499+
ntripClientStop(false);
504500
}
505501
}
506502
}
@@ -510,10 +506,40 @@ void ntripClientUpdate()
510506
char response[512];
511507
ntripClientResponse(&response[0], sizeof(response));
512508

513-
log_d("Caster Response: %s", response);
509+
//log_d("Caster Response: %s", response);
514510

515511
// Look for various responses
516-
if (strstr(response, "401") != nullptr)
512+
if (strstr(response, "200") != nullptr)
513+
{
514+
log_d("NTRIP Client connected to caster");
515+
516+
// Connection is now open, start the NTRIP receive data timer
517+
ntripClientTimer = millis();
518+
519+
if (settings.ntripClient_TransmitGGA == true)
520+
{
521+
// Set the Main Talker ID to "GP". The NMEA GGA messages will be GPGGA instead of GNGGA
522+
theGNSS.setVal8(UBLOX_CFG_NMEA_MAINTALKERID, 1);
523+
theGNSS.setNMEAGPGGAcallbackPtr(&pushGPGGA); // Set up the callback for GPGGA
524+
525+
float measurementFrequency = (1000.0 / settings.measurementRate) / settings.navigationRate;
526+
if (measurementFrequency < 0.2)
527+
measurementFrequency = 0.2; // 0.2Hz * 5 = 1 measurement every 5 seconds
528+
log_d("Adjusting GGA setting to %f", measurementFrequency);
529+
theGNSS.setVal8(
530+
UBLOX_CFG_MSGOUT_NMEA_ID_GGA_I2C,
531+
measurementFrequency); // Enable GGA over I2C. Tell the module to output GGA every second
532+
533+
lastGGAPush = millis() - NTRIPCLIENT_MS_BETWEEN_GGA; // Force immediate transmission of GGA message
534+
}
535+
536+
// We don't use a task because we use I2C hardware (and don't have a semphore).
537+
online.ntripClient = true;
538+
ntripClientStartTime = millis();
539+
ntripClientConnectionAttempts = 0;
540+
ntripClientSetState(NTRIP_CLIENT_CONNECTED);
541+
}
542+
else if (strstr(response, "401") != nullptr)
517543
{
518544
// Look for '401 Unauthorized'
519545
systemPrintf(
@@ -540,35 +566,33 @@ void ntripClientUpdate()
540566
// Stop WiFi operations
541567
ntripClientStop(true); // Do not allocate new wifiClient
542568
}
543-
else if (strstr(response, "200") != nullptr)
569+
// Other errors returned by the caster
570+
else
544571
{
545-
log_d("NTRIP Client connected to caster");
546-
547-
// Connection is now open, start the NTRIP receive data timer
548-
ntripClientTimer = millis();
572+
systemPrintf("NTRIP Client connected but caster responded with problem: %s\r\n", response);
549573

550-
if (settings.ntripClient_TransmitGGA == true)
574+
// Check for connection limit
575+
if (ntripClientConnectLimitReached())
551576
{
552-
// Set the Main Talker ID to "GP". The NMEA GGA messages will be GPGGA instead of GNGGA
553-
theGNSS.setVal8(UBLOX_CFG_NMEA_MAINTALKERID, 1);
554-
theGNSS.setNMEAGPGGAcallbackPtr(&pushGPGGA); // Set up the callback for GPGGA
555-
556-
float measurementFrequency = (1000.0 / settings.measurementRate) / settings.navigationRate;
557-
if (measurementFrequency < 0.2)
558-
measurementFrequency = 0.2; // 0.2Hz * 5 = 1 measurement every 5 seconds
559-
log_d("Adjusting GGA setting to %f", measurementFrequency);
560-
theGNSS.setVal8(
561-
UBLOX_CFG_MSGOUT_NMEA_ID_GGA_I2C,
562-
measurementFrequency); // Enable GGA over I2C. Tell the module to output GGA every second
577+
systemPrintln("NTRIP Client retry limit reached; do you have your caster address and port correct?");
563578

564-
lastGGAPush = millis() - NTRIPCLIENT_MS_BETWEEN_GGA; // Force immediate transmission of GGA message
579+
// Give up - Shutdown NTRIP client, no further retries
580+
ntripClientStop(true);
565581
}
566582

567-
// We don't use a task because we use I2C hardware (and don't have a semphore).
568-
online.ntripClient = true;
569-
ntripClientStartTime = millis();
570-
ntripClientConnectionAttempts = 0;
571-
ntripClientSetState(NTRIP_CLIENT_CONNECTED);
583+
// Attempt to reconnect after throttle controlled timeout
584+
else
585+
{
586+
if (ntripClientConnectionAttemptTimeout / 1000 < 120)
587+
systemPrintf("NTRIP Client attempting connection in %d seconds.\r\n",
588+
ntripClientConnectionAttemptTimeout / 1000);
589+
else
590+
systemPrintf("NTRIP Client attempting connection in %d minutes.\r\n",
591+
ntripClientConnectionAttemptTimeout / 1000 / 60);
592+
593+
// Restart network operation after delay
594+
ntripClientStop(false);
595+
}
572596
}
573597
}
574598
#endif // COMPILE_WIFI || COMPILE_ETHERNET

Firmware/RTK_Surveyor/NtripServer.ino

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ void ntripServerProcessRTCM(uint8_t incoming)
249249
}
250250

251251
// If we have not gotten new RTCM bytes for a period of time, assume end of frame
252-
if (millis() - ntripServerTimer > 100 && ntripServerBytesSent > 0)
252+
if (((millis() - ntripServerTimer) > 100) && (ntripServerBytesSent > 0))
253253
{
254254
if ((!inMainMenu) && settings.enablePrintNtripServerState)
255255
systemPrintf("NTRIP Server transmitted %d RTCM bytes to Caster\r\n", ntripServerBytesSent);
@@ -391,6 +391,7 @@ void ntripServerUpdate()
391391
{
392392
ntripServerLastConnectionAttempt = millis();
393393
log_d("NTRIP Server starting on Ethernet");
394+
ntripServerTimer = millis();
394395
ntripServerSetState(NTRIP_SERVER_WIFI_ETHERNET_STARTED);
395396
}
396397
}
@@ -415,6 +416,7 @@ void ntripServerUpdate()
415416
ntripServerLastConnectionAttempt = millis();
416417
log_d("NTRIP Server starting WiFi");
417418
wifiStart();
419+
ntripServerTimer = millis();
418420
ntripServerSetState(NTRIP_SERVER_WIFI_ETHERNET_STARTED);
419421
}
420422
}
@@ -423,32 +425,18 @@ void ntripServerUpdate()
423425

424426
// Wait for connection to an access point
425427
case NTRIP_SERVER_WIFI_ETHERNET_STARTED:
426-
if (HAS_ETHERNET) // && !settings.ntripServerUseWiFiNotEthernet) //For future expansion
428+
if ((millis() - ntripServerTimer) > (1 * 60 * 1000))
429+
// Failed to connect to to the network, attempt to restart the network
430+
ntripServerStop(false);
431+
else if (HAS_ETHERNET) // && !settings.ntripServerUseWiFiNotEthernet) //For future expansion
427432
{
428433
if (online.ethernetStatus == ETH_CONNECTED)
429434
ntripServerSetState(NTRIP_SERVER_WIFI_ETHERNET_CONNECTED);
430-
else if (online.ethernetStatus == ETH_CAN_NOT_BEGIN) // Ethernet hardware failure or not available
431-
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-
}
442435
}
443436
else
444437
{
445438
if (wifiIsConnected())
446439
ntripServerSetState(NTRIP_SERVER_WIFI_ETHERNET_CONNECTED);
447-
else if (wifiState == WIFI_OFF)
448-
{
449-
// WiFi failed to connect. Restart Client which will restart WiFi.
450-
ntripServerSetState(NTRIP_SERVER_ON);
451-
}
452440
}
453441
break;
454442

@@ -509,6 +497,9 @@ void ntripServerUpdate()
509497
if (ntripServerConnectLimitReached())
510498
{
511499
systemPrintln("Caster failed to respond. Do you have your caster address and port correct?");
500+
501+
// Give up - Shutdown NTRIP server, no further retries
502+
ntripServerStop(true);
512503
}
513504
else
514505
{
@@ -518,6 +509,9 @@ void ntripServerUpdate()
518509
else
519510
systemPrintf("NTRIP caster failed to respond. Trying again in %d minutes.\r\n",
520511
ntripServerConnectionAttemptTimeout / 1000 / 60);
512+
513+
// Restart network operation after delay
514+
ntripServerStop(false);
521515
}
522516
}
523517
}
@@ -528,48 +522,67 @@ void ntripServerUpdate()
528522
ntripServerResponse(response, sizeof(response));
529523

530524
// Look for various responses
531-
if (strstr(response, "401") != nullptr)
525+
if (strstr(response, "200") != nullptr) //'200' found
526+
{
527+
systemPrintf("NTRIP Server connected to %s:%d %s\r\n", settings.ntripServer_CasterHost,
528+
settings.ntripServer_CasterPort, settings.ntripServer_MountPoint);
529+
530+
// Connection is now open, start the RTCM correction data timer
531+
ntripServerTimer = millis();
532+
533+
// We don't use a task because we use I2C hardware (and don't have a semphore).
534+
online.ntripServer = true;
535+
ntripServerConnectionAttempts = 0;
536+
ntripServerSetState(NTRIP_SERVER_CASTING);
537+
}
538+
539+
// Look for '401 Unauthorized'
540+
else if (strstr(response, "401") != nullptr)
532541
{
533-
// Look for '401 Unauthorized'
534542
systemPrintf(
535543
"NTRIP Caster responded with bad news: %s. Are you sure your caster credentials are correct?\r\n",
536544
response);
537545

538-
// Give up - Stop WiFi operations
539-
ntripServerStop(true); // Do not allocate new wifiClient
546+
// Give up - Shutdown NTRIP server, no further retries
547+
ntripServerStop(true);
540548
}
549+
550+
// Look for banned IP information
541551
else if (strstr(response, "banned") != nullptr) //'Banned' found
542552
{
543-
// Look for 'HTTP/1.1 200 OK' and banned IP information
544-
systemPrintf("NTRIP Server connected to caster but caster responded with problem: %s", response);
553+
systemPrintf("NTRIP Server connected to caster but caster responded with problem: %s\r\n", response);
545554

546-
// Give up - Stop WiFi operations
547-
ntripServerStop(true); // Do not allocate new wifiClient
555+
// Give up - Shutdown NTRIP server, no further retries
556+
ntripServerStop(true);
548557
}
549-
else if (strstr(response, "200") == nullptr) //'200' not found
558+
559+
// Other errors returned by the caster
560+
else
550561
{
551-
// Look for 'ERROR - Mountpoint taken' from Emlid.
552-
systemPrintf("NTRIP Server connected but caster responded with problem: %s", response);
562+
systemPrintf("NTRIP Server connected but caster responded with problem: %s\r\n", response);
553563

554-
// Attempt to reconnect after throttle controlled timeout
564+
// Check for connection limit
555565
if (ntripServerConnectLimitReached())
556566
{
557-
systemPrintln("Caster failed to respond. Do you have your caster address and port correct?");
558-
}
559-
}
560-
else if (strstr(response, "200") != nullptr) //'200' found
567+
systemPrintln("NTRIP Server retry limit reached; do you have your caster address and port correct?");
561568

562-
{
563-
systemPrintf("NTRIP Server connected to %s:%d %s\r\n", settings.ntripServer_CasterHost,
564-
settings.ntripServer_CasterPort, settings.ntripServer_MountPoint);
569+
// Give up - Shutdown NTRIP server, no further retries
570+
ntripServerStop(true);
571+
}
565572

566-
// Connection is now open, start the RTCM correction data timer
567-
ntripServerTimer = millis();
573+
// Attempt to reconnect after throttle controlled timeout
574+
else
575+
{
576+
if (ntripServerConnectionAttemptTimeout / 1000 < 120)
577+
systemPrintf("NTRIP Server attempting connection in %d seconds.\r\n",
578+
ntripServerConnectionAttemptTimeout / 1000);
579+
else
580+
systemPrintf("NTRIP Server attempting connection in %d minutes.\r\n",
581+
ntripServerConnectionAttemptTimeout / 1000 / 60);
568582

569-
// We don't use a task because we use I2C hardware (and don't have a semphore).
570-
online.ntripServer = true;
571-
ntripServerConnectionAttempts = 0;
572-
ntripServerSetState(NTRIP_SERVER_CASTING);
583+
// Restart network operation after delay
584+
ntripServerStop(false);
585+
}
573586
}
574587
}
575588
#endif // COMPILE_WIFI || COMPILE_ETHERNET

0 commit comments

Comments
 (0)