Remove old and modified libs, switch to ESPAsyncWebserver, add support for ESP32-H2 and multiple Ethernet modules (#455)

* Asyncwebserver

* Squashed commit of the following:

commit 575ef02f593918ec6654c87407a4d11fc17071b8
Author: technyon <j.o.schuemann@gmx.de>
Date:   Mon Aug 12 17:56:11 2024 +0200

    merge master

commit 35e5adf4ecd80f9829e8801181f35dd2c1d94759
Merge: a2cc7be2 21adca01
Author: technyon <j.o.schuemann@gmx.de>
Date:   Mon Aug 12 17:41:04 2024 +0200

    Merge branch 'master' of github.com:technyon/nuki_hub into DM9051

commit a2cc7be2954cbd8767ab8186296c0b14134d1d0b
Author: technyon <j.o.schuemann@gmx.de>
Date:   Mon Aug 12 10:51:50 2024 +0200

    update nuki ble

commit 20c809f3dca28b29b219d1ff3a183f1981316de5
Author: technyon <j.o.schuemann@gmx.de>
Date:   Mon Aug 12 10:44:46 2024 +0200

    backup

commit dd41c218efb5270f5efeb734e64dff695920db16
Merge: 153000b5 e84b944a
Author: technyon <j.o.schuemann@gmx.de>
Date:   Mon Aug 12 10:40:03 2024 +0200

    Merge branch 'master' of github.com:technyon/nuki_hub into DM9051

commit 153000b5b1af7df1fbeb5263df94eb26f689cc0a
Author: technyon <j.o.schuemann@gmx.de>
Date:   Mon Aug 12 10:23:07 2024 +0200

    fix linker error

commit a93bbfbfc4301e46ff3696a763dd13c6c89efefb
Author: technyon <j.o.schuemann@gmx.de>
Date:   Sun Aug 11 11:27:07 2024 +0200

    backup

commit f611c75ce8c35f829bcad6cf7e86188f4b3ec331
Merge: f1964917 063fbab6
Author: technyon <j.o.schuemann@gmx.de>
Date:   Sun Aug 11 11:24:47 2024 +0200

    merge master

commit f1964917b4dade3920f1ecdb699c58630199e6da
Author: technyon <j.o.schuemann@gmx.de>
Date:   Sat Aug 10 15:17:45 2024 +0200

    update platformio.ini

commit f448e5e8a7e93be38e09e2ab0b622199a3721af6
Author: technyon <j.o.schuemann@gmx.de>
Date:   Sat Aug 10 11:28:09 2024 +0200

    add SPIClass instance for DM9051

commit 1f190e9aa08033535a2eb442a92e6e20409bbda1
Author: technyon <j.o.schuemann@gmx.de>
Date:   Sat Aug 10 11:22:26 2024 +0200

    add definitions and constructor for DM9051

commit 726b3602ae91594ee1210ad5b6714f75cc5e42a7
Merge: 50a2eb13 4af90cbc
Author: technyon <j.o.schuemann@gmx.de>
Date:   Sat Aug 10 10:19:34 2024 +0200

    merge master

commit 50a2eb136d75d90921f1c6974f18bc107bddc123
Author: technyon <j.o.schuemann@gmx.de>
Date:   Fri Aug 9 11:52:09 2024 +0200

    add comment

commit 9437e485cae169efdf8e5a7bf188a1c7e792d1e5
Author: technyon <j.o.schuemann@gmx.de>
Date:   Sun Aug 4 08:29:21 2024 +0200

    move LAN8720 definitions to seperate file

* Remove Core 2 Ethernet library

* Custom Ethernet

* GPIO and Preferences

* H2
This commit is contained in:
iranl
2024-08-16 13:02:37 +02:00
committed by GitHub
parent 346c5c65d1
commit 9a896a7ab1
206 changed files with 4055 additions and 20829 deletions

View File

@@ -1,75 +0,0 @@
/*
Copyright (c) 2022 Bert Melis. All rights reserved.
This work is licensed under the terms of the MIT license.
For a copy, see <https://opensource.org/licenses/MIT> or
the LICENSE file.
*/
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#include "ClientSyncW5500.h"
#include <lwip/sockets.h> // socket options
namespace espMqttClientInternals {
ClientSyncW5500::ClientSyncW5500()
: client() {
// empty
}
bool ClientSyncW5500::connect(IPAddress ip, uint16_t port) {
bool ret = client.connect(ip, port); // implicit conversion of return code int --> bool
if (ret) {
#if defined(ARDUINO_ARCH_ESP8266)
client.setNoDelay(true);
#elif defined(ARDUINO_ARCH_ESP32)
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
int val = true;
// TODO
// client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
#endif
}
return ret;
}
bool ClientSyncW5500::connect(const char* host, uint16_t port) {
bool ret = client.connect(host, port); // implicit conversion of return code int --> bool
if (ret) {
#if defined(ARDUINO_ARCH_ESP8266)
client.setNoDelay(true);
#elif defined(ARDUINO_ARCH_ESP32)
// Set TCP option directly to bypass lack of working setNoDelay for WiFiClientSecure (for consistency also here)
int val = true;
// TODO
// client.setSocketOption(IPPROTO_TCP, TCP_NODELAY, &val, sizeof(int));
#endif
}
return ret;
}
size_t ClientSyncW5500::write(const uint8_t* buf, size_t size) {
return client.write(buf, size);
}
int ClientSyncW5500::read(uint8_t* buf, size_t size) {
return client.read(buf, size);
}
void ClientSyncW5500::stop() {
client.stop();
}
bool ClientSyncW5500::connected() {
return client.connected();
}
bool ClientSyncW5500::disconnected() {
return !client.connected();
}
} // namespace espMqttClientInternals
#endif

View File

@@ -1,25 +0,0 @@
#pragma once
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
#include "Transport/Transport.h"
#include "EthernetClient.h"
namespace espMqttClientInternals {
class ClientSyncW5500 : public Transport {
public:
ClientSyncW5500();
bool connect(IPAddress ip, uint16_t port) override;
bool connect(const char* host, uint16_t port) override;
size_t write(const uint8_t* buf, size_t size) override;
int read(uint8_t* buf, size_t size) override;
void stop() override;
bool connected() override;
bool disconnected() override;
EthernetClient client;
};
} // namespace espMqttClientInternals
#endif

View File

@@ -0,0 +1,13 @@
#pragma once
#include <hal/spi_types.h>
#define ETH_PHY_TYPE_DM9051 ETH_PHY_DM9051
#define ETH_PHY_ADDR_ETH01EVO 1
#define ETH_PHY_CS_ETH01EVO 9
#define ETH_PHY_IRQ_ETH01EVO 8
#define ETH_PHY_RST_ETH01EVO 6
#define ETH_PHY_SPI_SCK_ETH01EVO 7
#define ETH_PHY_SPI_MISO_ETH01EVO 3
#define ETH_PHY_SPI_MOSI_ETH01EVO 10

View File

@@ -1,179 +0,0 @@
//#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
//#define ETH_PHY_POWER 12
#include <WiFi.h>
#include <ETH.h>
#include "EthLan8720Device.h"
#include "../PreferencesKeys.h"
#include "../Logger.h"
#ifndef NUKI_HUB_UPDATER
#include "../MqttTopics.h"
#include "espMqttClient.h"
#endif
#include "../RestartReason.h"
EthLan8720Device::EthLan8720Device(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration, const std::string& deviceName, uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t ethtype, eth_clock_mode_t clock_mode, bool use_mac_from_efuse)
: NetworkDevice(hostname, ipConfiguration),
_deviceName(deviceName),
_phy_addr(phy_addr),
_power(power),
_mdc(mdc),
_mdio(mdio),
_type(ethtype),
_clock_mode(clock_mode),
_use_mac_from_efuse(use_mac_from_efuse)
{
_restartOnDisconnect = preferences->getBool(preference_restart_on_disconnect);
#ifndef NUKI_HUB_UPDATER
size_t caLength = preferences->getString(preference_mqtt_ca, _ca, TLS_CA_MAX_SIZE);
size_t crtLength = preferences->getString(preference_mqtt_crt, _cert, TLS_CERT_MAX_SIZE);
size_t keyLength = preferences->getString(preference_mqtt_key, _key, TLS_KEY_MAX_SIZE);
_useEncryption = caLength > 1; // length is 1 when empty
if(_useEncryption)
{
Log->println(F("MQTT over TLS."));
Log->println(_ca);
_mqttClientSecure = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO);
_mqttClientSecure->setCACert(_ca);
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
{
Log->println(F("MQTT with client certificate."));
Log->println(_cert);
Log->println(_key);
_mqttClientSecure->setCertificate(_cert);
_mqttClientSecure->setPrivateKey(_key);
}
} else
{
Log->println(F("MQTT without TLS."));
_mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO);
}
if(preferences->getBool(preference_mqtt_log_enabled, false) || preferences->getBool(preference_webserial_enabled, false))
{
MqttLoggerMode mode;
if(preferences->getBool(preference_mqtt_log_enabled, false) && preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::MqttAndSerialAndWeb;
else if (preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::SerialAndWeb;
else mode = MqttLoggerMode::MqttAndSerial;
_path = new char[200];
memset(_path, 0, sizeof(_path));
String pathStr = preferences->getString(preference_mqtt_lock_path);
pathStr.concat(mqtt_topic_log);
strcpy(_path, pathStr.c_str());
Log = new MqttLogger(*getMqttClient(), _path, mode);
}
#endif
}
const String EthLan8720Device::deviceName() const
{
return _deviceName.c_str();
}
void EthLan8720Device::initialize()
{
delay(250);
WiFi.setHostname(_hostname.c_str());
#if CONFIG_IDF_TARGET_ESP32
_hardwareInitialized = ETH.begin(_type, _phy_addr, _mdc, _mdio, _power, _clock_mode);
#else
_hardwareInitialized = false;
#endif
ETH.setHostname(_hostname.c_str());
if(!_ipConfiguration->dhcpEnabled())
{
ETH.config(_ipConfiguration->ipAddress(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet(), _ipConfiguration->dnsServer());
}
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info)
{
if(event == ARDUINO_EVENT_WIFI_STA_DISCONNECTED)
{
onDisconnected();
}
});
if(_ipConfiguration->dhcpEnabled())
{
waitForIpAddressWithTimeout();
}
}
void EthLan8720Device::waitForIpAddressWithTimeout()
{
int count = 100;
Log->print(F("LAN8720: Obtaining IP address via DHCP"));
while(count > 0 && localIP().equals("0.0.0.0"))
{
Log->print(F("."));
count--;
delay(100);
}
if(localIP().equals("0.0.0.0"))
{
Log->println(F(" Failed"));
}
else
{
Log->println(localIP());
}
}
void EthLan8720Device::reconfigure()
{
delay(200);
restartEsp(RestartReason::ReconfigureLAN8720);
}
bool EthLan8720Device::supportsEncryption()
{
return true;
}
bool EthLan8720Device::isConnected()
{
return ETH.linkUp();
}
ReconnectStatus EthLan8720Device::reconnect(bool force)
{
if(!_hardwareInitialized)
{
return ReconnectStatus::CriticalFailure;
}
delay(200);
return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure;
}
void EthLan8720Device::onDisconnected()
{
if(_restartOnDisconnect && ((esp_timer_get_time() / 1000) > 60000)) restartEsp(RestartReason::RestartOnDisconnectWatchdog);
reconnect();
}
int8_t EthLan8720Device::signalStrength()
{
return -1;
}
String EthLan8720Device::localIP()
{
return ETH.localIP().toString();
}
String EthLan8720Device::BSSIDstr()
{
return "";
}

View File

@@ -1,83 +0,0 @@
#pragma once
#ifndef CONFIG_IDF_TARGET_ESP32
typedef enum {
ETH_CLOCK_GPIO0_IN = 0,
ETH_CLOCK_GPIO16_OUT = 2,
ETH_CLOCK_GPIO17_OUT = 3
} eth_clock_mode_t;
#define ETH_PHY_TYPE ETH_PHY_MAX
#else
#define ETH_PHY_TYPE ETH_PHY_LAN8720
#endif
#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN
#define ETH_PHY_ADDR 0
#define ETH_PHY_MDC 23
#define ETH_PHY_MDIO 18
#define ETH_PHY_POWER -1
#define ETH_RESET_PIN 1
#include <WiFiClient.h>
#include <NetworkClientSecure.h>
#include <Preferences.h>
#include "NetworkDevice.h"
#ifndef NUKI_HUB_UPDATER
#include "espMqttClient.h"
#endif
#include <ETH.h>
class EthLan8720Device : public NetworkDevice
{
public:
EthLan8720Device(const String& hostname,
Preferences* preferences,
const IPConfiguration* ipConfiguration,
const std::string& deviceName,
uint8_t phy_addr = ETH_PHY_ADDR,
int power = ETH_PHY_POWER,
int mdc = ETH_PHY_MDC,
int mdio = ETH_PHY_MDIO,
eth_phy_type_t ethtype = ETH_PHY_TYPE,
eth_clock_mode_t clock_mode = ETH_CLK_MODE,
bool use_mac_from_efuse = false);
const String deviceName() const override;
virtual void initialize();
virtual void reconfigure();
virtual ReconnectStatus reconnect(bool force = false);
bool supportsEncryption() override;
virtual bool isConnected();
int8_t signalStrength() override;
String localIP() override;
String BSSIDstr() override;
private:
void onDisconnected();
void waitForIpAddressWithTimeout();
bool _restartOnDisconnect = false;
bool _startAp = false;
char* _path;
bool _hardwareInitialized = false;
const std::string _deviceName;
uint8_t _phy_addr;
int _power;
int _mdc;
int _mdio;
eth_phy_type_t _type;
eth_clock_mode_t _clock_mode;
bool _use_mac_from_efuse;
#ifndef NUKI_HUB_UPDATER
char _ca[TLS_CA_MAX_SIZE] = {0};
char _cert[TLS_CERT_MAX_SIZE] = {0};
char _key[TLS_KEY_MAX_SIZE] = {0};
#endif
};

View File

@@ -0,0 +1,221 @@
#include "EthernetDevice.h"
#include "../PreferencesKeys.h"
#include "../Logger.h"
#ifndef NUKI_HUB_UPDATER
#include "../MqttTopics.h"
#include "espMqttClient.h"
#endif
#include "../RestartReason.h"
EthernetDevice::EthernetDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration, const std::string& deviceName, uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t ethtype, eth_clock_mode_t clock_mode, bool use_mac_from_efuse)
: NetworkDevice(hostname, ipConfiguration),
_deviceName(deviceName),
_phy_addr(phy_addr),
_power(power),
_mdc(mdc),
_mdio(mdio),
_type(ethtype),
_clock_mode(clock_mode),
_use_mac_from_efuse(use_mac_from_efuse),
_useSpi(false),
_preferences(preferences)
{
init();
}
EthernetDevice::EthernetDevice(const String &hostname,
Preferences *preferences,
const IPConfiguration *ipConfiguration,
const std::string &deviceName,
uint8_t phy_addr,
int cs,
int irq,
int rst,
int spi_sck,
int spi_miso,
int spi_mosi,
uint8_t spi_freq_mhz,
eth_phy_type_t ethtype)
: NetworkDevice(hostname, ipConfiguration),
_deviceName(deviceName),
_phy_addr(phy_addr),
_cs(cs),
_irq(irq),
_rst(rst),
_spi_sck(spi_sck),
_spi_miso(spi_miso),
_spi_mosi(spi_mosi),
_spi_freq_mhz(spi_freq_mhz),
_type(ethtype),
_useSpi(true),
_preferences(preferences)
{
init();
}
void EthernetDevice::init()
{
_restartOnDisconnect = _preferences->getBool(preference_restart_on_disconnect);
#ifndef NUKI_HUB_UPDATER
size_t caLength = _preferences->getString(preference_mqtt_ca, _ca, TLS_CA_MAX_SIZE);
size_t crtLength = _preferences->getString(preference_mqtt_crt, _cert, TLS_CERT_MAX_SIZE);
size_t keyLength = _preferences->getString(preference_mqtt_key, _key, TLS_KEY_MAX_SIZE);
_useEncryption = caLength > 1; // length is 1 when empty
if(_useEncryption)
{
Log->println(F("MQTT over TLS."));
Log->println(_ca);
_mqttClientSecure = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO);
_mqttClientSecure->setCACert(_ca);
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
{
Log->println(F("MQTT with client certificate."));
Log->println(_cert);
Log->println(_key);
_mqttClientSecure->setCertificate(_cert);
_mqttClientSecure->setPrivateKey(_key);
}
} else
{
Log->println(F("MQTT without TLS."));
_mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO);
}
if(_preferences->getBool(preference_mqtt_log_enabled, false) || _preferences->getBool(preference_webserial_enabled, false))
{
MqttLoggerMode mode;
if(_preferences->getBool(preference_mqtt_log_enabled, false) && _preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::MqttAndSerialAndWeb;
else if (_preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::SerialAndWeb;
else mode = MqttLoggerMode::MqttAndSerial;
_path = new char[200];
memset(_path, 0, sizeof(_path));
String pathStr = _preferences->getString(preference_mqtt_lock_path);
pathStr.concat(mqtt_topic_log);
strcpy(_path, pathStr.c_str());
Log = new MqttLogger(*getMqttClient(), _path, mode);
}
#endif
}
const String EthernetDevice::deviceName() const
{
return _deviceName.c_str();
}
void EthernetDevice::initialize()
{
delay(250);
Log->println(F("Init Ethernet"));
if(_useSpi)
{
Log->println(F("Use SPI"));
SPI.begin(_spi_sck, _spi_miso, _spi_mosi);
_hardwareInitialized = ETH.begin(_type, _phy_addr, _cs, _irq, _rst, SPI);
}
#ifdef CONFIG_IDF_TARGET_ESP32
else
{
Log->println(F("Use RMII"));
_hardwareInitialized = ETH.begin(_type, _phy_addr, _mdc, _mdio, _power, _clock_mode);
}
#endif
if(_hardwareInitialized)
{
Log->println(F("Ethernet hardware Initialized"));
Network.onEvent([&](arduino_event_id_t event, arduino_event_info_t info)
{
switch (event) {
case ARDUINO_EVENT_ETH_START:
Log->println("ETH Started");
ETH.setHostname(_hostname.c_str());
if(!_ipConfiguration->dhcpEnabled()) ETH.config(_ipConfiguration->ipAddress(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet(), _ipConfiguration->dnsServer());
break;
case ARDUINO_EVENT_ETH_CONNECTED:
Log->println("ETH Connected");
if(!localIP().equals("0.0.0.0")) _connected = true;
break;
case ARDUINO_EVENT_ETH_GOT_IP:
Log->printf("ETH Got IP: '%s'\n", esp_netif_get_desc(info.got_ip.esp_netif));
Log->println(ETH);
_connected = true;
if(_preferences->getBool(preference_ntw_reconfigure, false)) _preferences->putBool(preference_ntw_reconfigure, false);
break;
case ARDUINO_EVENT_ETH_LOST_IP:
Log->println("ETH Lost IP");
_connected = false;
onDisconnected();
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
Log->println("ETH Disconnected");
_connected = false;
onDisconnected();
break;
case ARDUINO_EVENT_ETH_STOP:
Log->println("ETH Stopped");
_connected = false;
onDisconnected();
break;
default:
break;
}
});
}
else Log->println(F("Failed to initialize ethernet hardware"));
}
void EthernetDevice::reconfigure()
{
delay(200);
restartEsp(RestartReason::ReconfigureETH);
}
bool EthernetDevice::supportsEncryption()
{
return true;
}
bool EthernetDevice::isConnected()
{
return _connected;
}
ReconnectStatus EthernetDevice::reconnect(bool force)
{
if(!_hardwareInitialized)
{
return ReconnectStatus::CriticalFailure;
}
delay(200);
return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure;
}
void EthernetDevice::onDisconnected()
{
if(_restartOnDisconnect && ((esp_timer_get_time() / 1000) > 60000)) restartEsp(RestartReason::RestartOnDisconnectWatchdog);
reconnect();
}
int8_t EthernetDevice::signalStrength()
{
return -1;
}
String EthernetDevice::localIP()
{
return ETH.localIP().toString();
}
String EthernetDevice::BSSIDstr()
{
return "";
}

View File

@@ -0,0 +1,102 @@
#pragma once
#include <ETH.h>
#include <cstdint>
#include <hal/spi_types.h>
#include <SPI.h>
#include "LAN8720Definitions.h"
#include "DM9051Definitions.h"
#include "W5500Definitions.h"
#include <NetworkClient.h>
#include <NetworkClientSecure.h>
#include <Preferences.h>
#include "NetworkDevice.h"
#ifndef NUKI_HUB_UPDATER
#include "espMqttClient.h"
#endif
class EthernetDevice : public NetworkDevice
{
public:
EthernetDevice(const String& hostname,
Preferences* preferences,
const IPConfiguration* ipConfiguration,
const std::string& deviceName,
uint8_t phy_addr = ETH_PHY_ADDR_LAN8720,
int power = ETH_PHY_POWER_LAN8720,
int mdc = ETH_PHY_MDC_LAN8720,
int mdio = ETH_PHY_MDIO_LAN8720,
eth_phy_type_t ethtype = ETH_PHY_TYPE_LAN8720,
eth_clock_mode_t clock_mode = ETH_CLK_MODE_LAN8720,
bool use_mac_from_efuse = false);
EthernetDevice(const String& hostname,
Preferences* preferences,
const IPConfiguration* ipConfiguration,
const std::string& deviceName,
uint8_t phy_addr,
int cs,
int irq,
int rst,
int spi_sck,
int spi_miso,
int spi_mosi,
uint8_t spi_freq_mhz,
eth_phy_type_t ethtype);
const String deviceName() const override;
virtual void initialize();
virtual void reconfigure();
virtual ReconnectStatus reconnect(bool force = false);
bool supportsEncryption() override;
virtual bool isConnected();
int8_t signalStrength() override;
String localIP() override;
String BSSIDstr() override;
private:
Preferences* _preferences;
void init();
void onDisconnected();
void waitForIpAddressWithTimeout();
bool _connected = false;
bool _restartOnDisconnect = false;
bool _startAp = false;
char* _path;
bool _hardwareInitialized = false;
const std::string _deviceName;
uint8_t _phy_addr;
// LAN8720
int _power;
int _mdc;
int _mdio;
// W55000 and DM9051
int _cs;
int _irq;
int _rst;
int _spi_sck;
int _spi_miso;
int _spi_mosi;
uint8_t _spi_freq_mhz;
eth_phy_type_t _type;
eth_clock_mode_t _clock_mode;
bool _use_mac_from_efuse;
bool _useSpi = false;
#ifndef NUKI_HUB_UPDATER
char _ca[TLS_CA_MAX_SIZE] = {0};
char _cert[TLS_CERT_MAX_SIZE] = {0};
char _key[TLS_KEY_MAX_SIZE] = {0};
#endif
};

View File

@@ -5,7 +5,7 @@
IPConfiguration::IPConfiguration(Preferences *preferences)
: _preferences(preferences)
{
if(_preferences->getString(preference_ip_address, "").length() <= 0)
if(!dhcpEnabled() && _preferences->getString(preference_ip_address, "").length() <= 0)
{
Log->println("IP address empty, falling back to DHCP.");
_preferences->putBool(preference_ip_dhcp_enabled, true);

View File

@@ -0,0 +1,20 @@
#pragma once
#ifndef CONFIG_IDF_TARGET_ESP32
typedef enum {
ETH_CLOCK_GPIO0_IN = 0,
ETH_CLOCK_GPIO16_OUT = 2,
ETH_CLOCK_GPIO17_OUT = 3
} eth_clock_mode_t;
#define ETH_PHY_TYPE_LAN8720 ETH_PHY_MAX
#else
#define ETH_PHY_TYPE_LAN8720 ETH_PHY_LAN8720
#endif
#define ETH_CLK_MODE_LAN8720 ETH_CLOCK_GPIO0_IN
#define ETH_PHY_ADDR_LAN8720 0
#define ETH_PHY_MDC_LAN8720 23
#define ETH_PHY_MDIO_LAN8720 18
#define ETH_PHY_POWER_LAN8720 -1
#define ETH_RESET_PIN_LAN8720 1

View File

@@ -0,0 +1,22 @@
#pragma once
#include <hal/spi_types.h>
#define ETH_PHY_TYPE_W5500 ETH_PHY_W5500
#define ETH_PHY_ADDR_W5500 1
#define ETH_PHY_IRQ_M5_W5500 -1
#define ETH_PHY_RST_M5_W5500 -1
#define ETH_PHY_CS_M5_W5500 19
#define ETH_PHY_SPI_SCK_M5_W5500 22
#define ETH_PHY_SPI_MISO_M5_W5500 23
#define ETH_PHY_SPI_MOSI_M5_W5500 33
#define ETH_PHY_CS_M5_W5500_S3 6
#define ETH_PHY_SPI_SCK_M5_W5500_S3 5
#define ETH_PHY_SPI_MISO_M5_W5500_S3 7
#define ETH_PHY_SPI_MOSI_M5_W5500_S3 8
#define ETH_PHY_IRQ_GENERIC_W5500 3
#define ETH_PHY_RST_GENERIC_W5500 4
#define ETH_PHY_CS_GENERIC_W5500 5
#define ETH_PHY_SPI_SCK_GENERIC_W5500 8
#define ETH_PHY_SPI_MISO_GENERIC_W5500 9
#define ETH_PHY_SPI_MOSI_GENERIC_W5500 10

View File

@@ -1,238 +0,0 @@
#include <Arduino.h>
#include <WiFi.h>
#include "W5500Device.h"
#include "../PreferencesKeys.h"
#include "../Logger.h"
#ifndef NUKI_HUB_UPDATER
#include "../MqttTopics.h"
#endif
#include "sdkconfig.h"
W5500Device::W5500Device(const String &hostname, Preferences* preferences, const IPConfiguration* ipConfiguration, int variant)
: NetworkDevice(hostname, ipConfiguration),
_preferences(preferences),
_variant((W5500Variant)variant)
{
initializeMacAddress(_mac);
Log->print("MAC Adress: ");
for(int i=0; i < 6; i++)
{
if(_mac[i] < 10)
{
Log->print(F("0"));
}
Log->print(_mac[i], 16);
if(i < 5)
{
Log->print(F(":"));
}
}
Log->println();
#ifndef NUKI_HUB_UPDATER
_mqttClient = new espMqttClientW5500();
#endif
}
W5500Device::~W5500Device()
{}
const String W5500Device::deviceName() const
{
return "Wiznet W5500";
}
void W5500Device::initialize()
{
WiFi.mode(WIFI_STA);
resetDevice();
switch(_variant)
{
case W5500Variant::M5StackAtomPoe:
_resetPin = -1;
#if defined(CONFIG_IDF_TARGET_ESP32S3)
Ethernet.init(6, 5, 7, 8);
#else
Ethernet.init(19, 22, 23, 33);
#endif
break;
default:
_resetPin = -1;
Ethernet.init(5);
break;
}
#ifndef NUKI_HUB_UPDATER
if(_preferences->getBool(preference_mqtt_log_enabled, false) || _preferences->getBool(preference_webserial_enabled, false))
{
MqttLoggerMode mode;
if(_preferences->getBool(preference_mqtt_log_enabled, false) && _preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::MqttAndSerialAndWeb;
else if (_preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::SerialAndWeb;
else mode = MqttLoggerMode::MqttAndSerial;
String pathStr = _preferences->getString(preference_mqtt_lock_path);
pathStr.concat(mqtt_topic_log);
_path = new char[pathStr.length() + 1];
memset(_path, 0, sizeof(_path));
strcpy(_path, pathStr.c_str());
Log = new MqttLogger(*getMqttClient(), _path, mode);
}
#endif
reconnect();
}
ReconnectStatus W5500Device::reconnect(bool force)
{
_hasDHCPAddress = false;
// start the Ethernet connection:
Log->println(F("Initialize Ethernet with DHCP:"));
int dhcpRetryCnt = 0;
bool hardwareFound = false;
while(dhcpRetryCnt < 3)
{
Log->print(F("DHCP connect try #"));
Log->print(dhcpRetryCnt);
Log->println();
dhcpRetryCnt++;
if (Ethernet.begin(_mac, 1000, 1000) == 0)
{
Log->println(F("Failed to configure Ethernet using DHCP"));
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware)
{
Log->println(F("Ethernet module not found"));
continue;
}
if (Ethernet.linkStatus() == LinkOFF)
{
Log->println(F("Ethernet cable is not connected."));
}
hardwareFound = true;
IPAddress ip;
ip.fromString("192.168.4.1");
IPAddress subnet;
subnet.fromString("255.255.255.0");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(_mac, ip);
Ethernet.setSubnetMask(subnet);
delay(1000);
}
else
{
hardwareFound = true;
_hasDHCPAddress = true;
dhcpRetryCnt = 1000;
if(_ipConfiguration->dhcpEnabled())
{
Log->print(F(" DHCP assigned IP "));
Log->println(Ethernet.localIP());
}
}
if(!_ipConfiguration->dhcpEnabled())
{
Ethernet.setLocalIP(_ipConfiguration->ipAddress());
Ethernet.setSubnetMask(_ipConfiguration->subnet());
Ethernet.setGatewayIP(_ipConfiguration->defaultGateway());
Ethernet.setDnsServerIP(_ipConfiguration->dnsServer());
}
}
if(!hardwareFound)
{
return ReconnectStatus::CriticalFailure;
}
return _hasDHCPAddress ? ReconnectStatus::Success : ReconnectStatus::Failure;
}
void W5500Device::reconfigure()
{
Log->println(F("Reconfigure W5500 not implemented."));
}
void W5500Device::resetDevice()
{
if(_resetPin == -1) return;
Log->println(F("Resetting network hardware."));
pinMode(_resetPin, OUTPUT);
digitalWrite(_resetPin, HIGH);
delay(50);
digitalWrite(_resetPin, LOW);
delay(50);
digitalWrite(_resetPin, HIGH);
delay(50);
}
bool W5500Device::supportsEncryption()
{
return false;
}
bool W5500Device::isConnected()
{
return (Ethernet.linkStatus() == EthernetLinkStatus::LinkON && _maintainResult == 0 && _hasDHCPAddress);
}
void W5500Device::initializeMacAddress(byte *mac)
{
memset(mac, 0, 6);
mac[0] = 0x00; // wiznet prefix
mac[1] = 0x08; // wiznet prefix
mac[2] = 0xDC; // wiznet prefix
if(_preferences->getBool(preference_has_mac_saved, false))
{
mac[3] = _preferences->getChar(preference_has_mac_byte_0);
mac[4] = _preferences->getChar(preference_has_mac_byte_1);
mac[5] = _preferences->getChar(preference_has_mac_byte_2);
}
else
{
mac[3] = random(0,255);
mac[4] = random(0,255);
mac[5] = random(0,255);
_preferences->putChar(preference_has_mac_byte_0, mac[3]);
_preferences->putChar(preference_has_mac_byte_1, mac[4]);
_preferences->putChar(preference_has_mac_byte_2, mac[5]);
_preferences->putBool(preference_has_mac_saved, true);
}
}
void W5500Device::update()
{
_maintainResult = Ethernet.maintain();
NetworkDevice::update();
}
int8_t W5500Device::signalStrength()
{
return 127;
}
String W5500Device::localIP()
{
return Ethernet.localIP().toString();
}
String W5500Device::BSSIDstr()
{
return "";
}

View File

@@ -1,53 +0,0 @@
#pragma once
#include "NetworkDevice.h"
#ifndef NUKI_HUB_UPDATER
#include "espMqttClient.h"
#include "espMqttClientW5500.h"
#endif
#include <Ethernet.h>
#include <Preferences.h>
enum class W5500Variant
{
Generic = 2,
M5StackAtomPoe = 3
};
class W5500Device : public NetworkDevice
{
public:
explicit W5500Device(const String& hostname, Preferences* _preferences, const IPConfiguration* ipConfiguration, int variant);
~W5500Device();
const String deviceName() const override;
virtual void initialize();
virtual ReconnectStatus reconnect(bool force = false);
virtual void reconfigure();
bool supportsEncryption() override;
virtual void update() override;
virtual bool isConnected();
int8_t signalStrength() override;
String localIP() override;
String BSSIDstr() override;
private:
void resetDevice();
void initializeMacAddress(byte* mac);
Preferences* _preferences = nullptr;
int _maintainResult = 0;
int _resetPin = -1;
bool _hasDHCPAddress = false;
char* _path;
W5500Variant _variant;
byte _mac[6];
};

View File

@@ -72,10 +72,11 @@ const String WifiDevice::deviceName() const
void WifiDevice::initialize()
{
_wifiFallbackDisabled = _preferences->getBool(preference_network_wifi_fallback_disabled, false);
std::vector<const char *> wm_menu;
wm_menu.push_back("wifi");
wm_menu.push_back("exit");
_wm.setEnableConfigPortal(_startAp || !_preferences->getBool(preference_network_wifi_fallback_disabled, false));
_wm.setEnableConfigPortal(_startAp || !_wifiFallbackDisabled);
// reduced timeout if ESP is set to restart on disconnect
_wm.setFindBestRSSI(_preferences->getBool(preference_find_best_rssi));
_wm.setConnectTimeout(20);
@@ -92,11 +93,13 @@ void WifiDevice::initialize()
_wm.setAPCallback(clearRtcInitVar);
bool res = false;
bool connectedFromPortal = false;
if(_startAp)
{
Log->println(F("Opening Wi-Fi configuration portal."));
res = _wm.startConfigPortal();
connectedFromPortal = true;
}
else
{
@@ -116,6 +119,13 @@ void WifiDevice::initialize()
else {
Log->print(F("Wi-Fi connected: "));
Log->println(WiFi.localIP().toString());
if(connectedFromPortal)
{
Log->println(F("Connected using WifiManager portal. Wait for ESP restart."));
delay(1000);
restartEsp(RestartReason::ConfigurationUpdated);
}
}
WiFi.onEvent([&](WiFiEvent_t event, WiFiEventInfo_t info)
@@ -167,14 +177,14 @@ ReconnectStatus WifiDevice::reconnect(bool force)
_isReconnecting = false;
}
if(!isConnected() && _disconnectTs > (esp_timer_get_time() / 1000) - 120000) _wm.setEnableConfigPortal(_startAp || !_preferences->getBool(preference_network_wifi_fallback_disabled, false));
if(!isConnected() && _disconnectTs > (esp_timer_get_time() / 1000) - 120000) _wm.setEnableConfigPortal(_startAp || !_wifiFallbackDisabled);
return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure;
}
void WifiDevice::onConnected()
{
_isReconnecting = false;
_wm.setEnableConfigPortal(_startAp || !_preferences->getBool(preference_network_wifi_fallback_disabled, false));
_wm.setEnableConfigPortal(_startAp || !_wifiFallbackDisabled);
}
void WifiDevice::onDisconnected()

View File

@@ -41,6 +41,7 @@ private:
bool _restartOnDisconnect = false;
bool _startAp = false;
bool _isReconnecting = false;
bool _wifiFallbackDisabled = false;
char* _path;
int64_t _disconnectTs = 0;

View File

@@ -1,13 +0,0 @@
#include "espMqttClientW5500.h"
espMqttClientW5500::espMqttClientW5500()
: espMqttClient(espMqttClientTypes::UseInternalTask::NO),
_client()
{
_transport = &_client;
}
void espMqttClientW5500::update()
{
loop();
}

View File

@@ -1,22 +0,0 @@
#pragma once
#include "espMqttClient.h"
#include "ClientSyncW5500.h"
class espMqttClientW5500 : public espMqttClient {
public:
#if defined(ARDUINO_ARCH_ESP32)
explicit espMqttClientW5500();
#else
espMqttClient();
#endif
void update();
protected:
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
espMqttClientInternals::ClientSyncW5500 _client;
#elif defined(__linux__)
espMqttClientInternals::ClientPosix _client;
#endif
};