Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
61f58a5963 | |||
fbb0c27ce6 | |||
7d897ae764 | |||
fee9d3d3ab | |||
38b0dc4092 | |||
e47b9a9df9 | |||
0634843815 | |||
928ee7b152 |
@@ -1,4 +1,5 @@
|
||||
#include "MiFloraHandler.h"
|
||||
#include "NimBLEAttValue.h"
|
||||
|
||||
namespace MiFloraHandler {
|
||||
|
||||
|
@@ -30,7 +30,7 @@ CONFIG_SCHEMA = cv.Schema({
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
# await cg.register_component(var, config)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
cg.add(var.set_room(config[CONF_ROOM_KEY]))
|
||||
# cg.add(var.set_base_topic(config[CONF_BASE_TOPIC_KEY]))
|
||||
@@ -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)
|
5
components/esp32_presense/defaults.h
Normal file
5
components/esp32_presense/defaults.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#define BLE_SCAN_INTERVAL 0x80
|
||||
#define BLE_SCAN_WINDOW 0x80
|
||||
#define SCAN_TASK_STACK_SIZE 2562
|
||||
|
||||
#define CHANNEL "espresense"
|
@@ -8,17 +8,101 @@ 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;
|
||||
|
||||
DynamicJsonDocument doc(1024);
|
||||
std::string _id, roomsTopic;
|
||||
|
||||
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::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()
|
||||
{
|
||||
BleFingerprintCollection::Setup();
|
||||
xTaskCreatePinnedToCore(scanTask, "scanTask", SCAN_TASK_STACK_SIZE, nullptr, 1, &scanTaskHandle, CONFIG_BT_NIMBLE_PINNED_TO_CORE);
|
||||
}
|
||||
|
||||
void ESP32Presense::loop()
|
||||
{
|
||||
reportLoop();
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
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) {
|
||||
@@ -28,20 +112,46 @@ namespace esphome
|
||||
totalFpSeen++;
|
||||
}
|
||||
|
||||
/*
|
||||
if (f->hasReport()) {
|
||||
if (reportBuffer(f))
|
||||
f->clearReport();
|
||||
}
|
||||
|
||||
if (reportDevice(f)) {
|
||||
|
||||
if (this->reportDevice(f)) {
|
||||
totalFpReported++;
|
||||
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(
|
||||
unsigned int totalSeen,
|
||||
unsigned int totalFpSeen,
|
||||
@@ -49,17 +159,17 @@ namespace esphome
|
||||
unsigned int totalFpReported,
|
||||
unsigned int count
|
||||
) {
|
||||
this->publish(this->room_ + "/status", "online");
|
||||
this->publish(this->room_ + "/max_distance", BleFingerprintCollection::maxDistance);
|
||||
this->publish(this->room_ + "/absorption", BleFingerprintCollection::absorption);
|
||||
this->publish(this->room_ + "/tx_ref_rssi", BleFingerprintCollection::txRefRssi);
|
||||
this->publish(this->room_ + "/rx_adj_rssi", BleFingerprintCollection::rxAdjRssi);
|
||||
this->publish(this->room_ + "/query", BleFingerprintCollection::query);
|
||||
this->publish(this->room_ + "/include", BleFingerprintCollection::include);
|
||||
this->publish(this->room_ + "/exclude", BleFingerprintCollection::exclude);
|
||||
this->publish(this->room_ + "/known_macs", BleFingerprintCollection::knownMacs);
|
||||
this->publish(this->room_ + "/known_irks", BleFingerprintCollection::knownIrks);
|
||||
this->publish(this->room_ + "/count_ids", BleFingerprintCollection::countIds);
|
||||
this->publish(roomsTopic + "/status", "online");
|
||||
this->publish(roomsTopic + "/max_distance", BleFingerprintCollection::maxDistance);
|
||||
this->publish(roomsTopic + "/absorption", BleFingerprintCollection::absorption);
|
||||
this->publish(roomsTopic + "/tx_ref_rssi", BleFingerprintCollection::txRefRssi);
|
||||
this->publish(roomsTopic + "/rx_adj_rssi", BleFingerprintCollection::rxAdjRssi);
|
||||
this->publish(roomsTopic + "/query", BleFingerprintCollection::query);
|
||||
this->publish(roomsTopic + "/include", BleFingerprintCollection::include);
|
||||
this->publish(roomsTopic + "/exclude", BleFingerprintCollection::exclude);
|
||||
this->publish(roomsTopic + "/known_macs", BleFingerprintCollection::knownMacs);
|
||||
this->publish(roomsTopic + "/known_irks", BleFingerprintCollection::knownIrks);
|
||||
this->publish(roomsTopic + "/count_ids", BleFingerprintCollection::countIds);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@@ -1,21 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/mqtt/custom_mqtt_device.h"
|
||||
#include "BleFingerprintCollection.h"
|
||||
#include "defaults.h"
|
||||
// #include "esphome/components/nimble_distance_custom/nimble_distance_custom.h"
|
||||
|
||||
namespace esphome
|
||||
{
|
||||
namespace esp32_presense
|
||||
{
|
||||
class ESP32Presense :
|
||||
class ESP32Presense :
|
||||
public Component,
|
||||
public mqtt::CustomMQTTDevice
|
||||
// public nimble_distance_custom::NimbleDistanceCustomComponent
|
||||
{
|
||||
protected:
|
||||
std::string room_;
|
||||
|
||||
BleFingerprint *ble_fingerprint = NULL;
|
||||
|
||||
bool sendTelemetry(
|
||||
@@ -26,11 +27,14 @@ namespace esphome
|
||||
unsigned int count
|
||||
);
|
||||
public:
|
||||
void setup() {};
|
||||
void loop() {};
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
void reportLoop();
|
||||
// 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; }
|
||||
};
|
||||
} // namespace esp32_nimble_tracker
|
||||
|
Reference in New Issue
Block a user