From 490bff20c55ffb56c520de1e71b12548fac6cfe8 Mon Sep 17 00:00:00 2001 From: technyon Date: Sat, 30 Apr 2022 19:59:07 +0200 Subject: [PATCH] fix W5500 reconnect --- Network.cpp | 25 ++++++++++++++++---- Network.h | 2 ++ Version.h | 2 +- networkDevices/NetworkDevice.h | 3 +++ networkDevices/W5500Device.cpp | 43 ++++++++++++++++++++++++++++------ networkDevices/W5500Device.h | 9 +++++++ networkDevices/WifiDevice.cpp | 10 ++++++++ networkDevices/WifiDevice.h | 3 +++ 8 files changed, 84 insertions(+), 13 deletions(-) diff --git a/Network.cpp b/Network.cpp index 59c2f7b..e94ee00 100644 --- a/Network.cpp +++ b/Network.cpp @@ -121,6 +121,8 @@ void Network::initialize() bool Network::reconnect() { + _mqttConnected = false; + while (!_device->mqttClient()->connected() && millis() > _nextReconnect) { Serial.println(F("Attempting MQTT connection")); @@ -137,11 +139,11 @@ bool Network::reconnect() success = _device->mqttClient()->connect(_preferences->getString(preference_hostname).c_str(), _mqttUser, _mqttPass); } - - if (success) { + if (success) + { Serial.println(F("MQTT connected")); _mqttConnected = true; - delay(200); + delay(100); subscribe(mqtt_topic_lock_action); for(auto topic : _configTopics) @@ -162,10 +164,23 @@ bool Network::reconnect() void Network::update() { + long ts = millis(); + + if((ts - _lastMaintain) > 1000) + { + _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")); - vTaskDelay( 1000 / portTICK_PERIOD_MS); + return; } if(!_device->mqttClient()->connected()) diff --git a/Network.h b/Network.h index e049e3e..b50ae11 100644 --- a/Network.h +++ b/Network.h @@ -74,6 +74,8 @@ private: bool _firstTunerStatePublish = true; + long _lastMaintain = 0; + void (*_lockActionReceivedCallback)(const char* value) = nullptr; void (*_configUpdateReceivedCallback)(const char* path, const char* value) = nullptr; }; diff --git a/Version.h b/Version.h index 3884dad..c3bdf64 100644 --- a/Version.h +++ b/Version.h @@ -1,3 +1,3 @@ #pragma once -#define nuki_hub_version "1.11" \ No newline at end of file +#define nuki_hub_version "1.12" \ No newline at end of file diff --git a/networkDevices/NetworkDevice.h b/networkDevices/NetworkDevice.h index 85d1a84..09e064a 100644 --- a/networkDevices/NetworkDevice.h +++ b/networkDevices/NetworkDevice.h @@ -12,8 +12,11 @@ public: virtual PubSubClient* mqttClient() = 0; virtual void initialize() = 0; + virtual bool reconnect() = 0; virtual void reconfigure() = 0; + virtual void update() = 0; + virtual bool isConnected() = 0; protected: diff --git a/networkDevices/W5500Device.cpp b/networkDevices/W5500Device.cpp index a70f4d7..de32ff7 100644 --- a/networkDevices/W5500Device.cpp +++ b/networkDevices/W5500Device.cpp @@ -35,6 +35,16 @@ void W5500Device::initialize() _ethClient = new EthernetClient(); _mqttClient = new PubSubClient(*_ethClient); + reconnect(); + + _fromTask = true; +} + + +bool W5500Device::reconnect() +{ + _hasDHCPAddress = false; + // start the Ethernet connection: Serial.println(F("Initialize Ethernet with DHCP:")); @@ -54,8 +64,6 @@ void W5500Device::initialize() if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println(F("Ethernet module not found")); - delay(10000); - ESP.restart(); } if (Ethernet.linkStatus() == LinkOFF) { @@ -72,17 +80,21 @@ void W5500Device::initialize() Ethernet.begin(_mac, ip); Ethernet.setSubnetMask(subnet); - delay(2000); + nwDelay(1000); } else { + _hasDHCPAddress = true; dhcpRetryCnt = 1000; Serial.print(F(" DHCP assigned IP ")); Serial.println(Ethernet.localIP()); } } + + return _hasDHCPAddress; } + void W5500Device::reconfigure() { Serial.println(F("Reconfigure W5500 not implemented.")); @@ -93,11 +105,11 @@ void W5500Device::resetDevice() Serial.println(F("Resetting network hardware.")); pinMode(ETHERNET_RESET_PIN, OUTPUT); digitalWrite(ETHERNET_RESET_PIN, HIGH); - delay(250); + nwDelay(250); digitalWrite(ETHERNET_RESET_PIN, LOW); - delay(50); + nwDelay(50); digitalWrite(ETHERNET_RESET_PIN, HIGH); - delay(1500); + nwDelay(1500); } PubSubClient *W5500Device::mqttClient() @@ -107,7 +119,7 @@ PubSubClient *W5500Device::mqttClient() bool W5500Device::isConnected() { - return _ethClient->connected(); + return Ethernet.linkStatus() == EthernetLinkStatus::LinkON && _maintainResult == 0 && _hasDHCPAddress; } void W5500Device::initializeMacAddress(byte *mac) @@ -136,3 +148,20 @@ void W5500Device::initializeMacAddress(byte *mac) _preferences->putBool(preference_has_mac_saved, true); } } + +void W5500Device::nwDelay(unsigned long ms) +{ + if(_fromTask) + { + vTaskDelay( ms / portTICK_PERIOD_MS); + } + else + { + delay(ms); + } +} + +void W5500Device::update() +{ + _maintainResult = Ethernet.maintain(); +} diff --git a/networkDevices/W5500Device.h b/networkDevices/W5500Device.h index 2ff1f0d..6cd1bc5 100644 --- a/networkDevices/W5500Device.h +++ b/networkDevices/W5500Device.h @@ -11,8 +11,11 @@ public: ~W5500Device(); virtual void initialize(); + virtual bool reconnect(); virtual void reconfigure(); + virtual void update(); + virtual bool isConnected(); virtual PubSubClient *mqttClient(); @@ -20,10 +23,16 @@ public: private: void resetDevice(); void initializeMacAddress(byte* mac); + void nwDelay(unsigned long ms); + + bool _fromTask = false; EthernetClient* _ethClient = nullptr; PubSubClient* _mqttClient = nullptr; Preferences* _preferences = nullptr; + int _maintainResult = 0; + bool _hasDHCPAddress = false; + byte _mac[6]; }; \ No newline at end of file diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index 42e5ac0..0dc14ef 100644 --- a/networkDevices/WifiDevice.cpp +++ b/networkDevices/WifiDevice.cpp @@ -60,3 +60,13 @@ bool WifiDevice::isConnected() { return WiFi.isConnected(); } + +bool WifiDevice::reconnect() +{ + return isConnected(); +} + +void WifiDevice::update() +{ + +} diff --git a/networkDevices/WifiDevice.h b/networkDevices/WifiDevice.h index bf6e773..4680210 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -11,6 +11,9 @@ public: virtual void initialize(); virtual void reconfigure(); + virtual bool reconnect(); + + virtual void update(); virtual bool isConnected();