Skip to content

Commit edf5b05

Browse files
committed
Setup RTC if available and use it for time-keeping after an initial valid time has been retrieved from either the network or via NTP
1 parent 3bab053 commit edf5b05

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

src/ArduinoIoTCloudTCP.cpp

+1-13
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
#include <ArduinoECCX08.h>
2626
#endif
2727

28-
#ifdef ARDUINO_ARCH_SAMD
29-
#include <RTCZero.h>
30-
RTCZero rtc;
31-
#endif
32-
3328
TimeService time_service;
3429

3530
#ifdef BOARD_HAS_ECCX08
@@ -44,11 +39,7 @@ const static int CONNECT_FAILURE = 0;
4439
const static int CONNECT_FAILURE_SUBSCRIBE = -1;
4540

4641
static unsigned long getTime() {
47-
unsigned long const time = time_service.getTime();
48-
#ifdef ARDUINO_ARCH_SAMD
49-
rtc.setEpoch(time);
50-
#endif
51-
return time;
42+
return time_service.getTime();
5243
}
5344

5445
ArduinoIoTCloudTCP::ArduinoIoTCloudTCP():
@@ -87,9 +78,6 @@ int ArduinoIoTCloudTCP::begin(TcpIpConnectionHandler & connection, String broker
8778
_brokerAddress = brokerAddress;
8879
_brokerPort = brokerPort;
8980
time_service.begin(&connection);
90-
#ifdef ARDUINO_ARCH_SAMD
91-
rtc.begin();
92-
#endif
9381
return begin(_connection->getClient(), _brokerAddress, _brokerPort);
9482
}
9583

src/utility/TimeService.cpp

+32-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828

2929
#include "NTPUtils.h"
3030

31+
/**************************************************************************************
32+
* GLOBAL VARIABLES
33+
**************************************************************************************/
34+
35+
#ifdef ARDUINO_ARCH_SAMD
36+
RTCZero rtc;
37+
#endif
38+
3139
/**************************************************************************************
3240
* INTERNAL FUNCTION DECLARATION
3341
**************************************************************************************/
@@ -46,6 +54,9 @@ static time_t const EPOCH_AT_COMPILE_TIME = cvt_time(__DATE__);
4654

4755
TimeService::TimeService()
4856
: _con_hdl(nullptr)
57+
#ifdef ARDUINO_ARCH_SAMD
58+
, _is_rtc_configured(false)
59+
#endif
4960
{
5061

5162
}
@@ -57,9 +68,30 @@ TimeService::TimeService()
5768
void TimeService::begin(ConnectionHandler * con_hdl)
5869
{
5970
_con_hdl = con_hdl;
71+
#ifdef ARDUINO_ARCH_SAMD
72+
rtc.begin();
73+
#endif
6074
}
6175

6276
unsigned long TimeService::getTime()
77+
{
78+
#ifdef ARDUINO_ARCH_SAMD
79+
if(!_is_rtc_configured)
80+
{
81+
rtc.setEpoch(getRemoteTime());
82+
_is_rtc_configured = true;
83+
}
84+
return rtc.getEpoch();
85+
#else
86+
return getRemoteTime();
87+
#endif
88+
}
89+
90+
/**************************************************************************************
91+
* PRIVATE MEMBER FUNCTIONS
92+
**************************************************************************************/
93+
94+
unsigned long TimeService::getRemoteTime()
6395
{
6496
if(_con_hdl == nullptr) return 0;
6597

@@ -83,10 +115,6 @@ unsigned long TimeService::getTime()
83115
return 0;
84116
}
85117

86-
/**************************************************************************************
87-
* PRIVATE MEMBER FUNCTIONS
88-
**************************************************************************************/
89-
90118
bool TimeService::isTimeValid(unsigned long const time)
91119
{
92120
return (time >= EPOCH_AT_COMPILE_TIME);

src/utility/TimeService.h

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727

2828
#include <Arduino_ConnectionHandler.h>
2929

30+
#ifdef ARDUINO_ARCH_SAMD
31+
#include <RTCZero.h>
32+
#endif
33+
3034
/**************************************************************************************
3135
* CLASS DECLARATION
3236
**************************************************************************************/
@@ -45,7 +49,11 @@ class TimeService
4549
private:
4650

4751
ConnectionHandler * _con_hdl;
52+
#ifdef ARDUINO_ARCH_SAMD
53+
bool _is_rtc_configured;
54+
#endif
4855

56+
unsigned long getRemoteTime();
4957
static bool isTimeValid(unsigned long const time);
5058

5159
};

0 commit comments

Comments
 (0)