Load credentials and settings from config file
This commit is contained in:
29
src/config.cpp
Normal file
29
src/config.cpp
Normal 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); }
|
||||
Reference in New Issue
Block a user