36 explicit ESPETHSPI(eth_phy_type_t type, int32_t phy_addr,
37 int cs,
int irq,
int rst, spi_host_device_t spi_host,
38 int sck = -1,
int miso = -1,
int mosi = -1) {
41 ethspi_phy_addr = phy_addr;
45 ethspi_spi_host = spi_host;
52 if (thisSpiEth ==
this) {
57 static void networkEventHandler(arduino_event_id_t event) {
59 case ARDUINO_EVENT_ETH_GOT_IP: {
60 Serial.print(F(
"[Ethernet] local IP: "));
61 Serial.println(ETH.localIP());
62 Serial.print(F(
"subnetMask: "));
63 Serial.println(ETH.subnetMask());
64 Serial.print(F(
"gatewayIP: "));
65 Serial.println(ETH.gatewayIP());
66 Serial.print(F(
"ETH MAC: "));
67 Serial.println(ETH.macAddress());
68 if (ETH.fullDuplex()) {
69 Serial.print(F(
"FULL_DUPLEX , "));
71 Serial.print(ETH.linkSpeed());
72 Serial.println(F(
"Mbps"));
74 thisSpiEth->setIpv4Addr(ETH.localIP());
78 case ARDUINO_EVENT_ETH_DISCONNECTED: {
79 Serial.println(F(
"[Ethernet] Disconnected"));
81 thisSpiEth->setIpv4Addr(0);
88 void setup()
override {
94 ::Network.onEvent(Supla::ESPETHSPI::networkEventHandler);
96 Serial.println(F(
"[Ethernet] establishing LAN connection"));
97 ETH.begin(ethspi_type,
106 if (hasStaticIpConfig()) {
107 const auto &cfg = getNetifConfig();
108 IPAddress localIp = toArduinoIpAddress(cfg.ip);
109 IPAddress gateway = toArduinoIpAddress(cfg.gateway);
110 IPAddress subnet = toArduinoIpAddress(cfg.netmask);
111 IPAddress dns1 = toArduinoIpAddress(cfg.dns1);
112 IPAddress dns2 = toArduinoIpAddress(cfg.dns2);
113 if (!ETH.config(localIp, gateway, subnet, dns1, dns2)) {
114 SUPLA_LOG_WARNING(
"ETH SPI static IP config failed, continuing");
119 char newHostname[32] = {};
120 generateHostname(hostname, macSizeForHostname, newHostname);
121 strncpy(hostname, newHostname,
sizeof(hostname) - 1);
122 SUPLA_LOG_DEBUG(
"[%s] Network LAN/hostname: %s", getIntfName(), hostname);
123 ETH.setHostname(hostname);
126 void disable()
override {
131 allowDisable =
false;
132 SUPLA_LOG_DEBUG(
"[%s] disabling ETH connection", getIntfName());
133 DisconnectProtocols();
137 bool getMacAddr(uint8_t *mac)
override {
144 const char *getIntfName()
const override {
148 void setHostname(
const char *prefix,
int macSize)
override {
149 macSizeForHostname = macSize;
150 strncpy(hostname, prefix,
sizeof(hostname) - 1);
151 SUPLA_LOG_DEBUG(
"[%s] Network LAN/hostname: %s", getIntfName(), hostname);
155 eth_phy_type_t ethspi_type;
156 int32_t ethspi_phy_addr;
160 spi_host_device_t ethspi_spi_host;
164 bool allowDisable =
false;
165 int macSizeForHostname = 0;
166 bool initDone =
false;