Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
4dbb575a15 | |||
d021d7c29d | |||
ae7e408b4e | |||
7b3b746a1b | |||
37989e25ae | |||
922dbf98b9 | |||
34089f1add | |||
4c710f7a0a | |||
b766c0ce04 | |||
32fe442cce | |||
61f58a5963 | |||
fbb0c27ce6 |
@ -19,7 +19,7 @@ class ClientCallbacks : public BLEClientCallbacks {
|
|||||||
static ClientCallbacks clientCB;
|
static ClientCallbacks clientCB;
|
||||||
|
|
||||||
BleFingerprint::BleFingerprint(BLEAdvertisedDevice *advertisedDevice, float fcmin, float beta, float dcutoff) : filteredDistance{FilteredDistance(fcmin, beta, dcutoff)} {
|
BleFingerprint::BleFingerprint(BLEAdvertisedDevice *advertisedDevice, float fcmin, float beta, float dcutoff) : filteredDistance{FilteredDistance(fcmin, beta, dcutoff)} {
|
||||||
firstSeenMillis = millis();
|
firstSeenMillis = esphome::millis();
|
||||||
address = NimBLEAddress(advertisedDevice->getAddress());
|
address = NimBLEAddress(advertisedDevice->getAddress());
|
||||||
addressType = advertisedDevice->getAddressType();
|
addressType = advertisedDevice->getAddressType();
|
||||||
rssi = advertisedDevice->getRSSI();
|
rssi = advertisedDevice->getRSSI();
|
||||||
@ -70,10 +70,10 @@ bool BleFingerprint::setId(const std::string &newId, short newIdType, const std:
|
|||||||
qryAttempts = 0;
|
qryAttempts = 0;
|
||||||
if (rssi < -80) {
|
if (rssi < -80) {
|
||||||
qryDelayMillis = 30000;
|
qryDelayMillis = 30000;
|
||||||
lastQryMillis = millis();
|
lastQryMillis = esphome::millis();
|
||||||
} else if (rssi < -70) {
|
} else if (rssi < -70) {
|
||||||
qryDelayMillis = 5000;
|
qryDelayMillis = 5000;
|
||||||
lastQryMillis = millis();
|
lastQryMillis = esphome::millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -411,7 +411,7 @@ void BleFingerprint::fingerprintManufactureData(NimBLEAdvertisedDevice *advertis
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool BleFingerprint::seen(BLEAdvertisedDevice *advertisedDevice) {
|
bool BleFingerprint::seen(BLEAdvertisedDevice *advertisedDevice) {
|
||||||
lastSeenMillis = millis();
|
lastSeenMillis = esphome::millis();
|
||||||
reported = false;
|
reported = false;
|
||||||
|
|
||||||
seenCount++;
|
seenCount++;
|
||||||
@ -443,17 +443,17 @@ bool BleFingerprint::fill(JsonObject *doc) {
|
|||||||
(*doc)["rssi@1m"] = get1mRssi();
|
(*doc)["rssi@1m"] = get1mRssi();
|
||||||
(*doc)["rssi"] = rssi;
|
(*doc)["rssi"] = rssi;
|
||||||
|
|
||||||
if (isnormal(raw)) (*doc)["raw"] = serialized(std::string(raw, 2));
|
if (isnormal(raw)) (*doc)["raw"] = serialized(std::to_string(raw));
|
||||||
if (isnormal(dist)) (*doc)["distance"] = serialized(std::string(dist, 2));
|
if (isnormal(dist)) (*doc)["distance"] = serialized(std::to_string(dist));
|
||||||
if (isnormal(vari)) (*doc)["var"] = serialized(std::string(vari, 2));
|
if (isnormal(vari)) (*doc)["var"] = serialized(std::to_string(vari));
|
||||||
if (close) (*doc)["close"] = true;
|
if (close) (*doc)["close"] = true;
|
||||||
|
|
||||||
(*doc)["int"] = (millis() - firstSeenMillis) / seenCount;
|
(*doc)["int"] = (millis() - firstSeenMillis) / seenCount;
|
||||||
|
|
||||||
if (mv) (*doc)["mV"] = mv;
|
if (mv) (*doc)["mV"] = mv;
|
||||||
if (battery != 0xFF) (*doc)["batt"] = battery;
|
if (battery != 0xFF) (*doc)["batt"] = battery;
|
||||||
if (temp) (*doc)["temp"] = serialized(std::string(temp, 1));
|
if (temp) (*doc)["temp"] = serialized(std::to_string(temp));
|
||||||
if (humidity) (*doc)["rh"] = serialized(std::string(humidity, 1));
|
if (humidity) (*doc)["rh"] = serialized(std::to_string(humidity));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -465,7 +465,7 @@ bool BleFingerprint::report(JsonObject *doc) {
|
|||||||
if (maxDistance > 0 && dist > maxDistance)
|
if (maxDistance > 0 && dist > maxDistance)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
auto now = millis();
|
auto now = esphome::millis();
|
||||||
if ((abs(dist - lastReported) < BleFingerprintCollection::skipDistance) && (lastReportedMillis > 0) && (now - lastReportedMillis < BleFingerprintCollection::skipMs))
|
if ((abs(dist - lastReported) < BleFingerprintCollection::skipDistance) && (lastReportedMillis > 0) && (now - lastReportedMillis < BleFingerprintCollection::skipMs))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -483,7 +483,7 @@ bool BleFingerprint::query() {
|
|||||||
if (!allowQuery || isQuerying) return false;
|
if (!allowQuery || isQuerying) return false;
|
||||||
if (rssi < -90) return false; // Too far away
|
if (rssi < -90) return false; // Too far away
|
||||||
|
|
||||||
auto now = millis();
|
auto now = esphome::millis();
|
||||||
if (now - lastSeenMillis > 5) return false; // Haven't seen lately
|
if (now - lastSeenMillis > 5) return false; // Haven't seen lately
|
||||||
if (now - lastQryMillis < qryDelayMillis) return false; // Too soon
|
if (now - lastQryMillis < qryDelayMillis) return false; // Too soon
|
||||||
|
|
||||||
|
@ -20,8 +20,6 @@
|
|||||||
#include "esphome/core/time.h"
|
#include "esphome/core/time.h"
|
||||||
|
|
||||||
#define TAG "esp32_presense"
|
#define TAG "esp32_presense"
|
||||||
#define millis esphome::millis
|
|
||||||
// #define delay esphome::delay
|
|
||||||
|
|
||||||
#define NO_RSSI int8_t(-128)
|
#define NO_RSSI int8_t(-128)
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ async def to_code(config):
|
|||||||
cg.add(var.set_room(config[CONF_ROOM_KEY]))
|
cg.add(var.set_room(config[CONF_ROOM_KEY]))
|
||||||
# cg.add(var.set_base_topic(config[CONF_BASE_TOPIC_KEY]))
|
# cg.add(var.set_base_topic(config[CONF_BASE_TOPIC_KEY]))
|
||||||
# cg.add(var.set_addresses(config[CONF_MAC_KEY]))
|
# cg.add(var.set_addresses(config[CONF_MAC_KEY]))
|
||||||
# cg.add(var.set_max_distance(config[CONF_MAX_DISTANCE]))
|
cg.add(var.set_max_distance(config[CONF_MAX_DISTANCE]))
|
||||||
|
|
||||||
add_idf_sdkconfig_option("CONFIG_BT_ENABLED", True)
|
add_idf_sdkconfig_option("CONFIG_BT_ENABLED", True)
|
||||||
add_idf_sdkconfig_option("CONFIG_BT_BLUEDROID_ENABLED", False)
|
add_idf_sdkconfig_option("CONFIG_BT_BLUEDROID_ENABLED", False)
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
#define BLE_SCAN_INTERVAL 0x80
|
#define BLE_SCAN_INTERVAL 0x80
|
||||||
#define BLE_SCAN_WINDOW 0x80
|
#define BLE_SCAN_WINDOW 0x80
|
||||||
#define SCAN_TASK_STACK_SIZE 2562
|
#define SCAN_TASK_STACK_SIZE 2562
|
||||||
|
|
||||||
|
#define CHANNEL "espresense"
|
@ -1,5 +1,4 @@
|
|||||||
#include "esp32_presense.h"
|
#include "esp32_presense.h"
|
||||||
#include "defaults.h"
|
|
||||||
|
|
||||||
namespace esphome
|
namespace esphome
|
||||||
{
|
{
|
||||||
@ -22,6 +21,9 @@ namespace esphome
|
|||||||
bool sentDiscovery = false; // Have we successfully sent discovery
|
bool sentDiscovery = false; // Have we successfully sent discovery
|
||||||
UBaseType_t bleStack = 0;
|
UBaseType_t bleStack = 0;
|
||||||
|
|
||||||
|
DynamicJsonDocument doc(1024);
|
||||||
|
std::string _id, roomsTopic;
|
||||||
|
|
||||||
bool discovery, publishTele, publishRooms, publishDevices;
|
bool discovery, publishTele, publishRooms, publishDevices;
|
||||||
|
|
||||||
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
|
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
|
||||||
@ -63,20 +65,31 @@ namespace esphome
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ESP32Presense::set_room(std::string room) {
|
||||||
|
_id = slugify(room);
|
||||||
|
roomsTopic = std::string(CHANNEL) + std::string("/rooms/") + _id;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESP32Presense::set_max_distance(float maxDistance) {
|
||||||
|
BleFingerprintCollection::maxDistance = maxDistance;
|
||||||
|
}
|
||||||
|
|
||||||
void ESP32Presense::setup()
|
void ESP32Presense::setup()
|
||||||
{
|
{
|
||||||
ESP_LOGD(TAG, "SETUP1");
|
|
||||||
BleFingerprintCollection::Setup();
|
BleFingerprintCollection::Setup();
|
||||||
ESP_LOGD(TAG, "SETUP2");
|
|
||||||
xTaskCreatePinnedToCore(scanTask, "scanTask", SCAN_TASK_STACK_SIZE, nullptr, 1, &scanTaskHandle, CONFIG_BT_NIMBLE_PINNED_TO_CORE);
|
xTaskCreatePinnedToCore(scanTask, "scanTask", SCAN_TASK_STACK_SIZE, nullptr, 1, &scanTaskHandle, CONFIG_BT_NIMBLE_PINNED_TO_CORE);
|
||||||
ESP_LOGD(TAG, "SETUP3");
|
publishDevices = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESP32Presense::loop()
|
void ESP32Presense::loop()
|
||||||
{
|
{
|
||||||
ESP_LOGD(TAG, "LOOP1");
|
|
||||||
reportLoop();
|
reportLoop();
|
||||||
ESP_LOGD(TAG, "LOOP2");
|
}
|
||||||
|
|
||||||
|
bool ESP32Presense::reportBuffer(BleFingerprint *f) {
|
||||||
|
auto report = f->getReport();
|
||||||
|
std::string topic = Sprintf(CHANNEL "/devices/%s/%s/%s", f->getId().c_str(), _id.c_str(), report.getId().c_str());
|
||||||
|
return this->publish(topic, report.getPayload());
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESP32Presense::reportLoop()
|
void ESP32Presense::reportLoop()
|
||||||
@ -100,20 +113,47 @@ namespace esphome
|
|||||||
totalFpSeen++;
|
totalFpSeen++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
if (f->hasReport()) {
|
if (f->hasReport()) {
|
||||||
if (reportBuffer(f))
|
if (reportBuffer(f)) {
|
||||||
f->clearReport();
|
f->clearReport();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (reportDevice(f)) {
|
|
||||||
|
if (this->reportDevice(f)) {
|
||||||
totalFpReported++;
|
totalFpReported++;
|
||||||
reported++;
|
reported++;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ESP32Presense::reportDevice(BleFingerprint *f) {
|
||||||
|
doc.clear();
|
||||||
|
JsonObject obj = doc.to<JsonObject>();
|
||||||
|
if (!f->report(&obj))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
std::string buffer;
|
||||||
|
serializeJson(doc, buffer);
|
||||||
|
std::string devicesTopic = Sprintf(CHANNEL "/devices/%s/%s", f->getId().c_str(), _id.c_str());
|
||||||
|
|
||||||
|
bool p1 = false, p2 = false;
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
if (!p1 && (!publishRooms || this->publish(roomsTopic.c_str(), buffer.c_str())))
|
||||||
|
p1 = true;
|
||||||
|
|
||||||
|
if (!p2 && (!publishDevices || this->publish(devicesTopic.c_str(), buffer.c_str())))
|
||||||
|
p2 = true;
|
||||||
|
|
||||||
|
if (p1 && p2)
|
||||||
|
return true;
|
||||||
|
delay(20);
|
||||||
|
}
|
||||||
|
|
||||||
|
reportFailed++;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool ESP32Presense::sendTelemetry(
|
bool ESP32Presense::sendTelemetry(
|
||||||
unsigned int totalSeen,
|
unsigned int totalSeen,
|
||||||
unsigned int totalFpSeen,
|
unsigned int totalFpSeen,
|
||||||
@ -121,17 +161,17 @@ namespace esphome
|
|||||||
unsigned int totalFpReported,
|
unsigned int totalFpReported,
|
||||||
unsigned int count
|
unsigned int count
|
||||||
) {
|
) {
|
||||||
this->publish(this->room_ + "/status", "online");
|
this->publish(roomsTopic + "/status", "online");
|
||||||
this->publish(this->room_ + "/max_distance", BleFingerprintCollection::maxDistance);
|
this->publish(roomsTopic + "/max_distance", BleFingerprintCollection::maxDistance);
|
||||||
this->publish(this->room_ + "/absorption", BleFingerprintCollection::absorption);
|
this->publish(roomsTopic + "/absorption", BleFingerprintCollection::absorption);
|
||||||
this->publish(this->room_ + "/tx_ref_rssi", BleFingerprintCollection::txRefRssi);
|
this->publish(roomsTopic + "/tx_ref_rssi", BleFingerprintCollection::txRefRssi);
|
||||||
this->publish(this->room_ + "/rx_adj_rssi", BleFingerprintCollection::rxAdjRssi);
|
this->publish(roomsTopic + "/rx_adj_rssi", BleFingerprintCollection::rxAdjRssi);
|
||||||
this->publish(this->room_ + "/query", BleFingerprintCollection::query);
|
this->publish(roomsTopic + "/query", BleFingerprintCollection::query);
|
||||||
this->publish(this->room_ + "/include", BleFingerprintCollection::include);
|
this->publish(roomsTopic + "/include", BleFingerprintCollection::include);
|
||||||
this->publish(this->room_ + "/exclude", BleFingerprintCollection::exclude);
|
this->publish(roomsTopic + "/exclude", BleFingerprintCollection::exclude);
|
||||||
this->publish(this->room_ + "/known_macs", BleFingerprintCollection::knownMacs);
|
this->publish(roomsTopic + "/known_macs", BleFingerprintCollection::knownMacs);
|
||||||
this->publish(this->room_ + "/known_irks", BleFingerprintCollection::knownIrks);
|
this->publish(roomsTopic + "/known_irks", BleFingerprintCollection::knownIrks);
|
||||||
this->publish(this->room_ + "/count_ids", BleFingerprintCollection::countIds);
|
this->publish(roomsTopic + "/count_ids", BleFingerprintCollection::countIds);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <ArduinoJson.h>
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/components/mqtt/custom_mqtt_device.h"
|
#include "esphome/components/mqtt/custom_mqtt_device.h"
|
||||||
#include "BleFingerprintCollection.h"
|
#include "BleFingerprintCollection.h"
|
||||||
|
#include "defaults.h"
|
||||||
// #include "esphome/components/nimble_distance_custom/nimble_distance_custom.h"
|
// #include "esphome/components/nimble_distance_custom/nimble_distance_custom.h"
|
||||||
|
|
||||||
namespace esphome
|
namespace esphome
|
||||||
@ -15,8 +17,6 @@ namespace esphome
|
|||||||
// public nimble_distance_custom::NimbleDistanceCustomComponent
|
// public nimble_distance_custom::NimbleDistanceCustomComponent
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
std::string room_;
|
|
||||||
|
|
||||||
BleFingerprint *ble_fingerprint = NULL;
|
BleFingerprint *ble_fingerprint = NULL;
|
||||||
|
|
||||||
bool sendTelemetry(
|
bool sendTelemetry(
|
||||||
@ -31,7 +31,10 @@ namespace esphome
|
|||||||
void loop() override;
|
void loop() override;
|
||||||
void reportLoop();
|
void reportLoop();
|
||||||
// void on_result(nimble_distance_custom::NimbleDistanceCustomResult&) override;
|
// void on_result(nimble_distance_custom::NimbleDistanceCustomResult&) override;
|
||||||
void set_room(std::string room) { room_ = room; }
|
void set_room(std::string room);
|
||||||
|
bool reportBuffer(BleFingerprint *f);
|
||||||
|
bool reportDevice(BleFingerprint *f);
|
||||||
|
void set_max_distance(float maxDistance);
|
||||||
//void set_base_topic(std::string base_topic) { base_topic_ = base_topic; }
|
//void set_base_topic(std::string base_topic) { base_topic_ = base_topic; }
|
||||||
};
|
};
|
||||||
} // namespace esp32_nimble_tracker
|
} // namespace esp32_nimble_tracker
|
||||||
|
Loading…
Reference in New Issue
Block a user