Skip to content

Commit 58986bd

Browse files
committed
Add Example 16 - works better on Mbed OS
1 parent f21d982 commit 58986bd

File tree

4 files changed

+678
-1
lines changed

4 files changed

+678
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ An Arduino library for the u-blox SARA-R5 LTE-M / NB-IoT modules with secure clo
1616

1717
v1.1 has had a thorough update and includes new features and examples. This library now supports up to 7 simultaneous TCP or UDP sockets. There are new examples to show how to play ping pong with both TCP and UDP sockets.
1818

19-
v1.1 also supports binary data transfers correctly. There are new examples showing how you can integrate this library with the [SparkFun u-blox GNSS Arduino Library](https://github.com/sparkfun/SparkFun_u-blox_GNSS_Arduino_Library), to use the SARA-R5 to: download AssistNow Online and Offline data and push it to the GNSS; open a connection to a NTRIP Caster (such as RTK2go, Skylark or Emlid Caster) and push RTK correction data to the GNSS.
19+
v1.1 also supports binary data transfers correctly. There are new examples showing how you can integrate this library with the [SparkFun u-blox GNSS Arduino Library](https://github.com/sparkfun/SparkFun_u-blox_GNSS_Arduino_Library) and use the SARA-R5 to: download AssistNow Online and Offline data and push it to the GNSS; open a connection to a NTRIP Caster (such as RTK2go, Skylark or Emlid Caster) and push RTK correction data to the GNSS.
2020

2121
You can install this library using the Arduino IDE Library Manager: search for _**SparkFun u-blox SARA-R5**_
2222

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
/*
2+
3+
SARA-R5 Example
4+
===============
5+
6+
u-blox GNSS NTRIP Caster Client - Polling
7+
8+
This version uses polling to check for the arrival of new RTK correction and NMEA GPGGA data.
9+
It performs better on Mbed OS platforms: SparkFun Artemis; Arduino Nano
10+
11+
Written by: Paul Clark
12+
Date: January 18th 2021
13+
14+
This example uses the SARA's mobile data connection to:
15+
* Request RTK RTCM data from a NTRIP Caster service
16+
* Push the RTCM data to an external u-blox GNSS module over I2C (not to the one built-in to the SARA-R510M8S)
17+
* NMEA GPGGA data is pushed to the Caster every 10 seconds
18+
19+
The PDP profile is read from NVM. Please make sure you have run examples 4 & 7 previously to set up the profile.
20+
21+
Update secrets.h with your NTRIP Caster username and password
22+
23+
**************************************************************************************************
24+
* Important Note: *
25+
* *
26+
* This example pulls kBytes of correction data from the NTRIP Caster. *
27+
* Depending on your location and service provider, the data rate may exceed the allowable *
28+
* rates for LTE-M or NB-IoT. *
29+
* Worst case, your service provider may throttle or block the connection - now or in the future. *
30+
* We are looking for a long-term solution to this - almost certainly using LTE Cat 1 instead. *
31+
**************************************************************************************************
32+
33+
Feel like supporting open source hardware?
34+
Buy a board from SparkFun!
35+
36+
Licence: MIT
37+
Please see LICENSE.md for full details
38+
39+
*/
40+
41+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
42+
43+
// The ESP32 core has a built in base64 library but not every platform does
44+
// We'll use an external lib if necessary.
45+
46+
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_APOLLO3) || defined(ARDUINO_ARDUINO_NANO33BLE)
47+
#include "base64.h" //Built-in ESP32 library
48+
#else
49+
#include <Base64.h> //nfriendly library from https://github.com/adamvr/arduino-base64, will work with any platform
50+
#endif
51+
52+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
53+
54+
#include <SparkFun_u-blox_SARA-R5_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_u-blox_SARA-R5_Arduino_Library
55+
56+
// Uncomment the next line to connect to the SARA-R5 using hardware Serial1
57+
#define saraSerial Serial1
58+
59+
// Uncomment the next line to create a SoftwareSerial object to pass to the SARA-R5 library instead
60+
//SoftwareSerial saraSerial(8, 9);
61+
62+
// Create a SARA_R5 object to use throughout the sketch
63+
// Usually we would tell the library which GPIO pin to use to control the SARA power (see below),
64+
// but we can start the SARA without a power pin. It just means we need to manually
65+
// turn the power on if required! ;-D
66+
SARA_R5 mySARA;
67+
68+
// Create a SARA_R5 object to use throughout the sketch
69+
// We need to tell the library what GPIO pin is connected to the SARA power pin.
70+
// If you're using the MicroMod Asset Tracker and the MicroMod Artemis Processor Board,
71+
// the pin name is G2 which is connected to pin AD34.
72+
// Change the pin number if required.
73+
//SARA_R5 mySARA(34);
74+
75+
// Create a SARA_R5 object to use throughout the sketch
76+
// If you are using the LTE GNSS Breakout, and have access to the SARA's RESET_N pin, you can pass that to the library too
77+
// allowing it to do an emergency shutdown if required.
78+
// Change the pin numbers if required.
79+
//SARA_R5 mySARA(34, 35); // PWR_ON, RESET_N
80+
81+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
82+
83+
#include <SparkFun_u-blox_GNSS_Arduino_Library.h> //http://librarymanager/All#SparkFun_u-blox_GNSS
84+
SFE_UBLOX_GNSS myGNSS;
85+
86+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
87+
88+
// Globals
89+
90+
volatile int socketNum = -1; // The TCP socket number. -1 indicates invalid/closed socket
91+
volatile bool connectionOpen = false; // Flag to indicate if the connection to the NTRIP Caster is open
92+
volatile unsigned long lastReceivedRTCM_ms; // Record when data last arrived - so we can time out if required
93+
94+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
95+
96+
void setup()
97+
{
98+
String currentOperator = "";
99+
100+
Serial.begin(115200); // Start the serial console
101+
102+
// Wait for user to press key to begin
103+
Serial.println(F("SARA-R5 Example"));
104+
Serial.println(F("Wait for the SARA NI LED to light up - then press any key to begin"));
105+
106+
while (!Serial.available()) // Wait for the user to press a key (send any serial character)
107+
;
108+
while (Serial.available()) // Empty the serial RX buffer
109+
Serial.read();
110+
111+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
112+
113+
// Start communication with the SARA-R5. Load and activate the Packet Switched Data profile.
114+
115+
//mySARA.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial
116+
117+
// For the MicroMod Asset Tracker, we need to invert the power pin so it pulls high instead of low
118+
// Comment the next line if required
119+
mySARA.invertPowerPin(true);
120+
121+
// Initialize the SARA
122+
if (mySARA.begin(saraSerial, 115200) )
123+
{
124+
Serial.println(F("SARA-R5 connected!"));
125+
}
126+
else
127+
{
128+
Serial.println(F("Unable to communicate with the SARA."));
129+
Serial.println(F("Manually power-on (hold the SARA On button for 3 seconds) on and try again."));
130+
while (1) ; // Loop forever on fail
131+
}
132+
Serial.println();
133+
134+
// First check to see if we're connected to an operator:
135+
if (mySARA.getOperator(&currentOperator) == SARA_R5_SUCCESS)
136+
{
137+
Serial.print(F("Connected to: "));
138+
Serial.println(currentOperator);
139+
}
140+
else
141+
{
142+
Serial.print(F("The SARA is not yet connected to an operator. Please use the previous examples to connect. Or wait and retry. Freezing..."));
143+
while (1)
144+
; // Do nothing more
145+
}
146+
147+
// Deactivate the PSD profile - in case one is already active
148+
if (mySARA.performPDPaction(0, SARA_R5_PSD_ACTION_DEACTIVATE) != SARA_R5_SUCCESS)
149+
{
150+
Serial.println(F("Warning: performPDPaction (deactivate profile) failed. Probably because no profile was active."));
151+
}
152+
153+
// Load the PSD profile from NVM - these were saved by a previous example
154+
if (mySARA.performPDPaction(0, SARA_R5_PSD_ACTION_LOAD) != SARA_R5_SUCCESS)
155+
{
156+
Serial.println(F("performPDPaction (load from NVM) failed! Freezing..."));
157+
while (1)
158+
; // Do nothing more
159+
}
160+
161+
// Activate the profile
162+
if (mySARA.performPDPaction(0, SARA_R5_PSD_ACTION_ACTIVATE) != SARA_R5_SUCCESS)
163+
{
164+
Serial.println(F("performPDPaction (activate profile) failed! Freezing..."));
165+
while (1)
166+
; // Do nothing more
167+
}
168+
169+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
170+
171+
//Print the dynamic IP Address (for profile 0)
172+
IPAddress myAddress;
173+
mySARA.getNetworkAssignedIPAddress(0, &myAddress);
174+
Serial.print(F("\r\nMy IP Address is: "));
175+
Serial.print(myAddress[0]);
176+
Serial.print(F("."));
177+
Serial.print(myAddress[1]);
178+
Serial.print(F("."));
179+
Serial.print(myAddress[2]);
180+
Serial.print(F("."));
181+
Serial.println(myAddress[3]);
182+
183+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
184+
185+
// Start I2C. Connect to the GNSS.
186+
187+
Wire.begin(); //Start I2C
188+
189+
// Uncomment the next line to enable the 'major' GNSS debug messages on Serial so you can see what AssistNow data is being sent
190+
//myGNSS.enableDebugging(Serial, true);
191+
192+
if (myGNSS.begin() == false) //Connect to the Ublox module using Wire port
193+
{
194+
Serial.println(F("u-blox GPS not detected at default I2C address. Please check wiring. Freezing."));
195+
while (1);
196+
}
197+
Serial.println(F("u-blox module connected"));
198+
199+
myGNSS.setI2COutput(COM_TYPE_UBX | COM_TYPE_NMEA); //Set the I2C port to output both NMEA and UBX messages
200+
myGNSS.setPortInput(COM_PORT_I2C, COM_TYPE_UBX | COM_TYPE_NMEA | COM_TYPE_RTCM3); //Be sure RTCM3 input is enabled. UBX + RTCM3 is not a valid state.
201+
202+
myGNSS.setDGNSSConfiguration(SFE_UBLOX_DGNSS_MODE_FIXED); // Set the differential mode - ambiguities are fixed whenever possible
203+
204+
myGNSS.setNavigationFrequency(1); //Set the navigation rate to 1Hz
205+
206+
myGNSS.setAutoPVT(true); // Enable automatic PVT reports at the navigation frequency
207+
208+
// Set the Main Talker ID to "GP". The NMEA GGA messages will be GPGGA instead of GNGGA
209+
myGNSS.setMainTalkerID(SFE_UBLOX_MAIN_TALKER_ID_GP);
210+
211+
myGNSS.enableNMEAMessage(UBX_NMEA_GGA, COM_PORT_I2C, 10); // Tell the module to output GGA every 10 seconds
212+
213+
//myGNSS.saveConfiguration(VAL_CFG_SUBSEC_IOPORT | VAL_CFG_SUBSEC_MSGCONF); //Optional: Save the ioPort and message settings to NVM
214+
215+
}
216+
217+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
218+
219+
void loop()
220+
{
221+
enum states // Use a 'state machine' to open and close the connection
222+
{
223+
open_connection,
224+
check_connection_and_wait_for_keypress,
225+
close_connection,
226+
waiting_for_keypress
227+
};
228+
static states state = open_connection;
229+
230+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
231+
232+
switch (state)
233+
{
234+
case open_connection:
235+
Serial.println(F("Connecting to the NTRIP caster..."));
236+
if (beginClient((int *)&socketNum, (bool *)&connectionOpen)) // Try to open the connection to the caster
237+
{
238+
Serial.println(F("Connected to the NTRIP caster! Press any key to disconnect..."));
239+
state = check_connection_and_wait_for_keypress; // Move on
240+
}
241+
else
242+
{
243+
Serial.print(F("Could not connect to the caster. Trying again in 5 seconds."));
244+
for (int i = 0; i < 5; i++)
245+
{
246+
delay(1000);
247+
Serial.print(F("."));
248+
}
249+
Serial.println();
250+
}
251+
break;
252+
253+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
254+
255+
case check_connection_and_wait_for_keypress:
256+
// If the connection has dropped or timed out, or if the user has pressed a key
257+
if ((checkConnection((int)socketNum, (bool)connectionOpen) == false) || (keyPressed()))
258+
{
259+
state = close_connection; // Move on
260+
}
261+
delay(50);
262+
break;
263+
264+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
265+
266+
case close_connection:
267+
Serial.println(F("Closing the connection to the NTRIP caster..."));
268+
closeConnection((int *)&socketNum, (bool *)&connectionOpen);
269+
Serial.println(F("Press any key to reconnect..."));
270+
state = waiting_for_keypress; // Move on
271+
break;
272+
273+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
274+
275+
case waiting_for_keypress:
276+
// Wait for the user to press a key
277+
checkConnection((int)socketNum, (bool)connectionOpen); // 'Check' the connection - to print the latest PVT data
278+
if (keyPressed())
279+
state = open_connection; // Move on
280+
break;
281+
}
282+
}
283+
284+
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

0 commit comments

Comments
 (0)