Skip to content

Commit

Permalink
BLE fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
iranl committed Jul 25, 2024
1 parent a243978 commit 85ef2c0
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "lib/nuki_ble"]
path = lib/nuki_ble
url = https://github.com/I-Connect/NukiBleEsp32
url = https://github.com/iranl/NukiBleEsp32
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Feel free to join us on Discord: https://discord.gg/9nPq85bP4p
<b>Supported ESP32 devices:</b>
- Nuki Hub is compiled against all ESP32 models with Wi-Fi and Bluetooh Low Energy (BLE) which are supported by ESP-IDF 5.1.4 and Arduino Core 3.0.1.
- Tested stable builds are provided for the ESP32, ESP32-S3 and ESP32-C3.
- Support for the ESP32-C6 is ***HIGHLY*** experimental. Expect frequent crashes, especially when running Nuki Hub paired as an app (when not using in Hybrid mode). Pairing is also not supported yet and needs to be done manually on the /advanced page of the web configurator.
- The ESP32-Solo1 is not supported by ESP-IDF 5.1 and as such can't be build using Arduino Core 3 and ESP-IDF 5.1. Untested build against Arduino Core 2.0.14 and ESP-IDF 4.4 are provided.
- Support for the ESP32-C6 is experimental. There could be more frequent crashes than on other ESP32 devices and connections with the Nuki device are created more slowly than on other ESP32 devices. Pairing is also not supported yet and needs to be done manually on the /advanced page of the web configurator by copying the pairing information of another already paired ESP32(-S3/-C3) device running Nuki Hub.
- The ESP32-Solo1 is not supported by ESP-IDF 5.1 and as such can't be build using Arduino Core 3 and ESP-IDF 5.1. Untested builds against Arduino Core 2.0.14 and ESP-IDF 4.4 are provided.

<b>Not supported ESP32 devices:</b>
- The ESP32-S2 has no BLE and as such can't run Nuki Hub.
Expand Down
6 changes: 6 additions & 0 deletions lib/BleScanner/src/BleScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void Scanner::initialize(const std::string& deviceName, const bool wantDuplicate
}
BLEDevice::init(deviceName);
}

bleScan = BLEDevice::getScan();

#ifndef BLESCANNER_USE_LATEST_NIMBLE
Expand Down Expand Up @@ -98,4 +99,9 @@ void Scanner::onResult(NimBLEAdvertisedDevice* advertisedDevice) {
}
}

void Scanner::whitelist(BLEAddress bleAddress) {
BLEDevice::whiteListAdd(bleAddress);
bleScan->setFilterPolicy(BLE_HCI_SCAN_FILT_USE_WL);
}

} // namespace BleScanner
8 changes: 8 additions & 0 deletions lib/BleScanner/src/BleScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ class Scanner : public Publisher, BLEAdvertisedDeviceCallbacks {
* @param advertisedDevice
*/
void onResult(NimBLEAdvertisedDevice* advertisedDevice) override;

/**
* @brief Whitelist a specific BLE Address
*
* @param whiteListBleAddress
*/
void whitelist(BLEAddress bleAddress);


private:
uint32_t scanDuration = 0; //default indefinite scanning time
Expand Down
2 changes: 0 additions & 2 deletions lib/esp-nimble-cpp/src/NimBLEClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,6 @@ bool NimBLEClient::connect(const NimBLEAddress &address, bool deleteAttributes)
if(isConnected()) {
NIMBLE_LOGE(LOG_TAG, "Connect timeout - no response");
disconnect();
NIMBLE_LOGE(LOG_TAG, "Connect timeout - cancelling");
ble_gap_conn_cancel();
} else {
// workaround; if the controller doesn't cancel the connection
// at the timeout, cancel it here.
Expand Down
2 changes: 1 addition & 1 deletion lib/nuki_ble
Submodule nuki_ble updated 3 files
+201 −10 src/NukiBle.cpp
+18 −3 src/NukiBle.h
+3 −1 src/NukiBle.hpp
113 changes: 60 additions & 53 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ default_envs = esp32dev
boards_dir = boards

[env]
platform = https://github.com/tasmota/platform-espressif32/releases/download/2024.06.11/platform-espressif32.zip
platform = https://github.com/tasmota/platform-espressif32/releases/download/2024.07.11/platform-espressif32.zip
platform_packages =
framework = arduino, espidf
build_type = release
Expand All @@ -25,13 +25,13 @@ build_unflags =
-Werror=all
-Wall
build_flags =
-fexceptions
-DTLS_CA_MAX_SIZE=2200
-DTLS_CERT_MAX_SIZE=1500
-DTLS_KEY_MAX_SIZE=1800
-DESP_PLATFORM
-DESP32
-DARDUINO_ARCH_ESP32
-fexceptions
-DTLS_CA_MAX_SIZE=2200
-DTLS_CERT_MAX_SIZE=1500
-DTLS_KEY_MAX_SIZE=1800
-DESP_PLATFORM
-DESP32
-DARDUINO_ARCH_ESP32
-DCONFIG_BTDM_BLE_SCAN_DUPL=y
-DCONFIG_ASYNC_TCP_MAX_ACK_TIME=3000
-DCONFIG_ASYNC_TCP_PRIORITY=10
Expand Down Expand Up @@ -61,14 +61,15 @@ monitor_filters =
board = esp32dev
extra_scripts = post:pio_package.py
build_flags =
${env.build_flags}
${env.build_flags}
-DNUKI_ALT_CONNECT
-DBLESCANNER_USE_LATEST_NIMBLE
-DNUKI_USE_LATEST_NIMBLE
-DNUKI_NO_WDT_RESET
-DNUKI_MUTEX_RECURSIVE
-DNUKI_64BIT_TIME
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_BT_NIMBLE_LOG_LEVEL=0

[env:esp32-c3]
Expand All @@ -89,11 +90,12 @@ framework = arduino
board = esp32-solo1
extra_scripts = post:pio_package.py
build_flags =
${env.build_flags}
-DFRAMEWORK_ARDUINO_SOLO1
${env.build_flags}
-DFRAMEWORK_ARDUINO_SOLO1
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_BT_NIMBLE_LOG_LEVEL=0
-DNUKI_ALT_CONNECT
-DNUKI_64BIT_TIME
lib_deps =
BleScanner=symlink://lib/BleScanner
Expand All @@ -107,99 +109,104 @@ lib_ignore =
extends = env:esp32dev
custom_build = debug
build_flags =
${env.build_flags}
${env.build_flags}
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_BT_NIMBLE_LOG_LEVEL=0
-DNUKI_ALT_CONNECT
-DBLESCANNER_USE_LATEST_NIMBLE
-DNUKI_USE_LATEST_NIMBLE
-DNUKI_NO_WDT_RESET
-DNUKI_MUTEX_RECURSIVE
-DNUKI_64BIT_TIME
-DDEBUG_NUKIHUB
-DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND
-DDEBUG_NUKI_CONNECT
-DDEBUG_NUKI_COMMUNICATION
;-DDEBUG_NUKI_HEX_DATA
-DDEBUG_NUKI_READABLE_DATA
-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
custom_build = debug
build_flags =
${env.build_flags}
${env.build_flags}
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_BT_NIMBLE_LOG_LEVEL=0
-DNUKI_ALT_CONNECT
-DBLESCANNER_USE_LATEST_NIMBLE
-DNUKI_USE_LATEST_NIMBLE
-DNUKI_NO_WDT_RESET
-DNUKI_MUTEX_RECURSIVE
-DNUKI_64BIT_TIME
-DDEBUG_NUKIHUB
-DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND
-DDEBUG_NUKI_CONNECT
-DDEBUG_NUKI_COMMUNICATION
;-DDEBUG_NUKI_HEX_DATA
-DDEBUG_NUKI_READABLE_DATA
-DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND
-DDEBUG_NUKI_CONNECT
-DDEBUG_NUKI_COMMUNICATION
;-DDEBUG_NUKI_HEX_DATA
-DDEBUG_NUKI_READABLE_DATA

[env:esp32-c6_dbg]
extends = env:esp32-c6
custom_build = debug
build_flags =
${env.build_flags}
${env.build_flags}
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_BT_NIMBLE_LOG_LEVEL=0
-DNUKI_ALT_CONNECT
-DBLESCANNER_USE_LATEST_NIMBLE
-DNUKI_USE_LATEST_NIMBLE
-DNUKI_NO_WDT_RESET
-DNUKI_MUTEX_RECURSIVE
-DNUKI_64BIT_TIME
-DDEBUG_NUKIHUB
-DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND
-DDEBUG_NUKI_CONNECT
-DDEBUG_NUKI_COMMUNICATION
;-DDEBUG_NUKI_HEX_DATA
-DDEBUG_NUKI_READABLE_DATA
-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
custom_build = debug
build_flags =
${env.build_flags}
${env.build_flags}
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_BT_NIMBLE_LOG_LEVEL=0
-DNUKI_ALT_CONNECT
-DBLESCANNER_USE_LATEST_NIMBLE
-DNUKI_USE_LATEST_NIMBLE
-DNUKI_NO_WDT_RESET
-DNUKI_MUTEX_RECURSIVE
-DNUKI_64BIT_TIME
-DDEBUG_NUKIHUB
-DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND
-DDEBUG_NUKI_CONNECT
-DDEBUG_NUKI_COMMUNICATION
;-DDEBUG_NUKI_HEX_DATA
-DDEBUG_NUKI_READABLE_DATA
-DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND
-DDEBUG_NUKI_CONNECT
-DDEBUG_NUKI_COMMUNICATION
;-DDEBUG_NUKI_HEX_DATA
-DDEBUG_NUKI_READABLE_DATA

[env:esp32solo1_dbg]
extends = env:esp32solo1
custom_build = debug
build_flags =
${env.build_flags}
${env.build_flags}
-DFRAMEWORK_ARDUINO_SOLO1
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DCONFIG_NIMBLE_CPP_LOG_LEVEL=0
-DNUKI_ALT_CONNECT
-DNUKI_64BIT_TIME
-DDEBUG_NUKIHUB
-DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND
-DDEBUG_NUKI_CONNECT
-DDEBUG_NUKI_COMMUNICATION
;-DDEBUG_NUKI_HEX_DATA
-DDEBUG_NUKI_READABLE_DATA
-DDEBUG_SENSE_NUKI
-DDEBUG_NUKI_COMMAND
-DDEBUG_NUKI_CONNECT
-DDEBUG_NUKI_COMMUNICATION
;-DDEBUG_NUKI_HEX_DATA
-DDEBUG_NUKI_READABLE_DATA
3 changes: 2 additions & 1 deletion sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ CONFIG_BT_NIMBLE_50_FEATURE_SUPPORT=n
CONFIG_IEEE802154_ENABLED=n
CONFIG_ARDUINO_SELECTIVE_COMPILATION=y
CONFIG_ARDUINO_SELECTIVE_HTTPClient=n
CONFIG_ARDUINO_SELECTIVE_WebServer=n
CONFIG_ARDUINO_SELECTIVE_WebServer=n
CONFIG_HEAP_TASK_TRACKING=n
3 changes: 2 additions & 1 deletion src/NukiOpenerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ void NukiOpenerWrapper::initialize()
}

_nukiOpener.setEventHandler(this);
_nukiOpener.setDisonnectTimeout(5000);
_nukiOpener.setConnectTimeout(3);
_nukiOpener.setDisconnectTimeout(5000);

Log->print(F("Lock state interval: "));
Log->print(_intervalLockstate);
Expand Down
3 changes: 2 additions & 1 deletion src/NukiWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ void NukiWrapper::initialize(const bool& firstStart)
}

_nukiLock.setEventHandler(this);
_nukiLock.setDisonnectTimeout(5000);
_nukiLock.setConnectTimeout(3);
_nukiLock.setDisconnectTimeout(5000);

Log->print(F("Lock state interval: "));
Log->print(_intervalLockstate);
Expand Down
2 changes: 1 addition & 1 deletion src/WebCfgServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ void WebCfgServer::sendSettings()
}
}

if(pairing && _preferences->getBool(preference_show_secrets))
if(pairing)
{
if(_nuki != nullptr)
{
Expand Down
17 changes: 16 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ void networkTask(void *pvParameters)
void nukiTask(void *pvParameters)
{
int64_t nukiLoopTs = 0;
bool whiteListed = false;

while(true)
{
Expand All @@ -128,6 +129,20 @@ void nukiTask(void *pvParameters)
{
delay(5000);
}
#ifndef PRESENCE_DETECTION_ENABLED
else if (!whiteListed)
{
whiteListed = true;
if(lockEnabled)
{
bleScanner->whitelist(nuki->getBleAddress());
}
if(openerEnabled)
{
bleScanner->whitelist(nukiOpener->getBleAddress());
}
}
#endif

if(lockEnabled)
{
Expand Down Expand Up @@ -393,7 +408,7 @@ void setup()
// Scan interval and window according to Nuki recommendations:
// https://developer.nuki.io/t/bluetooth-specification-questions/1109/27
bleScanner->initialize("NukiHub", true, 40, 40);
bleScanner->setScanDuration(10);
bleScanner->setScanDuration(0);

#if PRESENCE_DETECTION_ENABLED
if(preferences->getInt(preference_presence_detection_timeout) >= 0)
Expand Down
2 changes: 1 addition & 1 deletion updater/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ default_envs = updater_esp32dev
boards_dir = ../boards

[env]
platform = https://github.com/tasmota/platform-espressif32/releases/download/2024.06.11/platform-espressif32.zip
platform = https://github.com/tasmota/platform-espressif32/releases/download/2024.07.11/platform-espressif32.zip
platform_packages =
framework = arduino, espidf
build_type = release
Expand Down

0 comments on commit 85ef2c0

Please sign in to comment.