@@ -86,7 +86,6 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
86
86
, _password(" " )
87
87
#endif
88
88
, _mqttClient{nullptr }
89
- , _syncStatus{ArduinoIoTSynchronizationStatus::SYNC_STATUS_SYNCHRONIZED}
90
89
, _stdinTopic(" " )
91
90
, _stdoutTopic(" " )
92
91
, _shadowTopicOut(" " )
@@ -188,11 +187,17 @@ void ArduinoIoTCloudTCP::update()
188
187
189
188
if (checkPhyConnection () != NetworkConnectionState::CONNECTED) return ;
190
189
190
+ /* Retrieve the latest data from the MQTT Client. */
191
+ if (_mqttClient.connected ())
192
+ _mqttClient.poll ();
193
+
194
+ /* Run through the state machine. */
191
195
State next_state = _state;
192
196
switch (_state)
193
197
{
194
198
case State::ConnectMqttBroker: next_state = handle_ConnectMqttBroker (); break ;
195
199
case State::SubscribeMqttTopics: next_state = handle_SubscribeMqttTopics (); break ;
200
+ case State::RequestLastValues: next_state = handle_RequestLastValues (); break ;
196
201
case State::Connected: next_state = handle_Connected (); break ;
197
202
}
198
203
_state = next_state;
@@ -214,30 +219,7 @@ void ArduinoIoTCloudTCP::update()
214
219
_mqtt_data_request_retransmit = false ;
215
220
}
216
221
217
- // MTTQClient connected!, poll() used to retrieve data from MQTT broker
218
- _mqttClient.poll ();
219
-
220
- switch (_syncStatus)
221
- {
222
- case ArduinoIoTSynchronizationStatus::SYNC_STATUS_SYNCHRONIZED: sendPropertiesToCloud (); break ;
223
-
224
- case ArduinoIoTSynchronizationStatus::SYNC_STATUS_WAIT_FOR_CLOUD_VALUES:
225
- {
226
- if (millis () - _lastSyncRequestTickTime > TIMEOUT_FOR_LASTVALUES_SYNC)
227
- {
228
- requestLastValue ();
229
- _lastSyncRequestTickTime = millis ();
230
- }
231
- }
232
- break ;
233
-
234
- case ArduinoIoTSynchronizationStatus::SYNC_STATUS_VALUES_PROCESSED:
235
- {
236
- execCloudEventCallback (ArduinoIoTCloudEvent::SYNC);
237
- _syncStatus = ArduinoIoTSynchronizationStatus::SYNC_STATUS_SYNCHRONIZED;
238
- }
239
- break ;
240
- }
222
+ sendPropertiesToCloud ();
241
223
}
242
224
243
225
int ArduinoIoTCloudTCP::connected ()
@@ -302,13 +284,25 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics()
302
284
DBG_ERROR (" ArduinoIoTCloudTCP::%s could not subscribe to %s" , __FUNCTION__, _ota_topic_in.c_str ());
303
285
return State::SubscribeMqttTopics;
304
286
}
305
- _syncStatus = ArduinoIoTSynchronizationStatus::SYNC_STATUS_WAIT_FOR_CLOUD_VALUES;
306
- _lastSyncRequestTickTime = 0 ;
307
287
}
308
288
309
289
DBG_VERBOSE (" Connected to Arduino IoT Cloud" );
310
290
execCloudEventCallback (ArduinoIoTCloudEvent::CONNECT);
311
- return State::Connected;
291
+ return State::RequestLastValues;
292
+ }
293
+
294
+ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues ()
295
+ {
296
+ /* Check wether or not we need to send a new request. */
297
+ unsigned long const now = millis ();
298
+ if ((now - _lastSyncRequestTickTime) > TIMEOUT_FOR_LASTVALUES_SYNC)
299
+ {
300
+ DBG_VERBOSE (" ArduinoIoTCloudTCP::%s [%d] last values requested" , __FUNCTION__, now);
301
+ requestLastValue ();
302
+ _lastSyncRequestTickTime = now;
303
+ }
304
+
305
+ return State::RequestLastValues;
312
306
}
313
307
314
308
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected ()
@@ -345,11 +339,16 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
345
339
if (_dataTopicIn == topic) {
346
340
CBORDecoder::decode (_property_container, (uint8_t *)bytes, length);
347
341
}
348
- if ((_shadowTopicIn == topic) && _syncStatus == ArduinoIoTSynchronizationStatus::SYNC_STATUS_WAIT_FOR_CLOUD_VALUES) {
342
+
343
+ if ((_shadowTopicIn == topic) && (_state == State::RequestLastValues))
344
+ {
345
+ DBG_VERBOSE (" ArduinoIoTCloudTCP::%s [%d] last values received" , __FUNCTION__, millis ());
349
346
CBORDecoder::decode (_property_container, (uint8_t *)bytes, length, true );
350
347
sendPropertiesToCloud ();
351
- _syncStatus = ArduinoIoTSynchronizationStatus::SYNC_STATUS_VALUES_PROCESSED;
348
+ execCloudEventCallback (ArduinoIoTCloudEvent::SYNC);
349
+ _state = State::Connected;
352
350
}
351
+
353
352
#if OTA_ENABLED
354
353
if (_ota_topic_in == topic) {
355
354
_ota_logic.onOTADataReceived (bytes, length);
0 commit comments