diff --git a/src/data_source.cpp b/src/data_source.cpp index 091e5b3..f3063c5 100644 --- a/src/data_source.cpp +++ b/src/data_source.cpp @@ -62,8 +62,14 @@ void DataSource::loop() } if (m_connected && (millis() - m_last_data_received_at > c_no_data_reconnect_interval)) { - DEBUG_SERIAL.printf_P(PSTR("[WSc] No data received for %i secs, forcing reconnect\n"), c_no_data_reconnect_interval / 1000); - reconnect(); + if (++m_num_connection_tries < 2) { + DEBUG_SERIAL.printf_P(PSTR("[WSc] No data received for %i secs, forcing reconnect\n"), c_no_data_reconnect_interval / 1000); + reconnect(); + } else { + DEBUG_SERIAL.println(F("[WSc] Max reconnection attempts, forcing restart")); + ESP.restart(); + } + } // send heartbeat @@ -241,6 +247,7 @@ void DataSource::callback(WStype_t type, uint8_t * payload, size_t length) m_connected = true; m_last_connected_at = millis(); m_last_data_received_at = millis(); + m_num_connection_tries = 0; if (payload==nullptr) DEBUG_SERIAL.printf_P(PSTR("[WSc] Connected to url: \n")); else diff --git a/src/data_source.hpp b/src/data_source.hpp index 2255555..10ae694 100644 --- a/src/data_source.hpp +++ b/src/data_source.hpp @@ -50,7 +50,9 @@ class DataSource m_should_send_hello(false), m_hello_sent(true), m_last_heartbeat_sent_at(0), m_last_data_received_at(0), m_text_last_sent_at(0), m_on_price_change(nullptr), m_on_price_ath(nullptr), m_on_update_request(nullptr), m_on_announcement(nullptr), - m_on_countdown(nullptr), m_on_sound(nullptr), m_on_otp(nullptr), m_on_otp_ack(nullptr), m_on_price_timeout_set(nullptr), m_on_new_settings(nullptr) + m_on_countdown(nullptr), m_on_sound(nullptr), m_on_otp(nullptr), m_on_otp_ack(nullptr), m_on_price_timeout_set(nullptr), + m_on_new_settings(nullptr), m_num_connection_tries(0) + { m_websocket.onEvent(DataSource::s_callback); } @@ -110,4 +112,5 @@ class DataSource on_new_settings_t m_on_new_settings; WebSocketsClient m_websocket; std::queue m_send_queue; + int m_num_connection_tries; };