LoRa_APRS_iGate/src/configuration.cpp

372 lines
15 KiB
C++
Raw Normal View History

2023-06-12 01:33:16 +02:00
#include <ArduinoJson.h>
2023-08-30 00:09:51 +02:00
#include <SPIFFS.h>
2023-06-06 17:21:59 +02:00
#include "configuration.h"
2023-06-08 06:44:13 +02:00
#include "display.h"
2023-06-06 17:21:59 +02:00
2024-05-14 05:30:15 +02:00
2024-03-07 17:46:38 +01:00
void Configuration::check() {
if (reload) {
show_display("------- UPDATE ------", "config is old", "device will update", "and then reboot", 1000);
writeFile();
ESP.restart();
}
}
2023-06-06 17:21:59 +02:00
2024-02-24 14:09:05 +01:00
void Configuration::writeFile() {
Serial.println("Saving config..");
2024-05-24 02:13:05 +02:00
StaticJsonDocument<2560> data;
2024-02-24 14:09:05 +01:00
File configFile = SPIFFS.open("/igate_conf.json", "w");
if (wifiAPs[0].ssid != "") { // We don't want to save Auto AP empty SSID
for (int i = 0; i < wifiAPs.size(); i++) {
data["wifi"]["AP"][i]["ssid"] = wifiAPs[i].ssid;
data["wifi"]["AP"][i]["password"] = wifiAPs[i].password;
}
2023-06-06 17:21:59 +02:00
}
2024-05-23 01:19:25 +02:00
data["wifi"]["autoAP"]["password"] = wifiAutoAP.password;
data["wifi"]["autoAP"]["powerOff"] = wifiAutoAP.powerOff;
2023-06-06 17:21:59 +02:00
2024-05-23 01:19:25 +02:00
data["callsign"] = callsign;
2024-02-24 14:09:05 +01:00
2024-05-23 01:19:25 +02:00
data["aprs_is"]["active"] = aprs_is.active;
data["aprs_is"]["passcode"] = aprs_is.passcode;
data["aprs_is"]["server"] = aprs_is.server;
data["aprs_is"]["port"] = aprs_is.port;
data["aprs_is"]["filter"] = aprs_is.filter;
data["aprs_is"]["messagesToRF"] = aprs_is.messagesToRF;
data["aprs_is"]["objectsToRF"] = aprs_is.objectsToRF;
2024-03-07 17:46:38 +01:00
2024-05-23 01:19:25 +02:00
data["beacon"]["comment"] = beacon.comment;
data["beacon"]["interval"] = beacon.interval;
data["beacon"]["latitude"] = beacon.latitude;
data["beacon"]["longitude"] = beacon.longitude;
data["beacon"]["overlay"] = beacon.overlay;
data["beacon"]["symbol"] = beacon.symbol;
data["beacon"]["sendViaAPRSIS"] = beacon.sendViaAPRSIS;
data["beacon"]["sendViaRF"] = beacon.sendViaRF;
data["beacon"]["path"] = beacon.path;
2024-03-07 17:46:38 +01:00
2024-05-24 16:43:06 +02:00
data["digi"]["mode"] = digi.mode;
2024-05-23 01:19:25 +02:00
data["lora"]["rxFreq"] = loramodule.rxFreq;
data["lora"]["txFreq"] = loramodule.txFreq;
data["lora"]["spreadingFactor"] = loramodule.spreadingFactor;
data["lora"]["signalBandwidth"] = loramodule.signalBandwidth;
data["lora"]["codingRate4"] = loramodule.codingRate4;
data["lora"]["power"] = loramodule.power;
data["lora"]["txActive"] = loramodule.txActive;
data["lora"]["rxActive"] = loramodule.rxActive;
2024-02-24 14:09:05 +01:00
2024-05-23 01:19:25 +02:00
data["display"]["alwaysOn"] = display.alwaysOn;
data["display"]["timeout"] = display.timeout;
data["display"]["turn180"] = display.turn180;
2023-06-06 17:21:59 +02:00
2024-05-24 16:43:06 +02:00
data["battery"]["sendInternalVoltage"] = battery.sendInternalVoltage;
data["battery"]["monitorInternalVoltage"] = battery.monitorInternalVoltage;
data["battery"]["internalSleepVoltage"] = battery.internalSleepVoltage;
data["battery"]["sendExternalVoltage"] = battery.sendExternalVoltage;
data["battery"]["externalVoltagePin"] = battery.externalVoltagePin;
data["battery"]["monitorExternalVoltage"] = battery.monitorExternalVoltage;
data["battery"]["externalSleepVoltage"] = battery.externalSleepVoltage;
2024-02-24 14:09:05 +01:00
2024-05-23 01:19:25 +02:00
data["bme"]["active"] = bme.active;
data["bme"]["heightCorrection"] = bme.heightCorrection;
data["bme"]["temperatureCorrection"] = bme.temperatureCorrection;
2024-02-24 14:09:05 +01:00
2024-05-24 16:43:06 +02:00
data["syslog"]["active"] = syslog.active;
data["syslog"]["server"] = syslog.server;
data["syslog"]["port"] = syslog.port;
data["tnc"]["enableServer"] = tnc.enableServer;
data["tnc"]["enableSerial"] = tnc.enableSerial;
data["tnc"]["acceptOwn"] = tnc.acceptOwn;
data["other"]["rebootMode"] = rebootMode;
data["other"]["rebootModeTime"] = rebootModeTime;
2024-05-23 01:19:25 +02:00
data["ota"]["username"] = ota.username;
data["ota"]["password"] = ota.password;
2024-03-07 17:46:38 +01:00
2024-05-24 16:43:06 +02:00
data["other"]["rememberStationTime"] = rememberStationTime;
2024-03-28 18:00:46 +01:00
2024-05-24 16:43:06 +02:00
data["other"]["backupDigiMode"] = backupDigiMode;
2024-05-23 01:19:25 +02:00
2024-05-24 16:43:06 +02:00
data["other"]["lowPowerMode"] = lowPowerMode;
data["other"]["lowVoltageCutOff"] = lowVoltageCutOff;
2024-05-22 22:19:45 +02:00
2024-02-24 14:09:05 +01:00
serializeJson(data, configFile);
2023-06-06 17:21:59 +02:00
configFile.close();
2024-02-24 14:09:05 +01:00
Serial.println("Config saved");
}
bool Configuration::readFile() {
Serial.println("Reading config..");
File configFile = SPIFFS.open("/igate_conf.json", "r");
if (configFile) {
2024-05-24 02:13:05 +02:00
StaticJsonDocument<2560> data;
2024-02-24 14:09:05 +01:00
DeserializationError error = deserializeJson(data, configFile);
if (error) {
Serial.println("Failed to read file, using default configuration");
}
JsonArray WiFiArray = data["wifi"]["AP"];
for (int i = 0; i < WiFiArray.size(); i++) {
WiFi_AP wifiap;
wifiap.ssid = WiFiArray[i]["ssid"].as<String>();
wifiap.password = WiFiArray[i]["password"].as<String>();
wifiAPs.push_back(wifiap);
}
wifiAutoAP.password = data["wifi"]["autoAP"]["password"].as<String>();
wifiAutoAP.powerOff = data["wifi"]["autoAP"]["powerOff"].as<int>();
callsign = data["callsign"].as<String>();
rememberStationTime = data["other"]["rememberStationTime"].as<int>();
2024-05-24 02:13:05 +02:00
2024-05-24 16:43:06 +02:00
battery.sendInternalVoltage = data["battery"]["sendInternalVoltage"].as<bool>();
battery.monitorInternalVoltage = data["battery"]["monitorInternalVoltage"].as<bool>();
battery.internalSleepVoltage = data["battery"]["internalSleepVoltage"].as<float>();
battery.sendExternalVoltage = data["battery"]["sendExternalVoltage"].as<bool>();
battery.externalVoltagePin = data["battery"]["externalVoltagePin"].as<int>();
battery.monitorExternalVoltage = data["battery"]["monitorExternalVoltage"].as<bool>();
battery.externalSleepVoltage = data["battery"]["externalSleepVoltage"].as<float>();
2024-02-24 14:09:05 +01:00
aprs_is.passcode = data["aprs_is"]["passcode"].as<String>();
aprs_is.server = data["aprs_is"]["server"].as<String>();
aprs_is.port = data["aprs_is"]["port"].as<int>();
loramodule.spreadingFactor = data["lora"]["spreadingFactor"].as<int>();
loramodule.signalBandwidth = data["lora"]["signalBandwidth"].as<long>();
loramodule.codingRate4 = data["lora"]["codingRate4"].as<int>();
loramodule.power = data["lora"]["power"].as<int>();
display.alwaysOn = data["display"]["alwaysOn"].as<bool>();
display.timeout = data["display"]["timeout"].as<int>();
display.turn180 = data["display"]["turn180"].as<bool>();
syslog.active = data["syslog"]["active"].as<bool>();
syslog.server = data["syslog"]["server"].as<String>();
syslog.port = data["syslog"]["port"].as<int>();
bme.active = data["bme"]["active"].as<bool>();
2024-05-13 20:00:07 +02:00
bme.heightCorrection = data["bme"]["heightCorrection"].as<int>();
bme.temperatureCorrection = data["bme"]["temperatureCorrection"].as<float>();
2024-02-24 14:09:05 +01:00
ota.username = data["ota"]["username"].as<String>();
ota.password = data["ota"]["password"].as<String>();
2024-03-17 12:21:11 +01:00
tnc.enableServer = data["tnc"]["enableServer"].as<bool>();
tnc.enableSerial = data["tnc"]["enableSerial"].as<bool>();
tnc.acceptOwn = data["tnc"]["acceptOwn"].as<bool>();
2024-03-28 18:00:46 +01:00
lowPowerMode = data["other"]["lowPowerMode"].as<bool>();
lowVoltageCutOff = data["other"]["lowVoltageCutOff"].as<double>();
2024-03-28 18:00:46 +01:00
2024-05-22 22:19:45 +02:00
backupDigiMode = data["other"]["backupDigiMode"].as<bool>();
2024-05-23 01:19:25 +02:00
rebootMode = data["other"]["rebootMode"].as<bool>();
rebootModeTime = data["other"]["rebootModeTime"].as<int>();
int stationMode = data["stationMode"].as<int>(); // deprecated but need to specify config version
2024-03-07 17:46:38 +01:00
if (stationMode == 0) {
// Load new settings
beacon.latitude = data["beacon"]["latitude"].as<double>();
beacon.longitude = data["beacon"]["longitude"].as<double>();
beacon.comment = data["beacon"]["comment"].as<String>();
beacon.overlay = data["beacon"]["overlay"].as<String>();
beacon.symbol = data["beacon"]["symbol"].as<String>();
beacon.interval = data["beacon"]["interval"].as<int>();
beacon.sendViaAPRSIS = data["beacon"]["sendViaAPRSIS"].as<bool>();
beacon.sendViaRF = data["beacon"]["sendViaRF"].as<bool>();
beacon.path = data["beacon"]["path"].as<String>();
digi.mode = data["digi"]["mode"].as<int>();
aprs_is.active = data["aprs_is"]["active"].as<bool>();
aprs_is.filter = data["aprs_is"]["filter"].as<String>();
2024-05-20 23:57:58 +02:00
aprs_is.messagesToRF = data["aprs_is"]["messagesToRF"].as<bool>();
aprs_is.objectsToRF = data["aprs_is"]["objectsToRF"].as<bool>();
2024-03-07 17:46:38 +01:00
loramodule.txFreq = data["lora"]["txFreq"].as<long>();
loramodule.rxFreq = data["lora"]["rxFreq"].as<long>();
loramodule.txActive = data["lora"]["txActive"].as<bool>();
loramodule.rxActive = data["lora"]["rxActive"].as<bool>();
} else {
// Load old settings and put into new variables not actual config
String iGateComment = data["iGateComment"].as<String>();
int beaconInterval = data["other"]["beaconInterval"].as<int>();
long iGateFreq = data["lora"]["iGateFreq"].as<long>();
long digirepeaterTxFreq = data["lora"]["digirepeaterTxFreq"].as<long>();
long digirepeaterRxFreq = data["lora"]["digirepeaterRxFreq"].as<long>();
String digiComment = data["digi"]["comment"].as<String>();
double digiLatitude = data["digi"]["latitude"].as<double>();
double digiLongitude = data["digi"]["longitude"].as<double>();
beacon.latitude = digiLatitude;
beacon.longitude = digiLongitude;
beacon.interval = beaconInterval;
2024-05-24 16:43:06 +02:00
2024-03-07 17:46:38 +01:00
loramodule.txFreq = digirepeaterTxFreq;
loramodule.rxFreq = digirepeaterRxFreq;
loramodule.rxActive = true;
beacon.sendViaAPRSIS = true;
beacon.sendViaRF = false;
switch (stationMode) {
case 1: // IGate only
// aprs_is.active = true; // better don't do that automatically
beacon.comment = iGateComment;
loramodule.rxFreq = iGateFreq;
break;
case 5: // Digi + IGate
case 2: // Digi + IGate
// aprs_is.active = true; // better don't do that automatically
// digi.mode = 2; // better don't do that automatically
beacon.comment = digiComment;
loramodule.rxFreq = iGateFreq;
break;
case 3: // Digi
case 4: // Digi
// digi.mode = 2; // better don't do that automatically
beacon.comment = digiComment;
break;
}
reload = true;
}
2024-02-24 14:09:05 +01:00
if (wifiAPs.size() == 0) { // If we don't have any WiFi's from config we need to add "empty" SSID for AUTO AP
WiFi_AP wifiap;
wifiap.ssid = "";
wifiap.password = "";
wifiAPs.push_back(wifiap);
}
configFile.close();
Serial.println("Config read successfuly");
return true;
} else {
Serial.println("Config file not found");
return false;
}
}
void Configuration::init() {
2024-03-07 17:46:38 +01:00
reload = false;
2024-02-24 14:09:05 +01:00
WiFi_AP wifiap;
2024-05-20 23:57:58 +02:00
wifiap.ssid = "";
wifiap.password = "";
2024-05-24 16:43:06 +02:00
2024-02-24 14:09:05 +01:00
wifiAPs.push_back(wifiap);
2024-05-20 23:57:58 +02:00
wifiAutoAP.password = "1234567890";
wifiAutoAP.powerOff = 15;
2024-02-24 14:09:05 +01:00
2024-05-20 23:57:58 +02:00
callsign = "N0CALL";
2024-03-07 17:46:38 +01:00
2024-05-20 23:57:58 +02:00
beacon.comment = "LoRa APRS"; // new
beacon.latitude = 0.0; // new
beacon.longitude = 0.0; // new
beacon.interval = 15; // new
beacon.overlay = "L"; // new
beacon.symbol = "#"; // new
beacon.sendViaAPRSIS = true; // new
beacon.sendViaRF = false; // new
beacon.path = "WIDE1-1"; // new
2024-03-07 17:46:38 +01:00
2024-05-24 16:43:06 +02:00
digi.mode = 0;
2024-02-24 14:09:05 +01:00
2024-05-20 23:57:58 +02:00
tnc.enableServer = false;
tnc.enableSerial = false;
tnc.acceptOwn = false;
2024-03-17 12:21:11 +01:00
2024-05-20 23:57:58 +02:00
aprs_is.active = false; // new
aprs_is.passcode = "XYZVW";
aprs_is.server = "rotate.aprs2.net";
aprs_is.port = 14580;
aprs_is.filter = "m/10"; // new
aprs_is.messagesToRF = false;
aprs_is.objectsToRF = false;
2024-03-07 17:46:38 +01:00
2024-05-20 23:57:58 +02:00
loramodule.txFreq = 433775000; // new
loramodule.rxFreq = 433775000; // new
loramodule.spreadingFactor = 12;
loramodule.signalBandwidth = 125000;
loramodule.codingRate4 = 5;
loramodule.power = 20;
loramodule.txActive = false; // new
loramodule.rxActive = true; // new
display.alwaysOn = true;
display.timeout = 4;
display.turn180 = false;
syslog.active = false;
syslog.server = "192.168.0.100";
syslog.port = 514;
bme.active = false;
bme.heightCorrection = 0;
bme.temperatureCorrection = 0.0;
ota.username = "";
ota.password = "";
2024-02-24 14:09:05 +01:00
2024-05-24 04:52:17 +02:00
2024-05-20 23:57:58 +02:00
rememberStationTime = 30;
2024-05-24 16:43:06 +02:00
battery.sendInternalVoltage = false;
battery.monitorInternalVoltage = false;
battery.internalSleepVoltage = 3.0;
battery.sendExternalVoltage = false;
battery.externalVoltagePin = 34;
battery.monitorExternalVoltage = false;
battery.externalSleepVoltage = 3.0;
2024-02-24 14:09:05 +01:00
2024-05-20 23:57:58 +02:00
lowPowerMode = false;
lowVoltageCutOff = 0;
2024-03-28 18:00:46 +01:00
2024-05-22 22:19:45 +02:00
backupDigiMode = false;
2024-05-23 01:19:25 +02:00
rebootMode = false;
rebootModeTime = 0;
2024-05-22 22:19:45 +02:00
Serial.println("All is Written!");
2023-06-06 17:21:59 +02:00
}
2024-02-24 14:09:05 +01:00
Configuration::Configuration() {
if (!SPIFFS.begin(false)) {
Serial.println("SPIFFS Mount Failed");
return;
} else {
2024-03-07 17:46:38 +01:00
Serial.println("SPIFFS Mounted");
2024-02-24 14:09:05 +01:00
}
2024-03-07 17:46:38 +01:00
2024-02-24 14:09:05 +01:00
bool exists = SPIFFS.exists("/igate_conf.json");
if (!exists) {
init();
writeFile();
ESP.restart();
2023-06-06 17:21:59 +02:00
}
2024-03-07 17:46:38 +01:00
2024-02-24 14:09:05 +01:00
readFile();
2023-06-06 17:21:59 +02:00
}