Merge pull request #7 from htotoo/settings

save and load radio config
This commit is contained in:
Totoo
2025-09-18 14:14:00 +02:00
committed by GitHub
3 changed files with 90 additions and 6 deletions
+10
View File
@@ -469,6 +469,16 @@
case 'debug':
logToDebug(data.message);
break;
case 'radio_config':
if (data.config) {
loraPresetSelector.value = 'custom';
document.getElementById('radio-freq').value = data.config.frequency || 869.25;
loraBw.value = data.config.bandwidth || 250.0;
loraSf.value = data.config.spreading_factor || 11;
loraCr.value = data.config.coding_rate || 5;
document.getElementById('radio-power').value = data.config.power || 20;
break;
}
default:
logToDebug(`Unknown message type: ${data.type}`);
}
+18 -6
View File
@@ -15,6 +15,7 @@
#include "MeshtasticCompact.hpp"
#include "webserver.h"
#include "TMAttack.hpp"
#include "settings.hpp"
static const char* TAG = "DarkMesh";
@@ -25,16 +26,17 @@ extern "C" {
void app_main();
}
TMAttack tmAttack;
TConfig config;
Radio_PINS radio_pins = {9, 11, 10, 8, 14, 12, 13}; // Default radio pins for Heltec WSL V3.
LoraConfig lora_config = {
.frequency = 869.25,
.bandwidth = 250.0,
.spreading_factor = 11,
.coding_rate = 5,
.frequency = 869.25, // config
.bandwidth = 250.0, // config
.spreading_factor = 11, // config
.coding_rate = 5, // config
.sync_word = 0x2b,
.preamble_length = 16,
.output_power = 22,
.output_power = 22, // config
.tcxo_voltage = 1.8,
.use_regulator_ldo = false,
}; // default LoRa configuration for EU LONGFAST 433
@@ -87,6 +89,8 @@ void app_main(void) {
ESP_LOGI(TAG, "WiFi AP initialized. SSID: %s, Password: %s", ssid, password);
init_httpd();
ESP_LOGI(TAG, "Loading radio config.");
config.load_radio(lora_config);
ESP_LOGI(TAG, "Radio initializing...");
meshtasticCompact.RadioInit(RadioType::SX1262, radio_pins, lora_config);
@@ -235,6 +239,12 @@ void handle_set_config(JSON_Object* params) {
meshtasticCompact.setRadioSpreadingFactor(static_cast<uint8_t>(sf));
meshtasticCompact.setRadioCodingRate(static_cast<uint8_t>(cr));
meshtasticCompact.setRadioPower(static_cast<int8_t>(power));
lora_config.frequency = frequency;
lora_config.bandwidth = bandwidth;
lora_config.spreading_factor = static_cast<uint8_t>(sf);
lora_config.coding_rate = static_cast<uint8_t>(cr);
lora_config.output_power = static_cast<int8_t>(power);
config.save_radio(lora_config);
sendDebugMessage("Radio configuration updated.");
}
@@ -266,5 +276,7 @@ void handle_initme() {
std::string attack_json = "{ \"type\": \"status_update\", \"current_attack\": \"" + tmAttack.getCurrentAttackTypeString() + "\" }";
ws_sendall((uint8_t*)attack_json.c_str(), attack_json.length(), true);
vTaskDelay(pdMS_TO_TICKS(20));
// todo send radio config
// send radio config
std::string config_json = "{ \"type\": \"radio_config\", \"config\": { \"frequency\": " + std::to_string(lora_config.frequency) + ", \"bandwidth\": " + std::to_string(lora_config.bandwidth) + ", \"spreading_factor\": " + std::to_string(lora_config.spreading_factor) + ", \"coding_rate\": " + std::to_string(lora_config.coding_rate) + ", \"power\": " + std::to_string(lora_config.output_power) + " } }";
ws_sendall((uint8_t*)config_json.c_str(), config_json.length(), true);
}
+62
View File
@@ -0,0 +1,62 @@
#pragma once
#include <string>
#include <nvs_flash.h>
#include <nvs.h>
#include "MeshasticCompactStructs.hpp"
class TConfig {
public:
std::string ssid;
std::string password;
int channel;
TConfig() : ssid(""), password(""), channel(1) {}
bool load_radio(LoraConfig& lora_config) {
nvs_handle_t handle;
esp_err_t err = nvs_open("radio_cfg", NVS_READONLY, &handle);
if (err != ESP_OK) return false;
char tmpstr[64] = {0};
size_t tmpstrlen = sizeof(tmpstr);
err = nvs_get_str(handle, "freqstr", tmpstr, &tmpstrlen);
if (err == ESP_OK) {
// Parse frequency string
std::string freq_string(tmpstr);
lora_config.frequency = std::stof(freq_string);
}
err = nvs_get_str(handle, "bwstr", tmpstr, &tmpstrlen);
if (err == ESP_OK) {
// Parse bandwidth string
std::string bw_string(tmpstr);
lora_config.bandwidth = std::stof(bw_string);
}
err = nvs_get_u8(handle, "sf", &lora_config.spreading_factor);
if (err != ESP_OK) lora_config.spreading_factor = 11;
err = nvs_get_u8(handle, "cr", &lora_config.coding_rate);
if (err != ESP_OK) lora_config.coding_rate = 5;
err = nvs_get_i8(handle, "outpower", &lora_config.output_power);
if (err != ESP_OK) lora_config.output_power = 20;
nvs_close(handle);
return true;
}
bool
save_radio(LoraConfig& lora_config) {
nvs_handle_t handle;
esp_err_t err = nvs_open("radio_cfg", NVS_READWRITE, &handle);
if (err != ESP_OK) return false;
err = nvs_set_str(handle, "freqstr", std::to_string(lora_config.frequency).c_str());
err = nvs_set_str(handle, "bwstr", std::to_string(lora_config.bandwidth).c_str());
err = nvs_set_u8(handle, "sf", lora_config.spreading_factor);
err = nvs_set_u8(handle, "cr", lora_config.coding_rate);
err = nvs_set_i8(handle, "outpower", lora_config.output_power);
nvs_commit(handle);
nvs_close(handle);
return true;
}
};