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,72 @@ 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
140- if (myGNSS.saveConfiguration () == false ) // Save the current settings to flash and BBR
141- Serial.println (F (" Module failed to save." ));
138+ // If you were setting up a full GNSS station, you would want to save these settings.
139+ // Because setting an incorrect static position will disable the ability to get a lock, we will skip saving during this example
140+ // if (myGNSS.saveConfiguration() == false) //Save the current settings to flash and BBR
141+ // Serial.println(F("Module failed to save"));
142142
143143 Serial.println (F (" Module configuration complete" ));
144144}
145145
146146void loop ()
147147{
148- if (Serial.available ()) beginServing ();
148+ if (Serial.available ())
149+ beginServing ();
149150
150- Serial.println (F (" Press any key to start serving. " ));
151+ Serial.println (F (" Press any key to start serving" ));
151152
152153 delay (1000 );
153154}
154155
155156void beginServing ()
156157{
157- Serial.println (" Xmit to RTK2Go . Press any key to stop" );
158+ Serial.println (" Begin transmitting to caster . Press any key to stop" );
158159 delay (10 ); // Wait for any serial to arrive
159- while (Serial.available ()) Serial.read (); // Flush
160+ while (Serial.available ())
161+ Serial.read (); // Flush
160162
161163 while (Serial.available () == 0 )
162164 {
163165 // Connect if we are not already
164- if (client .connected () == false )
166+ if (ntripCaster .connected () == false )
165167 {
166168 Serial.printf (" Opening socket to %s\n " , casterHost);
167169
168- if (client .connect (casterHost, casterPort) == true ) // Attempt connection
170+ if (ntripCaster .connect (casterHost, casterPort) == true ) // Attempt connection
169171 {
170172 Serial.printf (" Connected to %s:%d\n " , casterHost, casterPort);
171173
172- const int SERVER_BUFFER_SIZE = 512 ;
173- char serverBuffer [SERVER_BUFFER_SIZE];
174+ const int SERVER_BUFFER_SIZE = 512 ;
175+ char serverRequest [SERVER_BUFFER_SIZE];
174176
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" );
177+ snprintf (serverRequest,
178+ SERVER_BUFFER_SIZE,
179+ " SOURCE %s /%s\r\n Source-Agent: NTRIP SparkFun u-blox Server v1.0\r\n\r\n " ,
180+ mountPointPW, mountPoint);
177181
178- Serial.printf (" Sending credentials:\n %s\n " , serverBuffer);
179- client.write (serverBuffer, strlen (serverBuffer));
182+ Serial.println (F (" Sending server request:" ));
183+ Serial.println (serverRequest);
184+ ntripCaster.write (serverRequest, strlen (serverRequest));
180185
181186 // Wait for response
182187 unsigned long timeout = millis ();
183- while (client .available () == 0 )
188+ while (ntripCaster .available () == 0 )
184189 {
185190 if (millis () - timeout > 5000 )
186191 {
187- Serial.println (" >>> Client Timeout !" );
188- client .stop ();
192+ Serial.println (" Caster timed out !" );
193+ ntripCaster .stop ();
189194 return ;
190195 }
191196 delay (10 );
@@ -195,38 +200,43 @@ void beginServing()
195200 bool connectionSuccess = false ;
196201 char response[512 ];
197202 int responseSpot = 0 ;
198- while (client .available ())
203+ while (ntripCaster .available ())
199204 {
200- response[responseSpot++] = client .read ();
205+ response[responseSpot++] = ntripCaster .read ();
201206 if (strstr (response, " 200" ) > 0 ) // Look for 'ICY 200 OK'
202207 connectionSuccess = true ;
203- if (responseSpot == 512 - 1 ) break ;
208+ if (responseSpot == 512 - 1 )
209+ break ;
204210 }
205211 response[responseSpot] = ' \0 ' ;
206212
207213 if (connectionSuccess == false )
208214 {
209- Serial.printf (" Failed to connect to RTK2Go: %s" , response);
215+ Serial.printf (" Failed to connect to Caster: %s" , response);
216+ return ;
210217 }
211218 } // End attempt to connect
212219 else
213220 {
214221 Serial.println (" Connection to host failed" );
222+ return ;
215223 }
216224 } // End connected == false
217225
218- if (client .connected () == true )
226+ if (ntripCaster .connected () == true )
219227 {
220228 delay (10 );
221- while (Serial.available ()) Serial.read (); // Flush any endlines or carriage returns
229+ while (Serial.available ())
230+ Serial.read (); // Flush any endlines or carriage returns
222231
223232 lastReport_ms = millis ();
224233 lastSentRTCM_ms = millis ();
225234
226235 // This is the main sending loop. We scan for new ublox data but processRTCM() is where the data actually gets sent out.
227236 while (1 )
228237 {
229- if (Serial.available ()) break ;
238+ if (Serial.available ())
239+ break ;
230240
231241 myGNSS.checkUblox (); // See if new data is available. Process bytes as they come in.
232242
@@ -236,7 +246,7 @@ void beginServing()
236246 if (millis () - lastSentRTCM_ms > maxTimeBeforeHangup_ms)
237247 {
238248 Serial.println (" RTCM timeout. Disconnecting..." );
239- client .stop ();
249+ ntripCaster .stop ();
240250 return ;
241251 }
242252
@@ -256,21 +266,22 @@ void beginServing()
256266
257267 Serial.println (" User pressed a key" );
258268 Serial.println (" Disconnecting..." );
259- client .stop ();
269+ ntripCaster .stop ();
260270
261271 delay (10 );
262- while (Serial.available ()) Serial.read (); // Flush any endlines or carriage returns
272+ while (Serial.available ())
273+ Serial.read (); // Flush any endlines or carriage returns
263274}
264275
265276// This function gets called from the SparkFun u-blox Arduino Library.
266277// As each RTCM byte comes in you can specify what to do with it
267278// Useful for passing the RTCM correction data to a radio, Ntrip broadcaster, etc.
268279void SFE_UBLOX_GNSS::processRTCM (uint8_t incoming)
269280{
270- if (client .connected () == true )
281+ if (ntripCaster .connected () == true )
271282 {
272- client .write (incoming); // Send this byte to socket
283+ ntripCaster .write (incoming); // Send this byte to socket
273284 serverBytesSent++;
274285 lastSentRTCM_ms = millis ();
275286 }
276- }
287+ }
0 commit comments