1
1
/*
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
5
3
By: SparkFun Electronics / Nathan Seidle
6
4
Date: December 14th, 2020
7
5
License: MIT. See license file for more information but you can
33
31
34
32
#include < WiFi.h>
35
33
#include " secrets.h"
36
- WiFiClient client ;
34
+ WiFiClient ntripCaster ;
37
35
38
- #include < Wire.h> // Needed for I2C to GNSS
36
+ #include < Wire.h>
39
37
#include < SparkFun_u-blox_GNSS_Arduino_Library.h> // http://librarymanager/All#SparkFun_u-blox_GNSS
40
38
SFE_UBLOX_GNSS myGNSS;
41
39
42
- // Basic Connection settings to RTK2Go NTRIP Caster - See secrets for mount specific credentials
40
+ // Global Variables
43
41
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
49
43
int maxTimeBeforeHangup_ms = 10000 ; // If we fail to get a complete RTCM frame after 10s, then disconnect from caster
50
44
51
45
uint32_t serverBytesSent = 0 ; // Just a running total
46
+ long lastReport_ms = 0 ; // Time of last report of bytes sent
52
47
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
53
48
54
- long lastReport_ms = 0 ; // Time of last report of bytes sent
55
-
56
49
void setup ()
57
50
{
58
51
Serial.begin (115200 ); // You may need to increase this for high navigation rates!
@@ -73,7 +66,8 @@ void setup()
73
66
74
67
Serial.print (" Connecting to local WiFi" );
75
68
WiFi.begin (ssid, password);
76
- while (WiFi.status () != WL_CONNECTED) {
69
+ while (WiFi.status () != WL_CONNECTED)
70
+ {
77
71
delay (500 );
78
72
Serial.print (" ." );
79
73
}
@@ -98,7 +92,8 @@ void setup()
98
92
if (response == false )
99
93
{
100
94
Serial.println (F (" Failed to disable NMEA. Freezing..." ));
101
- while (1 );
95
+ while (1 )
96
+ ;
102
97
}
103
98
else
104
99
Serial.println (F (" NMEA disabled" ));
@@ -114,7 +109,8 @@ void setup()
114
109
if (response == false )
115
110
{
116
111
Serial.println (F (" Failed to enable RTCM. Freezing..." ));
117
- while (1 );
112
+ while (1 )
113
+ ;
118
114
}
119
115
else
120
116
Serial.println (F (" RTCM sentences enabled" ));
@@ -129,63 +125,72 @@ void setup()
129
125
if (response == false )
130
126
{
131
127
Serial.println (F (" Failed to enter static position. Freezing..." ));
132
- while (1 );
128
+ while (1 )
129
+ ;
133
130
}
134
131
else
135
132
Serial.println (F (" Static position set" ));
136
133
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
138
136
// myGNSS.enableSurveyMode(60, 5.000); //Enable Survey in, 60 seconds, 5.0m
139
137
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"));
142
142
143
143
Serial.println (F (" Module configuration complete" ));
144
144
}
145
145
146
146
void loop ()
147
147
{
148
- if (Serial.available ()) beginServing ();
148
+ if (Serial.available ())
149
+ beginServing ();
149
150
150
- Serial.println (F (" Press any key to start serving. " ));
151
+ Serial.println (F (" Press any key to start serving" ));
151
152
152
153
delay (1000 );
153
154
}
154
155
155
156
void beginServing ()
156
157
{
157
- Serial.println (" Xmit to RTK2Go . Press any key to stop" );
158
+ Serial.println (" Begin transmitting to caster . Press any key to stop" );
158
159
delay (10 ); // Wait for any serial to arrive
159
- while (Serial.available ()) Serial.read (); // Flush
160
+ while (Serial.available ())
161
+ Serial.read (); // Flush
160
162
161
163
while (Serial.available () == 0 )
162
164
{
163
165
// Connect if we are not already
164
- if (client .connected () == false )
166
+ if (ntripCaster .connected () == false )
165
167
{
166
168
Serial.printf (" Opening socket to %s\n " , casterHost);
167
169
168
- if (client .connect (casterHost, casterPort) == true ) // Attempt connection
170
+ if (ntripCaster .connect (casterHost, casterPort) == true ) // Attempt connection
169
171
{
170
172
Serial.printf (" Connected to %s:%d\n " , casterHost, casterPort);
171
173
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];
174
176
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);
177
181
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));
180
185
181
186
// Wait for response
182
187
unsigned long timeout = millis ();
183
- while (client .available () == 0 )
188
+ while (ntripCaster .available () == 0 )
184
189
{
185
190
if (millis () - timeout > 5000 )
186
191
{
187
- Serial.println (" >>> Client Timeout !" );
188
- client .stop ();
192
+ Serial.println (" Caster timed out !" );
193
+ ntripCaster .stop ();
189
194
return ;
190
195
}
191
196
delay (10 );
@@ -195,38 +200,43 @@ void beginServing()
195
200
bool connectionSuccess = false ;
196
201
char response[512 ];
197
202
int responseSpot = 0 ;
198
- while (client .available ())
203
+ while (ntripCaster .available ())
199
204
{
200
- response[responseSpot++] = client .read ();
205
+ response[responseSpot++] = ntripCaster .read ();
201
206
if (strstr (response, " 200" ) > 0 ) // Look for 'ICY 200 OK'
202
207
connectionSuccess = true ;
203
- if (responseSpot == 512 - 1 ) break ;
208
+ if (responseSpot == 512 - 1 )
209
+ break ;
204
210
}
205
211
response[responseSpot] = ' \0 ' ;
206
212
207
213
if (connectionSuccess == false )
208
214
{
209
- Serial.printf (" Failed to connect to RTK2Go: %s" , response);
215
+ Serial.printf (" Failed to connect to Caster: %s" , response);
216
+ return ;
210
217
}
211
218
} // End attempt to connect
212
219
else
213
220
{
214
221
Serial.println (" Connection to host failed" );
222
+ return ;
215
223
}
216
224
} // End connected == false
217
225
218
- if (client .connected () == true )
226
+ if (ntripCaster .connected () == true )
219
227
{
220
228
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
222
231
223
232
lastReport_ms = millis ();
224
233
lastSentRTCM_ms = millis ();
225
234
226
235
// This is the main sending loop. We scan for new ublox data but processRTCM() is where the data actually gets sent out.
227
236
while (1 )
228
237
{
229
- if (Serial.available ()) break ;
238
+ if (Serial.available ())
239
+ break ;
230
240
231
241
myGNSS.checkUblox (); // See if new data is available. Process bytes as they come in.
232
242
@@ -236,7 +246,7 @@ void beginServing()
236
246
if (millis () - lastSentRTCM_ms > maxTimeBeforeHangup_ms)
237
247
{
238
248
Serial.println (" RTCM timeout. Disconnecting..." );
239
- client .stop ();
249
+ ntripCaster .stop ();
240
250
return ;
241
251
}
242
252
@@ -256,21 +266,22 @@ void beginServing()
256
266
257
267
Serial.println (" User pressed a key" );
258
268
Serial.println (" Disconnecting..." );
259
- client .stop ();
269
+ ntripCaster .stop ();
260
270
261
271
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
263
274
}
264
275
265
276
// This function gets called from the SparkFun u-blox Arduino Library.
266
277
// As each RTCM byte comes in you can specify what to do with it
267
278
// Useful for passing the RTCM correction data to a radio, Ntrip broadcaster, etc.
268
279
void SFE_UBLOX_GNSS::processRTCM (uint8_t incoming)
269
280
{
270
- if (client .connected () == true )
281
+ if (ntripCaster .connected () == true )
271
282
{
272
- client .write (incoming); // Send this byte to socket
283
+ ntripCaster .write (incoming); // Send this byte to socket
273
284
serverBytesSent++;
274
285
lastSentRTCM_ms = millis ();
275
286
}
276
- }
287
+ }
0 commit comments