connect to configured mqtt broker

This commit is contained in:
technyon
2022-03-27 10:20:23 +02:00
parent 5941a98f7e
commit ba864e0b67
7 changed files with 52 additions and 23 deletions

View File

@@ -21,6 +21,7 @@ file(GLOB SRCFILES
Nuki.cpp Nuki.cpp
MqttTopics.h MqttTopics.h
WebCfgServer.cpp WebCfgServer.cpp
PreferencesKeys.h
lib/ESP32_BLE_Arduino-1.0.1/src/*.cpp lib/ESP32_BLE_Arduino-1.0.1/src/*.cpp
lib/ESP32_BLE_Arduino-1.0.1/src/*.h lib/ESP32_BLE_Arduino-1.0.1/src/*.h
lib/WiFiManager/WiFiManager.cpp lib/WiFiManager/WiFiManager.cpp

View File

@@ -3,11 +3,13 @@
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include "Arduino.h" #include "Arduino.h"
#include "MqttTopics.h" #include "MqttTopics.h"
#include "PreferencesKeys.h"
Network* nwInst; Network* nwInst;
Network::Network() Network::Network(Preferences* preferences)
: _mqttClient(_wifiClient) : _mqttClient(_wifiClient),
_preferences(preferences)
{ {
nwInst = this; nwInst = this;
} }
@@ -39,15 +41,20 @@ void Network::initialize()
Serial.println("connected...yeey :)"); Serial.println("connected...yeey :)");
} }
_mqttClient.setServer("192.168.0.100", 1883); const char* brokerAddr = _preferences->getString(preference_mqtt_broker).c_str();
strcpy(_mqttBrokerAddr, brokerAddr);
Serial.print("MQTT Broker: ");
Serial.println(_mqttBrokerAddr);
_mqttClient.setServer(_mqttBrokerAddr, 1883);
_mqttClient.setCallback(Network::onMqttDataReceivedCallback); _mqttClient.setCallback(Network::onMqttDataReceivedCallback);
_mqttClient.publish("nuki/test", "OK");
} }
bool Network::reconnect() bool Network::reconnect()
{ {
while (!_mqttClient.connected()) { while (!_mqttClient.connected() && millis() > _nextReconnect)
{
Serial.println("Attempting MQTT connection"); Serial.println("Attempting MQTT connection");
// Attempt to connect // Attempt to connect
if (_mqttClient.connect("nukiHub")) { if (_mqttClient.connect("nukiHub")) {
@@ -55,10 +62,12 @@ bool Network::reconnect()
// ... and resubscribe // ... and resubscribe
_mqttClient.subscribe(mqtt_topc_lockstate_action); _mqttClient.subscribe(mqtt_topc_lockstate_action);
} else { }
else
{
Serial.print("MQTT connect failed, rc="); Serial.print("MQTT connect failed, rc=");
Serial.println(_mqttClient.state()); Serial.println(_mqttClient.state());
vTaskDelay( 5000 / portTICK_PERIOD_MS); _nextReconnect = millis() + 5000;
} }
} }
} }

View File

@@ -2,12 +2,14 @@
#include <PubSubClient.h> #include <PubSubClient.h>
#include <WiFiClient.h> #include <WiFiClient.h>
#include <Preferences.h>
#include "NukiConstants.h" #include "NukiConstants.h"
class Network class Network
{ {
public: public:
Network(); explicit Network(Preferences* preferences);
virtual ~Network() = default;
void initialize(); void initialize();
void update(); void update();
@@ -25,6 +27,10 @@ private:
PubSubClient _mqttClient; PubSubClient _mqttClient;
WiFiClient _wifiClient; WiFiClient _wifiClient;
Preferences* _preferences;
unsigned long _nextReconnect = 0;
char _mqttBrokerAddr[100] = {0};
void (*_lockActionReceivedCallback)(const char* value) = NULL; void (*_lockActionReceivedCallback)(const char* value) = NULL;
}; };

3
PreferencesKeys.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
#define preference_mqtt_broker "mqttbroker"

View File

@@ -1,8 +1,10 @@
#include "WebCfgServer.h" #include "WebCfgServer.h"
#include <WiFiClient.h> #include <WiFiClient.h>
#include "PreferencesKeys.h"
WebCfgServer::WebCfgServer() WebCfgServer::WebCfgServer(Preferences* preferences)
: _wifiServer(80) : _wifiServer(80),
_preferences(preferences)
{} {}
@@ -13,6 +15,8 @@ void WebCfgServer::initialize()
void WebCfgServer::update() void WebCfgServer::update()
{ {
bool configChanged = false;
// Create a client connections // Create a client connections
WiFiClient client = _wifiServer.available(); WiFiClient client = _wifiServer.available();
@@ -60,21 +64,25 @@ void WebCfgServer::update()
if(lastTokenType == TokenType::MQTT_SERVER && tokenType == TokenType::NONE) if(lastTokenType == TokenType::MQTT_SERVER && tokenType == TokenType::NONE)
{ {
configChanged = true; configChanged = true;
Serial.print("### "); _preferences->putString(preference_mqtt_broker, token);
Serial.println(token); configChanged = true;
// strcpy(_configuration->mqttServerAddress, token);
} }
} }
lastToken = token; lastToken = token;
token = strtok(NULL, "?=&"); token = strtok(NULL, "?=&");
} }
//
// if(configChanged) if(configChanged)
// { {
// _configuration->writeEeprom(); _enabled = false;
// _enabled = false; _preferences->end();
// } Serial.println(F("Restarting"));
vTaskDelay( 200 / portTICK_PERIOD_MS);
ESP.restart();
} }
}
vTaskDelay(200 / portTICK_PERIOD_MS);
} }
@@ -93,7 +101,7 @@ void WebCfgServer::serveHtml(WiFiClient &client)
client.println("<FORM ACTION=method=get >"); client.println("<FORM ACTION=method=get >");
client.print("MQTT Server: <INPUT TYPE=TEXT VALUE=\""); client.print("MQTT Server: <INPUT TYPE=TEXT VALUE=\"");
client.print(""); client.print(_preferences->getString(preference_mqtt_broker));
client.println("\" NAME=\"MQTTSERVER\" SIZE=\"25\" MAXLENGTH=\"40\"><BR>"); client.println("\" NAME=\"MQTTSERVER\" SIZE=\"25\" MAXLENGTH=\"40\"><BR>");
// client.print("DNS Server: <INPUT TYPE=TEXT VALUE=\""); // client.print("DNS Server: <INPUT TYPE=TEXT VALUE=\"");

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <WiFiServer.h> #include <WiFiServer.h>
#include <Preferences.h>
enum class TokenType enum class TokenType
{ {
@@ -11,7 +12,7 @@ enum class TokenType
class WebCfgServer class WebCfgServer
{ {
public: public:
WebCfgServer(); WebCfgServer(Preferences* preferences);
~WebCfgServer() = default; ~WebCfgServer() = default;
void initialize(); void initialize();
@@ -23,6 +24,7 @@ private:
TokenType getParameterType(char*& token); TokenType getParameterType(char*& token);
WiFiServer _wifiServer; WiFiServer _wifiServer;
Preferences* _preferences;
bool _enabled = true; bool _enabled = true;
}; };

View File

@@ -38,8 +38,8 @@ void setup()
{ {
preferences = new Preferences(); preferences = new Preferences();
preferences->begin("nukihub", false); preferences->begin("nukihub", false);
network = new Network(); network = new Network(preferences);
webCfgServer = new WebCfgServer(); webCfgServer = new WebCfgServer(preferences);
nuki = new Nuki("Main Door", 2020001, network); nuki = new Nuki("Main Door", 2020001, network);
network->initialize(); network->initialize();