add network timeout to restart option

This commit is contained in:
technyon
2022-05-15 20:09:56 +02:00
parent 2875eb3421
commit ec277b1013
3 changed files with 31 additions and 12 deletions

View File

@@ -116,6 +116,13 @@ void Network::initialize()
_device->mqttClient()->setServer(_mqttBrokerAddr, port);
_device->mqttClient()->setCallback(Network::onMqttDataReceivedCallback);
_networkTimeout = _preferences->getInt(preference_network_timeout);
if(_networkTimeout == 0)
{
_networkTimeout = -1;
_preferences->putInt(preference_network_timeout, _networkTimeout);
}
}
bool Network::reconnect()
@@ -165,23 +172,28 @@ void Network::update()
{
long ts = millis();
if((ts - _lastMaintain) > 1000)
{
_device->update();
_device->update();
if(!_device->isConnected())
{
Serial.println(F("Network not connected. Trying reconnect."));
bool success = _device->reconnect();
Serial.println(success ? F("Reconnect successful") : F("Reconnect failed"));
}
if(!_device->isConnected())
{
Serial.println(F("Network not connected. Trying reconnect."));
bool success = _device->reconnect();
Serial.println(success ? F("Reconnect successful") : F("Reconnect failed"));
}
if(!_device->isConnected())
{
if(ts - _lastConnectedTs > _networkTimeout * 1000)
{
Serial.println("Network timeout has been reached, restarting ...");
delay(200);
ESP.restart();
}
return;
}
_lastConnectedTs = ts;
if(!_device->mqttClient()->connected())
{
bool success = reconnect();