implement fallback to wifi if W5500 hardware not present
This commit is contained in:
31
Network.cpp
31
Network.cpp
@@ -8,6 +8,8 @@
|
|||||||
|
|
||||||
Network* Network::_inst = nullptr;
|
Network* Network::_inst = nullptr;
|
||||||
|
|
||||||
|
RTC_NOINIT_ATTR char WiFi_fallbackDetect[14];
|
||||||
|
|
||||||
Network::Network(const NetworkDeviceType networkDevice, Preferences *preferences, const String& maintenancePathPrefix)
|
Network::Network(const NetworkDeviceType networkDevice, Preferences *preferences, const String& maintenancePathPrefix)
|
||||||
: _preferences(preferences)
|
: _preferences(preferences)
|
||||||
{
|
{
|
||||||
@@ -25,8 +27,14 @@ Network::Network(const NetworkDeviceType networkDevice, Preferences *preferences
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Network::setupDevice(const NetworkDeviceType hardware)
|
void Network::setupDevice(NetworkDeviceType hardware)
|
||||||
{
|
{
|
||||||
|
if(strcmp(WiFi_fallbackDetect, "wifi_fallback") == 0)
|
||||||
|
{
|
||||||
|
Log->println(F("Switching to WiFi device as fallabck."));
|
||||||
|
hardware = NetworkDeviceType::WiFi;
|
||||||
|
}
|
||||||
|
|
||||||
switch(hardware)
|
switch(hardware)
|
||||||
{
|
{
|
||||||
case NetworkDeviceType::W5500:
|
case NetworkDeviceType::W5500:
|
||||||
@@ -119,8 +127,25 @@ int Network::update()
|
|||||||
}
|
}
|
||||||
|
|
||||||
Log->println(F("Network not connected. Trying reconnect."));
|
Log->println(F("Network not connected. Trying reconnect."));
|
||||||
bool success = _device->reconnect();
|
ReconnectStatus reconnectStatus = _device->reconnect();
|
||||||
Log->println(success ? F("Reconnect successful") : F("Reconnect failed"));
|
|
||||||
|
switch(reconnectStatus)
|
||||||
|
{
|
||||||
|
case ReconnectStatus::CriticalFailure:
|
||||||
|
strcpy(WiFi_fallbackDetect, "wifi_fallback");
|
||||||
|
Log->println("Network device has a critical failure, enable fallback to Wifi and reboot.");
|
||||||
|
delay(200);
|
||||||
|
ESP.restart();
|
||||||
|
break;
|
||||||
|
case ReconnectStatus::Success:
|
||||||
|
memset(WiFi_fallbackDetect, 0, sizeof(WiFi_fallbackDetect));
|
||||||
|
Log->println(F("Reconnect successful"));
|
||||||
|
break;
|
||||||
|
case ReconnectStatus::Failure:
|
||||||
|
Log->println(F("Reconnect failed"));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!_device->isConnected())
|
if(!_device->isConnected())
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
static void onMqttDataReceivedCallback(char* topic, byte* payload, unsigned int length);
|
static void onMqttDataReceivedCallback(char* topic, byte* payload, unsigned int length);
|
||||||
void onMqttDataReceived(char*& topic, byte*& payload, unsigned int& length);
|
void onMqttDataReceived(char*& topic, byte*& payload, unsigned int& length);
|
||||||
void setupDevice(const NetworkDeviceType hardware);
|
void setupDevice(NetworkDeviceType hardware);
|
||||||
bool reconnect();
|
bool reconnect();
|
||||||
|
|
||||||
void buildMqttPath(const char* prefix, const char* path, char* outPath);
|
void buildMqttPath(const char* prefix, const char* path, char* outPath);
|
||||||
|
|||||||
@@ -332,7 +332,7 @@ void setup() {
|
|||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
// start the Ethernet connection:
|
// start the Ethernet connection:
|
||||||
if (Ethernet.begin(mac) == 0) {
|
if (Ethernet.begin(mac) == 0) {
|
||||||
Serial.println("Failed to configure Ethernet using DHCP");
|
Serial.println("Failure to configure Ethernet using DHCP");
|
||||||
// no point in carrying on, so do nothing forevermore:
|
// no point in carrying on, so do nothing forevermore:
|
||||||
for(;;)
|
for(;;)
|
||||||
;
|
;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ All notable changes to this project will be documented in this file.
|
|||||||
### Fixed
|
### Fixed
|
||||||
- Compile warning removed for esp32c3
|
- Compile warning removed for esp32c3
|
||||||
- NimBLEDevice::getPower incorrect value when power level is -3db.
|
- NimBLEDevice::getPower incorrect value when power level is -3db.
|
||||||
- Failed pairing when already in progress.
|
- Failure pairing when already in progress.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- Revert previous change that forced writing with response when subscribing in favor of allowing the application to decide.
|
- Revert previous change that forced writing with response when subscribing in favor of allowing the application to decide.
|
||||||
|
|||||||
@@ -2,6 +2,13 @@
|
|||||||
|
|
||||||
#include "PubSubClient.h"
|
#include "PubSubClient.h"
|
||||||
|
|
||||||
|
enum class ReconnectStatus
|
||||||
|
{
|
||||||
|
Failure = 0,
|
||||||
|
Success = 1,
|
||||||
|
CriticalFailure = 2
|
||||||
|
};
|
||||||
|
|
||||||
class NetworkDevice
|
class NetworkDevice
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -12,7 +19,7 @@ public:
|
|||||||
virtual PubSubClient* mqttClient() = 0;
|
virtual PubSubClient* mqttClient() = 0;
|
||||||
|
|
||||||
virtual void initialize() = 0;
|
virtual void initialize() = 0;
|
||||||
virtual bool reconnect() = 0;
|
virtual ReconnectStatus reconnect() = 0;
|
||||||
virtual void reconfigure() = 0;
|
virtual void reconfigure() = 0;
|
||||||
virtual void printError() = 0;
|
virtual void printError() = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -57,8 +57,7 @@ void W5500Device::initialize()
|
|||||||
reconnect();
|
reconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ReconnectStatus W5500Device::reconnect()
|
||||||
bool W5500Device::reconnect()
|
|
||||||
{
|
{
|
||||||
_hasDHCPAddress = false;
|
_hasDHCPAddress = false;
|
||||||
|
|
||||||
@@ -66,6 +65,7 @@ bool W5500Device::reconnect()
|
|||||||
Log->println(F("Initialize Ethernet with DHCP:"));
|
Log->println(F("Initialize Ethernet with DHCP:"));
|
||||||
|
|
||||||
int dhcpRetryCnt = 0;
|
int dhcpRetryCnt = 0;
|
||||||
|
bool hardwareFound = false;
|
||||||
|
|
||||||
while(dhcpRetryCnt < 3)
|
while(dhcpRetryCnt < 3)
|
||||||
{
|
{
|
||||||
@@ -81,12 +81,15 @@ bool W5500Device::reconnect()
|
|||||||
if (Ethernet.hardwareStatus() == EthernetNoHardware)
|
if (Ethernet.hardwareStatus() == EthernetNoHardware)
|
||||||
{
|
{
|
||||||
Log->println(F("Ethernet module not found"));
|
Log->println(F("Ethernet module not found"));
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
if (Ethernet.linkStatus() == LinkOFF)
|
if (Ethernet.linkStatus() == LinkOFF)
|
||||||
{
|
{
|
||||||
Log->println(F("Ethernet cable is not connected."));
|
Log->println(F("Ethernet cable is not connected."));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hardwareFound = true;
|
||||||
|
|
||||||
IPAddress ip;
|
IPAddress ip;
|
||||||
ip.fromString("192.168.4.1");
|
ip.fromString("192.168.4.1");
|
||||||
|
|
||||||
@@ -108,7 +111,12 @@ bool W5500Device::reconnect()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return _hasDHCPAddress;
|
if(!hardwareFound)
|
||||||
|
{
|
||||||
|
return ReconnectStatus::CriticalFailure;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _hasDHCPAddress ? ReconnectStatus::Success : ReconnectStatus::Failure;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ public:
|
|||||||
~W5500Device();
|
~W5500Device();
|
||||||
|
|
||||||
virtual void initialize();
|
virtual void initialize();
|
||||||
virtual bool reconnect();
|
virtual ReconnectStatus reconnect();
|
||||||
virtual void reconfigure();
|
virtual void reconfigure();
|
||||||
virtual void printError();
|
virtual void printError();
|
||||||
|
|
||||||
|
|||||||
@@ -126,10 +126,10 @@ bool WifiDevice::isConnected()
|
|||||||
return WiFi.isConnected();
|
return WiFi.isConnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WifiDevice::reconnect()
|
ReconnectStatus WifiDevice::reconnect()
|
||||||
{
|
{
|
||||||
delay(3000);
|
delay(3000);
|
||||||
return isConnected();
|
return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WifiDevice::update()
|
void WifiDevice::update()
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public:
|
|||||||
|
|
||||||
virtual void initialize();
|
virtual void initialize();
|
||||||
virtual void reconfigure();
|
virtual void reconfigure();
|
||||||
virtual bool reconnect();
|
virtual ReconnectStatus reconnect();
|
||||||
virtual void printError();
|
virtual void printError();
|
||||||
|
|
||||||
virtual void update();
|
virtual void update();
|
||||||
|
|||||||
Reference in New Issue
Block a user