add static ip config for wifi device
This commit is contained in:
@@ -10,8 +10,8 @@
|
||||
#include "espMqttClient.h"
|
||||
#include "../RestartReason.h"
|
||||
|
||||
EthLan8720Device::EthLan8720Device(const String& hostname, Preferences* preferences, const std::string& deviceName, uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t ethtype, eth_clock_mode_t clock_mode, bool use_mac_from_efuse)
|
||||
: NetworkDevice(hostname),
|
||||
EthLan8720Device::EthLan8720Device(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration, const std::string& deviceName, uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t ethtype, eth_clock_mode_t clock_mode, bool use_mac_from_efuse)
|
||||
: NetworkDevice(hostname, ipConfiguration),
|
||||
_deviceName(deviceName),
|
||||
_phy_addr(phy_addr),
|
||||
_power(power),
|
||||
|
||||
@@ -13,6 +13,7 @@ class EthLan8720Device : public NetworkDevice
|
||||
public:
|
||||
EthLan8720Device(const String& hostname,
|
||||
Preferences* preferences,
|
||||
const IPConfiguration* ipConfiguration,
|
||||
const std::string& deviceName,
|
||||
uint8_t phy_addr = ETH_PHY_ADDR,
|
||||
int power = ETH_PHY_POWER,
|
||||
|
||||
50
networkDevices/IPConfiguration.cpp
Normal file
50
networkDevices/IPConfiguration.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#include "IPConfiguration.h"
|
||||
#include "../PreferencesKeys.h"
|
||||
#include "../Logger.h"
|
||||
|
||||
IPConfiguration::IPConfiguration(Preferences *preferences, const bool& firstStart)
|
||||
: _preferences(preferences)
|
||||
{
|
||||
if(firstStart)
|
||||
{
|
||||
_preferences->putBool(preference_ip_dhcp_enabled, true);
|
||||
}
|
||||
|
||||
Log->print(F("IP configuration: "));
|
||||
if(dhcpEnabled())
|
||||
{
|
||||
Log->println(F("DHCP"));
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
bool IPConfiguration::dhcpEnabled() const
|
||||
{
|
||||
return _preferences->getBool(preference_ip_dhcp_enabled);
|
||||
}
|
||||
|
||||
String IPConfiguration::ipAddress() const
|
||||
{
|
||||
return _preferences->getString(preference_ip_address);
|
||||
}
|
||||
|
||||
String IPConfiguration::subnet() const
|
||||
{
|
||||
return _preferences->getString(preference_ip_subnet);
|
||||
}
|
||||
|
||||
String IPConfiguration::defaultGateway() const
|
||||
{
|
||||
return _preferences->getString(preference_ip_gateway);
|
||||
}
|
||||
|
||||
String IPConfiguration::dnsServer() const
|
||||
{
|
||||
return _preferences->getString(preference_ip_dns_server);
|
||||
}
|
||||
19
networkDevices/IPConfiguration.h
Normal file
19
networkDevices/IPConfiguration.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <Preferences.h>
|
||||
|
||||
class IPConfiguration
|
||||
{
|
||||
public:
|
||||
explicit IPConfiguration(Preferences* preferences, const bool& firstStart);
|
||||
|
||||
bool dhcpEnabled() const;
|
||||
String ipAddress() const;
|
||||
String subnet() const;
|
||||
String defaultGateway() const;
|
||||
String dnsServer() const;
|
||||
|
||||
private:
|
||||
Preferences* _preferences = nullptr;
|
||||
};
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "MqttClient.h"
|
||||
#include "MqttClientSetup.h"
|
||||
#include "IPConfiguration.h"
|
||||
|
||||
enum class ReconnectStatus
|
||||
{
|
||||
@@ -13,8 +14,9 @@ enum class ReconnectStatus
|
||||
class NetworkDevice
|
||||
{
|
||||
public:
|
||||
explicit NetworkDevice(const String& hostname)
|
||||
: _hostname(hostname)
|
||||
explicit NetworkDevice(const String& hostname, const IPConfiguration* ipConfiguration)
|
||||
: _hostname(hostname),
|
||||
_ipConfiguration(ipConfiguration)
|
||||
{}
|
||||
|
||||
virtual const String deviceName() const = 0;
|
||||
@@ -49,4 +51,5 @@ public:
|
||||
protected:
|
||||
const uint16_t _mqttMaxBufferSize = 6144;
|
||||
const String _hostname;
|
||||
const IPConfiguration* _ipConfiguration = nullptr;
|
||||
};
|
||||
@@ -6,8 +6,8 @@
|
||||
#include "../Logger.h"
|
||||
#include "../MqttTopics.h"
|
||||
|
||||
W5500Device::W5500Device(const String &hostname, Preferences* preferences, int variant)
|
||||
: NetworkDevice(hostname),
|
||||
W5500Device::W5500Device(const String &hostname, Preferences* preferences, const IPConfiguration* ipConfiguration, int variant)
|
||||
: NetworkDevice(hostname, ipConfiguration),
|
||||
_preferences(preferences),
|
||||
_variant((W5500Variant)variant)
|
||||
{
|
||||
|
||||
@@ -15,7 +15,7 @@ enum class W5500Variant
|
||||
class W5500Device : public NetworkDevice
|
||||
{
|
||||
public:
|
||||
explicit W5500Device(const String& hostname, Preferences* _preferences, int variant);
|
||||
explicit W5500Device(const String& hostname, Preferences* _preferences, const IPConfiguration* ipConfiguration, int variant);
|
||||
~W5500Device();
|
||||
|
||||
const String deviceName() const override;
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
|
||||
RTC_NOINIT_ATTR char WiFiDevice_reconfdetect[17];
|
||||
|
||||
WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences)
|
||||
: NetworkDevice(hostname)
|
||||
WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences, const IPConfiguration* ipConfiguration)
|
||||
: NetworkDevice(hostname, ipConfiguration)
|
||||
{
|
||||
_startAp = strcmp(WiFiDevice_reconfdetect, "reconfigure_wifi") == 0;
|
||||
|
||||
@@ -69,6 +69,20 @@ void WifiDevice::initialize()
|
||||
_wm.setMenu(wm_menu);
|
||||
_wm.setHostname(_hostname);
|
||||
|
||||
if(!_ipConfiguration->dhcpEnabled())
|
||||
{
|
||||
IPAddress address;
|
||||
address.fromString(_ipConfiguration->ipAddress());
|
||||
IPAddress gateway;
|
||||
gateway.fromString(_ipConfiguration->defaultGateway());
|
||||
IPAddress subnet;
|
||||
subnet.fromString(_ipConfiguration->subnet());
|
||||
IPAddress dns;
|
||||
dns.fromString(_ipConfiguration->dnsServer());
|
||||
|
||||
_wm.setSTAStaticIPConfig(address, gateway, subnet, dns);
|
||||
}
|
||||
|
||||
_wm.setAPCallback(clearRtcInitVar);
|
||||
|
||||
bool res = false;
|
||||
|
||||
@@ -6,11 +6,12 @@
|
||||
#include "NetworkDevice.h"
|
||||
#include "WiFiManager.h"
|
||||
#include "espMqttClient.h"
|
||||
#include "IPConfiguration.h"
|
||||
|
||||
class WifiDevice : public NetworkDevice
|
||||
{
|
||||
public:
|
||||
WifiDevice(const String& hostname, Preferences* _preferences);
|
||||
WifiDevice(const String& hostname, Preferences* _preferences, const IPConfiguration* ipConfiguration);
|
||||
|
||||
const String deviceName() const override;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user