Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions examples/SARA-R5_Example11_Clock/SARA-R5_Example11_Clock.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*

SARA-R5 Example
===============

Clock

Written by: Paul Clark
Date: January 3rd 2022

This example demonstrates how to use the clock function to read the time from the SARA-R5.

Note: the clock will be set to network time only if your network supports NITZ (Network Identity and Time Zone)

Feel like supporting open source hardware?
Buy a board from SparkFun!

Licence: MIT
Please see LICENSE.md for full details

*/

#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

// Uncomment the next line to connect to the SARA-R5 using hardware Serial1
#define saraSerial Serial1

// Uncomment the next line to create a SoftwareSerial object to pass to the SARA-R5 library instead
//SoftwareSerial saraSerial(8, 9);

// Create a SARA_R5 object to use throughout the sketch
// Usually we would tell the library which GPIO pin to use to control the SARA power (see below),
// but we can start the SARA without a power pin. It just means we need to manually
// turn the power on if required! ;-D
SARA_R5 mySARA;

// Create a SARA_R5 object to use throughout the sketch
// We need to tell the library what GPIO pin is connected to the SARA power pin.
// If you're using the MicroMod Asset Tracker and the MicroMod Artemis Processor Board,
// the pin name is G2 which is connected to pin AD34.
// Change the pin number if required.
//SARA_R5 mySARA(34);

void setup()
{
Serial.begin(115200); // Start the serial console

// Wait for user to press key to begin
Serial.println(F("SARA-R5 Example"));

//mySARA.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial

// For the MicroMod Asset Tracker, we need to invert the power pin so it pulls high instead of low
// Comment the next line if required
mySARA.invertPowerPin(true);

// Initialize the SARA
if (mySARA.begin(saraSerial, 9600) )
{
Serial.println(F("SARA-R5 connected!"));
}
else
{
Serial.println(F("Unable to communicate with the SARA."));
Serial.println(F("Manually power-on (hold the SARA On button for 3 seconds) on and try again."));
while (1) ; // Loop forever on fail
}
Serial.println();

// Make sure automatic time zone updates are enabled
if (mySARA.autoTimeZone(true) != SARA_R5_SUCCESS)
Serial.println(F("Enable autoTimeZone failed!"));

// Read and print the clock as a String
String theTime = mySARA.clock();
Serial.println(theTime);

// Read and print the hour, minute, etc. separately
uint8_t year, month, day, hour, minute, second;
int8_t timeZone;
if (mySARA.clock( &year, &month, &day, &hour, &minute, &second, &timeZone ) == SARA_R5_SUCCESS)
// Note: not all Arduino boards implement printf correctly. The formatting may not be correct on some boards.
// Note: the timeZone is defined in 15 minute increments, not hours. -28 indicates the time zone is 7 hours behind UTC/GMT.
Serial.printf("%02d/%02d/%02d %02d:%02d:%02d %+d\r\n", year, month, day, hour, minute, second, timeZone);

}

void loop()
{
// Nothing to do here
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SparkFun u-blox SARA-R5 Arduino Library
version=1.0.3
version=1.0.4
author=SparkFun Electronics <techsupport@sparkfun.com>
maintainer=SparkFun Electronics <sparkfun.com>
sentence=Library for the u-blox SARA-R5 LTE-M / NB-IoT modules with secure cloud
Expand Down
48 changes: 27 additions & 21 deletions src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,18 +847,21 @@ String SARA_R5::clock(void)
}
*(clockEnd) = '\0'; // Set last quote to null char -- end string

String clock = String(clockBegin); // Extract the clock as a String _before_ freeing response

free(command);
free(response);

return String(clockBegin);
return (clock);
}

SARA_R5_error_t SARA_R5::clock(uint8_t *y, uint8_t *mo, uint8_t *d,
uint8_t *h, uint8_t *min, uint8_t *s, uint8_t *tz)
uint8_t *h, uint8_t *min, uint8_t *s, int8_t *tz)
{
SARA_R5_error_t err;
char *command;
char *response;
char tzPlusMinus;

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

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

// Response format: \r\n+CCLK: "YY/MM/DD,HH:MM:SS-TZ"\r\n\r\nOK\r\n
// Response format (if TZ is negative): \r\n+CCLK: "YY/MM/DD,HH:MM:SS-TZ"\r\n\r\nOK\r\n
if (err == SARA_R5_ERROR_SUCCESS)
{
if (sscanf(response, "\r\n+CCLK: \"%d/%d/%d,%d:%d:%d-%d\"\r\n",
&iy, &imo, &id, &ih, &imin, &is, &itz) == 7)
if (sscanf(response, "\r\n+CCLK: \"%d/%d/%d,%d:%d:%d%c%d\"\r\n",
&iy, &imo, &id, &ih, &imin, &is, &tzPlusMinus, &itz) == 8)
{
*y = iy;
*mo = imo;
*d = id;
*h = ih;
*min = imin;
*s = is;
*tz = itz;
if (tzPlusMinus == '+')
*tz = itz;
else
*tz = 0 - itz;
}
else
err = SARA_R5_ERROR_UNEXPECTED_RESPONSE;
Expand Down Expand Up @@ -3640,12 +3646,12 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse(
int index = 0;
int destIndex = 0;
unsigned int charsRead = 0;
//bool printedSomething = false;
bool printedSomething = false;

// if (_printDebug == true)
// _debugPort->print(F("sendCommandWithResponse: Command: "));
// if (_printDebug == true)
// _debugPort->println(String(command));
if (_printDebug == true)
_debugPort->print(F("sendCommandWithResponse: Command: "));
if (_printDebug == true)
_debugPort->println(String(command));

int backlogIndex = sendCommand(command, at); //Sending command needs to dump data to backlog buffer as well.
unsigned long timeIn = millis();
Expand All @@ -3655,13 +3661,13 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse(
if (hwAvailable() > 0) //hwAvailable can return -1 if the serial port is NULL
{
char c = readChar();
// if (_printDebug == true)
// {
// if (printedSomething == false)
// _debugPort->print(F("sendCommandWithResponse: Response: "));
// _debugPort->print(c);
// printedSomething = true;
// }
if (_printDebug == true)
{
if (printedSomething == false)
_debugPort->print(F("sendCommandWithResponse: Response: "));
_debugPort->print(c);
printedSomething = true;
}
if (responseDest != NULL)
{
responseDest[destIndex++] = c;
Expand All @@ -3686,9 +3692,9 @@ SARA_R5_error_t SARA_R5::sendCommandWithResponse(
}
}

// if (_printDebug == true)
// if (printedSomething)
// _debugPort->println();
if (_printDebug == true)
if (printedSomething)
_debugPort->println();

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

Expand Down
2 changes: 1 addition & 1 deletion src/SparkFun_u-blox_SARA-R5_Arduino_Library.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ class SARA_R5 : public Print
String clock(void);
// TODO: Return a clock struct
SARA_R5_error_t clock(uint8_t *y, uint8_t *mo, uint8_t *d,
uint8_t *h, uint8_t *min, uint8_t *s, uint8_t *tz);
uint8_t *h, uint8_t *min, uint8_t *s, int8_t *tz); // TZ can be +/-
SARA_R5_error_t autoTimeZone(bool enable);
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);
SARA_R5_error_t getUtimeMode(SARA_R5_utime_mode_t *mode, SARA_R5_utime_sensor_t *sensor);
Expand Down