Skip to content

Commit 3bcb5e0

Browse files
committed
Adding explicit SyncTime step to make the necessity of synchronising the time more visible
1 parent 6e9aaf0 commit 3bcb5e0

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ void ArduinoIoTCloudTCP::update()
178178
switch (_state)
179179
{
180180
case State::ConnectPhy: next_state = handle_ConnectPhy(); break;
181+
case State::SyncTime: next_state = handle_SyncTime(); break;
181182
case State::ConnectMqttBroker: next_state = handle_ConnectMqttBroker(); break;
182183
case State::SubscribeMqttTopics: next_state = handle_SubscribeMqttTopics(); break;
183184
case State::RequestLastValues: next_state = handle_RequestLastValues(); break;
@@ -215,11 +216,18 @@ void ArduinoIoTCloudTCP::setOTAStorage(OTAStorage & ota_storage)
215216
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy()
216217
{
217218
if (_connection->check() == NetworkConnectionState::CONNECTED)
218-
return State::ConnectMqttBroker;
219+
return State::SyncTime;
219220
else
220221
return State::ConnectPhy;
221222
}
222223

224+
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime()
225+
{
226+
unsigned long const internal_posix_time = time_service.getTime();
227+
DBG_VERBOSE("ArduinoIoTCloudTCP::%s internal clock configured to posix timestamp %d", __FUNCTION__, internal_posix_time);
228+
return State::ConnectMqttBroker;
229+
}
230+
223231
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
224232
{
225233
if (_mqttClient.connect(_brokerAddress.c_str(), _brokerPort))

src/ArduinoIoTCloudTCP.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
9999
enum class State
100100
{
101101
ConnectPhy,
102+
SyncTime,
102103
ConnectMqttBroker,
103104
SubscribeMqttTopics,
104105
RequestLastValues,
@@ -148,6 +149,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
148149
inline String getTopic_ota_in () { return String("/a/d/" + getDeviceId() + "/ota/i"); }
149150

150151
State handle_ConnectPhy();
152+
State handle_SyncTime();
151153
State handle_ConnectMqttBroker();
152154
State handle_SubscribeMqttTopics();
153155
State handle_RequestLastValues();

0 commit comments

Comments
 (0)