@@ -224,181 +224,7 @@ void ntripClientSetState(byte newState)
224224 }
225225}
226226
227- #endif // COMPILE_WIFI
228-
229- // ----------------------------------------
230- // Constants - compiled out
231- // ----------------------------------------
232-
233- #ifdef COMPILE_WIFI
234-
235- // Size of the credentials buffer in bytes
236- static const int CREDENTIALS_BUFFER_SIZE = 512 ;
237-
238- // Give up connecting after this number of attempts
239- static const int MAX_NTRIP_CLIENT_CONNECTION_ATTEMPTS = 3 ;
240-
241- // NTRIP caster response timeout
242- static const uint32_t NTRIP_CLIENT_RESPONSE_TIMEOUT = 5 * 1000 ; // Milliseconds
243-
244- // NTRIP client receive data timeout
245- static const uint32_t NTRIP_CLIENT_RECEIVE_DATA_TIMEOUT = 10 * 1000 ; // Milliseconds
246-
247- // Most incoming data is around 500 bytes but may be larger
248- static const int RTCM_DATA_SIZE = 512 * 4 ;
249-
250- // NTRIP client server request buffer size
251- static const int SERVER_BUFFER_SIZE = CREDENTIALS_BUFFER_SIZE + 3 ;
252-
253- // ----------------------------------------
254- // Locals - compiled out
255- // ----------------------------------------
256-
257- // The WiFi connection to the NTRIP caster to obtain RTCM data.
258- static WiFiClient * ntripClient;
259-
260- // Count the number of connection attempts
261- static int ntripClientConnectionAttempts;
262-
263- // NTRIP client timer usage:
264- // * Measure the connection response time
265- // * Receive NTRIP data timeout
266- static uint32_t ntripClientTimer;
267-
268- // ----------------------------------------
269- // NTRIP Client Routines - compiled out
270- // ----------------------------------------
271-
272- void ntripClientAllowMoreConnections ()
273- {
274- ntripClientConnectionAttempts = 0 ;
275- }
276-
277- bool ntripClientConnect ()
278- {
279- if (!ntripClient->connect (settings.ntripClient_CasterHost , settings.ntripClient_CasterPort ))
280- return false ;
281- Serial.printf (" NTRIP Client connected to %s:%d\n\r " , settings.ntripClient_CasterHost , settings.ntripClient_CasterPort );
282-
283- // Set up the server request (GET)
284- char serverRequest[SERVER_BUFFER_SIZE];
285- snprintf (serverRequest,
286- SERVER_BUFFER_SIZE,
287- " GET /%s HTTP/1.0\r\n User-Agent: NTRIP SparkFun_RTK_%s_v%d.%d\r\n " ,
288- settings.ntripClient_MountPoint , platformPrefix, FIRMWARE_VERSION_MAJOR, FIRMWARE_VERSION_MINOR);
289-
290- // Set up the credentials
291- char credentials[CREDENTIALS_BUFFER_SIZE];
292- if (strlen (settings.ntripClient_CasterUser ) == 0 )
293- {
294- strncpy (credentials, " Accept: */*\r\n Connection: close\r\n " , sizeof (credentials));
295- }
296- else
297- {
298- // Pass base64 encoded user:pw
299- char userCredentials[sizeof (settings.ntripClient_CasterUser ) + sizeof (settings.ntripClient_CasterUserPW ) + 1 ]; // The ':' takes up a spot
300- snprintf (userCredentials, sizeof (userCredentials), " %s:%s" , settings.ntripClient_CasterUser , settings.ntripClient_CasterUserPW );
301-
302- Serial.print (F (" NTRIP Client sending credentials: " ));
303- Serial.println (userCredentials);
304-
305- // Encode with ESP32 built-in library
306- base64 b;
307- String strEncodedCredentials = b.encode (userCredentials);
308- char encodedCredentials[strEncodedCredentials.length () + 1 ];
309- strEncodedCredentials.toCharArray (encodedCredentials, sizeof (encodedCredentials)); // Convert String to char array
310-
311- snprintf (credentials, sizeof (credentials), " Authorization: Basic %s\r\n " , encodedCredentials);
312- }
313-
314- // Add the encoded credentials to the server request
315- strncat (serverRequest, credentials, SERVER_BUFFER_SIZE - 1 );
316- strncat (serverRequest, " \r\n " , SERVER_BUFFER_SIZE - 1 );
317-
318- Serial.print (F (" NTRIP Client serverRequest size: " ));
319- Serial.print (strlen (serverRequest));
320- Serial.print (F (" of " ));
321- Serial.print (sizeof (serverRequest));
322- Serial.println (F (" bytes available" ));
323-
324- // Send the server request
325- Serial.println (F (" NTRIP Client sending server request: " ));
326- Serial.println (serverRequest);
327- ntripClient->write (serverRequest, strlen (serverRequest));
328- ntripClientTimer = millis ();
329- return true ;
330- }
331-
332- // Determine if another connection is possible or if the limit has been reached
333- bool ntripClientConnectLimitReached ()
334- {
335- // Shutdown the NTRIP client
336- ntripClientStop ();
337-
338- // Retry the connection a few times
339- bool limitReached = (ntripClientConnectionAttempts++ >= MAX_NTRIP_CLIENT_CONNECTION_ATTEMPTS);
340- if (!limitReached)
341- // Display the heap state
342- reportHeapNow ();
343- else
344- // No more connection attempts, switching to Bluetooth
345- ntripClientSwitchToBluetooth ();
346- return limitReached;
347- }
348-
349- // Determine if NTRIP client data is available
350- int ntripClientReceiveDataAvailable ()
351- {
352- return ntripClient->available ();
353- }
354-
355- // Read the response from the NTRIP client
356- void ntripClientResponse (char * response, size_t maxLength)
357- {
358- char * responseEnd;
359-
360- // Make sure that we can zero terminate the response
361- responseEnd = &response[maxLength - 1 ];
362-
363- // Read bytes from the caster and store them
364- while ((response < responseEnd) && ntripClientReceiveDataAvailable ())
365- *response++ = ntripClient->read ();
366-
367- // Zero terminate the response
368- *response = ' \0 ' ;
369- }
370-
371- // Stop the NTRIP client
372- void ntripClientStop ()
373- {
374- if (ntripClient)
375- {
376- if (ntripClient->connected ())
377- ntripClient->stop ();
378- delete ntripClient;
379- ntripClient = NULL ;
380- }
381- if (ntripClientState > NTRIP_CLIENT_ON)
382- wifiStop ();
383- ntripClientState = NTRIP_CLIENT_ON;
384- }
385-
386- // Switch to Bluetooth operation
387- void ntripClientSwitchToBluetooth ()
388- {
389- Serial.println (F (" NTRIP Client failure, switching to Bluetooth!" ));
390-
391- // Stop WiFi operations
392- ntripClientStop ();
393-
394- // No more connection attempts
395- ntripClientState = NTRIP_CLIENT_OFF;
396-
397- // Turn on Bluetooth with 'Rover' name
398- startBluetooth ();
399- }
400-
401- #endif // COMPILE_WIFI
227+ #endif
402228
403229// ----------------------------------------
404230// Global NTRIP Client Routines
0 commit comments