11/*
2- Note: compiles OK with v2.0 but is currently untested
3-
4- Use ESP32 WiFi to push RTCM data to RTK2Go (caster) as a Server
2+ Use ESP32 WiFi to push RTCM data to RTK2Go (Caster) as a Server
53 By: SparkFun Electronics / Nathan Seidle
64 Date: December 14th, 2020
75 License: MIT. See license file for more information but you can
3331
3432#include < WiFi.h>
3533#include " secrets.h"
36- WiFiClient client ;
34+ WiFiClient ntripCaster ;
3735
38- #include < Wire.h> // Needed for I2C to GNSS
36+ #include < Wire.h>
3937#include < SparkFun_u-blox_GNSS_Arduino_Library.h> // http://librarymanager/All#SparkFun_u-blox_GNSS
4038SFE_UBLOX_GNSS myGNSS;
4139
42- // Basic Connection settings to RTK2Go NTRIP Caster - See secrets for mount specific credentials
40+ // Global Variables
4341// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
44- const uint16_t casterPort = 2101 ;
45- const char * casterHost = " rtk2go.com" ;
46- const char * ntrip_server_name = " SparkFun_RTK_Surveyor" ;
47-
48- long lastSentRTCM_ms = 0 ; // Time of last data pushed to socket
42+ long lastSentRTCM_ms = 0 ; // Time of last data pushed to socket
4943int maxTimeBeforeHangup_ms = 10000 ; // If we fail to get a complete RTCM frame after 10s, then disconnect from caster
5044
5145uint32_t serverBytesSent = 0 ; // Just a running total
46+ long lastReport_ms = 0 ; // Time of last report of bytes sent
5247// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5348
54- long lastReport_ms = 0 ; // Time of last report of bytes sent
55-
5649void setup ()
5750{
5851 Serial.begin (115200 ); // You may need to increase this for high navigation rates!
@@ -73,7 +66,8 @@ void setup()
7366
7467 Serial.print (" Connecting to local WiFi" );
7568 WiFi.begin (ssid, password);
76- while (WiFi.status () != WL_CONNECTED) {
69+ while (WiFi.status () != WL_CONNECTED)
70+ {
7771 delay (500 );
7872 Serial.print (" ." );
7973 }
@@ -98,7 +92,8 @@ void setup()
9892 if (response == false )
9993 {
10094 Serial.println (F (" Failed to disable NMEA. Freezing..." ));
101- while (1 );
95+ while (1 )
96+ ;
10297 }
10398 else
10499 Serial.println (F (" NMEA disabled" ));
@@ -114,7 +109,8 @@ void setup()
114109 if (response == false )
115110 {
116111 Serial.println (F (" Failed to enable RTCM. Freezing..." ));
117- while (1 );
112+ while (1 )
113+ ;
118114 }
119115 else
120116 Serial.println (F (" RTCM sentences enabled" ));
@@ -129,63 +125,70 @@ void setup()
129125 if (response == false )
130126 {
131127 Serial.println (F (" Failed to enter static position. Freezing..." ));
132- while (1 );
128+ while (1 )
129+ ;
133130 }
134131 else
135132 Serial.println (F (" Static position set" ));
136133
137- // You could instead do a survey-in but it takes much longer to start generating RTCM data. See Example4_BaseWithLCD
134+ // Alternatively to setting a static position, you could do a survey-in
135+ // but it takes much longer to start generating RTCM data. See Example4_BaseWithLCD
138136 // myGNSS.enableSurveyMode(60, 5.000); //Enable Survey in, 60 seconds, 5.0m
139137
140138 if (myGNSS.saveConfiguration () == false ) // Save the current settings to flash and BBR
141- Serial.println (F (" Module failed to save. " ));
139+ Serial.println (F (" Module failed to save" ));
142140
143141 Serial.println (F (" Module configuration complete" ));
144142}
145143
146144void loop ()
147145{
148- if (Serial.available ()) beginServing ();
146+ if (Serial.available ())
147+ beginServing ();
149148
150- Serial.println (F (" Press any key to start serving. " ));
149+ Serial.println (F (" Press any key to start serving" ));
151150
152151 delay (1000 );
153152}
154153
155154void beginServing ()
156155{
157- Serial.println (" Xmit to RTK2Go . Press any key to stop" );
156+ Serial.println (" Begin transmitting to caster . Press any key to stop" );
158157 delay (10 ); // Wait for any serial to arrive
159- while (Serial.available ()) Serial.read (); // Flush
158+ while (Serial.available ())
159+ Serial.read (); // Flush
160160
161161 while (Serial.available () == 0 )
162162 {
163163 // Connect if we are not already
164- if (client .connected () == false )
164+ if (ntripCaster .connected () == false )
165165 {
166166 Serial.printf (" Opening socket to %s\n " , casterHost);
167167
168- if (client .connect (casterHost, casterPort) == true ) // Attempt connection
168+ if (ntripCaster .connect (casterHost, casterPort) == true ) // Attempt connection
169169 {
170170 Serial.printf (" Connected to %s:%d\n " , casterHost, casterPort);
171171
172- const int SERVER_BUFFER_SIZE = 512 ;
173- char serverBuffer [SERVER_BUFFER_SIZE];
172+ const int SERVER_BUFFER_SIZE = 512 ;
173+ char serverRequest [SERVER_BUFFER_SIZE];
174174
175- snprintf (serverBuffer, SERVER_BUFFER_SIZE, " SOURCE %s /%s\r\n Source-Agent: NTRIP %s/%s\r\n\r\n " ,
176- mntpnt_pw, mntpnt, ntrip_server_name, " App Version 1.0" );
175+ snprintf (serverRequest,
176+ SERVER_BUFFER_SIZE,
177+ " SOURCE %s /%s\r\n Source-Agent: NTRIP SparkFun u-blox Server v1.0\r\n\r\n " ,
178+ mountPointPW, mountPoint);
177179
178- Serial.printf (" Sending credentials:\n %s\n " , serverBuffer);
179- client.write (serverBuffer, strlen (serverBuffer));
180+ Serial.println (F (" Sending server request:" ));
181+ Serial.println (serverRequest);
182+ ntripCaster.write (serverRequest, strlen (serverRequest));
180183
181184 // Wait for response
182185 unsigned long timeout = millis ();
183- while (client .available () == 0 )
186+ while (ntripCaster .available () == 0 )
184187 {
185188 if (millis () - timeout > 5000 )
186189 {
187- Serial.println (" >>> Client Timeout !" );
188- client .stop ();
190+ Serial.println (" Caster timed out !" );
191+ ntripCaster .stop ();
189192 return ;
190193 }
191194 delay (10 );
@@ -195,38 +198,43 @@ void beginServing()
195198 bool connectionSuccess = false ;
196199 char response[512 ];
197200 int responseSpot = 0 ;
198- while (client .available ())
201+ while (ntripCaster .available ())
199202 {
200- response[responseSpot++] = client .read ();
203+ response[responseSpot++] = ntripCaster .read ();
201204 if (strstr (response, " 200" ) > 0 ) // Look for 'ICY 200 OK'
202205 connectionSuccess = true ;
203- if (responseSpot == 512 - 1 ) break ;
206+ if (responseSpot == 512 - 1 )
207+ break ;
204208 }
205209 response[responseSpot] = ' \0 ' ;
206210
207211 if (connectionSuccess == false )
208212 {
209- Serial.printf (" Failed to connect to RTK2Go: %s" , response);
213+ Serial.printf (" Failed to connect to Caster: %s" , response);
214+ return ;
210215 }
211216 } // End attempt to connect
212217 else
213218 {
214219 Serial.println (" Connection to host failed" );
220+ return ;
215221 }
216222 } // End connected == false
217223
218- if (client .connected () == true )
224+ if (ntripCaster .connected () == true )
219225 {
220226 delay (10 );
221- while (Serial.available ()) Serial.read (); // Flush any endlines or carriage returns
227+ while (Serial.available ())
228+ Serial.read (); // Flush any endlines or carriage returns
222229
223230 lastReport_ms = millis ();
224231 lastSentRTCM_ms = millis ();
225232
226233 // This is the main sending loop. We scan for new ublox data but processRTCM() is where the data actually gets sent out.
227234 while (1 )
228235 {
229- if (Serial.available ()) break ;
236+ if (Serial.available ())
237+ break ;
230238
231239 myGNSS.checkUblox (); // See if new data is available. Process bytes as they come in.
232240
@@ -236,7 +244,7 @@ void beginServing()
236244 if (millis () - lastSentRTCM_ms > maxTimeBeforeHangup_ms)
237245 {
238246 Serial.println (" RTCM timeout. Disconnecting..." );
239- client .stop ();
247+ ntripCaster .stop ();
240248 return ;
241249 }
242250
@@ -256,21 +264,22 @@ void beginServing()
256264
257265 Serial.println (" User pressed a key" );
258266 Serial.println (" Disconnecting..." );
259- client .stop ();
267+ ntripCaster .stop ();
260268
261269 delay (10 );
262- while (Serial.available ()) Serial.read (); // Flush any endlines or carriage returns
270+ while (Serial.available ())
271+ Serial.read (); // Flush any endlines or carriage returns
263272}
264273
265274// This function gets called from the SparkFun u-blox Arduino Library.
266275// As each RTCM byte comes in you can specify what to do with it
267276// Useful for passing the RTCM correction data to a radio, Ntrip broadcaster, etc.
268277void SFE_UBLOX_GNSS::processRTCM (uint8_t incoming)
269278{
270- if (client .connected () == true )
279+ if (ntripCaster .connected () == true )
271280 {
272- client .write (incoming); // Send this byte to socket
281+ ntripCaster .write (incoming); // Send this byte to socket
273282 serverBytesSent++;
274283 lastSentRTCM_ms = millis ();
275284 }
276- }
285+ }
0 commit comments