fix W5500 reconnect
This commit is contained in:
25
Network.cpp
25
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())
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#define nuki_hub_version "1.11"
|
||||
#define nuki_hub_version "1.12"
|
||||
@@ -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:
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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];
|
||||
};
|
||||
@@ -60,3 +60,13 @@ bool WifiDevice::isConnected()
|
||||
{
|
||||
return WiFi.isConnected();
|
||||
}
|
||||
|
||||
bool WifiDevice::reconnect()
|
||||
{
|
||||
return isConnected();
|
||||
}
|
||||
|
||||
void WifiDevice::update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -11,6 +11,9 @@ public:
|
||||
|
||||
virtual void initialize();
|
||||
virtual void reconfigure();
|
||||
virtual bool reconnect();
|
||||
|
||||
virtual void update();
|
||||
|
||||
virtual bool isConnected();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user