Skip to content

Commit 6ecfcb9

Browse files
authored
Merge pull request #10 from sparkfun/release_candidate
v1.0.4
2 parents e1617af + 3a2254d commit 6ecfcb9

File tree

4 files changed

+120
-23
lines changed

4 files changed

+120
-23
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
3+
SARA-R5 Example
4+
===============
5+
6+
Clock
7+
8+
Written by: Paul Clark
9+
Date: January 3rd 2022
10+
11+
This example demonstrates how to use the clock function to read the time from the SARA-R5.
12+
13+
Note: the clock will be set to network time only if your network supports NITZ (Network Identity and Time Zone)
14+
15+
Feel like supporting open source hardware?
16+
Buy a board from SparkFun!
17+
18+
Licence: MIT
19+
Please see LICENSE.md for full details
20+
21+
*/
22+
23+
#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
24+
25+
// Uncomment the next line to connect to the SARA-R5 using hardware Serial1
26+
#define saraSerial Serial1
27+
28+
// Uncomment the next line to create a SoftwareSerial object to pass to the SARA-R5 library instead
29+
//SoftwareSerial saraSerial(8, 9);
30+
31+
// Create a SARA_R5 object to use throughout the sketch
32+
// Usually we would tell the library which GPIO pin to use to control the SARA power (see below),
33+
// but we can start the SARA without a power pin. It just means we need to manually
34+
// turn the power on if required! ;-D
35+
SARA_R5 mySARA;
36+
37+
// Create a SARA_R5 object to use throughout the sketch
38+
// We need to tell the library what GPIO pin is connected to the SARA power pin.
39+
// If you're using the MicroMod Asset Tracker and the MicroMod Artemis Processor Board,
40+
// the pin name is G2 which is connected to pin AD34.
41+
// Change the pin number if required.
42+
//SARA_R5 mySARA(34);
43+
44+
void setup()
45+
{
46+
Serial.begin(115200); // Start the serial console
47+
48+
// Wait for user to press key to begin
49+
Serial.println(F("SARA-R5 Example"));
50+
51+
//mySARA.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial
52+
53+
// For the MicroMod Asset Tracker, we need to invert the power pin so it pulls high instead of low
54+
// Comment the next line if required
55+
mySARA.invertPowerPin(true);
56+
57+
// Initialize the SARA
58+
if (mySARA.begin(saraSerial, 9600) )
59+
{
60+
Serial.println(F("SARA-R5 connected!"));
61+
}
62+
else
63+
{
64+
Serial.println(F("Unable to communicate with the SARA."));
65+
Serial.println(F("Manually power-on (hold the SARA On button for 3 seconds) on and try again."));
66+
while (1) ; // Loop forever on fail
67+
}
68+
Serial.println();
69+
70+
// Make sure automatic time zone updates are enabled
71+
if (mySARA.autoTimeZone(true) != SARA_R5_SUCCESS)
72+
Serial.println(F("Enable autoTimeZone failed!"));
73+
74+
// Read and print the clock as a String
75+
String theTime = mySARA.clock();
76+
Serial.println(theTime);
77+
78+
// Read and print the hour, minute, etc. separately
79+
uint8_t year, month, day, hour, minute, second;
80+
int8_t timeZone;
81+
if (mySARA.clock( &year, &month, &day, &hour, &minute, &second, &timeZone ) == SARA_R5_SUCCESS)
82+
// Note: not all Arduino boards implement printf correctly. The formatting may not be correct on some boards.
83+
// Note: the timeZone is defined in 15 minute increments, not hours. -28 indicates the time zone is 7 hours behind UTC/GMT.
84+
Serial.printf("%02d/%02d/%02d %02d:%02d:%02d %+d\r\n", year, month, day, hour, minute, second, timeZone);
85+
86+
}
87+
88+
void loop()
89+
{
90+
// Nothing to do here
91+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparkFun u-blox SARA-R5 Arduino Library
2-
version=1.0.3
2+
version=1.0.4
33
author=SparkFun Electronics <[email protected]>
44
maintainer=SparkFun Electronics <sparkfun.com>
55
sentence=Library for the u-blox SARA-R5 LTE-M / NB-IoT modules with secure cloud

src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -847,18 +847,21 @@ String SARA_R5::clock(void)
847847
}
848848
*(clockEnd) = '\0'; // Set last quote to null char -- end string
849849

850+
String clock = String(clockBegin); // Extract the clock as a String _before_ freeing response
851+
850852
free(command);
851853
free(response);
852854

853-
return String(clockBegin);
855+
return (clock);
854856
}
855857

856858
SARA_R5_error_t SARA_R5::clock(uint8_t *y, uint8_t *mo, uint8_t *d,
857-
uint8_t *h, uint8_t *min, uint8_t *s, uint8_t *tz)
859+
uint8_t *h, uint8_t *min, uint8_t *s, int8_t *tz)
858860
{
859861
SARA_R5_error_t err;
860862
char *command;
861863
char *response;
864+
char tzPlusMinus;
862865

863866
int iy, imo, id, ih, imin, is, itz;
864867

@@ -877,19 +880,22 @@ SARA_R5_error_t SARA_R5::clock(uint8_t *y, uint8_t *mo, uint8_t *d,
877880
err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK,
878881
response, SARA_R5_STANDARD_RESPONSE_TIMEOUT);
879882

880-
// Response format: \r\n+CCLK: "YY/MM/DD,HH:MM:SS-TZ"\r\n\r\nOK\r\n
883+
// Response format (if TZ is negative): \r\n+CCLK: "YY/MM/DD,HH:MM:SS-TZ"\r\n\r\nOK\r\n
881884
if (err == SARA_R5_ERROR_SUCCESS)
882885
{
883-
if (sscanf(response, "\r\n+CCLK: \"%d/%d/%d,%d:%d:%d-%d\"\r\n",
884-
&iy, &imo, &id, &ih, &imin, &is, &itz) == 7)
886+
if (sscanf(response, "\r\n+CCLK: \"%d/%d/%d,%d:%d:%d%c%d\"\r\n",
887+
&iy, &imo, &id, &ih, &imin, &is, &tzPlusMinus, &itz) == 8)
885888
{
886889
*y = iy;
887890
*mo = imo;
888891
*d = id;
889892
*h = ih;
890893
*min = imin;
891894
*s = is;
892-
*tz = itz;
895+
if (tzPlusMinus == '+')
896+
*tz = itz;
897+
else
898+
*tz = 0 - itz;
893899
}
894900
else
895901
err = SARA_R5_ERROR_UNEXPECTED_RESPONSE;
@@ -3640,12 +3646,12 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse(
36403646
int index = 0;
36413647
int destIndex = 0;
36423648
unsigned int charsRead = 0;
3643-
//bool printedSomething = false;
3649+
bool printedSomething = false;
36443650

3645-
// if (_printDebug == true)
3646-
// _debugPort->print(F("sendCommandWithResponse: Command: "));
3647-
// if (_printDebug == true)
3648-
// _debugPort->println(String(command));
3651+
if (_printDebug == true)
3652+
_debugPort->print(F("sendCommandWithResponse: Command: "));
3653+
if (_printDebug == true)
3654+
_debugPort->println(String(command));
36493655

36503656
int backlogIndex = sendCommand(command, at); //Sending command needs to dump data to backlog buffer as well.
36513657
unsigned long timeIn = millis();
@@ -3655,13 +3661,13 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse(
36553661
if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is NULL
36563662
{
36573663
char c = readChar();
3658-
// if (_printDebug == true)
3659-
// {
3660-
// if (printedSomething == false)
3661-
// _debugPort->print(F("sendCommandWithResponse: Response: "));
3662-
// _debugPort->print(c);
3663-
// printedSomething = true;
3664-
// }
3664+
if (_printDebug == true)
3665+
{
3666+
if (printedSomething == false)
3667+
_debugPort->print(F("sendCommandWithResponse: Response: "));
3668+
_debugPort->print(c);
3669+
printedSomething = true;
3670+
}
36653671
if (responseDest != NULL)
36663672
{
36673673
responseDest[destIndex++] = c;
@@ -3686,9 +3692,9 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse(
36863692
}
36873693
}
36883694

3689-
// if (_printDebug == true)
3690-
// if (printedSomething)
3691-
// _debugPort->println();
3695+
if (_printDebug == true)
3696+
if (printedSomething)
3697+
_debugPort->println();
36923698

36933699
pruneBacklog(); // Prune any incoming non-actionable URC's and responses/errors from the backlog
36943700

src/SparkFun_u-blox_SARA-R5_Arduino_Library.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ class SARA_R5 : public Print
519519
String clock(void);
520520
// TODO: Return a clock struct
521521
SARA_R5_error_t clock(uint8_t *y, uint8_t *mo, uint8_t *d,
522-
uint8_t *h, uint8_t *min, uint8_t *s, uint8_t *tz);
522+
uint8_t *h, uint8_t *min, uint8_t *s, int8_t *tz); // TZ can be +/-
523523
SARA_R5_error_t autoTimeZone(bool enable);
524524
SARA_R5_error_t setUtimeMode(SARA_R5_utime_mode_t mode = SARA_R5_UTIME_MODE_PPS, SARA_R5_utime_sensor_t sensor = SARA_R5_UTIME_SENSOR_GNSS_LTE);
525525
SARA_R5_error_t getUtimeMode(SARA_R5_utime_mode_t *mode, SARA_R5_utime_sensor_t *sensor);

0 commit comments

Comments
 (0)