Wifi configuration from NVS

This commit is contained in:
2025-07-20 20:53:12 -04:00
parent 82b2ad926e
commit a47d57ab29
5 changed files with 57 additions and 5 deletions

View File

@@ -45,6 +45,29 @@ ModuleType ConfigManager::get_module_type() {
return static_cast<ModuleType>(type);
}
std::string ConfigManager::get_wifi_ssid() {
return get_string(WIFI_SSID_KEY);
}
std::string ConfigManager::get_wifi_password() {
return get_string(WIFI_PASSWORD_KEY);
}
std::string ConfigManager::get_string(const char *key) {
nvs_handle config_handle;
nvs_open(NVS_FLASH_NAMESPACE, NVS_READONLY, &config_handle);
size_t required_size; // get size of the string
nvs_get_str(config_handle, key, nullptr, &required_size);
std::string str;
str.resize(required_size);
nvs_get_str(config_handle, key, str.data(), &required_size);
nvs_close(config_handle);
return str;
}
void ConfigManager::set_module_id(const uint16_t id) {
nvs_handle config_handle;
nvs_open(NVS_FLASH_NAMESPACE, NVS_READWRITE, &config_handle);
@@ -58,3 +81,18 @@ void ConfigManager::set_module_type(const ModuleType type) {
nvs_set_u16(config_handle, MODULE_TYPE_KEY, type);
nvs_close(config_handle);
}
void ConfigManager::set_wifi_ssid(const char* ssid) {
nvs_handle config_handle;
nvs_open(NVS_FLASH_NAMESPACE, NVS_READWRITE, &config_handle);
nvs_set_str(config_handle, WIFI_SSID_KEY, ssid);
nvs_close(config_handle);
}
void ConfigManager::set_wifi_password(const char* password) {
nvs_handle config_handle;
nvs_open(NVS_FLASH_NAMESPACE, NVS_READWRITE, &config_handle);
nvs_set_str(config_handle, WIFI_PASSWORD_KEY, password);
nvs_close(config_handle);
}

View File

@@ -14,9 +14,17 @@ public:
static uint16_t get_module_id();
static ModuleType get_module_type();
static std::string get_wifi_ssid();
static std::string get_wifi_password();
static void set_module_id(uint16_t id);
static void set_module_type(ModuleType type);
static void set_wifi_ssid(const char* ssid);
static void set_wifi_password(const char* password);
private:
static std::string get_string(const char* key);
};
#endif //CONFIGMANAGER_H

View File

@@ -6,10 +6,12 @@
#define CONFIG_H
#define NVS_FLASH_NAMESPACE "app"
#define DEFAULT_MODULE_ID 0
#define DEFAULT_MODULE_ID 1
#define DEFAULT_MODULE_TYPE 0
#define MODULE_ID_KEY "module_id"
#define MODULE_TYPE_KEY "module_type"
#define WIFI_SSID_KEY "wifi_ssid"
#define WIFI_PASSWORD_KEY "wifi_password"
#endif //CONFIG_H

View File

@@ -1,5 +1,6 @@
#include "WifiManager.h"
#include <ConfigManager.h>
#include <esp_netif.h>
#include <esp_event.h>
#include <freertos/semphr.h>
@@ -96,15 +97,19 @@ int WifiManager::init_connection() {
esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, this);
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, this);
// TEMP: get config from nvs key value store
wifi_config_t wifi_configuration;
wifi_configuration = {
.sta = {
.ssid = "",
.password = "",
}
};
std::string ssid = ConfigManager::get_wifi_ssid();
std::string pass = ConfigManager::get_wifi_password();
std::strncpy(reinterpret_cast<char *>(wifi_configuration.sta.ssid), ssid.c_str(), 32);
std::strncpy(reinterpret_cast<char *>(wifi_configuration.sta.password), pass.c_str(), 64);
wifi_configuration.sta.ssid[31] = '\0';
wifi_configuration.sta.password[63] = '\0';
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_set_config(static_cast<wifi_interface_t>(ESP_IF_WIFI_STA), &wifi_configuration);

View File

@@ -16,6 +16,5 @@ extern "C" [[noreturn]] void app_main(void) {
ESP_LOGI("MEM", "Free PSRAM: %d", heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
ConfigManager::init_config();
LoopManager::control_loop();
}