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: a2cc7be221adca01Author: 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: 153000b5e84b944aAuthor: 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: f1964917063fbab6Author: 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: 50a2eb134af90cbcAuthor: 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:
@@ -2,5 +2,8 @@
|
||||
# without default 'CMakeLists.txt' file.
|
||||
|
||||
FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)
|
||||
|
||||
if(DEFINED NUKI_TARGET_H2)
|
||||
list(REMOVE_ITEM app_sources "${CMAKE_SOURCE_DIR}/src/networkDevices/WifiDevice.cpp")
|
||||
list(REMOVE_ITEM app_sources "${CMAKE_SOURCE_DIR}/src/networkDevices/WifiDevice.h")
|
||||
endif()
|
||||
idf_component_register(SRCS ${app_sources})
|
||||
|
||||
15
src/Config.h
15
src/Config.h
@@ -4,7 +4,7 @@
|
||||
|
||||
#define NUKI_HUB_VERSION "9.01"
|
||||
#define NUKI_HUB_BUILD "unknownbuildnr"
|
||||
#define NUKI_HUB_DATE "2024-08-12"
|
||||
#define NUKI_HUB_DATE "unknownbuilddate"
|
||||
|
||||
#define GITHUB_LATEST_RELEASE_URL (char*)"https://github.com/technyon/nuki_hub/releases/latest"
|
||||
#define GITHUB_OTA_MANIFEST_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/manifest.json"
|
||||
@@ -48,6 +48,19 @@
|
||||
#define GITHUB_BETA_UPDATER_BINARY_URL_DBG (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/debug/beta/nuki_hub_updater_esp32c6.bin"
|
||||
#define GITHUB_MASTER_RELEASE_BINARY_URL_DBG (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/debug/master/nuki_hub_esp32c6.bin"
|
||||
#define GITHUB_MASTER_UPDATER_BINARY_URL_DBG (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/debug/master/nuki_hub_updater_esp32c6.bin"
|
||||
#elif defined(CONFIG_IDF_TARGET_ESP32H2)
|
||||
#define GITHUB_LATEST_RELEASE_BINARY_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/nuki_hub_esp32h2.bin"
|
||||
#define GITHUB_LATEST_UPDATER_BINARY_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/nuki_hub_updater_esp32h2.bin"
|
||||
#define GITHUB_BETA_RELEASE_BINARY_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/beta/nuki_hub_esp32h2.bin"
|
||||
#define GITHUB_BETA_UPDATER_BINARY_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/beta/nuki_hub_updater_esp32h2.bin"
|
||||
#define GITHUB_MASTER_RELEASE_BINARY_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/master/nuki_hub_esp32h2.bin"
|
||||
#define GITHUB_MASTER_UPDATER_BINARY_URL (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/master/nuki_hub_updater_esp32h2.bin"
|
||||
#define GITHUB_LATEST_RELEASE_BINARY_URL_DBG (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/debug/nuki_hub_esp32h2.bin"
|
||||
#define GITHUB_LATEST_UPDATER_BINARY_URL_DBG (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/debug/nuki_hub_updater_esp32h2.bin"
|
||||
#define GITHUB_BETA_RELEASE_BINARY_URL_DBG (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/debug/beta/nuki_hub_esp32h2.bin"
|
||||
#define GITHUB_BETA_UPDATER_BINARY_URL_DBG (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/debug/beta/nuki_hub_updater_esp32h2.bin"
|
||||
#define GITHUB_MASTER_RELEASE_BINARY_URL_DBG (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/debug/master/nuki_hub_esp32h2.bin"
|
||||
#define GITHUB_MASTER_UPDATER_BINARY_URL_DBG (char*)"https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/debug/master/nuki_hub_updater_esp32h2.bin"
|
||||
#else
|
||||
#if defined(CONFIG_FREERTOS_UNICORE)
|
||||
#define GITHUB_LATEST_RELEASE_BINARY_URL "https://raw.githubusercontent.com/technyon/nuki_hub/binary/ota/nuki_hub_esp32-solo1.bin"
|
||||
|
||||
148
src/Gpio.cpp
148
src/Gpio.cpp
@@ -6,6 +6,9 @@
|
||||
#include "PreferencesKeys.h"
|
||||
#include "RestartReason.h"
|
||||
#include "Gpio2Go.h"
|
||||
#include "networkDevices/LAN8720Definitions.h"
|
||||
#include "networkDevices/DM9051Definitions.h"
|
||||
#include "networkDevices/W5500Definitions.h"
|
||||
|
||||
Gpio* Gpio::_inst = nullptr;
|
||||
int64_t Gpio::_debounceTs = 0;
|
||||
@@ -97,6 +100,8 @@ void Gpio::init()
|
||||
case PinRole::GeneralInputPullUp:
|
||||
Gpio2Go::configurePin(entry.pin, PinMode::InputPullup, InterruptMode::Change, 300);
|
||||
break;
|
||||
case PinRole::Ethernet:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -112,6 +117,7 @@ const std::vector<uint8_t>& Gpio::availablePins() const
|
||||
|
||||
void Gpio::loadPinConfiguration()
|
||||
{
|
||||
Log->println("Load GPIO configuration");
|
||||
size_t storedLength = _preferences->getBytesLength(preference_gpio_configuration);
|
||||
if(storedLength == 0)
|
||||
{
|
||||
@@ -133,32 +139,162 @@ void Gpio::loadPinConfiguration()
|
||||
_pinConfiguration.clear();
|
||||
_pinConfiguration.reserve(numEntries);
|
||||
|
||||
std::vector<int> disabledPins = getDisabledPins();
|
||||
|
||||
for(int i=0; i < numEntries; i++)
|
||||
{
|
||||
PinEntry entry;
|
||||
entry.pin = serialized[i * 2];
|
||||
entry.role = (PinRole) serialized[(i * 2 + 1)];
|
||||
if(entry.role != PinRole::Disabled)
|
||||
Log->print(F("Pin "));
|
||||
Log->println(entry.pin);
|
||||
|
||||
if(std::find(disabledPins.begin(), disabledPins.end(), entry.pin) == disabledPins.end())
|
||||
{
|
||||
_pinConfiguration.push_back(entry);
|
||||
if(entry.role == PinRole::Ethernet) entry.role = PinRole::Disabled;
|
||||
entry.role = (PinRole) serialized[(i * 2 + 1)];
|
||||
Log->println("Not found in Ethernet disabled pins");
|
||||
Log->print(F("Role: "));
|
||||
Log->println(getRoleDescription(entry.role));
|
||||
}
|
||||
else
|
||||
{
|
||||
entry.role = PinRole::Ethernet;
|
||||
Log->println("Found in Ethernet disabled pins");
|
||||
Log->print(F("Role: "));
|
||||
Log->println(getRoleDescription(entry.role));
|
||||
}
|
||||
if(entry.role != PinRole::Disabled) _pinConfiguration.push_back(entry);
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<int> Gpio::getDisabledPins() const
|
||||
{
|
||||
std::vector<int> disabledPins;
|
||||
|
||||
switch(_preferences->getInt(preference_network_hardware, 0))
|
||||
{
|
||||
case 2:
|
||||
disabledPins.push_back(ETH_PHY_CS_GENERIC_W5500);
|
||||
disabledPins.push_back(ETH_PHY_IRQ_GENERIC_W5500);
|
||||
disabledPins.push_back(ETH_PHY_RST_GENERIC_W5500);
|
||||
disabledPins.push_back(ETH_PHY_SPI_SCK_GENERIC_W5500);
|
||||
disabledPins.push_back(ETH_PHY_SPI_MISO_GENERIC_W5500);
|
||||
disabledPins.push_back(ETH_PHY_SPI_MOSI_GENERIC_W5500);
|
||||
break;
|
||||
case 3:
|
||||
disabledPins.push_back(ETH_PHY_CS_M5_W5500);
|
||||
disabledPins.push_back(ETH_PHY_IRQ_M5_W5500);
|
||||
disabledPins.push_back(ETH_PHY_RST_M5_W5500);
|
||||
disabledPins.push_back(ETH_PHY_SPI_SCK_M5_W5500);
|
||||
disabledPins.push_back(ETH_PHY_SPI_MISO_M5_W5500);
|
||||
disabledPins.push_back(ETH_PHY_SPI_MOSI_M5_W5500);
|
||||
break;
|
||||
case 10:
|
||||
disabledPins.push_back(ETH_PHY_CS_M5_W5500_S3);
|
||||
disabledPins.push_back(ETH_PHY_IRQ_M5_W5500);
|
||||
disabledPins.push_back(ETH_PHY_RST_M5_W5500);
|
||||
disabledPins.push_back(ETH_PHY_SPI_SCK_M5_W5500_S3);
|
||||
disabledPins.push_back(ETH_PHY_SPI_MISO_M5_W5500_S3);
|
||||
disabledPins.push_back(ETH_PHY_SPI_MOSI_M5_W5500_S3);
|
||||
break;
|
||||
case 9:
|
||||
disabledPins.push_back(ETH_PHY_CS_ETH01EVO);
|
||||
disabledPins.push_back(ETH_PHY_IRQ_ETH01EVO);
|
||||
disabledPins.push_back(ETH_PHY_RST_ETH01EVO);
|
||||
disabledPins.push_back(ETH_PHY_SPI_SCK_ETH01EVO);
|
||||
disabledPins.push_back(ETH_PHY_SPI_MISO_ETH01EVO);
|
||||
disabledPins.push_back(ETH_PHY_SPI_MOSI_ETH01EVO);
|
||||
break;
|
||||
case 6:
|
||||
disabledPins.push_back(ETH_PHY_CS_M5_W5500);
|
||||
disabledPins.push_back(ETH_PHY_IRQ_M5_W5500);
|
||||
disabledPins.push_back(ETH_PHY_RST_M5_W5500);
|
||||
disabledPins.push_back(ETH_PHY_SPI_SCK_M5_W5500);
|
||||
disabledPins.push_back(ETH_PHY_SPI_MISO_M5_W5500);
|
||||
disabledPins.push_back(ETH_PHY_SPI_MOSI_M5_W5500);
|
||||
break;
|
||||
case 11:
|
||||
disabledPins.push_back(_preferences->getInt(preference_network_custom_cs, -1));
|
||||
disabledPins.push_back(_preferences->getInt(preference_network_custom_irq, -1));
|
||||
disabledPins.push_back(_preferences->getInt(preference_network_custom_rst, -1));
|
||||
disabledPins.push_back(_preferences->getInt(preference_network_custom_sck, -1));
|
||||
disabledPins.push_back(_preferences->getInt(preference_network_custom_miso, -1));
|
||||
disabledPins.push_back(_preferences->getInt(preference_network_custom_mosi, -1));
|
||||
disabledPins.push_back(_preferences->getInt(preference_network_custom_pwr, -1));
|
||||
disabledPins.push_back(_preferences->getInt(preference_network_custom_mdc, -1));
|
||||
disabledPins.push_back(_preferences->getInt(preference_network_custom_mdio, -1));
|
||||
break;
|
||||
#if defined(CONFIG_IDF_TARGET_ESP32)
|
||||
case 4:
|
||||
disabledPins.push_back(12);
|
||||
disabledPins.push_back(ETH_RESET_PIN_LAN8720);
|
||||
disabledPins.push_back(ETH_PHY_MDC_LAN8720);
|
||||
disabledPins.push_back(ETH_PHY_MDIO_LAN8720);
|
||||
break;
|
||||
case 5:
|
||||
disabledPins.push_back(16);
|
||||
disabledPins.push_back(ETH_RESET_PIN_LAN8720);
|
||||
disabledPins.push_back(ETH_PHY_MDC_LAN8720);
|
||||
disabledPins.push_back(ETH_PHY_MDIO_LAN8720);
|
||||
break;
|
||||
case 8:
|
||||
disabledPins.push_back(5);
|
||||
disabledPins.push_back(ETH_RESET_PIN_LAN8720);
|
||||
disabledPins.push_back(ETH_PHY_MDC_LAN8720);
|
||||
disabledPins.push_back(ETH_PHY_MDIO_LAN8720);
|
||||
break;
|
||||
case 7:
|
||||
disabledPins.push_back(-1);
|
||||
disabledPins.push_back(ETH_RESET_PIN_LAN8720);
|
||||
disabledPins.push_back(ETH_PHY_MDC_LAN8720);
|
||||
disabledPins.push_back(ETH_PHY_MDIO_LAN8720);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Log->print(F("GPIO Ethernet disabled pins:"));
|
||||
for_each_n(disabledPins.begin(), disabledPins.size(),
|
||||
[](int x) { Log->print(" "); Log->print(x); });
|
||||
Log->println();
|
||||
return disabledPins;
|
||||
}
|
||||
|
||||
void Gpio::savePinConfiguration(const std::vector<PinEntry> &pinConfiguration)
|
||||
{
|
||||
Log->println("Save GPIO configuration");
|
||||
int8_t serialized[std::max(pinConfiguration.size() * 2, _preferences->getBytesLength(preference_gpio_configuration))];
|
||||
memset(serialized, 0, sizeof(serialized));
|
||||
|
||||
std::vector<int> disabledPins = getDisabledPins();
|
||||
|
||||
int len = pinConfiguration.size();
|
||||
for(int i=0; i < len; i++)
|
||||
{
|
||||
const auto& entry = pinConfiguration[i];
|
||||
Log->print(F("Pin "));
|
||||
Log->println(entry.pin);
|
||||
|
||||
if(entry.role != PinRole::Disabled)
|
||||
if(std::find(disabledPins.begin(), disabledPins.end(), entry.pin) != disabledPins.end())
|
||||
{
|
||||
serialized[i * 2] = entry.pin;
|
||||
serialized[i * 2 + 1] = (int8_t) entry.role;
|
||||
serialized[i * 2 + 1] = (int8_t)PinRole::Ethernet;
|
||||
Log->println("Found in Ethernet disabled pins");
|
||||
Log->print(F("Role: "));
|
||||
Log->println(getRoleDescription(PinRole::Ethernet));
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if(entry.role != PinRole::Disabled && entry.role != PinRole::Ethernet)
|
||||
{
|
||||
serialized[i * 2] = entry.pin;
|
||||
serialized[i * 2 + 1] = (int8_t) entry.role;
|
||||
Log->println("Not found in Ethernet disabled pins");
|
||||
Log->print(F("Role: "));
|
||||
Log->println(getRoleDescription(entry.role));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,6 +364,8 @@ String Gpio::getRoleDescription(PinRole role) const
|
||||
return "General input (Pull-down)";
|
||||
case PinRole::GeneralInputPullUp:
|
||||
return "General input (Pull-up)";
|
||||
case PinRole::Ethernet:
|
||||
return "Ethernet";
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
22
src/Gpio.h
22
src/Gpio.h
@@ -26,7 +26,8 @@ enum class PinRole
|
||||
OutputHighRtoOrCmActive,
|
||||
GeneralOutput,
|
||||
GeneralInputPullDown,
|
||||
GeneralInputPullUp
|
||||
GeneralInputPullUp,
|
||||
Ethernet
|
||||
};
|
||||
|
||||
enum class GpioAction
|
||||
@@ -66,6 +67,7 @@ public:
|
||||
|
||||
const std::vector<uint8_t>& availablePins() const;
|
||||
const std::vector<PinEntry>& pinConfiguration() const;
|
||||
const std::vector<int> getDisabledPins() const;
|
||||
const PinRole getPinRole(const int& pin) const;
|
||||
|
||||
String getRoleDescription(PinRole role) const;
|
||||
@@ -79,7 +81,22 @@ private:
|
||||
void IRAM_ATTR notify(const GpioAction& action, const int& pin);
|
||||
static void inputCallback(const int & pin);
|
||||
|
||||
#if defined(CONFIG_IDF_TARGET_ESP32C3)
|
||||
//Based on https://docs.espressif.com/projects/esp-idf/en/stable/esp32c3/api-reference/peripherals/gpio.html and https://www.espressif.com/sites/default/files/documentation/esp32-c3_datasheet_en.pdf
|
||||
const std::vector<uint8_t> _availablePins = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 18, 19, 20, 21 };
|
||||
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
|
||||
//Based on https://github.com/atomic14/esp32-s3-pinouts?tab=readme-ov-file and https://docs.espressif.com/projects/esp-idf/en/v5.3/esp32s3/api-reference/peripherals/gpio.html and https://www.espressif.com/sites/default/files/documentation/esp32-s3_datasheet_en.pdf
|
||||
const std::vector<uint8_t> _availablePins = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48 };
|
||||
#elif defined(CONFIG_IDF_TARGET_ESP32C6)
|
||||
//Based on https://docs.espressif.com/projects/esp-idf/en/v5.3/esp32c6/api-reference/peripherals/gpio.html and https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf
|
||||
const std::vector<uint8_t> _availablePins = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23 };
|
||||
#elif defined(CONFIG_IDF_TARGET_ESP32H2)
|
||||
//Based on https://docs.espressif.com/projects/esp-idf/en/v5.3/esp32h2/api-reference/peripherals/gpio.html and https://www.espressif.com/sites/default/files/documentation/esp32-h2_datasheet_en.pdf
|
||||
const std::vector<uint8_t> _availablePins = { 0, 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 14, 22, 23, 24, 25, 26, 27 };
|
||||
#else
|
||||
//Based on https://randomnerdtutorials.com/esp32-pinout-reference-gpios/
|
||||
const std::vector<uint8_t> _availablePins = { 2, 4, 5, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 32, 33 };
|
||||
#endif
|
||||
const std::vector<PinRole> _allRoles =
|
||||
{
|
||||
PinRole::Disabled,
|
||||
@@ -101,7 +118,8 @@ private:
|
||||
PinRole::OutputHighRtoOrCmActive,
|
||||
PinRole::GeneralInputPullDown,
|
||||
PinRole::GeneralInputPullUp,
|
||||
PinRole::GeneralOutput
|
||||
PinRole::GeneralOutput,
|
||||
PinRole::Ethernet
|
||||
};
|
||||
|
||||
std::vector<PinEntry> _pinConfiguration;
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
#include "NukiNetwork.h"
|
||||
#include "PreferencesKeys.h"
|
||||
#include "networkDevices/W5500Device.h"
|
||||
#include "networkDevices/WifiDevice.h"
|
||||
#include "Logger.h"
|
||||
#include "Config.h"
|
||||
#include "RestartReason.h"
|
||||
#include <HTTPClient.h>
|
||||
#include <NetworkClientSecure.h>
|
||||
#if defined(CONFIG_IDF_TARGET_ESP32)
|
||||
#include "networkDevices/EthLan8720Device.h"
|
||||
#ifndef CONFIG_IDF_TARGET_ESP32H2
|
||||
#include "networkDevices/WifiDevice.h"
|
||||
#endif
|
||||
#include "networkDevices/EthernetDevice.h"
|
||||
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
#include <ArduinoJson.h>
|
||||
@@ -18,6 +17,9 @@
|
||||
NukiNetwork* NukiNetwork::_inst = nullptr;
|
||||
|
||||
RTC_NOINIT_ATTR char WiFi_fallbackDetect[14];
|
||||
extern bool forceEnableWebServer;
|
||||
extern const uint8_t x509_crt_imported_bundle_bin_start[] asm("_binary_x509_crt_bundle_start");
|
||||
extern const uint8_t x509_crt_imported_bundle_bin_end[] asm("_binary_x509_crt_bundle_end");
|
||||
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
NukiNetwork::NukiNetwork(Preferences *preferences, PresenceDetection* presenceDetection, Gpio* gpio, const String& maintenancePathPrefix, char* buffer, size_t bufferSize)
|
||||
@@ -39,6 +41,7 @@ NukiNetwork::NukiNetwork(Preferences *preferences)
|
||||
|
||||
_inst = this;
|
||||
_hostname = _preferences->getString(preference_hostname);
|
||||
_webEnabled = _preferences->getBool(preference_webserver_enabled, true);
|
||||
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
memset(_maintenancePathPrefix, 0, sizeof(_maintenancePathPrefix));
|
||||
@@ -65,20 +68,34 @@ NukiNetwork::NukiNetwork(Preferences *preferences)
|
||||
void NukiNetwork::setupDevice()
|
||||
{
|
||||
_ipConfiguration = new IPConfiguration(_preferences);
|
||||
int hardwareDetect = _preferences->getInt(preference_network_hardware, 0);
|
||||
Log->print(F("Hardware detect : "));
|
||||
Log->println(hardwareDetect);
|
||||
|
||||
int hardwareDetect = _preferences->getInt(preference_network_hardware);
|
||||
|
||||
Log->print(F("Hardware detect : ")); Log->println(hardwareDetect);
|
||||
_firstBootAfterDeviceChange = _preferences->getBool(preference_ntw_reconfigure, false);
|
||||
|
||||
if(hardwareDetect == 0)
|
||||
{
|
||||
#ifndef CONFIG_IDF_TARGET_ESP32H2
|
||||
hardwareDetect = 1;
|
||||
#else
|
||||
hardwareDetect = 11;
|
||||
_preferences->putInt(preference_network_custom_addr, 1);
|
||||
_preferences->putInt(preference_network_custom_cs, 8);
|
||||
_preferences->putInt(preference_network_custom_irq, 9);
|
||||
_preferences->putInt(preference_network_custom_rst, 10);
|
||||
_preferences->putInt(preference_network_custom_sck, 11);
|
||||
_preferences->putInt(preference_network_custom_miso, 12);
|
||||
_preferences->putInt(preference_network_custom_mosi, 13);
|
||||
_preferences->putBool(preference_ntw_reconfigure, true);
|
||||
#endif
|
||||
_preferences->putInt(preference_network_hardware, hardwareDetect);
|
||||
}
|
||||
|
||||
if(strcmp(WiFi_fallbackDetect, "wifi_fallback") == 0)
|
||||
{
|
||||
if(_preferences->getBool(preference_network_wifi_fallback_disabled))
|
||||
#ifndef CONFIG_IDF_TARGET_ESP32H2
|
||||
if(_preferences->getBool(preference_network_wifi_fallback_disabled) && !_firstBootAfterDeviceChange)
|
||||
{
|
||||
Log->println(F("Failed to connect to network. Wi-Fi fallback is disabled, rebooting."));
|
||||
memset(WiFi_fallbackDetect, 0, sizeof(WiFi_fallbackDetect));
|
||||
@@ -88,6 +105,14 @@ void NukiNetwork::setupDevice()
|
||||
|
||||
Log->println(F("Switching to Wi-Fi device as fallback."));
|
||||
_networkDeviceType = NetworkDeviceType::WiFi;
|
||||
#else
|
||||
int custEth = _preferences->getInt(preference_network_custom_phy, 0);
|
||||
|
||||
if(custEth<3) custEth++;
|
||||
else custEth = 0;
|
||||
_preferences->putInt(preference_network_custom_phy, custEth);
|
||||
_preferences->putBool(preference_ntw_reconfigure, true);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -99,14 +124,13 @@ void NukiNetwork::setupDevice()
|
||||
_networkDeviceType = NetworkDeviceType::WiFi;
|
||||
break;
|
||||
case 2:
|
||||
Log->print(F("Generic W5500"));
|
||||
Log->println(F("Generic W5500"));
|
||||
_networkDeviceType = NetworkDeviceType::W5500;
|
||||
break;
|
||||
case 3:
|
||||
Log->println(F("W5500 on M5Stack Atom POE"));
|
||||
_networkDeviceType = NetworkDeviceType::W5500;
|
||||
_networkDeviceType = NetworkDeviceType::W5500M5;
|
||||
break;
|
||||
#if defined(CONFIG_IDF_TARGET_ESP32)
|
||||
case 4:
|
||||
Log->println(F("Olimex ESP32-POE / ESP-POE-ISO"));
|
||||
_networkDeviceType = NetworkDeviceType::Olimex_LAN8720;
|
||||
@@ -127,7 +151,23 @@ void NukiNetwork::setupDevice()
|
||||
Log->println(F("GL-S10"));
|
||||
_networkDeviceType = NetworkDeviceType::GL_S10;
|
||||
break;
|
||||
#endif
|
||||
case 9:
|
||||
Log->println(F("ETH01-Evo"));
|
||||
_networkDeviceType = NetworkDeviceType::ETH01_Evo;
|
||||
break;
|
||||
case 10:
|
||||
Log->println(F("W5500 on M5Stack Atom POE S3"));
|
||||
_networkDeviceType = NetworkDeviceType::W5500M5S3;
|
||||
break;
|
||||
case 11:
|
||||
Log->println(F("Custom LAN Module"));
|
||||
if(_preferences->getInt(preference_network_custom_phy, 0) > 0) _networkDeviceType = NetworkDeviceType::CUSTOM;
|
||||
else
|
||||
{
|
||||
Log->println(F("Custom LAN Module not setup correctly, falling back to Wi-Fi"));
|
||||
_networkDeviceType = NetworkDeviceType::WiFi;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
Log->println(F("Unknown hardware selected, falling back to Wi-Fi."));
|
||||
_networkDeviceType = NetworkDeviceType::WiFi;
|
||||
@@ -138,31 +178,207 @@ void NukiNetwork::setupDevice()
|
||||
switch (_networkDeviceType)
|
||||
{
|
||||
case NetworkDeviceType::W5500:
|
||||
_device = new W5500Device(_hostname, _preferences, _ipConfiguration, hardwareDetect);
|
||||
_device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "Generic W5500",
|
||||
ETH_PHY_ADDR_W5500,
|
||||
ETH_PHY_CS_GENERIC_W5500,
|
||||
ETH_PHY_IRQ_GENERIC_W5500,
|
||||
ETH_PHY_RST_GENERIC_W5500,
|
||||
ETH_PHY_SPI_SCK_GENERIC_W5500,
|
||||
ETH_PHY_SPI_MISO_GENERIC_W5500,
|
||||
ETH_PHY_SPI_MOSI_GENERIC_W5500,
|
||||
ETH_PHY_SPI_FREQ_MHZ,
|
||||
ETH_PHY_W5500);
|
||||
break;
|
||||
case NetworkDeviceType::W5500M5:
|
||||
_device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "M5Stack Atom POE",
|
||||
ETH_PHY_ADDR_W5500,
|
||||
ETH_PHY_CS_M5_W5500,
|
||||
ETH_PHY_IRQ_M5_W5500,
|
||||
ETH_PHY_RST_M5_W5500,
|
||||
ETH_PHY_SPI_SCK_M5_W5500,
|
||||
ETH_PHY_SPI_MISO_M5_W5500,
|
||||
ETH_PHY_SPI_MOSI_M5_W5500,
|
||||
ETH_PHY_SPI_FREQ_MHZ,
|
||||
ETH_PHY_W5500);
|
||||
break;
|
||||
case NetworkDeviceType::W5500M5S3:
|
||||
_device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "M5Stack Atom POE S3",
|
||||
ETH_PHY_ADDR_W5500,
|
||||
ETH_PHY_CS_M5_W5500_S3,
|
||||
ETH_PHY_IRQ_M5_W5500,
|
||||
ETH_PHY_RST_M5_W5500,
|
||||
ETH_PHY_SPI_SCK_M5_W5500_S3,
|
||||
ETH_PHY_SPI_MISO_M5_W5500_S3,
|
||||
ETH_PHY_SPI_MOSI_M5_W5500_S3,
|
||||
ETH_PHY_SPI_FREQ_MHZ,
|
||||
ETH_PHY_W5500);
|
||||
break;
|
||||
case NetworkDeviceType::ETH01_Evo:
|
||||
_device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "ETH01-Evo",
|
||||
ETH_PHY_ADDR_ETH01EVO,
|
||||
ETH_PHY_CS_ETH01EVO,
|
||||
ETH_PHY_IRQ_ETH01EVO,
|
||||
ETH_PHY_RST_ETH01EVO,
|
||||
ETH_PHY_SPI_SCK_ETH01EVO,
|
||||
ETH_PHY_SPI_MISO_ETH01EVO,
|
||||
ETH_PHY_SPI_MOSI_ETH01EVO,
|
||||
ETH_PHY_SPI_FREQ_MHZ,
|
||||
ETH_PHY_TYPE_DM9051);
|
||||
break;
|
||||
case NetworkDeviceType::M5STACK_PoESP32_Unit:
|
||||
_device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "M5STACK PoESP32 Unit",
|
||||
ETH_PHY_ADDR_W5500,
|
||||
ETH_PHY_CS_M5_W5500,
|
||||
ETH_PHY_IRQ_M5_W5500,
|
||||
ETH_PHY_RST_M5_W5500,
|
||||
ETH_PHY_SPI_SCK_M5_W5500,
|
||||
ETH_PHY_SPI_MISO_M5_W5500,
|
||||
ETH_PHY_SPI_MOSI_M5_W5500,
|
||||
ETH_PHY_SPI_FREQ_MHZ,
|
||||
ETH_PHY_W5500);
|
||||
break;
|
||||
case NetworkDeviceType::CUSTOM:
|
||||
{
|
||||
int custPHY = _preferences->getInt(preference_network_custom_phy, 0);
|
||||
|
||||
if(custPHY >= 1 && custPHY <= 3)
|
||||
{
|
||||
std::string custName;
|
||||
eth_phy_type_t custEthtype;
|
||||
|
||||
switch(custPHY)
|
||||
{
|
||||
case 1:
|
||||
custName = "Custom (W5500)";
|
||||
custEthtype = ETH_PHY_W5500;
|
||||
break;
|
||||
case 2:
|
||||
custName = "Custom (DN9051)";
|
||||
custEthtype = ETH_PHY_DM9051;
|
||||
break;
|
||||
case 3:
|
||||
custName = "Custom (KSZ8851SNL)";
|
||||
custEthtype = ETH_PHY_KSZ8851;
|
||||
break;
|
||||
default:
|
||||
custName = "Custom (W5500)";
|
||||
custEthtype = ETH_PHY_W5500;
|
||||
break;
|
||||
}
|
||||
|
||||
_device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, custName,
|
||||
_preferences->getInt(preference_network_custom_addr, -1),
|
||||
_preferences->getInt(preference_network_custom_cs, -1),
|
||||
_preferences->getInt(preference_network_custom_irq, -1),
|
||||
_preferences->getInt(preference_network_custom_rst, -1),
|
||||
_preferences->getInt(preference_network_custom_sck, -1),
|
||||
_preferences->getInt(preference_network_custom_miso, -1),
|
||||
_preferences->getInt(preference_network_custom_mosi, -1),
|
||||
ETH_PHY_SPI_FREQ_MHZ,
|
||||
custEthtype);
|
||||
}
|
||||
#if defined(CONFIG_IDF_TARGET_ESP32)
|
||||
else if(custPHY >= 4 && custPHY <= 9)
|
||||
{
|
||||
std::string custName;
|
||||
eth_phy_type_t custEthtype;
|
||||
eth_clock_mode_t custCLK;
|
||||
|
||||
switch(custPHY)
|
||||
{
|
||||
case 4:
|
||||
custName = "Custom (LAN8720)";
|
||||
custEthtype = ETH_PHY_TYPE_LAN8720;
|
||||
break;
|
||||
case 5:
|
||||
custName = "Custom (RTL8201)";
|
||||
custEthtype = ETH_PHY_RTL8201;
|
||||
break;
|
||||
case 6:
|
||||
custName = "Custom (TLK110)";
|
||||
custEthtype = ETH_PHY_TLK110;
|
||||
break;
|
||||
case 7:
|
||||
custName = "Custom (DP83848)";
|
||||
custEthtype = ETH_PHY_DP83848;
|
||||
break;
|
||||
case 8:
|
||||
custName = "Custom (KSZ8041)";
|
||||
custEthtype = ETH_PHY_KSZ8041;
|
||||
break;
|
||||
case 9:
|
||||
custName = "Custom (KSZ8081)";
|
||||
custEthtype = ETH_PHY_KSZ8081;
|
||||
break;
|
||||
default:
|
||||
custName = "Custom (LAN8720)";
|
||||
custEthtype = ETH_PHY_TYPE_LAN8720;
|
||||
break;
|
||||
}
|
||||
|
||||
int custCLKpref = _preferences->getInt(preference_network_custom_clk, 0);
|
||||
|
||||
switch(custCLKpref)
|
||||
{
|
||||
case 0:
|
||||
custCLK = ETH_CLOCK_GPIO0_IN;
|
||||
break;
|
||||
case 2:
|
||||
custCLK = ETH_CLOCK_GPIO16_OUT;
|
||||
break;
|
||||
case 3:
|
||||
custCLK = ETH_CLOCK_GPIO17_OUT;
|
||||
break;
|
||||
default:
|
||||
custCLK = ETH_CLOCK_GPIO17_OUT;
|
||||
break;
|
||||
}
|
||||
_device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, custName, _preferences->getInt(preference_network_custom_addr, -1), _preferences->getInt(preference_network_custom_pwr, -1), _preferences->getInt(preference_network_custom_mdc, -1), _preferences->getInt(preference_network_custom_mdio, -1), custEthtype, custCLK);
|
||||
}
|
||||
#endif
|
||||
#ifndef CONFIG_IDF_TARGET_ESP32H2
|
||||
else
|
||||
{
|
||||
_device = new WifiDevice(_hostname, _preferences, _ipConfiguration);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
#if defined(CONFIG_IDF_TARGET_ESP32)
|
||||
case NetworkDeviceType::Olimex_LAN8720:
|
||||
_device = new EthLan8720Device(_hostname, _preferences, _ipConfiguration, "Olimex (LAN8720)", ETH_PHY_ADDR, 12, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLOCK_GPIO17_OUT);
|
||||
_device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "Olimex (LAN8720)", ETH_PHY_ADDR_LAN8720, 12, ETH_PHY_MDC_LAN8720, ETH_PHY_MDIO_LAN8720, ETH_PHY_TYPE_LAN8720, ETH_CLOCK_GPIO17_OUT);
|
||||
break;
|
||||
case NetworkDeviceType::WT32_LAN8720:
|
||||
_device = new EthLan8720Device(_hostname, _preferences, _ipConfiguration, "WT32-ETH01", 1, 16);
|
||||
break;
|
||||
case NetworkDeviceType::M5STACK_PoESP32_Unit:
|
||||
_device = new EthLan8720Device(_hostname, _preferences, _ipConfiguration, "M5STACK PoESP32 Unit", 1, 5, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_IP101);
|
||||
_device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "WT32-ETH01", 1, 16);
|
||||
break;
|
||||
case NetworkDeviceType::GL_S10:
|
||||
_device = new EthLan8720Device(_hostname, _preferences, _ipConfiguration, "GL-S10", 1, 5, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_IP101, ETH_CLOCK_GPIO0_IN);
|
||||
_device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "GL-S10", 1, 5, ETH_PHY_MDC_LAN8720, ETH_PHY_MDIO_LAN8720, ETH_PHY_IP101, ETH_CLOCK_GPIO0_IN);
|
||||
break;
|
||||
case NetworkDeviceType::LilyGO_T_ETH_POE:
|
||||
_device = new EthLan8720Device(_hostname, _preferences, _ipConfiguration, "LilyGO T-ETH-POE", 0, -1, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLOCK_GPIO17_OUT);
|
||||
_device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "LilyGO T-ETH-POE", 0, -1, ETH_PHY_MDC_LAN8720, ETH_PHY_MDIO_LAN8720, ETH_PHY_TYPE_LAN8720, ETH_CLOCK_GPIO17_OUT);
|
||||
break;
|
||||
#endif
|
||||
#ifndef CONFIG_IDF_TARGET_ESP32H2
|
||||
case NetworkDeviceType::WiFi:
|
||||
_device = new WifiDevice(_hostname, _preferences, _ipConfiguration);
|
||||
break;
|
||||
default:
|
||||
_device = new WifiDevice(_hostname, _preferences, _ipConfiguration);
|
||||
break;
|
||||
#else
|
||||
default:
|
||||
_device = new EthernetDevice(_hostname, _preferences, _ipConfiguration, "Custom (W5500)",
|
||||
_preferences->getInt(preference_network_custom_addr, -1),
|
||||
_preferences->getInt(preference_network_custom_cs, -1),
|
||||
_preferences->getInt(preference_network_custom_irq, -1),
|
||||
_preferences->getInt(preference_network_custom_rst, -1),
|
||||
_preferences->getInt(preference_network_custom_sck, -1),
|
||||
_preferences->getInt(preference_network_custom_miso, -1),
|
||||
_preferences->getInt(preference_network_custom_mosi, -1),
|
||||
ETH_PHY_SPI_FREQ_MHZ,
|
||||
ETH_PHY_W5500);
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
@@ -243,6 +459,7 @@ void NukiNetwork::initialize()
|
||||
_rssiPublishInterval = _preferences->getInt(preference_rssi_publish_interval, 0) * 1000;
|
||||
_hostname = _preferences->getString(preference_hostname, "");
|
||||
_discoveryTopic = _preferences->getString(preference_mqtt_hass_discovery, "");
|
||||
_mqttPort = _preferences->getInt(preference_mqtt_broker_port, 1883);
|
||||
|
||||
if(_hostname == "")
|
||||
{
|
||||
@@ -370,10 +587,8 @@ bool NukiNetwork::update()
|
||||
_device->mqttDisconnect(true);
|
||||
}
|
||||
|
||||
if(_restartOnDisconnect && (esp_timer_get_time() / 1000) > 60000)
|
||||
{
|
||||
restartEsp(RestartReason::RestartOnDisconnectWatchdog);
|
||||
}
|
||||
if(!_webEnabled) forceEnableWebServer = true;
|
||||
if(_restartOnDisconnect && (esp_timer_get_time() / 1000) > 60000) restartEsp(RestartReason::RestartOnDisconnectWatchdog);
|
||||
|
||||
Log->println(F("Network not connected. Trying reconnect."));
|
||||
ReconnectStatus reconnectStatus = _device->reconnect(true);
|
||||
@@ -415,6 +630,13 @@ bool NukiNetwork::update()
|
||||
return false;
|
||||
}
|
||||
_mqttConnectCounter = 0;
|
||||
if(forceEnableWebServer && !_webEnabled)
|
||||
{
|
||||
forceEnableWebServer = false;
|
||||
delay(200);
|
||||
restartEsp(RestartReason::ReconfigureWebServer);
|
||||
}
|
||||
else if(!_webEnabled) forceEnableWebServer = false;
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
@@ -422,17 +644,17 @@ bool NukiNetwork::update()
|
||||
{
|
||||
if(_networkTimeout > 0 && (ts - _lastConnectedTs > _networkTimeout * 1000) && ts > 60000)
|
||||
{
|
||||
if(!_webEnabled) forceEnableWebServer = true;
|
||||
Log->println("Network timeout has been reached, restarting ...");
|
||||
delay(200);
|
||||
restartEsp(RestartReason::NetworkTimeoutWatchdog);
|
||||
}
|
||||
|
||||
delay(2000);
|
||||
return false;
|
||||
}
|
||||
|
||||
_lastConnectedTs = ts;
|
||||
|
||||
|
||||
#if PRESENCE_DETECTION_ENABLED
|
||||
if(_presenceDetection != nullptr && (_lastPresenceTs == 0 || (ts - _lastPresenceTs) > 3000))
|
||||
{
|
||||
@@ -487,7 +709,8 @@ bool NukiNetwork::update()
|
||||
|
||||
NetworkClientSecure *client = new NetworkClientSecure;
|
||||
if (client) {
|
||||
client->setDefaultCACertBundle();
|
||||
//client->setDefaultCACertBundle();
|
||||
client->setCACertBundle(x509_crt_imported_bundle_bin_start, x509_crt_imported_bundle_bin_end - x509_crt_imported_bundle_bin_start);
|
||||
{
|
||||
HTTPClient https;
|
||||
https.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
|
||||
@@ -505,10 +728,10 @@ bool NukiNetwork::update()
|
||||
{
|
||||
String currentVersion = NUKI_HUB_VERSION;
|
||||
|
||||
if(atof(doc["release"]["version"]) >= atof(currentVersion.c_str())) _latestVersion = doc["release"]["version"];
|
||||
else if(currentVersion.indexOf("beta") > 0) _latestVersion = doc["beta"]["version"];
|
||||
else if(currentVersion.indexOf("master") > 0) _latestVersion = doc["master"]["version"];
|
||||
else _latestVersion = doc["release"]["version"];
|
||||
if(atof(doc["release"]["version"]) >= atof(currentVersion.c_str())) _latestVersion = doc["release"]["fullversion"];
|
||||
else if(currentVersion.indexOf("beta") > 0) _latestVersion = doc["beta"]["fullversion"];
|
||||
else if(currentVersion.indexOf("master") > 0) _latestVersion = doc["master"]["fullversion"];
|
||||
else _latestVersion = doc["release"]["fullversion"];
|
||||
|
||||
publishString(_maintenancePathPrefix, mqtt_topic_info_nuki_hub_latest, _latestVersion, true);
|
||||
|
||||
@@ -592,7 +815,6 @@ void NukiNetwork::onMqttDisconnect(const espMqttClientTypes::DisconnectReason &r
|
||||
bool NukiNetwork::reconnect()
|
||||
{
|
||||
_mqttConnectionState = 0;
|
||||
int port = _preferences->getInt(preference_mqtt_broker_port, 1883);
|
||||
|
||||
while (!_device->mqttConnected() && (esp_timer_get_time() / 1000) > _nextReconnect)
|
||||
{
|
||||
@@ -618,7 +840,7 @@ bool NukiNetwork::reconnect()
|
||||
}
|
||||
|
||||
_device->setWill(_mqttConnectionStateTopic, 1, true, _lastWillPayload);
|
||||
_device->mqttSetServer(_mqttBrokerAddr, port);
|
||||
_device->mqttSetServer(_mqttBrokerAddr, _mqttPort);
|
||||
_device->mqttConnect();
|
||||
|
||||
int64_t timeout = (esp_timer_get_time() / 1000) + 60000;
|
||||
|
||||
@@ -19,11 +19,15 @@ enum class NetworkDeviceType
|
||||
{
|
||||
WiFi,
|
||||
W5500,
|
||||
W5500M5,
|
||||
W5500M5S3,
|
||||
Olimex_LAN8720,
|
||||
WT32_LAN8720,
|
||||
M5STACK_PoESP32_Unit,
|
||||
LilyGO_T_ETH_POE,
|
||||
GL_S10
|
||||
GL_S10,
|
||||
ETH01_Evo,
|
||||
CUSTOM
|
||||
};
|
||||
|
||||
#define JSON_BUFFER_SIZE 1024
|
||||
@@ -124,6 +128,8 @@ private:
|
||||
std::vector<std::function<void()>> _reconnectedCallbacks;
|
||||
|
||||
NetworkDeviceType _networkDeviceType = (NetworkDeviceType)-1;
|
||||
bool _firstBootAfterDeviceChange = false;
|
||||
bool _webEnabled = true;
|
||||
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
static void onMqttDataReceivedCallback(const espMqttClientTypes::MessageProperties& properties, const char* topic, const uint8_t* payload, size_t len, size_t index, size_t total);
|
||||
@@ -161,10 +167,11 @@ private:
|
||||
|
||||
int _mqttConnectionState = 0;
|
||||
int _mqttConnectCounter = 0;
|
||||
int _mqttPort = 1883;
|
||||
long _mqttConnectedTs = -1;
|
||||
bool _connectReplyReceived = false;
|
||||
bool _firstDisconnected = true;
|
||||
|
||||
|
||||
int64_t _nextReconnect = 0;
|
||||
char _mqttBrokerAddr[101] = {0};
|
||||
char _mqttUser[31] = {0};
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "NukiNetworkLock.h"
|
||||
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
||||
#include "Arduino.h"
|
||||
#include "Config.h"
|
||||
#include "MqttTopics.h"
|
||||
@@ -9,6 +8,8 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <ctype.h>
|
||||
|
||||
extern bool forceEnableWebServer;
|
||||
|
||||
NukiNetworkLock::NukiNetworkLock(NukiNetwork* network, Preferences* preferences, char* buffer, size_t bufferSize)
|
||||
: _network(network),
|
||||
_preferences(preferences),
|
||||
@@ -79,7 +80,7 @@ void NukiNetworkLock::initialize()
|
||||
|
||||
_network->subscribe(_mqttPath, mqtt_topic_webserver_action);
|
||||
_network->initTopic(_mqttPath, mqtt_topic_webserver_action, "--");
|
||||
_network->initTopic(_mqttPath, mqtt_topic_webserver_state, (_preferences->getBool(preference_webserver_enabled, true) ? "1" : "0"));
|
||||
_network->initTopic(_mqttPath, mqtt_topic_webserver_state, (_preferences->getBool(preference_webserver_enabled, true) || forceEnableWebServer ? "1" : "0"));
|
||||
|
||||
_network->initTopic(_mqttPath, mqtt_topic_query_config, "0");
|
||||
_network->initTopic(_mqttPath, mqtt_topic_query_lockstate, "0");
|
||||
@@ -230,14 +231,14 @@ void NukiNetworkLock::onMqttDataReceived(const char* topic, byte* payload, const
|
||||
|
||||
if(strcmp(value, "1") == 0)
|
||||
{
|
||||
if(_preferences->getBool(preference_webserver_enabled, true)) return;
|
||||
if(_preferences->getBool(preference_webserver_enabled, true) || forceEnableWebServer) return;
|
||||
Log->println(F("Webserver enabled, restarting."));
|
||||
_preferences->putBool(preference_webserver_enabled, true);
|
||||
|
||||
}
|
||||
else if (strcmp(value, "0") == 0)
|
||||
{
|
||||
if(!_preferences->getBool(preference_webserver_enabled, true)) return;
|
||||
if(!_preferences->getBool(preference_webserver_enabled, true) && !forceEnableWebServer) return;
|
||||
Log->println(F("Webserver disabled, restarting."));
|
||||
_preferences->putBool(preference_webserver_enabled, false);
|
||||
}
|
||||
@@ -245,7 +246,7 @@ void NukiNetworkLock::onMqttDataReceived(const char* topic, byte* payload, const
|
||||
publishString(mqtt_topic_webserver_action, "--", true);
|
||||
_network->clearWifiFallback();
|
||||
delay(200);
|
||||
restartEsp(RestartReason::RequestedViaMqtt);
|
||||
restartEsp(RestartReason::ReconfigureWebServer);
|
||||
}
|
||||
else if(comparePrefixedPath(topic, mqtt_topic_lock_log_rolling_last))
|
||||
{
|
||||
@@ -1366,7 +1367,10 @@ void NukiNetworkLock::publishHASSConfig(char *deviceType, const char *baseTopic,
|
||||
{
|
||||
_network->removeHASSConfigTopic((char*)"binary_sensor", (char*)"door_sensor", uidString);
|
||||
}
|
||||
|
||||
#ifndef CONFIG_IDF_TARGET_ESP32H2
|
||||
_network->publishHASSWifiRssiConfig(deviceType, baseTopic, name, uidString);
|
||||
#endif
|
||||
|
||||
if(publishAuthData)
|
||||
{
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "networkDevices/NetworkDevice.h"
|
||||
#ifndef CONFIG_IDF_TARGET_ESP32H2
|
||||
#include "networkDevices/WifiDevice.h"
|
||||
#include "networkDevices/W5500Device.h"
|
||||
#endif
|
||||
#include <Preferences.h>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "networkDevices/NetworkDevice.h"
|
||||
#include "networkDevices/WifiDevice.h"
|
||||
#include "networkDevices/W5500Device.h"
|
||||
#include <Preferences.h>
|
||||
#include <vector>
|
||||
#include "NukiConstants.h"
|
||||
|
||||
@@ -82,38 +82,43 @@ void NukiOpenerWrapper::initialize()
|
||||
|
||||
if(_nrOfRetries < 0 || _nrOfRetries == 200)
|
||||
{
|
||||
Log->println("Invalid nrOfRetries, revert to default (3)");
|
||||
_nrOfRetries = 3;
|
||||
_preferences->putInt(preference_command_nr_of_retries, _nrOfRetries);
|
||||
}
|
||||
|
||||
if(_retryDelay <= 100)
|
||||
if(_retryDelay < 100)
|
||||
{
|
||||
Log->println("Invalid retryDelay, revert to default (100)");
|
||||
_retryDelay = 100;
|
||||
_preferences->putInt(preference_command_retry_delay, _retryDelay);
|
||||
}
|
||||
|
||||
if(_intervalLockstate == 0)
|
||||
{
|
||||
Log->println("Invalid intervalLockstate, revert to default (1800)");
|
||||
_intervalLockstate = 60 * 30;
|
||||
_preferences->putInt(preference_query_interval_lockstate, _intervalLockstate);
|
||||
}
|
||||
if(_intervalConfig == 0)
|
||||
{
|
||||
Log->println("Invalid intervalConfig, revert to default (3600)");
|
||||
_intervalConfig = 60 * 60;
|
||||
_preferences->putInt(preference_query_interval_configuration, _intervalConfig);
|
||||
}
|
||||
if(_intervalBattery == 0)
|
||||
{
|
||||
Log->println("Invalid intervalBattery, revert to default (1800)");
|
||||
_intervalBattery = 60 * 30;
|
||||
_preferences->putInt(preference_query_interval_battery, _intervalBattery);
|
||||
}
|
||||
if(_intervalKeypad == 0)
|
||||
{
|
||||
Log->println("Invalid intervalKeypad, revert to default (1800)");
|
||||
_intervalKeypad = 60 * 30;
|
||||
_preferences->putInt(preference_query_interval_keypad, _intervalKeypad);
|
||||
}
|
||||
if(_restartBeaconTimeout < 10)
|
||||
if(_restartBeaconTimeout != -1 && _restartBeaconTimeout < 10)
|
||||
{
|
||||
Log->println("Invalid restartBeaconTimeout, revert to default (-1)");
|
||||
_restartBeaconTimeout = -1;
|
||||
_preferences->putInt(preference_restart_ble_beacon_lost, _restartBeaconTimeout);
|
||||
}
|
||||
@@ -342,6 +347,11 @@ void NukiOpenerWrapper::setPin(const uint16_t pin)
|
||||
_nukiOpener.saveSecurityPincode(pin);
|
||||
}
|
||||
|
||||
uint16_t NukiOpenerWrapper::getPin()
|
||||
{
|
||||
return _nukiOpener.getSecurityPincode();
|
||||
}
|
||||
|
||||
void NukiOpenerWrapper::unpair()
|
||||
{
|
||||
_nukiOpener.unPairNuki();
|
||||
|
||||
@@ -27,6 +27,7 @@ public:
|
||||
bool isPinSet();
|
||||
bool isPinValid();
|
||||
void setPin(const uint16_t pin);
|
||||
uint16_t getPin();
|
||||
|
||||
void unpair();
|
||||
|
||||
|
||||
@@ -94,7 +94,6 @@ void NukiWrapper::initialize(const bool& firstStart)
|
||||
_preferences->putBool(preference_find_best_rssi, false);
|
||||
_preferences->putBool(preference_check_updates, true);
|
||||
_preferences->putBool(preference_opener_continuous_mode, false);
|
||||
_preferences->putBool(preference_network_wifi_fallback_disabled, false);
|
||||
_preferences->putBool(preference_official_hybrid, false);
|
||||
_preferences->putBool(preference_official_hybrid_actions, false);
|
||||
_preferences->putBool(preference_official_hybrid_retry, false);
|
||||
@@ -127,7 +126,7 @@ void NukiWrapper::initialize(const bool& firstStart)
|
||||
_preferences->putInt(preference_rssi_publish_interval, 60);
|
||||
_preferences->putInt(preference_network_timeout, 60);
|
||||
_preferences->putInt(preference_command_nr_of_retries, 3);
|
||||
_preferences->putInt(preference_command_retry_delay, 1000);
|
||||
_preferences->putInt(preference_command_retry_delay, 100);
|
||||
_preferences->putInt(preference_restart_ble_beacon_lost, 60);
|
||||
_preferences->putInt(preference_query_interval_lockstate, 1800);
|
||||
_preferences->putInt(preference_query_interval_configuration, 3600);
|
||||
@@ -144,14 +143,12 @@ void NukiWrapper::initialize(const bool& firstStart)
|
||||
_nrOfRetries = 3;
|
||||
_preferences->putInt(preference_command_nr_of_retries, _nrOfRetries);
|
||||
}
|
||||
|
||||
if(_retryDelay <= 100)
|
||||
if(_retryDelay < 100)
|
||||
{
|
||||
Log->println("Invalid retryDelay, revert to default (100)");
|
||||
_retryDelay = 100;
|
||||
_preferences->putInt(preference_command_retry_delay, _retryDelay);
|
||||
}
|
||||
|
||||
if(_intervalLockstate == 0)
|
||||
{
|
||||
Log->println("Invalid intervalLockstate, revert to default (1800)");
|
||||
@@ -182,7 +179,7 @@ void NukiWrapper::initialize(const bool& firstStart)
|
||||
_intervalKeypad = 60 * 30;
|
||||
_preferences->putInt(preference_query_interval_keypad, _intervalKeypad);
|
||||
}
|
||||
if(_restartBeaconTimeout < 10)
|
||||
if(_restartBeaconTimeout != -1 && _restartBeaconTimeout < 10)
|
||||
{
|
||||
Log->println("Invalid restartBeaconTimeout, revert to default (-1)");
|
||||
_restartBeaconTimeout = -1;
|
||||
@@ -419,6 +416,11 @@ void NukiWrapper::setPin(const uint16_t pin)
|
||||
_nukiLock.saveSecurityPincode(pin);
|
||||
}
|
||||
|
||||
uint16_t NukiWrapper::getPin()
|
||||
{
|
||||
return _nukiLock.getSecurityPincode();
|
||||
}
|
||||
|
||||
void NukiWrapper::unpair()
|
||||
{
|
||||
_nukiLock.unPairNuki();
|
||||
|
||||
@@ -27,6 +27,7 @@ public:
|
||||
bool isPinSet();
|
||||
bool isPinValid();
|
||||
void setPin(const uint16_t pin);
|
||||
uint16_t getPin();
|
||||
void unpair();
|
||||
|
||||
void disableHASS();
|
||||
|
||||
52
src/Ota.cpp
52
src/Ota.cpp
@@ -1,52 +0,0 @@
|
||||
#include <Arduino.h>
|
||||
#include "Ota.h"
|
||||
#include "Logger.h"
|
||||
#include "RestartReason.h"
|
||||
|
||||
#define FULL_PACKET 1436 // HTTP_UPLOAD_BUFLEN in WebServer,h
|
||||
|
||||
void Ota::updateFirmware(uint8_t* buf, size_t size)
|
||||
{
|
||||
if(!_updateStarted && size == 0)
|
||||
{
|
||||
Log->println("OTA upload cancelled, size is 0.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_updateStarted)
|
||||
{ //If it's the first packet of OTA since bootup, begin OTA
|
||||
Log->println("BeginOTA");
|
||||
esp_ota_begin(esp_ota_get_next_update_partition(NULL), OTA_SIZE_UNKNOWN, &otaHandler);
|
||||
_updateStarted = true;
|
||||
}
|
||||
esp_ota_write(otaHandler, buf, size);
|
||||
if (size != FULL_PACKET)
|
||||
{
|
||||
esp_ota_end(otaHandler);
|
||||
Log->println("EndOTA");
|
||||
if (ESP_OK == esp_ota_set_boot_partition(esp_ota_get_next_update_partition(NULL)))
|
||||
{
|
||||
_updateCompleted = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log->println("Upload Error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Ota::updateStarted()
|
||||
{
|
||||
return _updateStarted;
|
||||
}
|
||||
|
||||
bool Ota::updateCompleted()
|
||||
{
|
||||
return _updateCompleted;
|
||||
}
|
||||
|
||||
void Ota::restart()
|
||||
{
|
||||
_updateCompleted = false;
|
||||
_updateStarted = false;
|
||||
}
|
||||
20
src/Ota.h
20
src/Ota.h
@@ -1,20 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <cstddef>
|
||||
#include "esp_ota_ops.h"
|
||||
|
||||
class Ota
|
||||
{
|
||||
public:
|
||||
void updateFirmware(uint8_t* buf, size_t size);
|
||||
|
||||
bool updateStarted();
|
||||
bool updateCompleted();
|
||||
void restart();
|
||||
|
||||
private:
|
||||
bool _updateStarted = false;
|
||||
bool _updateCompleted = false;
|
||||
esp_ota_handle_t otaHandler = 0;
|
||||
};
|
||||
@@ -76,10 +76,6 @@
|
||||
#define preference_gpio_configuration (char*)"gpiocfg"
|
||||
#define preference_publish_debug_info (char*)"pubdbg"
|
||||
#define preference_presence_detection_timeout (char*)"prdtimeout"
|
||||
#define preference_has_mac_saved (char*)"hasmac"
|
||||
#define preference_has_mac_byte_0 (char*)"macb0"
|
||||
#define preference_has_mac_byte_1 (char*)"macb1"
|
||||
#define preference_has_mac_byte_2 (char*)"macb2"
|
||||
#define preference_latest_version (char*)"latest"
|
||||
#define preference_task_size_network (char*)"tsksznetw"
|
||||
#define preference_task_size_nuki (char*)"tsksznuki"
|
||||
@@ -100,6 +96,22 @@
|
||||
#define preference_show_secrets (char*)"showSecr"
|
||||
#define preference_ble_tx_power (char*)"bleTxPwr"
|
||||
#define preference_recon_netw_on_mqtt_discon (char*)"recNtwMqttDis"
|
||||
#define preference_network_custom_phy (char*)"ntwPHY"
|
||||
#define preference_network_custom_addr (char*)"ntwADDR"
|
||||
#define preference_network_custom_irq (char*)"ntwIRQ"
|
||||
#define preference_network_custom_rst (char*)"ntwRST"
|
||||
#define preference_network_custom_cs (char*)"ntwCS"
|
||||
#define preference_network_custom_sck (char*)"ntwSCK"
|
||||
#define preference_network_custom_miso (char*)"ntwMISO"
|
||||
#define preference_network_custom_mosi (char*)"ntwMOSI"
|
||||
#define preference_network_custom_pwr (char*)"ntwPWR"
|
||||
#define preference_network_custom_mdio (char*)"ntwMDIO"
|
||||
#define preference_network_custom_mdc (char*)"ntwMDC"
|
||||
#define preference_network_custom_clk (char*)"ntwCLK"
|
||||
#define preference_ntw_reconfigure (char*)"ntwRECONF"
|
||||
#define preference_updater_version (char*)"updVer"
|
||||
#define preference_updater_build (char*)"updBuild"
|
||||
#define preference_updater_date (char*)"updDate"
|
||||
|
||||
inline bool initPreferences(Preferences* preferences)
|
||||
{
|
||||
@@ -223,6 +235,13 @@ inline bool initPreferences(Preferences* preferences)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (configVer < 901)
|
||||
{
|
||||
#if defined(CONFIG_IDF_TARGET_ESP32S3)
|
||||
if (preferences->getInt(preference_network_hardware) == 3) preferences->putInt(preference_network_hardware, 10);
|
||||
#endif
|
||||
if (preferences->getInt(preference_network_hardware) == 2) preferences->putInt(preference_network_hardware, 3);
|
||||
}
|
||||
|
||||
preferences->putInt(preference_config_version, atof(NUKI_HUB_VERSION) * 100);
|
||||
}
|
||||
@@ -249,10 +268,12 @@ private:
|
||||
preference_keypad_info_enabled, preference_keypad_publish_code, preference_timecontrol_control_enabled, preference_timecontrol_info_enabled, preference_conf_info_enabled,
|
||||
preference_register_as_app, preference_register_opener_as_app, preference_command_nr_of_retries, preference_command_retry_delay, preference_cred_user,
|
||||
preference_cred_password, preference_disable_non_json, preference_publish_authdata, preference_publish_debug_info, preference_presence_detection_timeout,
|
||||
preference_official_hybrid, preference_query_interval_hybrid_lockstate, preference_official_hybrid_actions, preference_official_hybrid_retry, preference_has_mac_saved,
|
||||
preference_has_mac_byte_0, preference_has_mac_byte_1, preference_has_mac_byte_2, preference_latest_version, preference_task_size_network, preference_task_size_nuki,
|
||||
preference_authlog_max_entries, preference_keypad_max_entries, preference_timecontrol_max_entries, preference_update_from_mqtt, preference_show_secrets,
|
||||
preference_ble_tx_power, preference_recon_netw_on_mqtt_discon, preference_webserial_enabled
|
||||
preference_official_hybrid, preference_query_interval_hybrid_lockstate, preference_official_hybrid_actions, preference_official_hybrid_retry, preference_latest_version,
|
||||
preference_task_size_network, preference_task_size_nuki, preference_authlog_max_entries, preference_keypad_max_entries, preference_timecontrol_max_entries,
|
||||
preference_update_from_mqtt, preference_show_secrets, preference_ble_tx_power, preference_recon_netw_on_mqtt_discon, preference_webserial_enabled,
|
||||
preference_network_custom_mdc, preference_network_custom_clk, preference_network_custom_phy, preference_network_custom_addr, preference_network_custom_irq,
|
||||
preference_network_custom_rst, preference_network_custom_cs, preference_network_custom_sck, preference_network_custom_miso, preference_network_custom_mosi,
|
||||
preference_network_custom_pwr, preference_network_custom_mdio, preference_ntw_reconfigure
|
||||
};
|
||||
std::vector<char*> _redact =
|
||||
{
|
||||
@@ -265,9 +286,9 @@ private:
|
||||
preference_timecontrol_topic_per_entry, preference_keypad_topic_per_entry, preference_enable_bootloop_reset, preference_webserver_enabled, preference_find_best_rssi,
|
||||
preference_restart_on_disconnect, preference_keypad_control_enabled, preference_keypad_info_enabled, preference_keypad_publish_code, preference_show_secrets,
|
||||
preference_timecontrol_control_enabled, preference_timecontrol_info_enabled, preference_register_as_app, preference_register_opener_as_app, preference_ip_dhcp_enabled,
|
||||
preference_publish_authdata, preference_has_mac_saved, preference_publish_debug_info, preference_network_wifi_fallback_disabled, preference_official_hybrid,
|
||||
preference_publish_authdata, preference_publish_debug_info, preference_network_wifi_fallback_disabled, preference_official_hybrid,
|
||||
preference_official_hybrid_actions, preference_official_hybrid_retry, preference_conf_info_enabled, preference_disable_non_json, preference_update_from_mqtt,
|
||||
preference_recon_netw_on_mqtt_discon, preference_webserial_enabled
|
||||
preference_recon_netw_on_mqtt_discon, preference_webserial_enabled, preference_ntw_reconfigure
|
||||
};
|
||||
std::vector<char*> _bytePrefs =
|
||||
{
|
||||
@@ -283,11 +304,9 @@ private:
|
||||
preference_query_interval_configuration, preference_query_interval_battery, preference_query_interval_keypad, preference_command_nr_of_retries,
|
||||
preference_command_retry_delay, preference_presence_detection_timeout, preference_query_interval_hybrid_lockstate, preference_latest_version,
|
||||
preference_task_size_network, preference_task_size_nuki, preference_authlog_max_entries, preference_keypad_max_entries, preference_timecontrol_max_entries,
|
||||
preference_ble_tx_power
|
||||
};
|
||||
std::vector<char*> _charPrefs =
|
||||
{
|
||||
preference_has_mac_byte_0, preference_has_mac_byte_1, preference_has_mac_byte_2
|
||||
preference_ble_tx_power, preference_network_custom_mdc, preference_network_custom_clk, preference_network_custom_phy, preference_network_custom_addr,
|
||||
preference_network_custom_irq, preference_network_custom_rst, preference_network_custom_cs, preference_network_custom_sck, preference_network_custom_miso,
|
||||
preference_network_custom_mosi, preference_network_custom_pwr, preference_network_custom_mdio
|
||||
};
|
||||
public:
|
||||
const std::vector<char*> getPreferencesKeys()
|
||||
@@ -310,8 +329,4 @@ public:
|
||||
{
|
||||
return _intPrefs;
|
||||
}
|
||||
const std::vector<char*> getPreferencesCharKeys()
|
||||
{
|
||||
return _charPrefs;
|
||||
}
|
||||
};
|
||||
@@ -10,7 +10,7 @@ enum class RestartReason
|
||||
NetworkTimeoutWatchdog,
|
||||
WifiInitFailed,
|
||||
ReconfigureWifi,
|
||||
ReconfigureLAN8720,
|
||||
ReconfigureETH,
|
||||
NetworkDeviceCriticalFailure,
|
||||
NetworkDeviceCriticalFailureNoWifiFallback,
|
||||
ConfigurationUpdated,
|
||||
@@ -24,6 +24,7 @@ enum class RestartReason
|
||||
ImportCompleted,
|
||||
DeviceUnpaired,
|
||||
NukiHubReset,
|
||||
ReconfigureWebServer,
|
||||
NotApplicable
|
||||
};
|
||||
|
||||
@@ -72,6 +73,8 @@ inline static String getRestartReason()
|
||||
return "RequestedViaMqtt";
|
||||
case RestartReason::RequestedViaWebServer:
|
||||
return "RequestedViaWebServer";
|
||||
case RestartReason::ReconfigureWebServer:
|
||||
return "ReconfigureWebServer";
|
||||
case RestartReason::BLEBeaconWatchdog:
|
||||
return "BLEBeaconWatchdog";
|
||||
case RestartReason::RestartOnDisconnectWatchdog:
|
||||
@@ -84,8 +87,8 @@ inline static String getRestartReason()
|
||||
return "WifiInitFailed";
|
||||
case RestartReason::ReconfigureWifi:
|
||||
return "ReconfigureWifi";
|
||||
case RestartReason::ReconfigureLAN8720:
|
||||
return "ReconfigureLAN8720";
|
||||
case RestartReason::ReconfigureETH:
|
||||
return "ReconfigureETH";
|
||||
case RestartReason::NetworkDeviceCriticalFailure:
|
||||
return "NetworkDeviceCriticalFailure";
|
||||
case RestartReason::NetworkDeviceCriticalFailureNoWifiFallback:
|
||||
|
||||
3973
src/WebCfgServer.cpp
3973
src/WebCfgServer.cpp
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <Preferences.h>
|
||||
#include <WebServer.h>
|
||||
#include "Ota.h"
|
||||
#include <AsyncTCP.h>
|
||||
#include <DNSServer.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include "esp_ota_ops.h"
|
||||
#include "Config.h"
|
||||
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
#include "NukiWrapper.h"
|
||||
@@ -34,72 +37,82 @@ class WebCfgServer
|
||||
{
|
||||
public:
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
WebCfgServer(NukiWrapper* nuki, NukiOpenerWrapper* nukiOpener, NukiNetwork* network, Gpio* gpio, EthServer* ethServer, Preferences* preferences, bool allowRestartToPortal, uint8_t partitionType);
|
||||
WebCfgServer(NukiWrapper* nuki, NukiOpenerWrapper* nukiOpener, NukiNetwork* network, Gpio* gpio, Preferences* preferences, bool allowRestartToPortal, uint8_t partitionType, AsyncWebServer* asyncServer);
|
||||
#else
|
||||
WebCfgServer(NukiNetwork* network, EthServer* ethServer, Preferences* preferences, bool allowRestartToPortal, uint8_t partitionType);
|
||||
WebCfgServer(NukiNetwork* network, Preferences* preferences, bool allowRestartToPortal, uint8_t partitionType, AsyncWebServer* asyncServer);
|
||||
#endif
|
||||
~WebCfgServer() = default;
|
||||
|
||||
void initialize();
|
||||
void update();
|
||||
|
||||
private:
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
void sendSettings();
|
||||
bool processArgs(String& message);
|
||||
bool processImport(String& message);
|
||||
void processGpioArgs();
|
||||
void buildHtml();
|
||||
void buildAccLvlHtml();
|
||||
void buildCredHtml();
|
||||
void buildImportExportHtml();
|
||||
void buildMqttConfigHtml();
|
||||
void buildStatusHtml();
|
||||
void buildAdvancedConfigHtml();
|
||||
void buildNukiConfigHtml();
|
||||
void buildGpioConfigHtml();
|
||||
void buildConfigureWifiHtml();
|
||||
void buildInfoHtml();
|
||||
void processUnpair(bool opener);
|
||||
void processUpdate();
|
||||
void processFactoryReset();
|
||||
void printInputField(const char* token, const char* description, const char* value, const size_t& maxLength, const char* id, const bool& isPassword = false, const bool& showLengthRestriction = false);
|
||||
void printInputField(const char* token, const char* description, const int value, size_t maxLength, const char* id);
|
||||
void printCheckBox(const char* token, const char* description, const bool value, const char* htmlClass);
|
||||
void printTextarea(const char *token, const char *description, const char *value, const size_t& maxLength, const bool& enabled = true, const bool& showLengthRestriction = false);
|
||||
void printDropDown(const char *token, const char *description, const String preselectedValue, std::vector<std::pair<String, String>> options);
|
||||
void buildNavigationButton(const char* caption, const char* targetPath, const char* labelText = "");
|
||||
void buildNavigationMenuEntry(const char *title, const char *targetPath, const char* warningMessage = "");
|
||||
void sendSettings(AsyncWebServerRequest *request);
|
||||
bool processArgs(AsyncWebServerRequest *request, String& message);
|
||||
bool processImport(AsyncWebServerRequest *request, String& message);
|
||||
void processGpioArgs(AsyncWebServerRequest *request);
|
||||
void buildHtml(AsyncWebServerRequest *request);
|
||||
void buildAccLvlHtml(AsyncWebServerRequest *request, int aclPart = 0);
|
||||
void buildCredHtml(AsyncWebServerRequest *request);
|
||||
void buildImportExportHtml(AsyncWebServerRequest *request);
|
||||
void buildMqttConfigHtml(AsyncWebServerRequest *request);
|
||||
void buildStatusHtml(AsyncWebServerRequest *request);
|
||||
void buildAdvancedConfigHtml(AsyncWebServerRequest *request);
|
||||
void buildNukiConfigHtml(AsyncWebServerRequest *request);
|
||||
void buildGpioConfigHtml(AsyncWebServerRequest *request);
|
||||
#ifndef CONFIG_IDF_TARGET_ESP32H2
|
||||
void buildConfigureWifiHtml(AsyncWebServerRequest *request);
|
||||
#endif
|
||||
void buildInfoHtml(AsyncWebServerRequest *request);
|
||||
void buildCustomNetworkConfigHtml(AsyncWebServerRequest *request);
|
||||
void processUnpair(AsyncWebServerRequest *request, bool opener);
|
||||
void processUpdate(AsyncWebServerRequest *request);
|
||||
void processFactoryReset(AsyncWebServerRequest *request);
|
||||
void printInputField(AsyncResponseStream *response, const char* token, const char* description, const char* value, const size_t& maxLength, const char* id, const bool& isPassword = false, const bool& showLengthRestriction = false);
|
||||
void printInputField(AsyncResponseStream *response, const char* token, const char* description, const int value, size_t maxLength, const char* id);
|
||||
void printCheckBox(AsyncResponseStream *response, const char* token, const char* description, const bool value, const char* htmlClass);
|
||||
void printCheckBox(String &partString, const char* token, const char* description, const bool value, const char* htmlClass);
|
||||
void printTextarea(AsyncResponseStream *response, const char *token, const char *description, const char *value, const size_t& maxLength, const bool& enabled = true, const bool& showLengthRestriction = false);
|
||||
void printDropDown(AsyncResponseStream *response, const char *token, const char *description, const String preselectedValue, std::vector<std::pair<String, String>> options, const String className);
|
||||
void buildNavigationButton(AsyncResponseStream *response, const char* caption, const char* targetPath, const char* labelText = "");
|
||||
void buildNavigationMenuEntry(AsyncResponseStream *response, const char *title, const char *targetPath, const char* warningMessage = "");
|
||||
void partAccLvlHtml(String &partString, int aclPart);
|
||||
|
||||
const std::vector<std::pair<String, String>> getNetworkDetectionOptions() const;
|
||||
const std::vector<std::pair<String, String>> getGpioOptions() const;
|
||||
const std::vector<std::pair<String, String>> getNetworkCustomPHYOptions() const;
|
||||
#if defined(CONFIG_IDF_TARGET_ESP32)
|
||||
const std::vector<std::pair<String, String>> getNetworkCustomCLKOptions() const;
|
||||
#endif
|
||||
|
||||
String getPreselectionForGpio(const uint8_t& pin);
|
||||
String pinStateToString(uint8_t value);
|
||||
|
||||
void printParameter(const char* description, const char* value, const char *link = "", const char *id = "");
|
||||
void printParameter(AsyncResponseStream *response, const char* description, const char* value, const char *link = "", const char *id = "");
|
||||
|
||||
NukiWrapper* _nuki = nullptr;
|
||||
NukiOpenerWrapper* _nukiOpener = nullptr;
|
||||
Gpio* _gpio = nullptr;
|
||||
bool _pinsConfigured = false;
|
||||
bool _brokerConfigured = false;
|
||||
bool _rebootRequired = false;
|
||||
#endif
|
||||
|
||||
String generateConfirmCode();
|
||||
String _confirmCode = "----";
|
||||
void buildConfirmHtml(const String &message, uint32_t redirectDelay = 5, bool redirect = false);
|
||||
void buildOtaHtml(bool errored, bool debug = false);
|
||||
void buildOtaCompletedHtml();
|
||||
void sendCss();
|
||||
void sendFavicon();
|
||||
void buildHtmlHeader(String additionalHeader = "");
|
||||
void buildConfirmHtml(AsyncWebServerRequest *request, const String &message, uint32_t redirectDelay = 5, bool redirect = false);
|
||||
void buildOtaHtml(AsyncWebServerRequest *request, bool debug = false);
|
||||
void buildOtaCompletedHtml(AsyncWebServerRequest *request);
|
||||
void sendCss(AsyncWebServerRequest *request);
|
||||
void sendFavicon(AsyncWebServerRequest *request);
|
||||
void buildHtmlHeader(AsyncResponseStream *response, String additionalHeader = "");
|
||||
void waitAndProcess(const bool blocking, const uint32_t duration);
|
||||
void handleOtaUpload();
|
||||
|
||||
WebServer _server;
|
||||
void handleOtaUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final);
|
||||
void printProgress(size_t prg, size_t sz);
|
||||
|
||||
AsyncWebServer* _asyncServer = nullptr;
|
||||
NukiNetwork* _network = nullptr;
|
||||
Preferences* _preferences = nullptr;
|
||||
Ota _ota;
|
||||
|
||||
bool _hasCredentials = false;
|
||||
char _credUser[31] = {0};
|
||||
@@ -108,7 +121,7 @@ private:
|
||||
uint8_t _partitionType = 0;
|
||||
uint32_t _transferredSize = 0;
|
||||
int64_t _otaStartTs = 0;
|
||||
size_t _otaContentLen = 0;
|
||||
String _hostname;
|
||||
String _response;
|
||||
bool _enabled = true;
|
||||
};
|
||||
|
||||
137
src/main.cpp
137
src/main.cpp
@@ -1,16 +1,14 @@
|
||||
#define IS_VALID_DETECT 0xa00ab00bc00bd00d;
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "hardware/W5500EthServer.h"
|
||||
#include "hardware/WifiEthServer.h"
|
||||
#include "esp_crt_bundle.h"
|
||||
#include "esp_ota_ops.h"
|
||||
#include "esp_http_client.h"
|
||||
#include "esp_https_ota.h"
|
||||
#include <esp_task_wdt.h>
|
||||
#include "Config.h"
|
||||
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
#include "Config.h"
|
||||
#include "NukiWrapper.h"
|
||||
#include "NukiNetworkLock.h"
|
||||
#include "PresenceDetection.h"
|
||||
@@ -28,7 +26,6 @@
|
||||
#include <WString.h>
|
||||
#include <MycilaWebSerial.h>
|
||||
|
||||
AsyncWebServer webserialserver(81);
|
||||
char log_print_buffer[1024];
|
||||
|
||||
NukiNetworkLock* networkLock = nullptr;
|
||||
@@ -53,7 +50,6 @@ int64_t restartTs = ((2^64) - (5 * 1000 * 60000)) / 1000;
|
||||
#include "../../src/WebCfgServer.h"
|
||||
#include "../../src/Logger.h"
|
||||
#include "../../src/PreferencesKeys.h"
|
||||
#include "../../src/Config.h"
|
||||
#include "../../src/RestartReason.h"
|
||||
#include "../../src/NukiNetwork.h"
|
||||
|
||||
@@ -61,16 +57,17 @@ int64_t restartTs = 10 * 1000 * 60000;
|
||||
|
||||
#endif
|
||||
|
||||
AsyncWebServer* asyncServer = nullptr;
|
||||
NukiNetwork* network = nullptr;
|
||||
WebCfgServer* webCfgServer = nullptr;
|
||||
Preferences* preferences = nullptr;
|
||||
EthServer* ethServer = nullptr;
|
||||
|
||||
RTC_NOINIT_ATTR int restartReason;
|
||||
RTC_NOINIT_ATTR uint64_t restartReasonValidDetect;
|
||||
RTC_NOINIT_ATTR bool rebuildGpioRequested;
|
||||
RTC_NOINIT_ATTR uint64_t bootloopValidDetect;
|
||||
RTC_NOINIT_ATTR int8_t bootloopCounter;
|
||||
RTC_NOINIT_ATTR bool forceEnableWebServer;
|
||||
|
||||
bool restartReason_isValid;
|
||||
RestartReason currentRestartReason = RestartReason::NotApplicable;
|
||||
@@ -109,7 +106,7 @@ int _log_vprintf(const char *fmt, va_list args) {
|
||||
void setReroute(){
|
||||
esp_log_set_vprintf(_log_vprintf);
|
||||
if(preferences->getBool(preference_mqtt_log_enabled)) esp_log_level_set("*", ESP_LOG_INFO);
|
||||
else
|
||||
else
|
||||
{
|
||||
esp_log_level_set("*", ESP_LOG_DEBUG);
|
||||
esp_log_level_set("nvs", ESP_LOG_INFO);
|
||||
@@ -122,7 +119,6 @@ void networkTask(void *pvParameters)
|
||||
{
|
||||
int64_t networkLoopTs = 0;
|
||||
bool secrets = preferences->getBool(preference_show_secrets, false);
|
||||
bool webEnabled = preferences->getBool(preference_webserver_enabled, true);
|
||||
bool reroute = true;
|
||||
|
||||
while(true)
|
||||
@@ -149,9 +145,6 @@ void networkTask(void *pvParameters)
|
||||
}
|
||||
#endif
|
||||
if(connected && openerEnabled) networkOpener->update();
|
||||
if(preferences->getBool(preference_webserver_enabled, true)) webCfgServer->update();
|
||||
#else
|
||||
webCfgServer->update();
|
||||
#endif
|
||||
|
||||
if((esp_timer_get_time() / 1000) - networkLoopTs > 120000)
|
||||
@@ -328,20 +321,33 @@ void otaTask(void *pvParameter)
|
||||
};
|
||||
Log->print(F("Attempting to download update from "));
|
||||
Log->println(config.url);
|
||||
esp_err_t ret = esp_https_ota(&ota_config);
|
||||
if (ret == ESP_OK) {
|
||||
Log->println("OTA Succeeded, Rebooting...");
|
||||
esp_ota_set_boot_partition(esp_ota_get_next_update_partition(NULL));
|
||||
restartEsp(RestartReason::OTACompleted);
|
||||
} else {
|
||||
Log->println("Firmware upgrade failed");
|
||||
restartEsp(RestartReason::OTAAborted);
|
||||
}
|
||||
while (1) {
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
esp_task_wdt_reset();
|
||||
int retryMax = 3;
|
||||
int retryCount = 0;
|
||||
|
||||
while (retryCount <= retryMax)
|
||||
{
|
||||
esp_err_t ret = esp_https_ota(&ota_config);
|
||||
if (ret == ESP_OK) {
|
||||
Log->println("OTA Succeeded, Rebooting...");
|
||||
esp_ota_set_boot_partition(esp_ota_get_next_update_partition(NULL));
|
||||
restartEsp(RestartReason::OTACompleted);
|
||||
break;
|
||||
} else {
|
||||
Log->println("Firmware upgrade failed, retrying in 5 seconds");
|
||||
retryCount++;
|
||||
esp_task_wdt_reset();
|
||||
delay(5000);
|
||||
continue;
|
||||
}
|
||||
while (1) {
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
Log->println("Firmware upgrade failed, restarting");
|
||||
esp_ota_set_boot_partition(esp_ota_get_next_update_partition(NULL));
|
||||
restartEsp(RestartReason::OTAAborted);
|
||||
}
|
||||
|
||||
void setupTasks(bool ota)
|
||||
@@ -370,22 +376,6 @@ void setupTasks(bool ota)
|
||||
}
|
||||
}
|
||||
|
||||
void initEthServer(const NetworkDeviceType device)
|
||||
{
|
||||
switch (device)
|
||||
{
|
||||
case NetworkDeviceType::W5500:
|
||||
ethServer = new W5500EthServer(80);
|
||||
break;
|
||||
case NetworkDeviceType::WiFi:
|
||||
ethServer = new WifiEthServer(80);
|
||||
break;
|
||||
default:
|
||||
ethServer = new WifiEthServer(80);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
esp_log_level_set("*", ESP_LOG_ERROR);
|
||||
@@ -395,11 +385,13 @@ void setup()
|
||||
preferences = new Preferences();
|
||||
preferences->begin("nukihub", false);
|
||||
bool firstStart = initPreferences(preferences);
|
||||
|
||||
bool doOta = false;
|
||||
uint8_t partitionType = checkPartition();
|
||||
|
||||
initializeRestartReason();
|
||||
|
||||
if((partitionType==1 && preferences->getString(preference_ota_updater_url, "").length() > 0) || (partitionType==2 && preferences->getString(preference_ota_main_url, "").length() > 0)) doOta = true;
|
||||
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
if(preferences->getBool(preference_enable_bootloop_reset, false))
|
||||
{
|
||||
@@ -408,17 +400,31 @@ void setup()
|
||||
#endif
|
||||
|
||||
#ifdef NUKI_HUB_UPDATER
|
||||
Log->print(F("Nuki Hub OTA version ")); Log->println(NUKI_HUB_VERSION);
|
||||
Log->print(F("Nuki Hub OTA build ")); Log->println(NUKI_HUB_BUILD);
|
||||
Log->print(F("Nuki Hub OTA version "));
|
||||
Log->println(NUKI_HUB_VERSION);
|
||||
Log->print(F("Nuki Hub OTA build "));
|
||||
Log->println();
|
||||
|
||||
if(preferences->getString(preference_updater_version, "") != NUKI_HUB_VERSION) preferences->putString(preference_updater_version, NUKI_HUB_VERSION);
|
||||
if(preferences->getString(preference_updater_build, "") != NUKI_HUB_BUILD) preferences->putString(preference_updater_build, NUKI_HUB_BUILD);
|
||||
if(preferences->getString(preference_updater_date, "") != NUKI_HUB_DATE) preferences->putString(preference_updater_date, NUKI_HUB_DATE);
|
||||
|
||||
network = new NukiNetwork(preferences);
|
||||
network->initialize();
|
||||
initEthServer(network->networkDeviceType());
|
||||
webCfgServer = new WebCfgServer(network, ethServer, preferences, network->networkDeviceType() == NetworkDeviceType::WiFi, partitionType);
|
||||
webCfgServer->initialize();
|
||||
|
||||
if(!doOta)
|
||||
{
|
||||
asyncServer = new AsyncWebServer(80);
|
||||
webCfgServer = new WebCfgServer(network, preferences, network->networkDeviceType() == NetworkDeviceType::WiFi, partitionType, asyncServer);
|
||||
webCfgServer->initialize();
|
||||
asyncServer->onNotFound([](AsyncWebServerRequest* request) { request->redirect("/"); });
|
||||
asyncServer->begin();
|
||||
}
|
||||
#else
|
||||
Log->print(F("Nuki Hub version ")); Log->println(NUKI_HUB_VERSION);
|
||||
Log->print(F("Nuki Hub build ")); Log->println(NUKI_HUB_BUILD);
|
||||
Log->print(F("Nuki Hub version "));
|
||||
Log->println(NUKI_HUB_VERSION);
|
||||
Log->print(F("Nuki Hub build "));
|
||||
Log->println(NUKI_HUB_BUILD);
|
||||
|
||||
uint32_t devIdOpener = preferences->getUInt(preference_device_id_opener);
|
||||
|
||||
@@ -470,8 +476,6 @@ void setup()
|
||||
networkOpener->initialize();
|
||||
}
|
||||
|
||||
initEthServer(network->networkDeviceType());
|
||||
|
||||
Log->println(lockEnabled ? F("Nuki Lock enabled") : F("Nuki Lock disabled"));
|
||||
if(lockEnabled)
|
||||
{
|
||||
@@ -486,20 +490,33 @@ void setup()
|
||||
nukiOpener->initialize();
|
||||
}
|
||||
|
||||
if(preferences->getBool(preference_webserver_enabled, true))
|
||||
if(forceEnableWebServer || preferences->getBool(preference_webserver_enabled, true) || preferences->getBool(preference_webserial_enabled, false))
|
||||
{
|
||||
webCfgServer = new WebCfgServer(nuki, nukiOpener, network, gpio, ethServer, preferences, network->networkDeviceType() == NetworkDeviceType::WiFi, partitionType);
|
||||
webCfgServer->initialize();
|
||||
}
|
||||
if(!doOta)
|
||||
{
|
||||
asyncServer = new AsyncWebServer(80);
|
||||
|
||||
WebSerial.setAuthentication(preferences->getString(preference_cred_user), preferences->getString(preference_cred_password));
|
||||
WebSerial.begin(&webserialserver);
|
||||
WebSerial.setBuffer(1024);
|
||||
webserialserver.onNotFound([](AsyncWebServerRequest* request) { request->redirect("/webserial"); });
|
||||
webserialserver.begin();
|
||||
if(forceEnableWebServer || preferences->getBool(preference_webserver_enabled, true))
|
||||
{
|
||||
webCfgServer = new WebCfgServer(nuki, nukiOpener, network, gpio, preferences, network->networkDeviceType() == NetworkDeviceType::WiFi, partitionType, asyncServer);
|
||||
webCfgServer->initialize();
|
||||
asyncServer->onNotFound([](AsyncWebServerRequest* request) { request->redirect("/"); });
|
||||
}
|
||||
else asyncServer->onNotFound([](AsyncWebServerRequest* request) { request->redirect("/webserial"); });
|
||||
|
||||
if(preferences->getBool(preference_webserial_enabled, false))
|
||||
{
|
||||
WebSerial.setAuthentication(preferences->getString(preference_cred_user), preferences->getString(preference_cred_password));
|
||||
WebSerial.begin(asyncServer);
|
||||
WebSerial.setBuffer(1024);
|
||||
}
|
||||
|
||||
asyncServer->begin();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if((partitionType==1 && preferences->getString(preference_ota_updater_url, "").length() > 0) || (partitionType==2 && preferences->getString(preference_ota_main_url, "").length() > 0)) setupTasks(true);
|
||||
if(doOta) setupTasks(true);
|
||||
else setupTasks(false);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
13
src/networkDevices/DM9051Definitions.h
Normal file
13
src/networkDevices/DM9051Definitions.h
Normal 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
|
||||
|
||||
@@ -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 "";
|
||||
}
|
||||
@@ -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
|
||||
};
|
||||
221
src/networkDevices/EthernetDevice.cpp
Normal file
221
src/networkDevices/EthernetDevice.cpp
Normal 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 "";
|
||||
}
|
||||
102
src/networkDevices/EthernetDevice.h
Normal file
102
src/networkDevices/EthernetDevice.h
Normal 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
|
||||
};
|
||||
@@ -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);
|
||||
|
||||
20
src/networkDevices/LAN8720Definitions.h
Normal file
20
src/networkDevices/LAN8720Definitions.h
Normal 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
|
||||
22
src/networkDevices/W5500Definitions.h
Normal file
22
src/networkDevices/W5500Definitions.h
Normal 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
|
||||
@@ -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 "";
|
||||
}
|
||||
@@ -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];
|
||||
};
|
||||
@@ -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()
|
||||
|
||||
@@ -41,6 +41,7 @@ private:
|
||||
bool _restartOnDisconnect = false;
|
||||
bool _startAp = false;
|
||||
bool _isReconnecting = false;
|
||||
bool _wifiFallbackDisabled = false;
|
||||
char* _path;
|
||||
int64_t _disconnectTs = 0;
|
||||
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
#include "espMqttClientW5500.h"
|
||||
|
||||
espMqttClientW5500::espMqttClientW5500()
|
||||
: espMqttClient(espMqttClientTypes::UseInternalTask::NO),
|
||||
_client()
|
||||
{
|
||||
_transport = &_client;
|
||||
}
|
||||
|
||||
void espMqttClientW5500::update()
|
||||
{
|
||||
loop();
|
||||
}
|
||||
@@ -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
|
||||
};
|
||||
Reference in New Issue
Block a user