diff --git a/Network.cpp b/Network.cpp index 6d8a86c..5e3a63e 100644 --- a/Network.cpp +++ b/Network.cpp @@ -14,7 +14,7 @@ unsigned long Network::_ignoreSubscriptionsTs = 0; RTC_NOINIT_ATTR char WiFi_fallbackDetect[14]; -Network::Network(Preferences *preferences, const String& maintenancePathPrefix, const bool& firstStart) +Network::Network(Preferences *preferences, const String& maintenancePathPrefix) : _preferences(preferences) { _inst = this; @@ -27,12 +27,12 @@ Network::Network(Preferences *preferences, const String& maintenancePathPrefix, { _maintenancePathPrefix[i] = maintenancePathPrefix.charAt(i); } - setupDevice(firstStart); + setupDevice(); } -void Network::setupDevice(const bool& firstStart) +void Network::setupDevice() { - _ipConfiguration = new IPConfiguration(_preferences, firstStart); + _ipConfiguration = new IPConfiguration(_preferences); int hardwareDetect = _preferences->getInt(preference_network_hardware); int hardwareDetectGpio = _preferences->getInt(preference_network_hardware_gpio); diff --git a/Network.h b/Network.h index ccab0ba..e1894f6 100644 --- a/Network.h +++ b/Network.h @@ -20,7 +20,7 @@ enum class NetworkDeviceType class Network { public: - explicit Network(Preferences* preferences, const String& maintenancePathPrefix, const bool& firstStart); + explicit Network(Preferences* preferences, const String& maintenancePathPrefix); void initialize(); bool update(); @@ -67,7 +67,7 @@ public: private: static void onMqttDataReceivedCallback(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total); void onMqttDataReceived(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total); - void setupDevice(const bool& firstStart); + void setupDevice(); bool reconnect(); void publishHassTopic(const String& mqttDeviceType, diff --git a/main.cpp b/main.cpp index 48529af..19b37eb 100644 --- a/main.cpp +++ b/main.cpp @@ -177,7 +177,7 @@ void setup() openerEnabled = preferences->getBool(preference_opener_enabled); const String mqttLockPath = preferences->getString(preference_mqtt_lock_path); - network = new Network(preferences, mqttLockPath, firstStart); + network = new Network(preferences, mqttLockPath); network->initialize(); networkLock = new NetworkLock(network, preferences); diff --git a/networkDevices/IPConfiguration.cpp b/networkDevices/IPConfiguration.cpp index 4ce384f..70eeb19 100644 --- a/networkDevices/IPConfiguration.cpp +++ b/networkDevices/IPConfiguration.cpp @@ -2,11 +2,12 @@ #include "../PreferencesKeys.h" #include "../Logger.h" -IPConfiguration::IPConfiguration(Preferences *preferences, const bool& firstStart) +IPConfiguration::IPConfiguration(Preferences *preferences) : _preferences(preferences) { - if(firstStart) + if(_preferences->getString(preference_ip_address).length() <= 0) { + Log->println("IP address empty, falling back to DHCP."); _preferences->putBool(preference_ip_dhcp_enabled, true); } @@ -23,9 +24,9 @@ IPConfiguration::IPConfiguration(Preferences *preferences, const bool& firstStar else { Log->print(F("IP address: ")); Log->print(ipAddress()); - Log->print(F("Subnet: ")); Log->print(subnet()); - Log->print(F("Gateway: ")); Log->print(defaultGateway()); - Log->print(F("DNS: ")); Log->println(dnsServer()); + Log->print(F(", Subnet: ")); Log->print(subnet()); + Log->print(F(", Gateway: ")); Log->print(defaultGateway()); + Log->print(F(", DNS: ")); Log->println(dnsServer()); } } diff --git a/networkDevices/IPConfiguration.h b/networkDevices/IPConfiguration.h index ab3b4e7..13329a7 100644 --- a/networkDevices/IPConfiguration.h +++ b/networkDevices/IPConfiguration.h @@ -5,7 +5,7 @@ class IPConfiguration { public: - explicit IPConfiguration(Preferences* preferences, const bool& firstStart); + explicit IPConfiguration(Preferences* preferences); bool dhcpEnabled() const; const IPAddress ipAddress() const; diff --git a/networkDevices/W5500Device.cpp b/networkDevices/W5500Device.cpp index e343f9c..6d4e517 100644 --- a/networkDevices/W5500Device.cpp +++ b/networkDevices/W5500Device.cpp @@ -117,8 +117,19 @@ ReconnectStatus W5500Device::reconnect() { _hasDHCPAddress = true; dhcpRetryCnt = 1000; - Log->print(F(" DHCP assigned IP ")); - Log->println(Ethernet.localIP()); + if(_ipConfiguration->dhcpEnabled()) + { + Log->print(F(" DHCP assigned IP ")); + Log->println(Ethernet.localIP()); + } + } + + if(!_ipConfiguration->dhcpEnabled()) + { + Ethernet.setLocalIP(_ipConfiguration->ipAddress()); + Ethernet.setSubnetMask(_ipConfiguration->subnet()); + Ethernet.setGatewayIP(_ipConfiguration->defaultGateway()); + Ethernet.setDnsServerIP(_ipConfiguration->dnsServer()); } }