Load credentials and settings from config file

This commit is contained in:
2023-04-06 08:28:01 +02:00
parent 37e249a503
commit 92eeb0a638
8 changed files with 109 additions and 29 deletions

29
src/config.cpp Normal file
View File

@@ -0,0 +1,29 @@
#include "config.h"
#include <ArduinoJson.h>
bool load_config_file(const char *file_path, struct Config &config) {
if (!LittleFS.begin())
return false;
File config_file = LittleFS.open(file_path, "r");
if (!config_file)
return false;
StaticJsonDocument<512> json;
DeserializationError err = deserializeJson(json, config_file);
if (err)
return false;
config = {
.ssid = json["ssid"],
.psk = json["psk"],
.mqtt_host = json["mqtt_host"],
.mqtt_user = json["mqtt_user"],
.mqtt_password = json["mqtt_password"],
.mqtt_port = json["mqtt_port"],
.topic = json["mqtt_topic"],
.device_id = json["device_id"],
.sleep_time = minutes_to_microseconds(json["sleep_time"]),
.connection_attempts = json["connection_attempts"],
};
return true;
}
long minutes_to_microseconds(int minutes) { return (minutes * 6e7); }