report battery voltage via mqtt

This commit is contained in:
technyon
2022-03-26 00:01:41 +01:00
parent a202deef23
commit fa1b836d4f
5 changed files with 70 additions and 8 deletions

View File

@@ -1,4 +1,6 @@
#pragma once
#define mqtt_topc_lockstate "nuki/lockState"
#define mqtt_topc_lockstate_setpoint "nuki/lockActions"
#define mqtt_topc_voltage "nuki/battery/voltage"
#define mqtt_topc_lockstate "nuki/lock/state"
#define mqtt_topc_lockstate_action "nuki/lock/actions"

View File

@@ -54,7 +54,7 @@ bool Network::reconnect()
Serial.println("connected");
// ... and resubscribe
_mqttClient.subscribe(mqtt_topc_lockstate_setpoint);
_mqttClient.subscribe(mqtt_topc_lockstate_action);
} else {
Serial.print("failed, rc=");
Serial.print(_mqttClient.state());
@@ -118,7 +118,7 @@ void Network::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &len
value[l] = 0;
if(strcmp(topic, mqtt_topc_lockstate_setpoint) == 0)
if(strcmp(topic, mqtt_topc_lockstate_action) == 0)
{
if(strcmp(value, "") == 0) return;
@@ -128,7 +128,7 @@ void Network::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &len
{
_lockActionReceivedCallback(value);
}
_mqttClient.publish(mqtt_topc_lockstate_setpoint, "");
_mqttClient.publish(mqtt_topc_lockstate_action, "");
}
}
@@ -141,3 +141,10 @@ void Network::setLockActionReceived(void (*lockActionReceivedCallback)(const cha
{
_lockActionReceivedCallback = lockActionReceivedCallback;
}
void Network::publishBatteryVoltage(const float &value)
{
char str[30];
dtostrf(value, 0, 2, str);
_mqttClient.publish(mqtt_topc_voltage, str);
}

View File

@@ -13,6 +13,7 @@ public:
void update();
void publishKeyTurnerState(const char* state);
void publishBatteryVoltage(const float& value);
void setLockActionReceived(void (*lockActionReceivedCallback)(const char* value));

View File

@@ -11,6 +11,8 @@ Nuki::Nuki(const std::string& name, uint32_t id, Network* network)
memset(&_lastKeyTurnerState, sizeof(KeyTurnerState), 0);
memset(&_keyTurnerState, sizeof(KeyTurnerState), 0);
memset(&_lastBatteryReport, sizeof(KeyTurnerState), 0);
memset(&_batteryReport, sizeof(KeyTurnerState), 0);
network->setLockActionReceived(nukiInst->onLockActionReceived);
}
@@ -34,9 +36,37 @@ void Nuki::update()
vTaskDelay( 200 / portTICK_PERIOD_MS);
return;
}
vTaskDelay( 100 / portTICK_PERIOD_MS);
// Config config;
// uint8_t res = _nukiBle.requestConfig(&config, false);
// Serial.print("Result: ");
// Serial.println(res);
// Serial.print("Time: ");
// Serial.print(config.currentTimeHour);
// Serial.print(":");
// Serial.println(config.currentTimeMinute);
}
vTaskDelay( 100 / portTICK_PERIOD_MS);
unsigned long ts = millis();
updateKeyTurnerState();
if(_lastBatteryReportTs == 0 || _lastBatteryReportTs + 600000 < ts)
{
_lastBatteryReportTs = ts;
updateBatteryState();
}
vTaskDelay( 60000 / portTICK_PERIOD_MS);
}
void Nuki::updateKeyTurnerState()
{
_nukiBle.requestKeyTurnerState(&_keyTurnerState);
char str[20];
@@ -50,10 +80,24 @@ void Nuki::update()
}
memcpy(&_lastKeyTurnerState, &_keyTurnerState, sizeof(KeyTurnerState));
vTaskDelay( 20000 / portTICK_PERIOD_MS);
}
void Nuki::updateBatteryState()
{
_nukiBle.requestBatteryReport(&_batteryReport);
Serial.print("Voltage: "); Serial.println(_batteryReport.batteryVoltage);
Serial.print("Drain: "); Serial.println(_batteryReport.batteryDrain);
Serial.print("Resistance: "); Serial.println(_batteryReport.batteryResistance);
Serial.print("Max Current: "); Serial.println(_batteryReport.maxTurnCurrent);
Serial.print("Crit. State: "); Serial.println(_batteryReport.criticalBatteryState);
Serial.print("Lock Dist: "); Serial.println(_batteryReport.lockDistance);
_network->publishBatteryVoltage((float)_batteryReport.batteryVoltage / (float)1000);
}
void Nuki::lockstateToString(const LockState state, char* str)
{
switch(state)
@@ -116,7 +160,7 @@ void Nuki::onLockActionReceived(const char *value)
Serial.println((int)action);
if(action != (LockAction)0xff)
{
// nukiInst->_nukiBle.lockAction(action, 0, 0);
nukiInst->_nukiBle.lockAction(action, 0, 0);
vTaskDelay( 5000 / portTICK_PERIOD_MS);
}
}

8
Nuki.h
View File

@@ -14,6 +14,10 @@ public:
private:
static void onLockActionReceived(const char* value);
void updateKeyTurnerState();
void updateBatteryState();
void lockstateToString(const LockState state, char* str); // char array at least 14 characters
LockAction lockActionToEnum(const char* str); // char array at least 14 characters
@@ -23,5 +27,9 @@ private:
KeyTurnerState _lastKeyTurnerState;
KeyTurnerState _keyTurnerState;
BatteryReport _batteryReport;
BatteryReport _lastBatteryReport;
bool _paired = false;
unsigned long _lastBatteryReportTs = 0;
};