From 7ce71b8beab0671c0499ec65f456c2994c7bfb0b Mon Sep 17 00:00:00 2001 From: technyon Date: Wed, 6 Apr 2022 18:24:17 +0200 Subject: [PATCH] reduce scan interval; publish rssi --- NukiWrapper.cpp | 2 +- PresenceDetection.cpp | 36 +++++++++++++++++++++++++++++++++++- PresenceDetection.h | 4 +++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/NukiWrapper.cpp b/NukiWrapper.cpp index 286b075..029406d 100644 --- a/NukiWrapper.cpp +++ b/NukiWrapper.cpp @@ -31,7 +31,7 @@ void NukiWrapper::initialize() { _bleScanner = new BleScanner(); _bleScanner->initialize(); - _bleScanner->setScanDuration(30); + _bleScanner->setScanDuration(10); _nukiBle.initialize(); _nukiBle.registerBleScanner(_bleScanner); diff --git a/PresenceDetection.cpp b/PresenceDetection.cpp index fe41bfd..0eafdc8 100644 --- a/PresenceDetection.cpp +++ b/PresenceDetection.cpp @@ -53,7 +53,7 @@ void PresenceDetection::update() } // Prevent csv buffer overflow - if(_csvIndex > presence_detection_buffer_size - (sizeof(it.second.name) + sizeof(it.second.address) + 3)) + if(_csvIndex > presence_detection_buffer_size - (sizeof(it.second.name) + sizeof(it.second.address) + 10)) { break; } @@ -82,6 +82,24 @@ void PresenceDetection::buildCsv(const PdDevice &device) ++_csvIndex; ++i; } + + _csv[_csvIndex] = ';'; + ++_csvIndex; + + if(device.hasRssi) + { + char rssiStr[20] = {0}; + itoa(device.rssi, rssiStr, 10); + + int i=0; + while(rssiStr[i] != 0x00 && i < 20) + { + _csv[_csvIndex] = rssiStr[i]; + ++_csvIndex; + ++i; + } + } + _csv[_csvIndex] = '\n'; _csvIndex++; } @@ -136,16 +154,32 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device) _devices[addr] = pdDevice; } + + if(device->haveRSSI()) + { + pdDevice.hasRssi = true; + pdDevice.rssi = device->getRSSI(); + } } else { it->second.timestamp = millis(); + if(device->haveRSSI()) + { + it->second.hasRssi = true; + it->second.rssi = device->getRSSI(); + } } // if(device->haveName()) // { // Serial.print(" | "); // Serial.print(device->getName().c_str()); +// if(device->haveRSSI()) +// { +// Serial.print(" | "); +// Serial.print(device->getRSSI()); +// } // } // Serial.println(); diff --git a/PresenceDetection.h b/PresenceDetection.h index b0419be..bef9c91 100644 --- a/PresenceDetection.h +++ b/PresenceDetection.h @@ -8,7 +8,9 @@ struct PdDevice { char address[18] = {0}; char name[30] = {0}; - unsigned long timestamp; + unsigned long timestamp = 0; + int rssi = 0; + bool hasRssi = false; }; #define presence_detection_buffer_size 4096