First basic class structure for mqtt handling

This commit is contained in:
Thomas Basler 2022-04-18 15:19:26 +02:00
parent 5653d0f835
commit 50fa7e2abf
7 changed files with 73 additions and 2 deletions

View File

@ -3,7 +3,7 @@
#include <Arduino.h>
#define CONFIG_FILENAME "/config.bin"
#define CONFIG_VERSION 0x00010400 // 0.1.4 // make sure to clean all after change
#define CONFIG_VERSION 0x00010500 // 0.1.5 // make sure to clean all after change
#define WIFI_MAX_SSID_STRLEN 31
#define WIFI_MAX_PASSWORD_STRLEN 31
@ -13,6 +13,11 @@
#define NTP_MAX_TIMEZONE_STRLEN 50
#define NTP_MAX_TIMEZONEDESCR_STRLEN 50
#define MQTT_MAX_HOSTNAME_STRLEN 31
#define MQTT_MAX_USERNAME_STRLEN 32
#define MQTT_MAX_PASSWORD_STRLEN 32
#define MQTT_MAX_TOPIC_STRLEN 32
struct CONFIG_T {
uint32_t Cfg_Version;
uint Cfg_SaveCount;
@ -30,6 +35,13 @@ struct CONFIG_T {
char Ntp_Server[NTP_MAX_SERVER_STRLEN + 1];
char Ntp_Timezone[NTP_MAX_TIMEZONE_STRLEN + 1];
char Ntp_TimezoneDescr[NTP_MAX_TIMEZONEDESCR_STRLEN + 1];
bool Mqtt_Enabled;
char Mqtt_Hostname[MQTT_MAX_HOSTNAME_STRLEN + 1];
uint Mqtt_Port;
char Mqtt_Username[MQTT_MAX_USERNAME_STRLEN + 1];
char Mqtt_Password[MQTT_MAX_PASSWORD_STRLEN + 1];
char Mqtt_Topic[MQTT_MAX_TOPIC_STRLEN + 1];
};
class ConfigurationClass {

15
include/MqttSettings.h Normal file
View File

@ -0,0 +1,15 @@
#pragma once
#include <AsyncMqttClient.h>
#include <memory>
class MqttSettingsClass {
public:
MqttSettingsClass();
void init();
private:
std::unique_ptr<AsyncMqttClient> mqttClient;
};
extern MqttSettingsClass MqttSettings;

View File

@ -20,3 +20,10 @@
#define NTP_SERVER "pool.ntp.org"
#define NTP_TIMEZONE "CET-1CEST,M3.5.0,M10.5.0/3"
#define NTP_TIMEZONEDESCR "Europe/Berlin"
#define MQTT_ENABLED false
#define MQTT_HOST ""
#define MQTT_PORT 1883
#define MQTT_USER ""
#define MQTT_PASSWORD ""
#define MQTT_TOPIC "stripe/"

View File

@ -24,6 +24,7 @@ build_flags =
lib_deps =
https://github.com/me-no-dev/ESPAsyncWebServer.git
bblanchon/ArduinoJson @ ^6.19.4
https://github.com/marvinroger/async-mqtt-client.git
board = esp32dev
board_build.partitions = partitions_custom.csv

View File

@ -20,6 +20,14 @@ void ConfigurationClass::init()
strlcpy(config.Ntp_Server, NTP_SERVER, sizeof(config.Ntp_Server));
strlcpy(config.Ntp_Timezone, NTP_TIMEZONE, sizeof(config.Ntp_Timezone));
strlcpy(config.Ntp_TimezoneDescr, NTP_TIMEZONEDESCR, sizeof(config.Ntp_TimezoneDescr));
// MqTT Settings
config.Mqtt_Enabled = MQTT_ENABLED;
strlcpy(config.Mqtt_Hostname, MQTT_HOST, sizeof(config.Mqtt_Hostname));
config.Mqtt_Port = MQTT_PORT;
strlcpy(config.Mqtt_Username, MQTT_USER, sizeof(config.Mqtt_Username));
strlcpy(config.Mqtt_Password, MQTT_PASSWORD, sizeof(config.Mqtt_Password));
strlcpy(config.Mqtt_Topic, MQTT_TOPIC, sizeof(config.Mqtt_Topic));
}
bool ConfigurationClass::write()
@ -58,6 +66,16 @@ void ConfigurationClass::migrate()
strlcpy(config.Ntp_Timezone, NTP_TIMEZONE, sizeof(config.Ntp_Timezone));
strlcpy(config.Ntp_TimezoneDescr, NTP_TIMEZONEDESCR, sizeof(config.Ntp_TimezoneDescr));
}
if (config.Cfg_Version < 0x00010500) {
config.Mqtt_Enabled = MQTT_ENABLED;
strlcpy(config.Mqtt_Hostname, MQTT_HOST, sizeof(config.Mqtt_Hostname));
config.Mqtt_Port = MQTT_PORT;
strlcpy(config.Mqtt_Username, MQTT_USER, sizeof(config.Mqtt_Username));
strlcpy(config.Mqtt_Password, MQTT_PASSWORD, sizeof(config.Mqtt_Password));
strlcpy(config.Mqtt_Topic, MQTT_TOPIC, sizeof(config.Mqtt_Topic));
}
config.Cfg_Version = CONFIG_VERSION;
write();
}

12
src/MqttSettings.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "MqttSettings.h"
MqttSettingsClass::MqttSettingsClass()
: mqttClient()
{
}
void MqttSettingsClass::init()
{
}
MqttSettingsClass MqttSettings;

View File

@ -1,4 +1,5 @@
#include "Configuration.h"
#include "MqttSettings.h"
#include "NtpSettings.h"
#include "WebApi.h"
#include "WiFiSettings.h"
@ -51,6 +52,11 @@ void setup()
NtpSettings.init();
Serial.println(F("done"));
// Initialize MqTT
Serial.print(F("Initialize MqTT... "));
MqttSettings.init();
Serial.println(F("done"));
// Initialize WebApi
Serial.print(F("Initialize WebApi... "));
WebApi.init();