fix W5500 reconnect
This commit is contained in:
@@ -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