Skip to content

Commit

Permalink
Merge pull request #4 from peteh/feature/bssid
Browse files Browse the repository at this point in the history
ADD: bssid info
  • Loading branch information
peteh authored Apr 3, 2024
2 parents 6aef16c + c9ce634 commit adf6d2c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
11 changes: 10 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"files.associations": {
"type_traits": "cpp"
"type_traits": "cpp",
"array": "cpp",
"deque": "cpp",
"list": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"string_view": "cpp",
"initializer_list": "cpp"
}
}
38 changes: 32 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ bool g_keyTurnerUpdateNotification = false;

bool g_wifiConnected = false;
bool g_mqttConnected = false;
String g_bssid = "";

bool g_otaUpdate = false;

bool getKeyTurnerStateFromLock()
{
Expand Down Expand Up @@ -179,7 +182,7 @@ void callback(char *topic, byte *payload, unsigned int length)
NukiLock::KeyTurnerState state;
// only gets the current state in the lock, does not actively query it
nukiBle.retrieveKeyTunerState(&state);
g_mqttView.publishLockState(nukiBle, state.lockState, g_config);
g_mqttView.publishLockState(nukiBle, state.lockState, g_bssid.c_str(), g_config);
}

// publish config when homeassistant comes online and needs the configuration again
Expand Down Expand Up @@ -224,7 +227,11 @@ void setup()
WiFi.setHostname(composeClientID().c_str());
WiFi.mode(WIFI_STA);
WiFi.setAutoConnect(true);
WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SIGNAL);
WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);

WiFi.begin(wifi_ssid, wifi_pass);
WiFi.setTxPower(WIFI_POWER_19_5dBm);

// TODO: this is pointless
connectToWifi();
Expand All @@ -238,10 +245,13 @@ void setup()
}

// NOTE: if updating FS this would be the place to unmount FS using FS.end()
log_i("Start updating %s", type); });
log_i("Start updating %s", type);
g_otaUpdate = true;
});

ArduinoOTA.onEnd([]()
{ log_i("End Update"); });
{ g_otaUpdate = false;
log_i("End Update"); });

ArduinoOTA.onProgress([](unsigned int progress, unsigned int total)
{
Expand All @@ -268,6 +278,7 @@ void setup()

log_i("Connected to SSID: %s", wifi_ssid);
log_i("IP address: %s", WiFi.localIP());
g_bssid = WiFi.BSSIDstr();

g_client.setBufferSize(1024);
g_client.setServer(mqtt_server, mqtt_port);
Expand Down Expand Up @@ -339,6 +350,21 @@ void loop()

g_client.loop();
ArduinoOTA.handle();
if(g_otaUpdate)
{
// skip processing if we are processing ota updates
return;
}

if (!WiFi.BSSIDstr().equals(g_bssid))
{
g_bssid = WiFi.BSSIDstr();
NukiLock::KeyTurnerState state;
// only gets the current state in the lock, does not actively query it
nukiBle.retrieveKeyTunerState(&state);
g_mqttView.publishLockState(nukiBle, state.lockState, g_bssid.c_str(), g_config);
}

if (!nukiBle.isPairedWithLock())
{
if (nukiBle.pairNuki() == Nuki::PairingResult::Success)
Expand All @@ -355,12 +381,12 @@ void loop()
g_newCommandAvailable = false;
if (g_newCommand == NukiLock::LockAction::Lock)
{
g_mqttView.publishLockState(nukiBle, NukiLock::LockState::Locking, g_config);
g_mqttView.publishLockState(nukiBle, NukiLock::LockState::Locking, g_bssid.c_str(), g_config);
delay(100);
}
if (g_newCommand == NukiLock::LockAction::Unlock)
{
g_mqttView.publishLockState(nukiBle, NukiLock::LockState::Unlocking, g_config);
g_mqttView.publishLockState(nukiBle, NukiLock::LockState::Unlocking, g_bssid.c_str(), g_config);
delay(100);
}
for (int i = 0; i < 3 && nukiBle.lockAction(g_newCommand, deviceId, 0, NULL, 0) != Nuki::CmdResult::Success; i++)
Expand All @@ -378,7 +404,7 @@ void loop()
// only gets the current state in the lock, does not actively query it
nukiBle.retrieveKeyTunerState(&state);
g_keyTurnerUpdateNotification = false;
g_mqttView.publishLockState(nukiBle, state.lockState, g_config);
g_mqttView.publishLockState(nukiBle, state.lockState, g_bssid.c_str(), g_config);
if(state.lockState == NukiLock::LockState::Locking || state.lockState == NukiLock::LockState::Unlocking)
{
// force state update because we are transitioning anyway
Expand Down
13 changes: 11 additions & 2 deletions src/mqttview.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MqttView
m_batteryCritical(&m_device, "battery_critical", "Battery Critical"),

// Diagnostics Elements
m_bssid(&m_device, "diagnostics_bssid", "BSSID"),
m_diagnosticsResetButton(&m_device, "diagnostics_reset_btn", "Reset Counters"),
m_diagnosticsRestartCounter(&m_device, "diagnostics_restart_counter", "Restart Counter"),
m_diagnosticsWifiDisconnectCounter(&m_device, "diagnostics_wifidisconnect_counter", "WiFi Disconnect Counter"),
Expand All @@ -37,6 +38,11 @@ class MqttView
m_batteryCritical.setValueTemplate("{{value_json.battery_critical}}");
m_batteryCritical.setDeviceClass("battery");

m_bssid.setCustomStateTopic(m_lock.getStateTopic());
m_bssid.setValueTemplate("{{value_json.bssid}}");
m_bssid.setEntityType(EntityCategory::DIAGNOSTIC);
m_bssid.setIcon("mdi:wifi");

m_diagnosticsResetButton.setEntityType(EntityCategory::DIAGNOSTIC);
m_diagnosticsResetButton.setDeviceClass("restart");

Expand Down Expand Up @@ -85,9 +91,10 @@ class MqttView
publishConfig(m_diagnosticsRestartCounter);
publishConfig(m_diagnosticsWifiDisconnectCounter);
publishConfig(m_diagnosticsMqttDisconnectCounter);
publishConfig(m_bssid);
}

void publishLockState(NukiLock::NukiLock &nuki, NukiLock::LockState lockState, Config &config)
void publishLockState(NukiLock::NukiLock &nuki, NukiLock::LockState lockState, const char* bssid, Config &config)
{
char buffer[255];
const char *lockStateStr;
Expand All @@ -112,10 +119,11 @@ class MqttView
lockStateStr = m_lock.getUnlockedState();
break;
}
snprintf(buffer, sizeof(buffer), "{\"state\": \"%s\", \"battery\": %d, \"battery_critical\": \"%s\", \"wifi_disconnect_counter\": %u, \"mqtt_disconnect_counter\": %u, \"restart_counter\": %u}",
snprintf(buffer, sizeof(buffer), "{\"state\": \"%s\", \"battery\": %d, \"battery_critical\": \"%s\", \"bssid\": \"%s\", \"wifi_disconnect_counter\": %u, \"mqtt_disconnect_counter\": %u, \"restart_counter\": %u}",
lockStateStr,
nuki.getBatteryPerc(),
nuki.isBatteryCritical() ? m_batteryCritical.getOnState() : m_batteryCritical.getOffState(),
bssid,
config.wifiDisconnectCounter,
config.mqttDisconnectCounter,
config.restartCounter);
Expand All @@ -136,6 +144,7 @@ class MqttView
MqttSensor m_diagnosticsRestartCounter;
MqttSensor m_diagnosticsWifiDisconnectCounter;
MqttSensor m_diagnosticsMqttDisconnectCounter;
MqttSensor m_bssid;

void publishConfig(MqttEntity &entity)
{
Expand Down
2 changes: 1 addition & 1 deletion src/platform.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <Arduino.h>
#define VERSION "2024.2.0"
#define VERSION "2024.4.0"
#define CONFIG_FILENAME "/config.txt"

struct Config
Expand Down

0 comments on commit adf6d2c

Please sign in to comment.