23 explicit ESPETHSPI(eth_phy_type_t type, int32_t phy_addr,
24 int cs,
int irq,
int rst, spi_host_device_t spi_host,
25 int sck = -1,
int miso = -1,
int mosi = -1) {
28 ethspi_phy_addr = phy_addr;
32 ethspi_spi_host = spi_host;
39 if (thisSpiEth ==
this) {
44 static void networkEventHandler(arduino_event_id_t event) {
46 case ARDUINO_EVENT_ETH_GOT_IP: {
48 IPAddress subnetMaskIP = ETH.subnetMask();
52 SUPLA_LOG_INFO(
"localIP: %d.%d.%d.%d",
57 SUPLA_LOG_INFO(
"subnetMaskIP: %d.%d.%d.%d",
62 SUPLA_LOG_INFO(
"gatewayIP: %d.%d.%d.%d",
67 SUPLA_LOG_INFO(
"ETH MAC: %02X:%02X:%02X:%02X:%02X:%02X",
74 SUPLA_LOG_INFO(
"speed: %d Mbps", ETH.linkSpeed());
75 if (ETH.fullDuplex()) {
76 SUPLA_LOG_INFO(
"FULL_DUPLEX");
79 thisSpiEth->setIpv4Addr(ETH.localIP());
83 case ARDUINO_EVENT_ETH_DISCONNECTED: {
84 SUPLA_LOG_INFO(
"[Ethernet] Disconnected");
86 thisSpiEth->setIpv4Addr(0);
93 void setup()
override {
99 ::Network.onEvent(Supla::ESPETHSPI::networkEventHandler);
101 SUPLA_LOG_INFO(
"[Ethernet] establishing LAN connection");
102 ETH.begin(ethspi_type,
111 if (hasStaticIpConfig()) {
112 const auto &cfg = getNetifConfig();
113 IPAddress localIp = toArduinoIpAddress(cfg.ip);
114 IPAddress gateway = toArduinoIpAddress(cfg.gateway);
115 IPAddress subnet = toArduinoIpAddress(cfg.netmask);
116 IPAddress dns1 = toArduinoIpAddress(cfg.dns1);
117 IPAddress dns2 = toArduinoIpAddress(cfg.dns2);
118 if (!ETH.config(localIp, gateway, subnet, dns1, dns2)) {
119 SUPLA_LOG_WARNING(
"ETH SPI static IP config failed, continuing");
124 char newHostname[32] = {};
125 generateHostname(hostname, macSizeForHostname, newHostname);
126 strncpy(hostname, newHostname,
sizeof(hostname) - 1);
127 SUPLA_LOG_DEBUG(
"[%s] Network LAN/hostname: %s", getIntfName(), hostname);
128 ETH.setHostname(hostname);
131 void disable()
override {
136 allowDisable =
false;
137 SUPLA_LOG_DEBUG(
"[%s] disabling ETH connection", getIntfName());
138 DisconnectProtocols();
142 bool getMacAddr(uint8_t *mac)
override {
149 const char *getIntfName()
const override {
153 void setHostname(
const char *prefix,
int macSize)
override {
154 macSizeForHostname = macSize;
155 strncpy(hostname, prefix,
sizeof(hostname) - 1);
156 SUPLA_LOG_DEBUG(
"[%s] Network LAN/hostname: %s", getIntfName(), hostname);
160 eth_phy_type_t ethspi_type;
161 int32_t ethspi_phy_addr;
165 spi_host_device_t ethspi_spi_host;
169 bool allowDisable =
false;
170 int macSizeForHostname = 0;
171 bool initDone =
false;