Skip to content

Commit 23094f9

Browse files
committed
Adding invertPowerPin. Updating Example101 after testing on the MM Asset Tracker CB
1 parent 7b4f4b5 commit 23094f9

File tree

3 files changed

+101
-33
lines changed

3 files changed

+101
-33
lines changed

examples/Example101_GNSS_GPRMC/Example101_GNSS_GPRMC.ino

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@
88
99
This example demonstrates how to use the gpsGetRmc feature.
1010
11-
This code is intend to run on the MicroMod Asset Tracker Carrier Board
12-
using (e.g.) the MicroMod Artemis Processor Board
13-
14-
Select the SparkFun RedBoard Artemis ATP from the SparkFun Apollo3 boards
15-
1611
*/
1712

18-
//Click here to get the library: http://librarymanager/All#SparkFun_u-blox_SARA-R5_Arduino_Library
19-
#include <SparkFun_u-blox_SARA-R5_Arduino_Library.h>
13+
#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
2014

2115
// Uncomment the next line to connect to the SARA-R5 using hardware Serial1
2216
#define saraSerial Serial1
@@ -28,14 +22,20 @@
2822
// Usually we would tell the library which GPIO pin to use to control the SARA power (see below),
2923
// but we can start the SARA without a power pin. It just means we need to use a finger to
3024
// turn the power on manually if required! ;-D
31-
SARA_R5 assetTracker;
25+
SARA_R5 mySARA;
3226

3327
// Create a SARA_R5 object to use throughout the sketch
3428
// We need to tell the library what GPIO pin is connected to the SARA power pin.
3529
// If you're using the MicroMod Asset Tracker and the MicroMod Artemis Processor Board,
36-
// the pin number is GPIO2 which is connected to AD34. TO DO: Check this!
30+
// the pin name is G2 which is connected to pin AD34.
31+
// Change the pin number if required.
32+
//SARA_R5 mySARA(34);
33+
34+
// If you're using the MicroMod Asset Tracker then we need to define which pin controls the active antenna power.
35+
// If you're using the MicroMod Artemis Processor Board, the pin name is G6 which is connected to pin D14.
3736
// Change the pin number if required.
38-
//SARA_R5 assetTracker(34);
37+
//const int antennaPowerEnablePin = 14; // Uncomment this line to define the pin number for the antenna power enable
38+
const int antennaPowerEnablePin = -1; // Uncomment this line if your board does not have an antenna power enable
3939

4040
PositionData gps;
4141
SpeedData spd;
@@ -46,41 +46,56 @@ boolean valid;
4646
unsigned long lastGpsPoll = 0;
4747

4848
void setup() {
49-
Serial.begin(9600);
49+
Serial.begin(115200);
5050

5151
// Wait for user to press key in terminal to begin
52-
Serial.println("Press any key to begin GPS'ing");
52+
Serial.println(F("SARA-R5 Example"));
53+
Serial.println(F("Press any key to begin GPS'ing"));
5354
while (!Serial.available()) ;
5455
while (Serial.available()) Serial.read();
5556

57+
//mySARA.enableDebugging(); // Uncomment this line to enable helpful debug messages
58+
59+
// For the Asset Tracker, we need to invert the power pin so it pulls high instead of low
60+
// Comment the next line if required
61+
mySARA.invertPowerPin(true);
62+
5663
// Initialize the SARA
57-
if (assetTracker.begin(saraSerial, 9600) ) {
58-
Serial.println(F("Asset Tracker (SARA-R5) connected!"));
64+
if (mySARA.begin(saraSerial, 9600) ) {
65+
Serial.println(F("SARA-R5 connected!"));
5966
}
6067

6168
// Enable the GPS's RMC sentence output. This will also turn the
62-
// GPS module on ((assetTracker.gpsPower(true)) if it's not already
63-
if (assetTracker.gpsEnableRmc(true) != SARA_R5_SUCCESS) {
69+
// GPS module on ((mySARA.gpsPower(true)) if it's not already
70+
if (mySARA.gpsEnableRmc(true) != SARA_R5_SUCCESS) {
6471
Serial.println(F("Error initializing GPS. Freezing..."));
6572
while (1) ;
6673
}
74+
75+
// Enable power for the GNSS active antenna
76+
enableGNSSAntennaPower();
6777
}
6878

6979
void loop() {
70-
if ((lastGpsPoll == 0) || (lastGpsPoll + GPS_POLL_RATE < millis())) {
71-
// Call (assetTracker.gpsGetRmc to get coordinate, speed, and timing data
80+
if ((lastGpsPoll == 0) || (lastGpsPoll + GPS_POLL_RATE < millis()))
81+
{
82+
// Call (mySARA.gpsGetRmc to get coordinate, speed, and timing data
7283
// from the GPS module. Valid can be used to check if the GPS is
7384
// reporting valid data
74-
if (assetTracker.gpsGetRmc(&gps, &spd, &clk, &valid) == SARA_R5_SUCCESS) {
85+
if (mySARA.gpsGetRmc(&gps, &spd, &clk, &valid) == SARA_R5_SUCCESS)
86+
{
7587
printGPS();
7688
lastGpsPoll = millis();
77-
} else {
89+
}
90+
else
91+
{
7892
delay(1000); // If RMC read fails, wait a second and try again
7993
}
8094
}
8195
}
8296

83-
void printGPS(void) {
97+
void printGPS(void)
98+
{
8499
Serial.println();
85100
Serial.println("UTC: " + String(gps.utc));
86101
Serial.print("Time: ");
@@ -95,10 +110,29 @@ void printGPS(void) {
95110
Serial.println("Latitude: " + String(gps.lat, 7));
96111
Serial.println("Longitude: " + String(gps.lon, 7));
97112
Serial.println("Speed: " + String(spd.speed, 4) + " @ " + String(spd.cog, 4));
98-
Serial.println("Date: " + String(clk.date.month) + "/" +
113+
Serial.println("Date (MM/DD/YY): " + String(clk.date.month) + "/" +
99114
String(clk.date.day) + "/" + String(clk.date.year));
100115
Serial.println("Magnetic variation: " + String(spd.magVar));
101116
Serial.println("Status: " + String(gps.status));
102117
Serial.println("Mode: " + String(gps.mode));
103118
Serial.println();
104119
}
120+
121+
// Disable power for the GNSS active antenna
122+
void disableGNSSAntennaPower()
123+
{
124+
if (antennaPowerEnablePin >= 0)
125+
{
126+
pinMode(antennaPowerEnablePin, OUTPUT);
127+
digitalWrite(antennaPowerEnablePin, LOW);
128+
}
129+
}
130+
// Enable power for the GNSS active antenna
131+
void enableGNSSAntennaPower()
132+
{
133+
if (antennaPowerEnablePin >= 0)
134+
{
135+
pinMode(antennaPowerEnablePin, OUTPUT);
136+
digitalWrite(antennaPowerEnablePin, HIGH);
137+
}
138+
}

src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
#include <SparkFun_u-blox_SARA-R5_Arduino_Library.h>
1818

19-
static boolean parseGPRMCString(char *rmcString, PositionData *pos, ClockData *clk, SpeedData *spd);
20-
2119
SARA_R5::SARA_R5(int powerPin, int resetPin, uint8_t maxInitDepth)
2220
{
2321
#ifdef SARA_R5_SOFTWARE_SERIAL_ENABLED
@@ -27,6 +25,7 @@ SARA_R5::SARA_R5(int powerPin, int resetPin, uint8_t maxInitDepth)
2725
_baud = 0;
2826
_resetPin = resetPin;
2927
_powerPin = powerPin;
28+
_invertPowerPin = false;
3029
_maxInitDepth = maxInitDepth;
3130
_socketReadCallback = NULL;
3231
_socketCloseCallback = NULL;
@@ -1993,14 +1992,26 @@ SARA_R5_error_t SARA_R5::init(unsigned long baud,
19931992
return SARA_R5_ERROR_SUCCESS;
19941993
}
19951994

1995+
void SARA_R5::invertPowerPin(boolean invert)
1996+
{
1997+
_invertPowerPin = invert;
1998+
}
1999+
19962000
void SARA_R5::powerOn(void)
19972001
{
19982002
if (_powerPin >= 0)
19992003
{
2004+
if (_invertPowerPin) // Set the pin state before making it an output
2005+
digitalWrite(_powerPin, HIGH);
2006+
else
2007+
digitalWrite(_powerPin, LOW);
20002008
pinMode(_powerPin, OUTPUT);
2001-
digitalWrite(_powerPin, LOW);
2009+
if (_invertPowerPin) // Set the pin state
2010+
digitalWrite(_powerPin, HIGH);
2011+
else
2012+
digitalWrite(_powerPin, LOW);
20022013
delay(SARA_R5_POWER_PULSE_PERIOD);
2003-
pinMode(_powerPin, INPUT); // Return to high-impedance, rely on SARA module internal pull-up
2014+
pinMode(_powerPin, INPUT); // Return to high-impedance, rely on (e.g.) SARA module internal pull-up
20042015
delay(2000); //Wait before sending AT commands to module. 100 is too short.
20052016
if (_printDebug == true) _debugPort->println(F("Power cycle complete."));
20062017
}
@@ -2595,7 +2606,7 @@ void SARA_R5::pruneBacklog()
25952606
// GPS Helper Functions:
25962607

25972608
// Read a source string until a delimiter is hit, store the result in destination
2598-
static char *readDataUntil(char *destination, unsigned int destSize,
2609+
char *SARA_R5::readDataUntil(char *destination, unsigned int destSize,
25992610
char *source, char delimiter)
26002611
{
26012612

@@ -2614,15 +2625,19 @@ static char *readDataUntil(char *destination, unsigned int destSize,
26142625
return strEnd;
26152626
}
26162627

2617-
#define TEMP_NMEA_DATA_SIZE 16
2618-
2619-
static boolean parseGPRMCString(char *rmcString, PositionData *pos,
2628+
boolean SARA_R5::parseGPRMCString(char *rmcString, PositionData *pos,
26202629
ClockData *clk, SpeedData *spd)
26212630
{
26222631
char *ptr, *search;
26232632
unsigned long tTemp;
26242633
char tempData[TEMP_NMEA_DATA_SIZE];
26252634

2635+
if (_printDebug == true)
2636+
{
2637+
_debugPort->println(F("parseGPRMCString: rmcString: "));
2638+
_debugPort->println(rmcString);
2639+
}
2640+
26262641
// Fast-forward test to first value:
26272642
ptr = strchr(rmcString, ',');
26282643
ptr++; // Move ptr past first comma
@@ -2670,7 +2685,7 @@ static boolean parseGPRMCString(char *rmcString, PositionData *pos,
26702685
{
26712686
pos->lat = atof(tempData); // Extract ddmm.mmmmm as float
26722687
unsigned long lat_deg = pos->lat / 100; // Extract the degrees
2673-
pos->lat -= (float)lat_deg * 60.0; // Subtract the degrees leaving only the minutes
2688+
pos->lat -= (float)lat_deg * 100.0; // Subtract the degrees leaving only the minutes
26742689
pos->lat /= 60.0; // Convert minutes into degrees
26752690
pos->lat += (float)lat_deg; // Finally add the degrees back on again
26762691
}
@@ -2679,6 +2694,7 @@ static boolean parseGPRMCString(char *rmcString, PositionData *pos,
26792694
pos->lat = 0.0;
26802695
}
26812696
ptr = search + 1;
2697+
26822698
// Find latitude hemishpere
26832699
search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ',');
26842700
if ((search != NULL) && (search == ptr + 1))
@@ -2694,7 +2710,7 @@ static boolean parseGPRMCString(char *rmcString, PositionData *pos,
26942710
{
26952711
pos->lon = atof(tempData); // Extract dddmm.mmmmm as float
26962712
unsigned long lon_deg = pos->lon / 100; // Extract the degrees
2697-
pos->lon -= (float)lon_deg * 60.0; // Subtract the degrees leaving only the minutes
2713+
pos->lon -= (float)lon_deg * 100.0; // Subtract the degrees leaving only the minutes
26982714
pos->lon /= 60.0; // Convert minutes into degrees
26992715
pos->lon += (float)lon_deg; // Finally add the degrees back on again
27002716
}
@@ -2703,7 +2719,8 @@ static boolean parseGPRMCString(char *rmcString, PositionData *pos,
27032719
pos->lon = 0.0;
27042720
}
27052721
ptr = search + 1;
2706-
// Find latitude hemishpere
2722+
2723+
// Find longitude hemishpere
27072724
search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ',');
27082725
if ((search != NULL) && (search == ptr + 1))
27092726
{
@@ -2724,6 +2741,7 @@ static boolean parseGPRMCString(char *rmcString, PositionData *pos,
27242741
spd->speed = 0.0;
27252742
}
27262743
ptr = search + 1;
2744+
27272745
// Find course over ground
27282746
search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ',');
27292747
if ((search != NULL) && (search != ptr))
@@ -2766,6 +2784,7 @@ static boolean parseGPRMCString(char *rmcString, PositionData *pos,
27662784
spd->magVar = 0.0;
27672785
}
27682786
ptr = search + 1;
2787+
27692788
// Find magnetic variation direction
27702789
search = readDataUntil(tempData, TEMP_NMEA_DATA_SIZE, ptr, ',');
27712790
if ((search != NULL) && (search == ptr + 1))

src/SparkFun_u-blox_SARA-R5_Arduino_Library.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ const char SARA_R5_RESPONSE_ERROR[] = "ERROR\r\n";
118118
const char ASCII_CTRL_Z = 0x1A;
119119
const char ASCII_ESC = 0x1B;
120120

121+
// NMEA data size - used by parseGPRMCString
122+
#define TEMP_NMEA_DATA_SIZE 16
123+
121124
#define NOT_AT_COMMAND false
122125
#define AT_COMMAND true
123126

@@ -275,6 +278,11 @@ class SARA_R5 : public Print
275278
// Debug prints
276279
void enableDebugging(Stream &debugPort = Serial); //Turn on debug printing. If user doesn't specify then Serial will be used.
277280

281+
// Invert the polarity of the power pin - if required
282+
// Normally the SARA's power pin is pulled low and released to toggle the power
283+
// But the Asset Tracker needs this to be pulled high and released instead
284+
void invertPowerPin(boolean invert = false);
285+
278286
// Loop polling and polling setup
279287
boolean bufferedPoll(void);
280288
boolean processReadEvent(char* event);
@@ -466,6 +474,8 @@ class SARA_R5 : public Print
466474

467475
int _powerPin;
468476
int _resetPin;
477+
boolean _invertPowerPin = false;
478+
469479
unsigned long _baud;
470480
IPAddress _lastRemoteIP;
471481
IPAddress _lastLocalIP;
@@ -533,6 +543,11 @@ class SARA_R5 : public Print
533543
char *sara_r5_calloc_char(size_t num);
534544

535545
void pruneBacklog(void);
546+
547+
// GPS Helper functions
548+
char *readDataUntil(char *destination, unsigned int destSize, char *source, char delimiter);
549+
boolean parseGPRMCString(char *rmcString, PositionData *pos, ClockData *clk, SpeedData *spd);
550+
536551
};
537552

538553
#endif //SPARKFUN_SARA_R5_ARDUINO_LIBRARY_H

0 commit comments

Comments
 (0)