mirror of
https://github.com/BotChain-Robots/firmware.git
synced 2026-07-08 17:47:21 +02:00
Wifi configuration from NVS
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user