Skip to content

Commit

Permalink
bug fixes for presence detection
Browse files Browse the repository at this point in the history
  • Loading branch information
technyon committed Jun 8, 2022
1 parent 0f24e28 commit 20fc20e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
7 changes: 6 additions & 1 deletion Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ void Network::update()
if(_presenceCsv != nullptr && strlen(_presenceCsv) > 0)
{
publishString(mqtt_topic_presence, _presenceCsv);
// Serial.println(_presenceCsv);
_presenceCsv = nullptr;
}

Expand Down Expand Up @@ -218,7 +219,11 @@ void Network::publishString(const char *topic, const char *value)
{
char path[200] = {0};
buildMqttPath(topic, path);
_mqttClient.publish(path, value);
// Serial.println(path);
// Serial.println(_presenceCsv);

// _mqttClient.publish(path, value);
_mqttClient.publish_P(path, value, true);
}


Expand Down
30 changes: 20 additions & 10 deletions PresenceDetection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ void PresenceDetection::initialize()

void PresenceDetection::update()
{
vTaskDelay( 5000 / portTICK_PERIOD_MS);
delay(3000);

if(_timeout < 0) return;
if(_devices.size() == 0) return;
if(_devices.size() == 0)
{
_network->publishPresenceDetection(";;");
return;
}

memset(_csv, 0, presence_detection_buffer_size);
_csvIndex = 0;
Expand All @@ -61,6 +65,8 @@ void PresenceDetection::update()

_csv[_csvIndex-1] = 0x00;

Serial.print("Devices found: ");
Serial.println(_devices.size());
_network->publishPresenceDetection(_csv);
}

Expand Down Expand Up @@ -110,6 +116,8 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
std::string addressStr = device->getAddress().toString();
char addrArrComp[13] = {0};

// Serial.println(addressStr.c_str());

addrArrComp[0] = addressStr.at(0);
addrArrComp[1] = addressStr.at(1);
addrArrComp[2] = addressStr.at(3);
Expand All @@ -128,6 +136,7 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
auto it = _devices.find(addr);
if(it == _devices.end())
{

PdDevice pdDevice;

int i=0;
Expand All @@ -138,12 +147,19 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)
++i;
}

if(device->haveRSSI())
{
pdDevice.hasRssi = true;
pdDevice.rssi = device->getRSSI();
}

std::string nameStr = "-";
if(device->haveName())
{
std::string nameStr = device->getName();

int i=0;
size_t len = nameStr.length();
i=0;
len = nameStr.length();
while(i < len && i < sizeof(pdDevice.name)-1)
{
pdDevice.name[i] = nameStr.at(i);
Expand All @@ -154,12 +170,6 @@ void PresenceDetection::onResult(NimBLEAdvertisedDevice *device)

_devices[addr] = pdDevice;
}

if(device->haveRSSI())
{
pdDevice.hasRssi = true;
pdDevice.rssi = device->getRSSI();
}
}
else
{
Expand Down

0 comments on commit 20fc20e

Please sign in to comment.