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,70 @@ 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
138
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" ));
142
140
143
141
Serial.println (F (" Module configuration complete" ));
144
142
}
145
143
146
144
void loop ()
147
145
{
148
- if (Serial.available ()) beginServing ();
146
+ if (Serial.available ())
147
+ beginServing ();
149
148
150
- Serial.println (F (" Press any key to start serving. " ));
149
+ Serial.println (F (" Press any key to start serving" ));
151
150
152
151
delay (1000 );
153
152
}
154
153
155
154
void beginServing ()
156
155
{
157
- Serial.println (" Xmit to RTK2Go . Press any key to stop" );
156
+ Serial.println (" Begin transmitting to caster . Press any key to stop" );
158
157
delay (10 ); // Wait for any serial to arrive
159
- while (Serial.available ()) Serial.read (); // Flush
158
+ while (Serial.available ())
159
+ Serial.read (); // Flush
160
160
161
161
while (Serial.available () == 0 )
162
162
{
163
163
// Connect if we are not already
164
- if (client .connected () == false )
164
+ if (ntripCaster .connected () == false )
165
165
{
166
166
Serial.printf (" Opening socket to %s\n " , casterHost);
167
167
168
- if (client .connect (casterHost, casterPort) == true ) // Attempt connection
168
+ if (ntripCaster .connect (casterHost, casterPort) == true ) // Attempt connection
169
169
{
170
170
Serial.printf (" Connected to %s:%d\n " , casterHost, casterPort);
171
171
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];
174
174
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);
177
179
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));
180
183
181
184
// Wait for response
182
185
unsigned long timeout = millis ();
183
- while (client .available () == 0 )
186
+ while (ntripCaster .available () == 0 )
184
187
{
185
188
if (millis () - timeout > 5000 )
186
189
{
187
- Serial.println (" >>> Client Timeout !" );
188
- client .stop ();
190
+ Serial.println (" Caster timed out !" );
191
+ ntripCaster .stop ();
189
192
return ;
190
193
}
191
194
delay (10 );
@@ -195,38 +198,43 @@ void beginServing()
195
198
bool connectionSuccess = false ;
196
199
char response[512 ];
197
200
int responseSpot = 0 ;
198
- while (client .available ())
201
+ while (ntripCaster .available ())
199
202
{
200
- response[responseSpot++] = client .read ();
203
+ response[responseSpot++] = ntripCaster .read ();
201
204
if (strstr (response, " 200" ) > 0 ) // Look for 'ICY 200 OK'
202
205
connectionSuccess = true ;
203
- if (responseSpot == 512 - 1 ) break ;
206
+ if (responseSpot == 512 - 1 )
207
+ break ;
204
208
}
205
209
response[responseSpot] = ' \0 ' ;
206
210
207
211
if (connectionSuccess == false )
208
212
{
209
- Serial.printf (" Failed to connect to RTK2Go: %s" , response);
213
+ Serial.printf (" Failed to connect to Caster: %s" , response);
214
+ return ;
210
215
}
211
216
} // End attempt to connect
212
217
else
213
218
{
214
219
Serial.println (" Connection to host failed" );
220
+ return ;
215
221
}
216
222
} // End connected == false
217
223
218
- if (client .connected () == true )
224
+ if (ntripCaster .connected () == true )
219
225
{
220
226
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
222
229
223
230
lastReport_ms = millis ();
224
231
lastSentRTCM_ms = millis ();
225
232
226
233
// This is the main sending loop. We scan for new ublox data but processRTCM() is where the data actually gets sent out.
227
234
while (1 )
228
235
{
229
- if (Serial.available ()) break ;
236
+ if (Serial.available ())
237
+ break ;
230
238
231
239
myGNSS.checkUblox (); // See if new data is available. Process bytes as they come in.
232
240
@@ -236,7 +244,7 @@ void beginServing()
236
244
if (millis () - lastSentRTCM_ms > maxTimeBeforeHangup_ms)
237
245
{
238
246
Serial.println (" RTCM timeout. Disconnecting..." );
239
- client .stop ();
247
+ ntripCaster .stop ();
240
248
return ;
241
249
}
242
250
@@ -256,21 +264,22 @@ void beginServing()
256
264
257
265
Serial.println (" User pressed a key" );
258
266
Serial.println (" Disconnecting..." );
259
- client .stop ();
267
+ ntripCaster .stop ();
260
268
261
269
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
263
272
}
264
273
265
274
// This function gets called from the SparkFun u-blox Arduino Library.
266
275
// As each RTCM byte comes in you can specify what to do with it
267
276
// Useful for passing the RTCM correction data to a radio, Ntrip broadcaster, etc.
268
277
void SFE_UBLOX_GNSS::processRTCM (uint8_t incoming)
269
278
{
270
- if (client .connected () == true )
279
+ if (ntripCaster .connected () == true )
271
280
{
272
- client .write (incoming); // Send this byte to socket
281
+ ntripCaster .write (incoming); // Send this byte to socket
273
282
serverBytesSent++;
274
283
lastSentRTCM_ms = millis ();
275
284
}
276
- }
285
+ }
0 commit comments