publish lock state to mqtt
This commit is contained in:
@@ -19,6 +19,7 @@ file(GLOB SRCFILES
|
||||
# "Lib/FreeRTOS/src/*.cpp"
|
||||
Network.cpp
|
||||
Nuki.cpp
|
||||
MqttTopics.h
|
||||
lib/ESP32_BLE_Arduino-1.0.1/src/*.cpp
|
||||
lib/ESP32_BLE_Arduino-1.0.1/src/*.h
|
||||
lib/WiFiManager/WiFiManager.cpp
|
||||
|
||||
3
MqttTopics.h
Normal file
3
MqttTopics.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#define mqtt_topc_lockstate "nuki/lockstate"
|
||||
32
Network.cpp
32
Network.cpp
@@ -2,6 +2,7 @@
|
||||
#include "WiFi.h"
|
||||
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
|
||||
#include "Arduino.h"
|
||||
#include "MqttTopics.h"
|
||||
|
||||
Network* nwInst;
|
||||
|
||||
@@ -82,18 +83,18 @@ void Network::update()
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long ts = millis();
|
||||
if(_publishTs < ts)
|
||||
{
|
||||
_publishTs = ts + 1000;
|
||||
|
||||
++_count;
|
||||
|
||||
char cstr[16];
|
||||
itoa(_count, cstr, 10);
|
||||
|
||||
_mqttClient.publish("nuki/counter", cstr);
|
||||
}
|
||||
// unsigned long ts = millis();
|
||||
// if(_publishTs < ts)
|
||||
// {
|
||||
// _publishTs = ts + 1000;
|
||||
//
|
||||
// ++_count;
|
||||
//
|
||||
// char cstr[16];
|
||||
// itoa(_count, cstr, 10);
|
||||
//
|
||||
// _mqttClient.publish("nuki/counter", cstr);
|
||||
// }
|
||||
|
||||
_mqttClient.loop();
|
||||
|
||||
@@ -122,3 +123,10 @@ void Network::onMqttDataReceived(char *&topic, byte *&payload, unsigned int &len
|
||||
Serial.println(value);
|
||||
}
|
||||
}
|
||||
|
||||
void Network::publishKeyTurnerState(const KeyTurnerState &state)
|
||||
{
|
||||
char cstr[10];
|
||||
itoa((int)state.lockState, cstr, 10);
|
||||
_mqttClient.publish(mqtt_topc_lockstate, cstr);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <PubSubClient.h>
|
||||
#include <WiFiClient.h>
|
||||
#include "NukiConstants.h"
|
||||
|
||||
class Network
|
||||
{
|
||||
@@ -11,6 +12,8 @@ public:
|
||||
void initialize();
|
||||
void update();
|
||||
|
||||
void publishKeyTurnerState(const KeyTurnerState& state);
|
||||
|
||||
private:
|
||||
static void onMqttDataReceivedCallback(char* topic, byte* payload, unsigned int length);
|
||||
void onMqttDataReceived(char*& topic, byte*& payload, unsigned int& length);
|
||||
@@ -20,6 +23,5 @@ private:
|
||||
PubSubClient _mqttClient;
|
||||
WiFiClient _wifiClient;
|
||||
|
||||
uint32_t _count = 0;
|
||||
unsigned long _publishTs = 0;
|
||||
};
|
||||
|
||||
10
Nuki.cpp
10
Nuki.cpp
@@ -5,7 +5,8 @@ Nuki::Nuki(const std::string& name, uint32_t id, Network* network)
|
||||
: _nukiBle(name, id),
|
||||
_network(network)
|
||||
{
|
||||
|
||||
memset(&_lastKeyTurnerState, sizeof(KeyTurnerState), 0);
|
||||
memset(&_keyTurnerState, sizeof(KeyTurnerState), 0);
|
||||
}
|
||||
|
||||
void Nuki::initialize()
|
||||
@@ -33,5 +34,12 @@ void Nuki::update()
|
||||
Serial.print(F("Nuki lock state: "));
|
||||
Serial.println((int)_keyTurnerState.lockState);
|
||||
|
||||
if(_keyTurnerState.lockState != _lastKeyTurnerState.lockState)
|
||||
{
|
||||
_network->publishKeyTurnerState(_keyTurnerState);
|
||||
}
|
||||
|
||||
memcpy(&_lastKeyTurnerState, &_keyTurnerState, sizeof(KeyTurnerState));
|
||||
|
||||
vTaskDelay( 20000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user