Skip to content

Commit

Permalink
show pairing and mqtt status in configuration page
Browse files Browse the repository at this point in the history
  • Loading branch information
technyon committed Mar 27, 2022
1 parent 7694cdb commit 9279257
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 5 deletions.
9 changes: 8 additions & 1 deletion Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ bool Network::reconnect()
// Attempt to connect
if (_mqttClient.connect("nukiHub")) {
Serial.println(F("MQTT connected"));
_mqttConnected = true;

// ... and resubscribe
_mqttClient.subscribe(mqtt_topic_lockstate_action);
Expand All @@ -76,12 +77,13 @@ bool Network::reconnect()
{
Serial.print(F("MQTT connect failed, rc="));
Serial.println(_mqttClient.state());
_mqttConnected = false;
_nextReconnect = millis() + 5000;
}
}
return _mqttConnected;
}


void Network::update()
{
if(!WiFi.isConnected())
Expand Down Expand Up @@ -167,3 +169,8 @@ void Network::publishInt(const char *topic, const int value)
itoa(value, str, 10);
_mqttClient.publish(topic, str);
}

bool Network::isMqttConnected()
{
return _mqttConnected;
}
4 changes: 4 additions & 0 deletions Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Network
void initialize();
void update();

bool isMqttConnected();

void publishKeyTurnerState(const char* state, const char* trigger, const char* completionStatus);
void publishBatteryReport(const BatteryReport& batteryReport);

Expand All @@ -32,6 +34,8 @@ class Network
WiFiClient _wifiClient;
Preferences* _preferences;

bool _mqttConnected = false;

unsigned long _nextReconnect = 0;
char _mqttBrokerAddr[100] = {0};

Expand Down
5 changes: 5 additions & 0 deletions Nuki.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,8 @@ void Nuki::onLockActionReceived(const char *value)
Serial.print(F("Action: "));
Serial.println((int)nukiInst->_nextLockAction);
}

const bool Nuki::isPaired()
{
return _paired;
}
2 changes: 2 additions & 0 deletions Nuki.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Nuki
void initialize();
void update();

const bool isPaired();

private:
static void onLockActionReceived(const char* value);

Expand Down
16 changes: 14 additions & 2 deletions WebCfgServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#include <WiFiClient.h>
#include "PreferencesKeys.h"

WebCfgServer::WebCfgServer(Preferences* preferences)
WebCfgServer::WebCfgServer(Nuki* nuki, Network* network, Preferences* preferences)
: _wifiServer(80),
_nuki(nuki),
_network(network),
_preferences(preferences)
{}

Expand Down Expand Up @@ -112,6 +114,16 @@ void WebCfgServer::serveHtml(WiFiClient &client)
client.println("</HEAD>");
client.println("<BODY>");


client.println("<br><br><h3>");
client.print("Paired: ");
client.println(_nuki->isPaired() ? "Yes" : "No");
client.println("</h3>");
client.println("<h3>");
client.print("MQTT Connected: ");
client.println(_network->isMqttConnected() ? "Yes" : "No");
client.println("</h3><br><br>");

client.println("<FORM ACTION=method=get >");

client.print("MQTT Broker: <INPUT TYPE=TEXT VALUE=\"");
Expand All @@ -130,7 +142,7 @@ void WebCfgServer::serveHtml(WiFiClient &client)
client.print(_preferences->getInt(preference_query_interval_battery));
client.println("\" NAME=\"BATINT\" SIZE=\"25\" MAXLENGTH=\"16\"><BR>");

client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Save\">");
client.println("<br><INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Save\">");

client.println("</FORM>");

Expand Down
6 changes: 5 additions & 1 deletion WebCfgServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <WiFiServer.h>
#include <Preferences.h>
#include "Nuki.h"
#include "Network.h"

enum class TokenType
{
Expand All @@ -15,7 +17,7 @@ enum class TokenType
class WebCfgServer
{
public:
WebCfgServer(Preferences* preferences);
WebCfgServer(Nuki* nuki, Network* network, Preferences* preferences);
~WebCfgServer() = default;

void initialize();
Expand All @@ -28,6 +30,8 @@ class WebCfgServer
TokenType getParameterType(char*& token);

WiFiServer _wifiServer;
Nuki* _nuki;
Network* _network;
Preferences* _preferences;

bool _enabled = true;
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ void setup()
preferences = new Preferences();
preferences->begin("nukihub", false);
network = new Network(preferences);
webCfgServer = new WebCfgServer(preferences);
nuki = new Nuki("Main Door", 2020001, network, preferences);
webCfgServer = new WebCfgServer(nuki, network, preferences);

network->initialize();
webCfgServer->initialize();
Expand Down

0 comments on commit 9279257

Please sign in to comment.