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
MqttTopics.h
WebCfgServer.cpp
PreferencesKeys.h
lib/ESP32_BLE_Arduino-1.0.1/src/*.cpp
lib/ESP32_BLE_Arduino-1.0.1/src/*.h
lib/WiFiManager/WiFiManager.cpp

View File

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

View File

@@ -2,12 +2,14 @@
#include <PubSubClient.h>
#include <WiFiClient.h>
#include <Preferences.h>
#include "NukiConstants.h"
class Network
{
public:
Network();
explicit Network(Preferences* preferences);
virtual ~Network() = default;
void initialize();
void update();
@@ -25,6 +27,10 @@ private:
PubSubClient _mqttClient;
WiFiClient _wifiClient;
Preferences* _preferences;
unsigned long _nextReconnect = 0;
char _mqttBrokerAddr[100] = {0};
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 <WiFiClient.h>
#include "PreferencesKeys.h"
WebCfgServer::WebCfgServer()
: _wifiServer(80)
WebCfgServer::WebCfgServer(Preferences* preferences)
: _wifiServer(80),
_preferences(preferences)
{}
@@ -13,6 +15,8 @@ void WebCfgServer::initialize()
void WebCfgServer::update()
{
bool configChanged = false;
// Create a client connections
WiFiClient client = _wifiServer.available();
@@ -60,22 +64,26 @@ void WebCfgServer::update()
if(lastTokenType == TokenType::MQTT_SERVER && tokenType == TokenType::NONE)
{
configChanged = true;
Serial.print("### ");
Serial.println(token);
// strcpy(_configuration->mqttServerAddress, token);
_preferences->putString(preference_mqtt_broker, token);
configChanged = true;
}
}
lastToken = token;
token = strtok(NULL, "?=&");
}
//
// if(configChanged)
// {
// _configuration->writeEeprom();
// _enabled = false;
// }
if(configChanged)
{
_enabled = false;
_preferences->end();
Serial.println(F("Restarting"));
vTaskDelay( 200 / portTICK_PERIOD_MS);
ESP.restart();
}
}
vTaskDelay(200 / portTICK_PERIOD_MS);
}
void WebCfgServer::serveHtml(WiFiClient &client)
@@ -93,7 +101,7 @@ void WebCfgServer::serveHtml(WiFiClient &client)
client.println("<FORM ACTION=method=get >");
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.print("DNS Server: <INPUT TYPE=TEXT VALUE=\"");

View File

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

View File

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