From 69e2cf5c3f518c0e4df885361b5eec3809f641f9 Mon Sep 17 00:00:00 2001 From: iranl Date: Tue, 17 Dec 2024 22:23:04 +0100 Subject: [PATCH] Runtime alt connect and debug logging --- README.md | 1 + lib/nuki_ble | 2 +- platformio.ini | 48 ++------------------------- sdkconfig.defaults | 1 + sdkconfig.release.defaults | 6 ++-- src/Config.h | 2 +- src/NukiOpenerWrapper.cpp | 8 ++++- src/NukiWrapper.cpp | 8 ++++- src/PreferencesKeys.h | 20 +++++++++-- src/WebCfgServer.cpp | 68 ++++++++++++++++++++++++++++++++++++++ src/main.cpp | 33 +++++++++++++----- 11 files changed, 134 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 05eb7c9..9228740 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,7 @@ In a browser navigate to the IP address assigned to the ESP32. - Nuki Smartlock enabled: Enable if you want Nuki Hub to connect to a Nuki Lock (1.0-4.0) - Nuki Opener enabled: Enable if you want Nuki Hub to connect to a Nuki Opener +- New Nuki Bluetooth connection mode (disable if there are connection issues): Enable to use the latest Nuki BLE connection mode (recommended). Disable if you have issues communicating with the lock/opener #### Advanced Nuki Configuration diff --git a/lib/nuki_ble b/lib/nuki_ble index 0647d25..983b38c 160000 --- a/lib/nuki_ble +++ b/lib/nuki_ble @@ -1 +1 @@ -Subproject commit 0647d251b778fa679afef0caa7e8edef443a760c +Subproject commit 983b38ca2aaa86e9e1727b6002bbff28701e6a91 diff --git a/platformio.ini b/platformio.ini index f16dfef..d25e7ae 100644 --- a/platformio.ini +++ b/platformio.ini @@ -77,7 +77,7 @@ extra_scripts = post:pio_package_post.py build_flags = ${env.build_flags} - -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE + -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 @@ -138,12 +138,6 @@ build_flags = -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DDEBUG_NUKIHUB - -DDEBUG_SENSE_NUKI - -DDEBUG_NUKI_COMMAND - -DDEBUG_NUKI_CONNECT - -DDEBUG_NUKI_COMMUNICATION - ;-DDEBUG_NUKI_HEX_DATA - -DDEBUG_NUKI_READABLE_DATA [env:esp32-c3_dbg] extends = env:esp32-c3 @@ -155,13 +149,7 @@ build_flags = -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 - -DDEBUG_NUKIHUB - -DDEBUG_SENSE_NUKI - -DDEBUG_NUKI_COMMAND - -DDEBUG_NUKI_CONNECT - -DDEBUG_NUKI_COMMUNICATION - ;-DDEBUG_NUKI_HEX_DATA - -DDEBUG_NUKI_READABLE_DATA + -DDEBUG_NUKIHUB [env:esp32-c6_dbg] extends = env:esp32-c6 @@ -174,12 +162,6 @@ build_flags = -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DDEBUG_NUKIHUB - -DDEBUG_SENSE_NUKI - -DDEBUG_NUKI_COMMAND - -DDEBUG_NUKI_CONNECT - -DDEBUG_NUKI_COMMUNICATION - ;-DDEBUG_NUKI_HEX_DATA - -DDEBUG_NUKI_READABLE_DATA [env:esp32-h2_dbg] extends = env:esp32-h2 @@ -193,12 +175,6 @@ build_flags = -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DDEBUG_NUKIHUB - -DDEBUG_SENSE_NUKI - -DDEBUG_NUKI_COMMAND - -DDEBUG_NUKI_CONNECT - -DDEBUG_NUKI_COMMUNICATION - ;-DDEBUG_NUKI_HEX_DATA - -DDEBUG_NUKI_READABLE_DATA [env:esp32-s3_dbg] extends = env:esp32-s3 @@ -211,12 +187,6 @@ build_flags = -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DDEBUG_NUKIHUB - -DDEBUG_SENSE_NUKI - -DDEBUG_NUKI_COMMAND - -DDEBUG_NUKI_CONNECT - -DDEBUG_NUKI_COMMUNICATION - ;-DDEBUG_NUKI_HEX_DATA - -DDEBUG_NUKI_READABLE_DATA [env:esp32-s3-oct_dbg] extends = env:esp32-s3-oct @@ -229,12 +199,6 @@ build_flags = -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 -DDEBUG_NUKIHUB - -DDEBUG_SENSE_NUKI - -DDEBUG_NUKI_COMMAND - -DDEBUG_NUKI_CONNECT - -DDEBUG_NUKI_COMMUNICATION - ;-DDEBUG_NUKI_HEX_DATA - -DDEBUG_NUKI_READABLE_DATA [env:esp32-solo1_dbg] extends = env:esp32-solo1 @@ -246,10 +210,4 @@ build_flags = -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 - -DDEBUG_NUKIHUB - -DDEBUG_SENSE_NUKI - -DDEBUG_NUKI_COMMAND - -DDEBUG_NUKI_CONNECT - -DDEBUG_NUKI_COMMUNICATION - ;-DDEBUG_NUKI_HEX_DATA - -DDEBUG_NUKI_READABLE_DATA \ No newline at end of file + -DDEBUG_NUKIHUB \ No newline at end of file diff --git a/sdkconfig.defaults b/sdkconfig.defaults index 37e2539..06bfb9c 100644 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -86,6 +86,7 @@ CONFIG_HEAP_TASK_TRACKING=n CONFIG_LOG_COLORS=n CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT=n CONFIG_LOG_MAXIMUM_LEVEL=4 +CONFIG_ARDUHAL_ESP_LOG=y CONFIG_ETH_ENABLED=y CONFIG_ETH_USE_SPI_ETHERNET=y CONFIG_ETH_SPI_ETHERNET_W5500=y diff --git a/sdkconfig.release.defaults b/sdkconfig.release.defaults index 7127d99..9d2a2eb 100644 --- a/sdkconfig.release.defaults +++ b/sdkconfig.release.defaults @@ -1,5 +1,5 @@ -CONFIG_LOG_DEFAULT_LEVEL_NONE=y -CONFIG_LOG_DEFAULT_LEVEL=0 -CONFIG_ESP_IPC_TASK_STACK_SIZE=1024 +CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y +CONFIG_LOG_DEFAULT_LEVEL=4 +CONFIG_ESP_IPC_TASK_STACK_SIZE=2048 CONFIG_BOOTLOADER_LOG_LEVEL_ERROR=y CONFIG_BOOTLOADER_LOG_LEVEL=1 \ No newline at end of file diff --git a/src/Config.h b/src/Config.h index 599e969..62639c3 100644 --- a/src/Config.h +++ b/src/Config.h @@ -5,7 +5,7 @@ #define NUKI_HUB_VERSION "9.05" #define NUKI_HUB_VERSION_INT (uint32_t)905 #define NUKI_HUB_BUILD "unknownbuildnr" -#define NUKI_HUB_DATE "2024-12-17" +#define NUKI_HUB_DATE "2024-12-18" #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" diff --git a/src/NukiOpenerWrapper.cpp b/src/NukiOpenerWrapper.cpp index 599f6e0..6b65c37 100644 --- a/src/NukiOpenerWrapper.cpp +++ b/src/NukiOpenerWrapper.cpp @@ -48,7 +48,13 @@ NukiOpenerWrapper::~NukiOpenerWrapper() void NukiOpenerWrapper::initialize() { - _nukiOpener.initialize(); + _nukiOpener.setDebugConnect(_preferences->getBool(preference_debug_connect, false)); + _nukiOpener.setDebugCommunication(_preferences->getBool(preference_debug_communication, false)); + _nukiOpener.setDebugReadableData(_preferences->getBool(preference_debug_readable_data, false)); + _nukiOpener.setDebugHexData(_preferences->getBool(preference_debug_hex_data, false)); + _nukiOpener.setDebugCommand(_preferences->getBool(preference_debug_command, false)); + + _nukiOpener.initialize(_preferences->getBool(preference_connect_mode, false)); _nukiOpener.registerBleScanner(_bleScanner); _nukiOpener.setEventHandler(this); _nukiOpener.setConnectTimeout(3); diff --git a/src/NukiWrapper.cpp b/src/NukiWrapper.cpp index a347938..db8a02c 100644 --- a/src/NukiWrapper.cpp +++ b/src/NukiWrapper.cpp @@ -49,7 +49,13 @@ NukiWrapper::~NukiWrapper() void NukiWrapper::initialize() { - _nukiLock.initialize(); + _nukiLock.setDebugConnect(_preferences->getBool(preference_debug_connect, false)); + _nukiLock.setDebugCommunication(_preferences->getBool(preference_debug_communication, false)); + _nukiLock.setDebugReadableData(_preferences->getBool(preference_debug_readable_data, false)); + _nukiLock.setDebugHexData(_preferences->getBool(preference_debug_hex_data, false)); + _nukiLock.setDebugCommand(_preferences->getBool(preference_debug_command, false)); + + _nukiLock.initialize(_preferences->getBool(preference_connect_mode, false)); _nukiLock.registerBleScanner(_bleScanner); _nukiLock.setEventHandler(this); _nukiLock.setConnectTimeout(3); diff --git a/src/PreferencesKeys.h b/src/PreferencesKeys.h index 724180a..444d453 100644 --- a/src/PreferencesKeys.h +++ b/src/PreferencesKeys.h @@ -61,6 +61,12 @@ #define preference_wifi_ssid (char*)"wifiSSID" #define preference_wifi_pass (char*)"wifiPass" #define preference_disable_network_not_connected (char*)"disNtwNoCon" +#define preference_debug_connect (char*)"dbgConnect" +#define preference_debug_communication (char*)"dbgCommu" +#define preference_debug_readable_data (char*)"dbgReadData" +#define preference_debug_hex_data (char*)"dbgHexData" +#define preference_debug_command (char*)"dbgCommand" +#define preference_connect_mode (char*)"nukiConnMode" // CHANGE DOES NOT REQUIRE REBOOT TO TAKE EFFECT #define preference_find_best_rssi (char*)"nwbestrssi" @@ -203,6 +209,13 @@ inline void initPreferences(Preferences* preferences) preferences->putInt(preference_query_interval_configuration, 3600); preferences->putInt(preference_query_interval_battery, 1800); preferences->putInt(preference_query_interval_keypad, 1800); + + preferences->putBool(preference_debug_connect, false); + preferences->putBool(preference_debug_communication, false); + preferences->putBool(preference_debug_readable_data, false); + preferences->putBool(preference_debug_hex_data, false); + preferences->putBool(preference_debug_command, false); + preferences->putBool(preference_connect_mode, false); #ifndef CONFIG_IDF_TARGET_ESP32H2 WiFi.begin(); @@ -362,7 +375,9 @@ private: 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, preference_lock_max_auth_entry_count, preference_opener_max_auth_entry_count, preference_auth_control_enabled, preference_auth_topic_per_entry, preference_auth_info_enabled, preference_auth_max_entries, preference_wifi_ssid, preference_wifi_pass, - preference_keypad_check_code_enabled, preference_disable_network_not_connected, preference_mqtt_hass_enabled, preference_hass_device_discovery + preference_keypad_check_code_enabled, preference_disable_network_not_connected, preference_mqtt_hass_enabled, preference_hass_device_discovery, + preference_debug_connect, preference_debug_communication, preference_debug_readable_data, preference_debug_hex_data, preference_debug_command, preference_connect_mode + }; std::vector _redact = { @@ -378,7 +393,8 @@ private: preference_publish_authdata, preference_publish_debug_info, preference_official_hybrid_enabled, preference_mqtt_hass_enabled, preference_official_hybrid_actions, preference_official_hybrid_retry, preference_conf_info_enabled, preference_disable_non_json, preference_update_from_mqtt, preference_auth_control_enabled, preference_auth_topic_per_entry, preference_auth_info_enabled, preference_webserial_enabled, preference_hass_device_discovery, - preference_ntw_reconfigure, preference_keypad_check_code_enabled, preference_disable_network_not_connected, preference_find_best_rssi + preference_ntw_reconfigure, preference_keypad_check_code_enabled, preference_disable_network_not_connected, preference_find_best_rssi, + preference_debug_connect, preference_debug_communication, preference_debug_readable_data, preference_debug_hex_data, preference_debug_command, preference_connect_mode }; std::vector _bytePrefs = { diff --git a/src/WebCfgServer.cpp b/src/WebCfgServer.cpp index 1ee67a0..29de160 100644 --- a/src/WebCfgServer.cpp +++ b/src/WebCfgServer.cpp @@ -2190,6 +2190,56 @@ bool WebCfgServer::processArgs(PsychicRequest *request, String& message) //configChanged = true; } } + else if(key == "DBGCONN") + { + if(_preferences->getBool(preference_debug_connect, false) != (value == "1")) + { + _preferences->putBool(preference_debug_connect, (value == "1")); + Log->print(F("Setting changed: ")); + Log->println(key); + configChanged = true; + } + } + else if(key == "DBGCOMMU") + { + if(_preferences->getBool(preference_debug_communication, false) != (value == "1")) + { + _preferences->putBool(preference_debug_communication, (value == "1")); + Log->print(F("Setting changed: ")); + Log->println(key); + configChanged = true; + } + } + else if(key == "DBGREAD") + { + if(_preferences->getBool(preference_debug_readable_data, false) != (value == "1")) + { + _preferences->putBool(preference_debug_readable_data, (value == "1")); + Log->print(F("Setting changed: ")); + Log->println(key); + configChanged = true; + } + } + else if(key == "DBGHEX") + { + if(_preferences->getBool(preference_debug_hex_data, false) != (value == "1")) + { + _preferences->putBool(preference_debug_hex_data, (value == "1")); + Log->print(F("Setting changed: ")); + Log->println(key); + configChanged = true; + } + } + else if(key == "DBGCOMM") + { + if(_preferences->getBool(preference_debug_command, false) != (value == "1")) + { + _preferences->putBool(preference_debug_command, (value == "1")); + Log->print(F("Setting changed: ")); + Log->println(key); + configChanged = true; + } + } else if(key == "ACLLVLCHANGED") { aclLvlChanged = true; @@ -2728,6 +2778,16 @@ bool WebCfgServer::processArgs(PsychicRequest *request, String& message) configChanged = true; } } + else if(key == "CONNMODE") + { + if(_preferences->getBool(preference_connect_mode, false) != (value == "1")) + { + _preferences->putBool(preference_connect_mode, (value == "1")); + Log->print(F("Setting changed: ")); + Log->println(key); + configChanged = true; + } + } else if(key == "CREDUSER") { if(value == "#") @@ -3613,6 +3673,13 @@ esp_err_t WebCfgServer::buildAdvancedConfigHtml(PsychicRequest *request) } printInputField(&response, "OTAUPD", "Custom URL to update Nuki Hub updater", "", 255, ""); printInputField(&response, "OTAMAIN", "Custom URL to update Nuki Hub", "", 255, ""); + + printCheckBox(&response, "DBGCONN", "Enable Nuki connect debug logging", _preferences->getBool(preference_debug_connect, false), ""); + printCheckBox(&response, "DBGCOMMU", "Enable Nuki communication debug logging", _preferences->getBool(preference_debug_communication, false), ""); + printCheckBox(&response, "DBGREAD", "Enable Nuki readable data debug logging", _preferences->getBool(preference_debug_readable_data, false), ""); + printCheckBox(&response, "DBGHEX", "Enable Nuki hex data debug logging", _preferences->getBool(preference_debug_hex_data, false), ""); + printCheckBox(&response, "DBGCOMM", "Enable Nuki command debug logging", _preferences->getBool(preference_debug_command, false), ""); + response.print(""); response.print("
"); @@ -3930,6 +3997,7 @@ esp_err_t WebCfgServer::buildNukiConfigHtml(PsychicRequest *request) response.print(""); printCheckBox(&response, "LOCKENA", "Nuki Lock enabled", _preferences->getBool(preference_lock_enabled), ""); printCheckBox(&response, "OPENA", "Nuki Opener enabled", _preferences->getBool(preference_opener_enabled), ""); + printCheckBox(&response, "CONNMODE", "New Nuki Bluetooth connection mode (disable if there are connection issues)", _preferences->getBool(preference_connect_mode, false), ""); response.print("

"); response.print("

Advanced Nuki Configuration

"); response.print(""); diff --git a/src/main.cpp b/src/main.cpp index f585a43..71ec8c5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include "esp_https_ota.h" #include "esp_task_wdt.h" #include "Config.h" +#include "esp32-hal-log.h" #ifndef NUKI_HUB_UPDATER #include "NukiWrapper.h" @@ -115,17 +116,31 @@ int _log_vprintf(const char *fmt, va_list args) void setReroute() { esp_log_set_vprintf(_log_vprintf); + + #ifdef DEBUG_NUKIHUB + esp_log_level_set("*", ESP_LOG_DEBUG); + esp_log_level_set("nvs", ESP_LOG_INFO); + esp_log_level_set("wifi", ESP_LOG_INFO); + #else + esp_log_level_set("*", ESP_LOG_NONE); + esp_log_level_set("httpd", ESP_LOG_ERROR); + esp_log_level_set("httpd_sess", ESP_LOG_ERROR); + esp_log_level_set("httpd_parse", ESP_LOG_ERROR); + esp_log_level_set("httpd_txrx", ESP_LOG_ERROR); + esp_log_level_set("httpd_uri", ESP_LOG_ERROR); + esp_log_level_set("event", ESP_LOG_ERROR); + esp_log_level_set("psychic", ESP_LOG_ERROR); + esp_log_level_set("ARDUINO", ESP_LOG_DEBUG); + esp_log_level_set("nvs", ESP_LOG_ERROR); + esp_log_level_set("wifi", ESP_LOG_ERROR); + + #endif + if(preferences->getBool(preference_mqtt_log_enabled)) { - esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("mqtt", ESP_LOG_NONE); } - else - { - esp_log_level_set("*", ESP_LOG_DEBUG); - esp_log_level_set("nvs", ESP_LOG_INFO); - esp_log_level_set("wifi", ESP_LOG_INFO); - } + } #endif @@ -174,13 +189,13 @@ void networkTask(void *pvParameters) network->update(); bool connected = network->isConnected(); -#ifdef DEBUG_NUKIHUB + #ifndef NUKI_HUB_UPDATER if(connected && reroute) { reroute = false; setReroute(); } -#endif + #endif #ifndef NUKI_HUB_UPDATER wifiConnected = network->wifiConnected();