From 928ee7b1520596dd0ff2c7c460b92815a15d5527 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Sun, 10 Dec 2023 10:29:34 +0300 Subject: [PATCH] wip --- components/esp32_presense/MiFloraHandler.cpp | 1 + components/esp32_presense/__init__.py | 6 +- components/esp32_presense/defaults.h | 3 + components/esp32_presense/esp32_presense.cpp | 67 ++++++++++++++++++++ components/esp32_presense/esp32_presense.h | 4 +- 5 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 components/esp32_presense/defaults.h diff --git a/components/esp32_presense/MiFloraHandler.cpp b/components/esp32_presense/MiFloraHandler.cpp index 9e062c3..fd02e0b 100644 --- a/components/esp32_presense/MiFloraHandler.cpp +++ b/components/esp32_presense/MiFloraHandler.cpp @@ -1,4 +1,5 @@ #include "MiFloraHandler.h" +#include "NimBLEAttValue.h" namespace MiFloraHandler { diff --git a/components/esp32_presense/__init__.py b/components/esp32_presense/__init__.py index fb3af0c..3b4a8cb 100644 --- a/components/esp32_presense/__init__.py +++ b/components/esp32_presense/__init__.py @@ -42,11 +42,7 @@ async def to_code(config): add_idf_sdkconfig_option("CONFIG_BT_NIMBLE_ENABLED", True) add_idf_sdkconfig_option("CONFIG_MBEDTLS_HARDWARE_AES", False) - cg.add_library( - name="esp-nimble-cpp", - repository="https://github.com/h2zero/esp-nimble-cpp", - version="1.4.1" - ) + cg.add_library("esp-nimble-cpp", repository="https://github.com/h2zero/esp-nimble-cpp#v1.4.1", version="v1.4.1") # await nimble_tracker.device_listener_to_code(var, config) # await nimble_tracker.register_ble_device(var, config) \ No newline at end of file diff --git a/components/esp32_presense/defaults.h b/components/esp32_presense/defaults.h new file mode 100644 index 0000000..4174b29 --- /dev/null +++ b/components/esp32_presense/defaults.h @@ -0,0 +1,3 @@ +#define BLE_SCAN_INTERVAL 0x80 +#define BLE_SCAN_WINDOW 0x80 +#define SCAN_TASK_STACK_SIZE 2562 \ No newline at end of file diff --git a/components/esp32_presense/esp32_presense.cpp b/components/esp32_presense/esp32_presense.cpp index a1ad4a6..b546937 100644 --- a/components/esp32_presense/esp32_presense.cpp +++ b/components/esp32_presense/esp32_presense.cpp @@ -1,4 +1,5 @@ #include "esp32_presense.h" +#include "defaults.h" namespace esphome { @@ -8,17 +9,83 @@ namespace esphome unsigned int totalFpSeen = 0; unsigned int totalFpQueried = 0; unsigned int totalFpReported = 0; + + TimerHandle_t reconnectTimer; + TaskHandle_t scanTaskHandle; + unsigned long updateStartedMillis = 0; + unsigned long lastTeleMillis = 0; + int reconnectTries = 0; + int teleFails = 0; + int reportFailed = 0; + bool online = false; // Have we successfully sent status=online + bool sentDiscovery = false; // Have we successfully sent discovery + UBaseType_t bleStack = 0; + bool discovery, publishTele, publishRooms, publishDevices; + + class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks { + void onResult(BLEAdvertisedDevice *advertisedDevice) { + bleStack = uxTaskGetStackHighWaterMark(nullptr); + BleFingerprintCollection::Seen(advertisedDevice); + } + }; + + void scanTask(void *parameter) { + NimBLEDevice::init("ESPresense"); + // Enrollment::Setup(); + NimBLEDevice::setMTU(23); + + auto pBLEScan = NimBLEDevice::getScan(); + pBLEScan->setInterval(BLE_SCAN_INTERVAL); + pBLEScan->setWindow(BLE_SCAN_WINDOW); + pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(), true); + pBLEScan->setActiveScan(false); + pBLEScan->setDuplicateFilter(false); + pBLEScan->setMaxResults(0); + if (!pBLEScan->start(0, nullptr, false)) + ESP_LOGE(TAG, "Error starting continuous ble scan"); + + while (true) { + for (auto &f : BleFingerprintCollection::fingerprints) + if (f->query()) + totalFpQueried++; + + // Enrollment::Loop(); + + if (!pBLEScan->isScanning()) { + if (!pBLEScan->start(0, nullptr, true)) + ESP_LOGE(TAG, "Error re-starting continuous ble scan"); + delay(3000); // If we stopped scanning, don't query for 3 seconds in order for us to catch any missed broadcasts + } else { + delay(100); + } + } + } + + void ESP32Presense::setup() + { + BleFingerprintCollection::Setup(); + xTaskCreatePinnedToCore(scanTask, "scanTask", SCAN_TASK_STACK_SIZE, nullptr, 1, &scanTaskHandle, CONFIG_BT_NIMBLE_PINNED_TO_CORE); + } + + void ESP32Presense::loop() + { + reportLoop(); + } void ESP32Presense::reportLoop() { auto copy = BleFingerprintCollection::GetCopy(); unsigned int count = 0; + for (auto &i : copy) if (i->shouldCount()) count++; + + yield(); sendTelemetry(totalSeen, totalFpSeen, totalFpQueried, totalFpReported, count); + yield(); auto reported = 0; for (auto &f : copy) { diff --git a/components/esp32_presense/esp32_presense.h b/components/esp32_presense/esp32_presense.h index 58d3214..2a49599 100644 --- a/components/esp32_presense/esp32_presense.h +++ b/components/esp32_presense/esp32_presense.h @@ -26,8 +26,8 @@ namespace esphome unsigned int count ); public: - void setup() {}; - void loop() {}; + void setup(); + void loop(); void reportLoop(); // void on_result(nimble_distance_custom::NimbleDistanceCustomResult&) override; void set_room(std::string room) { room_ = room; }