Add max_distance option

Closes #1
This commit is contained in:
Maxim Slipenko 2023-12-06 21:01:11 +03:00
parent bc6f6402eb
commit f0a839667d
6 changed files with 12 additions and 45 deletions

View File

@ -12,8 +12,8 @@ CONF_NIMBLE_ID = "esp32_nimble_mqtt_room"
CONF_ROOM_KEY = 'room'
CONF_BASE_TOPIC_KEY = 'base_topic'
# CONF_MY_OPTIONAL_KEY = 'my_optional_key'
CONF_MAC_KEY = 'mac_addr'
CONF_MAX_DISTANCE = 'max_distance'
esp32_nimble_tracker_ns = cg.esphome_ns.namespace("esp32_nimble_mqtt_room")
ESP32NimbleMQTTRoom = esp32_nimble_tracker_ns.class_(
@ -24,6 +24,7 @@ CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(ESP32NimbleMQTTRoom),
cv.Required(CONF_ROOM_KEY): cv.string,
cv.Required(CONF_MAC_KEY): cv.All(cv.ensure_list(cv.string)),
cv.Optional(CONF_MAX_DISTANCE, default=16.0): cv.float_,
cv.Optional(CONF_BASE_TOPIC_KEY, default="esphome_presense"): cv.string,
}).extend(cv.COMPONENT_SCHEMA).extend(nimble_tracker.NIMBLE_DEVICE_LISTENER_SCHEMA)
@ -34,6 +35,7 @@ async def to_code(config):
cg.add(var.set_room(config[CONF_ROOM_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_max_distance(config[CONF_MAX_DISTANCE]))
await nimble_tracker.device_listener_to_code(var, config)
await nimble_tracker.register_ble_device(var, config)

View File

@ -1,30 +0,0 @@
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.const import (
CONF_ID,
)
DEPENDENCIES = ["esp32"]
# CONF_NIMBLE_ID = "esp32_nimble_tracker"
CONF_MY_REQUIRED_KEY = 'my_required_key'
CONF_MY_OPTIONAL_KEY = 'my_optional_key'
esp32_nimble_tracker_ns = cg.esphome_ns.namespace("esp32_nimble_tracker")
ESP32NimbleTracker = esp32_nimble_tracker_ns.class_(
"ESP32NimbleTracker", cg.Component
)
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_id(ESP32NimbleTracker),
cv.Required(CONF_MY_REQUIRED_KEY): cv.string,
cv.Optional(CONF_MY_OPTIONAL_KEY, default=10): cv.int_,
}).extend(cv.COMPONENT_SCHEMA)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
await nimble_tracker.device_listener_to_code(var, config)
await nimble_tracker.register_ble_device(var, config)

View File

@ -1,12 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
namespace esphome
{
namespace esp32_nimble_tracker
{
class ESP32NimbleTracker : public Component {};
} // namespace esp32_nimble_tracker
} // namespace esphome

View File

@ -39,6 +39,11 @@ namespace esphome
this->filter_ = new Filter(ONE_EURO_FCMIN, ONE_EURO_BETA, ONE_EURO_DCUTOFF);
}
void NimbleDistanceCustomComponent::set_max_distance(float max_distance) {
this->max_distance_ = max_distance;
}
// Defined distance formula using
// https://medium.com/beingcoders/convert-rssi-value-of-the-ble-bluetooth-low-energy-beacons-to-meters-63259f307283
// and copied a lot of code from
@ -59,8 +64,7 @@ namespace esphome
return false;
}
auto max_distance = 16.0f;
if (max_distance > 0 && this->filter_->output.value.position > max_distance)
if (this->max_distance_ > 0 && this->filter_->output.value.position > this->max_distance_)
return false;
auto skip_distance = 0.5f;

View File

@ -49,6 +49,7 @@ namespace esphome
public:
void setup() override;
int get_1m_rssi(nimble_tracker::NimbleTrackerEvent *tracker_event);
void set_max_distance(float);
protected:
bool update_state(nimble_tracker::NimbleTrackerEvent *tracker_event) override;
@ -60,6 +61,8 @@ namespace esphome
float absorption_ = 3.5f;
float last_reported_position_ = 0;
int64_t last_reported_micro_seconds_ = 0;
float max_distance_ = 16.0;
};
} // namespace nimble_distance
} // namespace esphome