Skip to content

Commit e4ce494

Browse files
committed
Handle systemReset in a dedicated IoTCloud state to be able to wait messages are synced before reset the board
1 parent 5436c05 commit e4ce494

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/ArduinoIoTCloudTCP.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@ void ArduinoIoTCloudTCP::update()
315315
case State::SubscribeMqttTopics: next_state = handle_SubscribeMqttTopics(); break;
316316
case State::RequestLastValues: next_state = handle_RequestLastValues(); break;
317317
case State::Connected: next_state = handle_Connected(); break;
318+
#if OTA_ENABLED
319+
case State::Reset: next_state = handle_Reset(); break;
320+
#endif
318321
}
319322
_state = next_state;
320323

@@ -520,8 +523,8 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
520523
_ota_req = false;
521524
/* Transmit the cleared error and request flags to the cloud. */
522525
sendPropertiesToCloud();
523-
/* Reset the board to apply OTA update */
524-
systemReset();
526+
/* We are now ready to reset and apply OTA update */
527+
return State::Reset;
525528
}
526529
}
527530
#endif /* OTA_ENABLED */
@@ -530,6 +533,19 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
530533
}
531534
}
532535

536+
#if OTA_ENABLED
537+
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Reset()
538+
{
539+
static unsigned long const timeToReset = millis() + 1000;
540+
/* We need to wait at least one second to make sure properties are synced up */
541+
if(static_cast<int32_t>(timeToReset - millis()) <= 0) {
542+
/* Reset the board to apply OTA update */
543+
systemReset();
544+
}
545+
return State::Reset;
546+
}
547+
#endif /* OTA_ENABLED */
548+
533549
void ArduinoIoTCloudTCP::onMessage(int length)
534550
{
535551
ArduinoCloud.handleMessage(length);

src/ArduinoIoTCloudTCP.h

+4
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
104104
SubscribeMqttTopics,
105105
RequestLastValues,
106106
Connected,
107+
#if OTA_ENABLED
108+
Reset,
109+
#endif
107110
};
108111

109112
State _state;
@@ -156,6 +159,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
156159
State handle_SubscribeMqttTopics();
157160
State handle_RequestLastValues();
158161
State handle_Connected();
162+
State handle_Reset();
159163

160164
static void onMessage(int length);
161165
void handleMessage(int length);

0 commit comments

Comments
 (0)