16
16
This is a proof of concept to show how to connect to a caster via HTTP. Using WiFi for a rover
17
17
is generally a bad idea because of limited WiFi range in the field.
18
18
19
+ For more information about NTRIP Clients and the differences between Rev1 and Rev2 of the protocol
20
+ please see: https://www.use-snip.com/kb/knowledge-base/ntrip-rev1-versus-rev2-formats/
21
+
19
22
Feel like supporting open source hardware?
20
23
Buy a board from SparkFun!
21
24
ZED-F9P RTK2: https://www.sparkfun.com/products/16481
@@ -50,7 +53,7 @@ int maxTimeBeforeHangup_ms = 10000; //If we fail to get a complete RTCM frame af
50
53
void setup ()
51
54
{
52
55
Serial.begin (115200 );
53
- Serial.println (" NTRIP testing" );
56
+ Serial.println (F ( " NTRIP testing" ) );
54
57
55
58
Wire.begin (); // Start I2C
56
59
@@ -66,23 +69,27 @@ void setup()
66
69
67
70
myGNSS.setNavigationFrequency (1 ); // Set output in Hz.
68
71
69
- Serial.print (" Connecting to local WiFi" );
72
+ Serial.print (F ( " Connecting to local WiFi" ) );
70
73
WiFi.begin (ssid, password);
71
74
while (WiFi.status () != WL_CONNECTED) {
72
75
delay (500 );
73
- Serial.print (" ." );
76
+ Serial.print (F ( " ." ) );
74
77
}
75
78
Serial.println ();
76
79
77
- Serial.print (" WiFi connected with IP: " );
80
+ Serial.print (F ( " WiFi connected with IP: " ) );
78
81
Serial.println (WiFi.localIP ());
79
82
80
83
while (Serial.available ()) Serial.read ();
81
84
}
82
85
83
86
void loop ()
84
87
{
85
- if (Serial.available ()) beginClient ();
88
+ if (Serial.available ())
89
+ {
90
+ beginClient ();
91
+ while (Serial.available ()) Serial.read (); // Empty buffer of any newline chars
92
+ }
86
93
87
94
Serial.println (F (" Press any key to start NTRIP Client." ));
88
95
@@ -95,36 +102,37 @@ void beginClient()
95
102
WiFiClient ntripClient;
96
103
long rtcmCount = 0 ;
97
104
98
- Serial.println (" Subscribing to Caster. Press key to stop" );
105
+ Serial.println (F ( " Subscribing to Caster. Press key to stop" ) );
99
106
delay (10 ); // Wait for any serial to arrive
100
107
while (Serial.available ()) Serial.read (); // Flush
101
108
102
109
while (Serial.available () == 0 )
103
110
{
104
- // Connect if we are not already
111
+ // Connect if we are not already. Limit to 5s between attempts.
105
112
if (ntripClient.connected () == false )
106
113
{
107
- Serial.print (" Opening socket to" );
114
+ Serial.print (F ( " Opening socket to " ) );
108
115
Serial.println (casterHost);
109
116
110
117
if (ntripClient.connect (casterHost, casterPort) == false ) // Attempt connection
111
118
{
112
- Serial.println (" Connection to caster failed" );
119
+ Serial.println (F (" Connection to caster failed" ));
120
+ return ;
113
121
}
114
122
else
115
123
{
116
- Serial.print (" Connected to " );
124
+ Serial.print (F ( " Connected to " ) );
117
125
Serial.print (casterHost);
118
- Serial.print (" : " );
126
+ Serial.print (F ( " : " ) );
119
127
Serial.println (casterPort);
120
128
121
- Serial.print (" Requesting NTRIP Data from mount point " );
129
+ Serial.print (F ( " Requesting NTRIP Data from mount point " ) );
122
130
Serial.println (mountPoint);
123
131
124
132
const int SERVER_BUFFER_SIZE = 512 ;
125
133
char serverRequest[SERVER_BUFFER_SIZE];
126
134
127
- snprintf (serverRequest, SERVER_BUFFER_SIZE, " GET /%s HTTP/1.0\r\n User-Agent: SparkFun u-blox NTRIPClient v1.0\r\n " ,
135
+ snprintf (serverRequest, SERVER_BUFFER_SIZE, " GET /%s HTTP/1.0\r\n User-Agent: NTRIP SparkFun u-blox Client v1.0\r\n " ,
128
136
mountPoint);
129
137
130
138
char credentials[512 ];
@@ -138,7 +146,7 @@ void beginClient()
138
146
char userCredentials[sizeof (casterUser) + sizeof (casterUserPW) + 1 ]; // The ':' takes up a spot
139
147
snprintf (userCredentials, sizeof (userCredentials), " %s:%s" , casterUser, casterUserPW);
140
148
141
- Serial.print (" Sending credentials: " );
149
+ Serial.print (F ( " Sending credentials: " ) );
142
150
Serial.println (userCredentials);
143
151
144
152
#if defined(ARDUINO_ARCH_ESP32)
@@ -158,13 +166,13 @@ void beginClient()
158
166
strncat (serverRequest, credentials, SERVER_BUFFER_SIZE);
159
167
strncat (serverRequest, " \r\n " , SERVER_BUFFER_SIZE);
160
168
161
- Serial.print (" serverRequest size: " );
169
+ Serial.print (F ( " serverRequest size: " ) );
162
170
Serial.print (strlen (serverRequest));
163
- Serial.print (" of " );
171
+ Serial.print (F ( " of " ) );
164
172
Serial.print (sizeof (serverRequest));
165
- Serial.println (" bytes available" );
173
+ Serial.println (F ( " bytes available" ) );
166
174
167
- Serial.println (" Sending server request:" );
175
+ Serial.println (F ( " Sending server request:" ) );
168
176
Serial.println (serverRequest);
169
177
ntripClient.write (serverRequest, strlen (serverRequest));
170
178
@@ -174,7 +182,7 @@ void beginClient()
174
182
{
175
183
if (millis () - timeout > 5000 )
176
184
{
177
- Serial.println (" Mountpoint timed out!" );
185
+ Serial.println (F ( " Caster timed out!" ) );
178
186
ntripClient.stop ();
179
187
return ;
180
188
}
@@ -194,23 +202,26 @@ void beginClient()
194
202
connectionSuccess = true ;
195
203
if (strstr (response, " 401" ) > 0 ) // Look for '401 Unauthorized'
196
204
{
197
- Serial.println (" Hey - your credentials look bad! Check you caster username and password." );
205
+ Serial.println (F ( " Hey - your credentials look bad! Check you caster username and password." ) );
198
206
connectionSuccess = false ;
199
207
}
200
208
}
201
209
response[responseSpot] = ' \0 ' ;
202
210
211
+ Serial.print (F (" Caster responded with: " ));
212
+ Serial.println (response);
213
+
203
214
if (connectionSuccess == false )
204
215
{
205
- Serial.print (" Failed to connect to " );
216
+ Serial.print (F ( " Failed to connect to " ) );
206
217
Serial.print (casterHost);
207
- Serial.print (" : " );
218
+ Serial.print (F ( " : " ) );
208
219
Serial.println (response);
209
- delay ( 5000 ); // Don't spam with lots of connection attempts
220
+ return ;
210
221
}
211
222
else
212
223
{
213
- Serial.print (" Connected to " );
224
+ Serial.print (F ( " Connected to " ) );
214
225
Serial.println (casterHost);
215
226
lastReceivedRTCM_ms = millis (); // Reset timeout
216
227
}
@@ -236,15 +247,15 @@ void beginClient()
236
247
237
248
// Push RTCM to GNSS module over I2C
238
249
myGNSS.pushRawData (rtcmData, rtcmCount, false );
239
- Serial.print (" RTCM pushed to ZED: " );
250
+ Serial.print (F ( " RTCM pushed to ZED: " ) );
240
251
Serial.println (rtcmCount);
241
252
}
242
253
}
243
254
244
255
// Close socket if we don't have new data for 10s
245
256
if (millis () - lastReceivedRTCM_ms > maxTimeBeforeHangup_ms)
246
257
{
247
- Serial.println (" RTCM timeout. Disconnecting..." );
258
+ Serial.println (F ( " RTCM timeout. Disconnecting..." ) );
248
259
if (ntripClient.connected () == true )
249
260
ntripClient.stop ();
250
261
return ;
@@ -253,9 +264,7 @@ void beginClient()
253
264
delay (10 );
254
265
}
255
266
256
- Serial.println (" User pressed a key" );
257
- Serial.println (" Disconnecting..." );
267
+ Serial.println (F ( " User pressed a key" ) );
268
+ Serial.println (F ( " Disconnecting..." ) );
258
269
ntripClient.stop ();
259
-
260
- while (Serial.available ()) Serial.read (); // Empty buffer of any newline chars
261
- }
270
+ }
0 commit comments