Improved ethernet connection

This commit is contained in:
Gunnar Skjold
2023-12-10 16:56:13 +01:00
parent ee462ec468
commit 0f22fd561e
11 changed files with 112 additions and 38 deletions

View File

@@ -26,6 +26,10 @@ public:
virtual bool isConnected();
virtual bool isConfigChanged();
virtual void getCurrentConfig(NetworkConfig& networkConfig);
virtual IPAddress getIP();
virtual IPAddress getSubnetMask();
virtual IPAddress getGateway();
virtual IPAddress getDns(uint8_t idx);
#if defined(ESP32)
virtual void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
#endif

View File

@@ -22,6 +22,10 @@ public:
bool isConnected();
bool isConfigChanged();
void getCurrentConfig(NetworkConfig& networkConfig);
IPAddress getIP();
IPAddress getSubnetMask();
IPAddress getGateway();
IPAddress getDns(uint8_t idx);
#if defined(ESP32)
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);
@@ -35,6 +39,10 @@ private:
bool configChanged = false;
unsigned long timeout = CONNECTION_TIMEOUT;
unsigned long lastRetry = 0;
int8_t ethPowerPin = -1;
uint8_t ethEnablePin = 0;
};
#endif

View File

@@ -21,6 +21,10 @@ public:
bool isConnected();
bool isConfigChanged();
void getCurrentConfig(NetworkConfig& networkConfig);
IPAddress getIP();
IPAddress getSubnetMask();
IPAddress getGateway();
IPAddress getDns(uint8_t idx);
#if defined(ESP32)
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);

View File

@@ -23,6 +23,10 @@ public:
bool isConnected();
bool isConfigChanged();
void getCurrentConfig(NetworkConfig& networkConfig);
IPAddress getIP();
IPAddress getSubnetMask();
IPAddress getGateway();
IPAddress getDns(uint8_t idx);
#if defined(ESP32)
void eventHandler(WiFiEvent_t event, WiFiEventInfo_t info);

View File

@@ -27,8 +27,6 @@ bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys)
eth_phy_type_t ethType = ETH_PHY_LAN8720;
eth_clock_mode_t ethClkMode = ETH_CLOCK_GPIO0_IN;
uint8_t ethAddr = 0;
int8_t ethPowerPin = -1;
uint8_t ethEnablePin = 0;
uint8_t ethMdc = 0;
uint8_t ethMdio = 0;
@@ -71,33 +69,33 @@ bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys)
if (debugger->isActive(RemoteDebug::INFO)) debugger->printf_P(PSTR("Connecting to Ethernet\n"));
#if defined(ESP32)
if(strlen(config.hostname) > 0) {
ETH.setHostname(config.hostname);
}
#endif
if(ETH.begin(ethAddr, ethPowerPin, ethMdc, ethMdio, ethType, ethClkMode)) {
#if defined(ESP32)
if(strlen(config.hostname) > 0) {
ETH.setHostname(config.hostname);
}
#endif
if(strlen(config.ip) > 0) {
IPAddress ip, gw, sn(255,255,255,0), dns1, dns2;
ip.fromString(config.ip);
gw.fromString(config.gateway);
sn.fromString(config.subnet);
if(strlen(config.dns1) > 0) {
dns1.fromString(config.dns1);
} else if(strlen(config.gateway) > 0) {
dns1.fromString(config.gateway); // If no DNS, set gateway by default
}
if(strlen(config.dns2) > 0) {
dns2.fromString(config.dns2);
} else if(dns1.toString().isEmpty()) {
dns2.fromString(F("208.67.220.220")); // Add OpenDNS as second by default if nothing is configured
}
if(!ETH.config(ip, gw, sn, dns1, dns2)) {
debugger->printf_P(PSTR("Static IP configuration is invalid, not using\n"));
}
}
if (!ETH.begin(ethAddr, ethPowerPin, ethMdc, ethMdio, ethType, ethClkMode)) {
if(strlen(config.ip) > 0) {
IPAddress ip, gw, sn(255,255,255,0), dns1, dns2;
ip.fromString(config.ip);
gw.fromString(config.gateway);
sn.fromString(config.subnet);
if(strlen(config.dns1) > 0) {
dns1.fromString(config.dns1);
} else if(strlen(config.gateway) > 0) {
dns1.fromString(config.gateway); // If no DNS, set gateway by default
}
if(strlen(config.dns2) > 0) {
dns2.fromString(config.dns2);
} else if(dns1.toString().isEmpty()) {
dns2.fromString(F("208.67.220.220")); // Add OpenDNS as second by default if nothing is configured
}
if(!ETH.config(ip, gw, sn, dns1, dns2)) {
debugger->printf_P(PSTR("Static IP configuration is invalid, not using\n"));
}
}
} else {
if (debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Unable to start Ethernet\n"));
}
}
@@ -106,7 +104,7 @@ bool EthernetConnectionHandler::connect(NetworkConfig config, SystemConfig sys)
}
void EthernetConnectionHandler::disconnect(unsigned long reconnectDelay) {
// TODO
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("Disconnecting!\n"));
}
bool EthernetConnectionHandler::isConnected() {
@@ -145,4 +143,20 @@ bool EthernetConnectionHandler::isConfigChanged() {
void EthernetConnectionHandler::getCurrentConfig(NetworkConfig& networkConfig) {
networkConfig = this->config;
}
}
IPAddress EthernetConnectionHandler::getIP() {
return ETH.localIP();
}
IPAddress EthernetConnectionHandler::getSubnetMask() {
return ETH.subnetMask();
}
IPAddress EthernetConnectionHandler::getGateway() {
return ETH.gatewayIP();
}
IPAddress EthernetConnectionHandler::getDns(uint8_t idx) {
return ETH.dnsIP(idx);
}

View File

@@ -69,4 +69,20 @@ bool WiFiAccessPointConnectionHandler::isConfigChanged() {
void WiFiAccessPointConnectionHandler::getCurrentConfig(NetworkConfig& networkConfig) {
networkConfig = this->config;
}
}
IPAddress WiFiAccessPointConnectionHandler::getIP() {
return WiFi.softAPIP();
}
IPAddress WiFiAccessPointConnectionHandler::getSubnetMask() {
return WiFi.softAPSubnetMask();
}
IPAddress WiFiAccessPointConnectionHandler::getGateway() {
return WiFi.softAPIP();
}
IPAddress WiFiAccessPointConnectionHandler::getDns(uint8_t idx) {
return WiFi.softAPIP();
}

View File

@@ -221,4 +221,20 @@ bool WiFiClientConnectionHandler::isConfigChanged() {
void WiFiClientConnectionHandler::getCurrentConfig(NetworkConfig& networkConfig) {
networkConfig = this->config;
}
}
IPAddress WiFiClientConnectionHandler::getIP() {
return WiFi.localIP();
}
IPAddress WiFiClientConnectionHandler::getSubnetMask() {
return WiFi.subnetMask();
}
IPAddress WiFiClientConnectionHandler::getGateway() {
return WiFi.gatewayIP();
}
IPAddress WiFiClientConnectionHandler::getDns(uint8_t idx) {
return WiFi.dnsIP(idx);
}

View File

@@ -19,6 +19,7 @@
#include "RemoteDebug.h"
#include "PriceService.h"
#include "RealtimePlot.h"
#include "ConnectionHandler.h"
#if defined(ESP8266)
#include <ESP8266WiFi.h>
@@ -56,6 +57,7 @@ public:
void setPriceSettings(String region, String currency);
void setMeterConfig(uint8_t distributionSystem, uint16_t mainFuse, uint16_t productionCapacity);
void setMqttHandler(AmsMqttHandler* mqttHandler);
void setConnectionHandler(ConnectionHandler* ch);
private:
RemoteDebug* debugger;
@@ -76,6 +78,7 @@ private:
EnergyAccounting* ea = NULL;
RealtimePlot* rtp = NULL;
AmsMqttHandler* mqttHandler = NULL;
ConnectionHandler* ch = NULL;
bool uploading = false;
File file;
bool performRestart = false;

View File

@@ -154,6 +154,10 @@ void AmsWebServer::setMqttHandler(AmsMqttHandler* mqttHandler) {
this->mqttHandler = mqttHandler;
}
void AmsWebServer::setConnectionHandler(ConnectionHandler* ch) {
this->ch = ch;
}
void AmsWebServer::setPriceService(PriceService* ps) {
this->ps = ps;
}
@@ -249,11 +253,11 @@ void AmsWebServer::sysinfoJson() {
hostname = "ams-"+chipIdStr;
}
IPAddress localIp = WiFi.localIP();
IPAddress subnet = WiFi.subnetMask();
IPAddress gateway = WiFi.gatewayIP();
IPAddress dns1 = WiFi.dnsIP(0);
IPAddress dns2 = WiFi.dnsIP(1);
IPAddress localIp = ch->getIP();
IPAddress subnet = ch->getSubnetMask();
IPAddress gateway = ch->getGateway();
IPAddress dns1 = ch->getDns(0);
IPAddress dns2 = ch->getDns(1);
char macStr[18] = { 0 };
char apMacStr[18] = { 0 };

View File

@@ -2,7 +2,7 @@
extra_configs = platformio-user.ini
[common]
lib_deps = EEPROM, LittleFS, DNSServer, https://github.com/256dpi/arduino-mqtt.git, OneWireNg@0.10.0, DallasTemperature@3.9.1, EspSoftwareSerial@6.14.1, https://github.com/gskjold/RemoteDebug.git, Time@1.6.1, Timezone@1.2.4, FirmwareVersion, AmsConfiguration, AmsData, AmsDataStorage, HwTools, Uptime, AmsDecoder, PriceService, EnergyAccounting, AmsMqttHandler, RawMqttHandler, JsonMqttHandler, DomoticzMqttHandler, HomeAssistantMqttHandler, RealtimePlot, SvelteUi
lib_deps = EEPROM, LittleFS, DNSServer, https://github.com/256dpi/arduino-mqtt.git, OneWireNg@0.10.0, DallasTemperature@3.9.1, EspSoftwareSerial@6.14.1, https://github.com/gskjold/RemoteDebug.git, Time@1.6.1, Timezone@1.2.4, FirmwareVersion, AmsConfiguration, AmsData, AmsDataStorage, HwTools, Uptime, AmsDecoder, PriceService, EnergyAccounting, AmsMqttHandler, RawMqttHandler, JsonMqttHandler, DomoticzMqttHandler, HomeAssistantMqttHandler, RealtimePlot, ConnectionHandler, SvelteUi
lib_ignore = OneWire
extra_scripts =
pre:scripts/addversion.py

View File

@@ -1019,6 +1019,7 @@ void connectToNetwork() {
toggleSetupMode();
}
ch->connect(network, sysConfig);
ws.setConnectionHandler(ch);
} else {
setupMode = false;
toggleSetupMode();