Skip to content

Commit

Permalink
First basic class structure for mqtt handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Apr 18, 2022
1 parent 5653d0f commit 50fa7e2
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 2 deletions.
14 changes: 13 additions & 1 deletion include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions include/MqttSettings.h
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 8 additions & 1 deletion include/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@

#define NTP_SERVER "pool.ntp.org"
#define NTP_TIMEZONE "CET-1CEST,M3.5.0,M10.5.0/3"
#define NTP_TIMEZONEDESCR "Europe/Berlin"
#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/"
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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();
}
Expand Down
12 changes: 12 additions & 0 deletions src/MqttSettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "MqttSettings.h"

MqttSettingsClass::MqttSettingsClass()
: mqttClient()
{
}

void MqttSettingsClass::init()
{
}

MqttSettingsClass MqttSettings;
6 changes: 6 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Configuration.h"
#include "MqttSettings.h"
#include "NtpSettings.h"
#include "WebApi.h"
#include "WiFiSettings.h"
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 50fa7e2

Please sign in to comment.