mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-08 08:49:52 +03:00
ETH optimizations
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -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());
|
||||
|
||||
19
src/system.h
19
src/system.h
@@ -22,18 +22,16 @@
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#if defined(ESP32)
|
||||
#include "driver/adc.h"
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_bt.h>
|
||||
#endif
|
||||
|
||||
#include "helpers.h"
|
||||
#include "console.h"
|
||||
#include "mqtt.h"
|
||||
#include "telegram.h"
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
#include "driver/adc.h"
|
||||
#include <esp_wifi.h>
|
||||
#include <esp_bt.h>
|
||||
#include <ETH.h>
|
||||
#include <uuid/syslog.h>
|
||||
#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_;
|
||||
|
||||
Reference in New Issue
Block a user