diff --git a/Network.cpp b/Network.cpp index c72dbe6..e67500a 100644 --- a/Network.cpp +++ b/Network.cpp @@ -8,6 +8,8 @@ Network* Network::_inst = nullptr; +RTC_NOINIT_ATTR char WiFi_fallbackDetect[14]; + Network::Network(const NetworkDeviceType networkDevice, Preferences *preferences, const String& maintenancePathPrefix) : _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) { case NetworkDeviceType::W5500: @@ -119,8 +127,25 @@ int Network::update() } Log->println(F("Network not connected. Trying reconnect.")); - bool success = _device->reconnect(); - Log->println(success ? F("Reconnect successful") : F("Reconnect failed")); + ReconnectStatus reconnectStatus = _device->reconnect(); + + 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()) diff --git a/Network.h b/Network.h index e6524bc..bcbb2e7 100644 --- a/Network.h +++ b/Network.h @@ -49,7 +49,7 @@ public: private: static void onMqttDataReceivedCallback(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(); void buildMqttPath(const char* prefix, const char* path, char* outPath); diff --git a/lib/Ethernet/docs/api.md b/lib/Ethernet/docs/api.md index 76265c8..2354a59 100644 --- a/lib/Ethernet/docs/api.md +++ b/lib/Ethernet/docs/api.md @@ -332,7 +332,7 @@ void setup() { Serial.begin(9600); // start the Ethernet connection: 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: for(;;) ; diff --git a/lib/NimBLE-Arduino/CHANGELOG.md b/lib/NimBLE-Arduino/CHANGELOG.md index 43fbc62..7e12ae5 100644 --- a/lib/NimBLE-Arduino/CHANGELOG.md +++ b/lib/NimBLE-Arduino/CHANGELOG.md @@ -7,7 +7,7 @@ All notable changes to this project will be documented in this file. ### Fixed - Compile warning removed for esp32c3 - NimBLEDevice::getPower incorrect value when power level is -3db. - - Failed pairing when already in progress. + - Failure pairing when already in progress. ### Changed - Revert previous change that forced writing with response when subscribing in favor of allowing the application to decide. diff --git a/networkDevices/NetworkDevice.h b/networkDevices/NetworkDevice.h index 0aa0dcb..560ad4a 100644 --- a/networkDevices/NetworkDevice.h +++ b/networkDevices/NetworkDevice.h @@ -2,6 +2,13 @@ #include "PubSubClient.h" +enum class ReconnectStatus +{ + Failure = 0, + Success = 1, + CriticalFailure = 2 +}; + class NetworkDevice { public: @@ -12,7 +19,7 @@ public: virtual PubSubClient* mqttClient() = 0; virtual void initialize() = 0; - virtual bool reconnect() = 0; + virtual ReconnectStatus reconnect() = 0; virtual void reconfigure() = 0; virtual void printError() = 0; diff --git a/networkDevices/W5500Device.cpp b/networkDevices/W5500Device.cpp index f4e0dc4..5ce2361 100644 --- a/networkDevices/W5500Device.cpp +++ b/networkDevices/W5500Device.cpp @@ -57,8 +57,7 @@ void W5500Device::initialize() reconnect(); } - -bool W5500Device::reconnect() +ReconnectStatus W5500Device::reconnect() { _hasDHCPAddress = false; @@ -66,6 +65,7 @@ bool W5500Device::reconnect() Log->println(F("Initialize Ethernet with DHCP:")); int dhcpRetryCnt = 0; + bool hardwareFound = false; while(dhcpRetryCnt < 3) { @@ -81,12 +81,15 @@ bool W5500Device::reconnect() if (Ethernet.hardwareStatus() == EthernetNoHardware) { Log->println(F("Ethernet module not found")); + continue; } if (Ethernet.linkStatus() == LinkOFF) { Log->println(F("Ethernet cable is not connected.")); } + hardwareFound = true; + IPAddress ip; 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; } diff --git a/networkDevices/W5500Device.h b/networkDevices/W5500Device.h index 2eb9c53..05662ac 100644 --- a/networkDevices/W5500Device.h +++ b/networkDevices/W5500Device.h @@ -11,7 +11,7 @@ public: ~W5500Device(); virtual void initialize(); - virtual bool reconnect(); + virtual ReconnectStatus reconnect(); virtual void reconfigure(); virtual void printError(); diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index 9d57ab9..3b49bb4 100644 --- a/networkDevices/WifiDevice.cpp +++ b/networkDevices/WifiDevice.cpp @@ -126,10 +126,10 @@ bool WifiDevice::isConnected() return WiFi.isConnected(); } -bool WifiDevice::reconnect() +ReconnectStatus WifiDevice::reconnect() { delay(3000); - return isConnected(); + return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure; } void WifiDevice::update() diff --git a/networkDevices/WifiDevice.h b/networkDevices/WifiDevice.h index 72e72bd..ab9caec 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -13,7 +13,7 @@ public: virtual void initialize(); virtual void reconfigure(); - virtual bool reconnect(); + virtual ReconnectStatus reconnect(); virtual void printError(); virtual void update();