diff --git a/lib/framework/APSettingsService.cpp b/lib/framework/APSettingsService.cpp index 6642a36a0..56104e303 100644 --- a/lib/framework/APSettingsService.cpp +++ b/lib/framework/APSettingsService.cpp @@ -21,7 +21,7 @@ void APSettingsService::reconfigureAP() { void APSettingsService::loop() { // if we have an ETH connection, quit - if (ETH.linkUp()) { + if (emsesp::System::ethernet_connected()) { return; } unsigned long currentMillis = uuid::get_uptime(); diff --git a/lib/framework/APSettingsService.h b/lib/framework/APSettingsService.h index 9f8800511..4c3dcd567 100644 --- a/lib/framework/APSettingsService.h +++ b/lib/framework/APSettingsService.h @@ -8,10 +8,10 @@ #include #include -#include - #include +#include "../../src/system.h" // proddy added + #define MANAGE_NETWORK_DELAY 10000 #define AP_MODE_ALWAYS 0 @@ -59,8 +59,7 @@ class APSettings { IPAddress subnetMask; bool operator==(const APSettings & settings) const { - return provisionMode == settings.provisionMode && ssid == settings.ssid && password == settings.password && localIP == settings.localIP - && gatewayIP == settings.gatewayIP && subnetMask == settings.subnetMask; + return provisionMode == settings.provisionMode && ssid == settings.ssid && password == settings.password && localIP == settings.localIP && gatewayIP == settings.gatewayIP && subnetMask == settings.subnetMask; } static void read(APSettings & settings, JsonObject & root) { diff --git a/lib/framework/NTPSettingsService.cpp b/lib/framework/NTPSettingsService.cpp index ef1b4312a..73a03a78d 100644 --- a/lib/framework/NTPSettingsService.cpp +++ b/lib/framework/NTPSettingsService.cpp @@ -34,8 +34,6 @@ void NTPSettingsService::WiFiEvent(WiFiEvent_t event) { configureNTP(); break; - - default: break; } diff --git a/lib/framework/NTPSettingsService.h b/lib/framework/NTPSettingsService.h index 1bc42c76b..5174ed45a 100644 --- a/lib/framework/NTPSettingsService.h +++ b/lib/framework/NTPSettingsService.h @@ -6,7 +6,6 @@ #include #include -#include #ifndef FACTORY_NTP_ENABLED #define FACTORY_NTP_ENABLED true diff --git a/lib/framework/NetworkStatus.cpp b/lib/framework/NetworkStatus.cpp index 5ae030209..1a99aeb8b 100644 --- a/lib/framework/NetworkStatus.cpp +++ b/lib/framework/NetworkStatus.cpp @@ -8,7 +8,7 @@ void NetworkStatus::networkStatus(AsyncWebServerRequest * request) { AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_NETWORK_STATUS_SIZE); JsonObject root = response->getRoot(); - bool ethernet_connected = ETH.linkUp(); + bool ethernet_connected = emsesp::System::ethernet_connected(); wl_status_t wifi_status = WiFi.status(); // see if Ethernet is connected @@ -36,7 +36,7 @@ void NetworkStatus::networkStatus(AsyncWebServerRequest * request) { if (dnsIP2 != INADDR_NONE) { root["dns_ip_2"] = dnsIP2.toString(); } - } else if (ETH.linkUp()) { + } else if (ethernet_connected) { // Ethernet root["local_ip"] = ETH.localIP().toString(); root["mac_address"] = ETH.macAddress(); diff --git a/lib/framework/NetworkStatus.h b/lib/framework/NetworkStatus.h index 1bf77704a..b907f547d 100644 --- a/lib/framework/NetworkStatus.h +++ b/lib/framework/NetworkStatus.h @@ -2,9 +2,10 @@ #define NetworkStatus_h #include -#include #include +#include "../../src/system.h" // proddy added + #include #include #include diff --git a/lib_standalone/Network.h b/lib_standalone/Network.h index b4a24be19..e5b3ee25b 100644 --- a/lib_standalone/Network.h +++ b/lib_standalone/Network.h @@ -10,6 +10,15 @@ #define WIFI_AP WIFI_MODE_AP #define WIFI_AP_STA WIFI_MODE_APSTA +typedef enum { + ETH_CLOCK_GPIO0_IN = 0, /*!< RMII clock input to GPIO0 */ + ETH_CLOCK_GPIO0_OUT = 1, /*!< RMII clock output from GPIO0 */ + ETH_CLOCK_GPIO16_OUT = 2, /*!< RMII clock output from GPIO16 */ + ETH_CLOCK_GPIO17_OUT = 3 /*!< RMII clock output from GPIO17 */ +} eth_clock_mode_t; + +typedef enum { ETH_PHY_LAN8720, ETH_PHY_TLK110, ETH_PHY_MAX } eth_phy_type_t; + typedef enum { SYSTEM_EVENT_WIFI_READY = 0, /**< ESP32 WiFi ready */ SYSTEM_EVENT_SCAN_DONE, /**< ESP32 finish scanning AP */ @@ -159,7 +168,7 @@ class WiFiClass { class ETHClass { public: - bool begin() { + bool begin(uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t type, eth_clock_mode_t clock_mode) { return false; }; diff --git a/src/WebStatusService.cpp b/src/WebStatusService.cpp index b65a0b9f1..dbbc0839f 100644 --- a/src/WebStatusService.cpp +++ b/src/WebStatusService.cpp @@ -73,23 +73,23 @@ void WebStatusService::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info) { case SYSTEM_EVENT_ETH_GOT_IP: // prevent double calls - if (!connected_) { + if (!System::ethernet_connected()) { #ifndef EMSESP_STANDALONE EMSESP::logger().info(F("Ethernet Connected with IP=%s, speed %d Mbps"), ETH.localIP().toString().c_str(), ETH.linkSpeed()); #endif EMSESP::system_.init_network(); // send out heartbeat MQTT as soon as we have a connection - connected_ = true; + System::ethernet_connected(true); } break; case SYSTEM_EVENT_ETH_DISCONNECTED: EMSESP::logger().info(F("Ethernet Disconnected")); - connected_ = false; + System::ethernet_connected(false); break; case SYSTEM_EVENT_ETH_STOP: EMSESP::logger().info(F("Ethernet Stopped")); - connected_ = false; + System::ethernet_connected(false); break; default: diff --git a/src/WebStatusService.h b/src/WebStatusService.h index c9d4f448c..c5582aba0 100644 --- a/src/WebStatusService.h +++ b/src/WebStatusService.h @@ -34,7 +34,6 @@ class WebStatusService { WebStatusService(AsyncWebServer * server, SecurityManager * securityManager); private: - bool connected_ = false; void webStatusService(AsyncWebServerRequest * request); void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info); }; diff --git a/src/system.cpp b/src/system.cpp index 797afab13..322f50c57 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -42,6 +42,7 @@ uint16_t System::analog_ = 0; bool System::analog_enabled_ = false; bool System::syslog_enabled_ = false; std::string System::hostname_; +bool System::ethernet_connected_ = false; // send on/off to a gpio pin // value: true = HIGH, false = LOW @@ -181,8 +182,11 @@ void System::start(uint32_t heap_start) { show_mem("Startup"); #endif - // print boot message - EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { LOG_INFO(F("System %s booted (EMS-ESP version %s)"), networkSettings.hostname.c_str(), EMSESP_APP_VERSION); }); + uint8_t ethernet_profile; + EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & networkSettings) { + LOG_INFO(F("System %s booted (EMS-ESP version %s)"), networkSettings.hostname.c_str(), EMSESP_APP_VERSION); // print boot message + ethernet_profile = networkSettings.ethernet_profile; + }); // these commands respond to the topic "system" and take a payload like {cmd:"", data:"", id:""} EMSESP::webSettingsService.read([&](WebSettings & settings) { @@ -198,43 +202,57 @@ void System::start(uint32_t heap_start) { #endif }); + // start other services first + init(); + // check ethernet profile, if we're using exclusive Ethernet then disabled wifi and AP/captive portal - uint8_t ethernet_profile; - EMSESP::esp8266React.getNetworkSettingsService()->read([&](NetworkSettings & settings) { ethernet_profile = settings.ethernet_profile; }); + if (ethernet_profile == 0) { + return; + } + + uint8_t phy_addr; // I²C-address of Ethernet PHY (0 or 1 for LAN8720, 31 for TLK110) + int power; // Pin# of the enable signal for the external crystal oscillator (-1 to disable for internal APLL source) + int mdc; // Pin# of the I²C clock signal for the Ethernet PHY + int mdio; // Pin# of the I²C IO signal for the Ethernet PHY + eth_phy_type_t type; // Type of the Ethernet PHY (LAN8720 or TLK110) + eth_clock_mode_t clock_mode; // ETH_CLOCK_GPIO0_IN or ETH_CLOCK_GPIO0_OUT, ETH_CLOCK_GPIO16_OUT, ETH_CLOCK_GPIO17_OUT for 50Hz inverted clock + + if (ethernet_profile == 1) { + // LAN8720 + phy_addr = 0; + power = -1; + mdc = 23; + mdio = 18; + type = ETH_PHY_LAN8720; + clock_mode = ETH_CLOCK_GPIO0_IN; + } else if (ethernet_profile == 2) { + // TLK110 + phy_addr = 31; + power = -1; + mdc = 23; + mdio = 18; + type = ETH_PHY_TLK110; + clock_mode = ETH_CLOCK_GPIO0_IN; + } #ifndef EMSESP_STANDALONE - uint8_t phy_addr = 0; // I²C-address of Ethernet PHY (0 or 1 for LAN8720, 31 for TLK110) - int power = -1; // Pin# of the enable signal for the external crystal oscillator (-1 to disable for internal APLL source) - int mdc = 23; // Pin# of the I²C clock signal for the Ethernet PHY - int mdio = 18; // Pin# of the I²C IO signal for the Ethernet PHY - eth_phy_type_t type = ETH_PHY_LAN8720; // Type of the Ethernet PHY (LAN8720 or TLK110) - eth_clock_mode_t clock_mode = ETH_CLOCK_GPIO0_IN; // ETH_CLOCK_GPIO0_IN or ETH_CLOCK_GPIO0_OUT, ETH_CLOCK_GPIO16_OUT, ETH_CLOCK_GPIO17_OUT for 50Hz inverted clock + if (ETH.begin(phy_addr, power, mdc, mdio, type, clock_mode)) { + // disable ssid and AP + EMSESP::esp8266React.getNetworkSettingsService()->update( + [&](NetworkSettings & settings) { + settings.ssid == ""; // remove SSID + return StateUpdateResult::CHANGED; + }, + "local"); - // see if we can start it using default settings - if (!ETH.begin(phy_addr, power, mdc, mdio, type, clock_mode)) { - // it failed. Now try again based on profile. 0 is the same as 1. 2 is for TLK110 - if (ethernet_profile == 2) { - if (ETH.begin(31, power, mdc, mdio, ETH_PHY_TLK110, clock_mode)) { - EMSESP::esp8266React.getNetworkSettingsService()->update( - [&](NetworkSettings & settings) { - settings.ssid == ""; // remove SSID - return StateUpdateResult::CHANGED; - }, - "local"); - - EMSESP::esp8266React.getAPSettingsService()->update( - [&](APSettings & settings) { - settings.provisionMode = AP_MODE_NEVER; - return StateUpdateResult::CHANGED; - }, - "local"); - } - } + EMSESP::esp8266React.getAPSettingsService()->update( + [&](APSettings & settings) { + settings.provisionMode = AP_MODE_NEVER; + return StateUpdateResult::CHANGED; + }, + "local"); } #endif - - // continue with init'ing services - init(); } void System::other_init() { @@ -576,7 +594,7 @@ void System::show_system(uuid::console::Shell & shell) { shell.println(); // show Ethernet - if (ETH.linkUp()) { + if (ethernet_connected()) { shell.printfln(F("Ethernet: Connected")); shell.printfln(F("MAC address: %s"), ETH.macAddress().c_str()); shell.printfln(F("Hostname: %s"), ETH.getHostname()); diff --git a/src/system.h b/src/system.h index a5b8e07e3..6d527ad4e 100644 --- a/src/system.h +++ b/src/system.h @@ -22,18 +22,16 @@ #include #include -#if defined(ESP32) -#include "driver/adc.h" -#include -#include -#endif - #include "helpers.h" #include "console.h" #include "mqtt.h" #include "telegram.h" #ifndef EMSESP_STANDALONE +#include "driver/adc.h" +#include +#include +#include #include #endif @@ -85,6 +83,14 @@ class System { hostname_ = hostname; } + static bool ethernet_connected() { + return ethernet_connected_; + } + + static void ethernet_connected(bool b) { + ethernet_connected_ = b; + } + private: static uuid::log::Logger logger_; @@ -119,6 +125,7 @@ class System { static bool upload_status_; // true if we're in the middle of a OTA firmware upload static uint32_t heap_start_; static uint16_t analog_; + static bool ethernet_connected_; // settings static std::string hostname_;