From 0f22fd561ee808f16ae58f9de6bb15c88574a9d4 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Sun, 10 Dec 2023 16:56:13 +0100 Subject: [PATCH] Improved ethernet connection --- .../include}/ConnectionHandler.h | 4 + .../include}/EthernetConnectionHandler.h | 8 ++ .../WiFiAccessPointConnectionHandler.h | 4 + .../include}/WiFiClientConnectionHandler.h | 4 + .../src}/EthernetConnectionHandler.cpp | 74 +++++++++++-------- .../src}/WiFiAccessPointConnectionHandler.cpp | 18 ++++- .../src}/WiFiClientConnectionHandler.cpp | 18 ++++- lib/SvelteUi/include/AmsWebServer.h | 3 + lib/SvelteUi/src/AmsWebServer.cpp | 14 ++-- platformio.ini | 2 +- src/AmsToMqttBridge.cpp | 1 + 11 files changed, 112 insertions(+), 38 deletions(-) rename {src => lib/ConnectionHandler/include}/ConnectionHandler.h (85%) rename {src => lib/ConnectionHandler/include}/EthernetConnectionHandler.h (83%) rename {src => lib/ConnectionHandler/include}/WiFiAccessPointConnectionHandler.h (88%) rename {src => lib/ConnectionHandler/include}/WiFiClientConnectionHandler.h (89%) rename {src => lib/ConnectionHandler/src}/EthernetConnectionHandler.cpp (69%) rename {src => lib/ConnectionHandler/src}/WiFiAccessPointConnectionHandler.cpp (87%) rename {src => lib/ConnectionHandler/src}/WiFiClientConnectionHandler.cpp (95%) diff --git a/src/ConnectionHandler.h b/lib/ConnectionHandler/include/ConnectionHandler.h similarity index 85% rename from src/ConnectionHandler.h rename to lib/ConnectionHandler/include/ConnectionHandler.h index 331c82a4..ac8ae64b 100644 --- a/src/ConnectionHandler.h +++ b/lib/ConnectionHandler/include/ConnectionHandler.h @@ -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 diff --git a/src/EthernetConnectionHandler.h b/lib/ConnectionHandler/include/EthernetConnectionHandler.h similarity index 83% rename from src/EthernetConnectionHandler.h rename to lib/ConnectionHandler/include/EthernetConnectionHandler.h index a02ced40..fbb3545c 100644 --- a/src/EthernetConnectionHandler.h +++ b/lib/ConnectionHandler/include/EthernetConnectionHandler.h @@ -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 diff --git a/src/WiFiAccessPointConnectionHandler.h b/lib/ConnectionHandler/include/WiFiAccessPointConnectionHandler.h similarity index 88% rename from src/WiFiAccessPointConnectionHandler.h rename to lib/ConnectionHandler/include/WiFiAccessPointConnectionHandler.h index 1e3c235c..941830c6 100644 --- a/src/WiFiAccessPointConnectionHandler.h +++ b/lib/ConnectionHandler/include/WiFiAccessPointConnectionHandler.h @@ -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); diff --git a/src/WiFiClientConnectionHandler.h b/lib/ConnectionHandler/include/WiFiClientConnectionHandler.h similarity index 89% rename from src/WiFiClientConnectionHandler.h rename to lib/ConnectionHandler/include/WiFiClientConnectionHandler.h index db9e0017..098fd2bd 100644 --- a/src/WiFiClientConnectionHandler.h +++ b/lib/ConnectionHandler/include/WiFiClientConnectionHandler.h @@ -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); diff --git a/src/EthernetConnectionHandler.cpp b/lib/ConnectionHandler/src/EthernetConnectionHandler.cpp similarity index 69% rename from src/EthernetConnectionHandler.cpp rename to lib/ConnectionHandler/src/EthernetConnectionHandler.cpp index 96b25eb3..48488cd4 100644 --- a/src/EthernetConnectionHandler.cpp +++ b/lib/ConnectionHandler/src/EthernetConnectionHandler.cpp @@ -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; -} \ No newline at end of file +} + +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); +} diff --git a/src/WiFiAccessPointConnectionHandler.cpp b/lib/ConnectionHandler/src/WiFiAccessPointConnectionHandler.cpp similarity index 87% rename from src/WiFiAccessPointConnectionHandler.cpp rename to lib/ConnectionHandler/src/WiFiAccessPointConnectionHandler.cpp index 3b500abe..6d08a0dd 100644 --- a/src/WiFiAccessPointConnectionHandler.cpp +++ b/lib/ConnectionHandler/src/WiFiAccessPointConnectionHandler.cpp @@ -69,4 +69,20 @@ bool WiFiAccessPointConnectionHandler::isConfigChanged() { void WiFiAccessPointConnectionHandler::getCurrentConfig(NetworkConfig& networkConfig) { networkConfig = this->config; -} \ No newline at end of file +} + +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(); +} diff --git a/src/WiFiClientConnectionHandler.cpp b/lib/ConnectionHandler/src/WiFiClientConnectionHandler.cpp similarity index 95% rename from src/WiFiClientConnectionHandler.cpp rename to lib/ConnectionHandler/src/WiFiClientConnectionHandler.cpp index e0068342..2d9a9720 100644 --- a/src/WiFiClientConnectionHandler.cpp +++ b/lib/ConnectionHandler/src/WiFiClientConnectionHandler.cpp @@ -221,4 +221,20 @@ bool WiFiClientConnectionHandler::isConfigChanged() { void WiFiClientConnectionHandler::getCurrentConfig(NetworkConfig& networkConfig) { networkConfig = this->config; -} \ No newline at end of file +} + +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); +} diff --git a/lib/SvelteUi/include/AmsWebServer.h b/lib/SvelteUi/include/AmsWebServer.h index 33b26174..466b87dd 100644 --- a/lib/SvelteUi/include/AmsWebServer.h +++ b/lib/SvelteUi/include/AmsWebServer.h @@ -19,6 +19,7 @@ #include "RemoteDebug.h" #include "PriceService.h" #include "RealtimePlot.h" +#include "ConnectionHandler.h" #if defined(ESP8266) #include @@ -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; diff --git a/lib/SvelteUi/src/AmsWebServer.cpp b/lib/SvelteUi/src/AmsWebServer.cpp index bed83683..121e5ed5 100644 --- a/lib/SvelteUi/src/AmsWebServer.cpp +++ b/lib/SvelteUi/src/AmsWebServer.cpp @@ -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 }; diff --git a/platformio.ini b/platformio.ini index 0e3a6bd8..07361ffe 100755 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/AmsToMqttBridge.cpp b/src/AmsToMqttBridge.cpp index 77e891e7..c7da7974 100644 --- a/src/AmsToMqttBridge.cpp +++ b/src/AmsToMqttBridge.cpp @@ -1019,6 +1019,7 @@ void connectToNetwork() { toggleSetupMode(); } ch->connect(network, sysConfig); + ws.setConnectionHandler(ch); } else { setupMode = false; toggleSetupMode();