diff --git a/Network.cpp b/Network.cpp index e67500a..4a2b2d9 100644 --- a/Network.cpp +++ b/Network.cpp @@ -10,7 +10,7 @@ Network* Network::_inst = nullptr; RTC_NOINIT_ATTR char WiFi_fallbackDetect[14]; -Network::Network(const NetworkDeviceType networkDevice, Preferences *preferences, const String& maintenancePathPrefix) +Network::Network(Preferences *preferences, const String& maintenancePathPrefix) : _preferences(preferences) { _inst = this; @@ -23,19 +23,44 @@ Network::Network(const NetworkDeviceType networkDevice, Preferences *preferences { _maintenancePathPrefix[i] = maintenancePathPrefix.charAt(i); } - setupDevice(networkDevice); + setupDevice(); } - -void Network::setupDevice(NetworkDeviceType hardware) +void Network::setupDevice() { + if(strcmp(WiFi_fallbackDetect, "wifi_fallback") == 0) { - Log->println(F("Switching to WiFi device as fallabck.")); - hardware = NetworkDeviceType::WiFi; + Log->println(F("Switching to WiFi device as fallback.")); + _networkDeviceType = NetworkDeviceType::WiFi; + } + else + { + int hardwareDetect = _preferences->getInt(preference_network_hardware_detect); + + if(hardwareDetect == 0) + { + hardwareDetect = 26; + _preferences->putInt(preference_network_hardware_detect, hardwareDetect); + } + + if(hardwareDetect == -1) + { + Log->println(F("W5500 hardware is disable, using Wifi.")); + _networkDeviceType = NetworkDeviceType::WiFi; + } + else + { + Log->print(F("Using PIN ")); + Log->print(hardwareDetect); + Log->println(F(" for network device selection")); + + pinMode(hardwareDetect, INPUT_PULLUP); + _networkDeviceType = digitalRead(hardwareDetect) == HIGH ? NetworkDeviceType::WiFi : NetworkDeviceType::W5500; + } } - switch(hardware) + switch(_networkDeviceType) { case NetworkDeviceType::W5500: Log->println(F("Network device: W5500")); @@ -775,3 +800,8 @@ void Network::publishPresenceDetection(char *csv) { _presenceCsv = csv; } + +const NetworkDeviceType Network::networkDeviceType() +{ + return _networkDeviceType; +} diff --git a/Network.h b/Network.h index bcbb2e7..9ecc6f8 100644 --- a/Network.h +++ b/Network.h @@ -15,7 +15,7 @@ enum class NetworkDeviceType class Network { public: - explicit Network(const NetworkDeviceType networkDevice, Preferences* preferences, const String& maintenancePathPrefix); + explicit Network(Preferences* preferences, const String& maintenancePathPrefix); void initialize(); int update(); @@ -46,10 +46,12 @@ public: PubSubClient* mqttClient(); bool isMqttConnected(); + const NetworkDeviceType networkDeviceType(); + private: static void onMqttDataReceivedCallback(char* topic, byte* payload, unsigned int length); void onMqttDataReceived(char*& topic, byte*& payload, unsigned int& length); - void setupDevice(NetworkDeviceType hardware); + void setupDevice(); bool reconnect(); void buildMqttPath(const char* prefix, const char* path, char* outPath); @@ -78,5 +80,7 @@ private: unsigned long _lastMaintenanceTs = 0; unsigned long _lastRssiTs = 0; + NetworkDeviceType _networkDeviceType = (NetworkDeviceType)-1; + int8_t _lastRssi = 127; }; diff --git a/Pins.h b/Pins.h index bfe2ec3..59a1d26 100644 --- a/Pins.h +++ b/Pins.h @@ -1,6 +1,5 @@ #pragma once -#define NETWORK_SELECT 26 #define ETHERNET_CS_PIN 5 #define ETHERNET_RESET_PIN 33 #define TRIGGER_LOCK_PIN 32 diff --git a/PreferencesKeys.h b/PreferencesKeys.h index 968d07c..c45f3ef 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -16,6 +16,7 @@ #define preference_mqtt_crt "mqttcrt" #define preference_mqtt_key "mqttkey" #define preference_mqtt_hass_discovery "hassdiscovery" +#define preference_network_hardware_detect "nwhwdt" #define preference_hostname "hostname" #define preference_network_timeout "nettmout" #define preference_restart_on_disconnect "restdisc" diff --git a/Version.h b/Version.h index de5d198..e679cb7 100644 --- a/Version.h +++ b/Version.h @@ -1,3 +1,3 @@ #pragma once -#define nuki_hub_version "6.7" \ No newline at end of file +#define nuki_hub_version "6.8" \ No newline at end of file diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index e46d292..144d48c 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -240,6 +240,11 @@ bool WebCfgServer::processArgs(String& message) _preferences->putString(preference_mqtt_key, value); configChanged = true; } + else if(key == "NWHWDT") + { + _preferences->putInt(preference_network_hardware_detect, value.toInt()); + configChanged = true; + } else if(key == "HASSDISCOVERY") { if(_preferences->getString(preference_mqtt_hass_discovery) != value) @@ -588,6 +593,7 @@ void WebCfgServer::buildMqttConfigHtml(String &response) printTextarea(response, "MQTTCA", "MQTT SSL CA Certificate (*, optional)", _preferences->getString(preference_mqtt_ca).c_str(), TLS_CA_MAX_SIZE); printTextarea(response, "MQTTCRT", "MQTT SSL Client Certificate (*, optional)", _preferences->getString(preference_mqtt_crt).c_str(), TLS_CERT_MAX_SIZE); printTextarea(response, "MQTTKEY", "MQTT SSL Client Key (*, optional)", _preferences->getString(preference_mqtt_key).c_str(), TLS_KEY_MAX_SIZE); + printDropDown(response, "NWHWDT", "Network hardward detection", String(_preferences->getInt(preference_network_hardware_detect)), getNetworkDetectionOptions()); printInputField(response, "NETTIMEOUT", "Network Timeout until restart (seconds; -1 to disable)", _preferences->getInt(preference_network_timeout), 5); printCheckBox(response, "RSTDISC", "Restart on disconnect", _preferences->getBool(preference_restart_on_disconnect)); printInputField(response, "RSTTMR", "Restart timer (minutes; -1 to disable)", _preferences->getInt(preference_restart_timer), 10); @@ -793,6 +799,36 @@ void WebCfgServer::printTextarea(String& response, response.concat(""); } +void WebCfgServer::printDropDown(String &response, const char *token, const char *description, const String preselectedValue, const std::vector> options) +{ + response.concat(""); + response.concat(description); + response.concat(""); + + response.concat(""); + response.concat(""); +} + void WebCfgServer::buildNavigationButton(String &response, const char *caption, const char *targetPath) { response.concat("
> WebCfgServer::getNetworkDetectionOptions() const +{ + std::vector> options; + + options.push_back(std::make_pair("-1", "Disable W5500")); + + for(int i=16; i <= 33; i++) + { + String txt = "Detect W5500 via GPIO "; + txt.concat(i); + options.push_back(std::make_pair(String(i), txt)); + } + + return options; +} diff --git a/WebCfgServer.h b/WebCfgServer.h index f33f01e..59bb597 100644 --- a/WebCfgServer.h +++ b/WebCfgServer.h @@ -47,8 +47,11 @@ private: void printInputField(String& response, const char* token, const char* description, const int value, size_t maxLength); void printCheckBox(String& response, const char* token, const char* description, const bool value); void printTextarea(String& response, const char *token, const char *description, const char *value, const size_t maxLength); + void printDropDown(String &response, const char *token, const char *description, const String preselectedValue, std::vector> options); void buildNavigationButton(String& response, const char* caption, const char* targetPath); + const std::vector> getNetworkDetectionOptions() const; + void printParameter(String& response, const char* description, const char* value); String generateConfirmCode(); diff --git a/main.cpp b/main.cpp index 1bc97bb..05586df 100644 --- a/main.cpp +++ b/main.cpp @@ -159,7 +159,6 @@ void initPreferences() void setup() { - pinMode(NETWORK_SELECT, INPUT_PULLUP); Serial.begin(115200); Log = &Serial; @@ -171,11 +170,8 @@ void setup() restartTs = preferences->getInt(preference_restart_timer) * 60 * 1000; } -// const NetworkDeviceType networkDevice = NetworkDeviceType::WiFi; - const NetworkDeviceType networkDevice = digitalRead(NETWORK_SELECT) == HIGH ? NetworkDeviceType::WiFi : NetworkDeviceType::W5500; - const String mqttLockPath = preferences->getString(preference_mqtt_lock_path); - network = new Network(networkDevice, preferences, mqttLockPath); + network = new Network(preferences, mqttLockPath); network->initialize(); networkLock = new NetworkLock(network, preferences); networkLock->initialize(); @@ -189,7 +185,7 @@ void setup() preferences->putUInt(preference_deviceId, deviceId); } - initEthServer(networkDevice); + initEthServer(network->networkDeviceType()); bleScanner = new BleScanner::Scanner(); bleScanner->initialize("NukiHub"); @@ -216,7 +212,7 @@ void setup() nukiOpener->initialize(); } - webCfgServer = new WebCfgServer(nuki, nukiOpener, network, ethServer, preferences, networkDevice == NetworkDeviceType::WiFi); + webCfgServer = new WebCfgServer(nuki, nukiOpener, network, ethServer, preferences, network->networkDeviceType() == NetworkDeviceType::WiFi); webCfgServer->initialize(); presenceDetection = new PresenceDetection(preferences, bleScanner, network); diff --git a/webflash/nuki_hub.bin b/webflash/nuki_hub.bin index 1fdf41b..0769032 100644 Binary files a/webflash/nuki_hub.bin and b/webflash/nuki_hub.bin differ