add nvs code.
This commit is contained in:
67
components/Communication/Application.cpp
Normal file
67
components/Communication/Application.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
#include "Application.h"
|
||||
#include <esp_log.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <string.h>
|
||||
|
||||
Application *Application::instance() {
|
||||
static Application self;
|
||||
return &self;
|
||||
}
|
||||
|
||||
void Application::initialize() {
|
||||
initializeWifi();
|
||||
}
|
||||
|
||||
void Application::eventHandler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
|
||||
auto self = reinterpret_cast<Application *>(arg);
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
esp_wifi_connect();
|
||||
xEventGroupClearBits(self->m_wifiEventGroup, CONNECTED_BIT);
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
xEventGroupSetBits(self->m_wifiEventGroup, CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
bool Application::wifiConnect(const std::string &ssid, const std::string &password, int timeoutMillisecond) {
|
||||
wifi_config_t config = {};
|
||||
strlcpy((char *)config.sta.ssid, ssid.c_str(), sizeof(config.sta.ssid));
|
||||
if (!password.empty()) {
|
||||
strlcpy((char *)config.sta.password, password.c_str(), sizeof(config.sta.password));
|
||||
}
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &config));
|
||||
esp_wifi_connect();
|
||||
|
||||
int bits =
|
||||
xEventGroupWaitBits(m_wifiEventGroup, CONNECTED_BIT, pdFALSE, pdTRUE, timeoutMillisecond / portTICK_PERIOD_MS);
|
||||
return (bits & CONNECTED_BIT) != 0;
|
||||
}
|
||||
|
||||
void Application::initializeWifi() {
|
||||
esp_log_level_set("wifi", ESP_LOG_WARN);
|
||||
static bool initialized = false;
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
|
||||
if (m_wifiEventGroup == nullptr) {
|
||||
m_wifiEventGroup = xEventGroupCreate();
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_t *ap_netif = esp_netif_create_default_wifi_ap();
|
||||
assert(ap_netif);
|
||||
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
|
||||
assert(sta_netif);
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
ESP_ERROR_CHECK(
|
||||
esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &Application::eventHandler, this));
|
||||
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &Application::eventHandler, this));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
initialized = true;
|
||||
}
|
59
components/Communication/Application.h
Normal file
59
components/Communication/Application.h
Normal file
@ -0,0 +1,59 @@
|
||||
#ifndef __APPLICATION_H__
|
||||
#define __APPLICATION_H__
|
||||
|
||||
#include <esp_event.h>
|
||||
#include <esp_log.h>
|
||||
#include <freertos/event_groups.h>
|
||||
#include <nvs.h>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
class Application {
|
||||
constexpr static int CONNECTED_BIT = BIT0;
|
||||
constexpr static auto Namespace = "settings";
|
||||
|
||||
public:
|
||||
constexpr static int WifiJoinTimeoutMillisecond = 10000;
|
||||
static Application *instance();
|
||||
bool wifiConnect(const std::string &ssid, const std::string &password,
|
||||
int timeoutMillisecond = WifiJoinTimeoutMillisecond);
|
||||
void initialize();
|
||||
|
||||
template <typename T>
|
||||
void setField(const std::string &key, const T &value) {
|
||||
nvs_handle_t hanlde;
|
||||
esp_err_t error = nvs_open(Namespace, NVS_READWRITE, &hanlde);
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGI("App", "nvs_open() failed.");
|
||||
}
|
||||
|
||||
if (std::is_same_v<char *, std::decay_t<T>>) {
|
||||
error = nvs_set_str(hanlde, key.c_str(), value);
|
||||
} else if constexpr (std::is_same_v<std::string, T>) {
|
||||
error = nvs_set_str(hanlde, key.c_str(), value.c_str());
|
||||
} else {
|
||||
ESP_LOGW("App", "unknown data");
|
||||
}
|
||||
|
||||
if (error == ESP_OK) {
|
||||
error = nvs_commit(hanlde);
|
||||
}
|
||||
nvs_close(hanlde);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T field(const std::string &key) {
|
||||
}
|
||||
|
||||
protected:
|
||||
static void eventHandler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||
void initializeWifi();
|
||||
|
||||
Application() = default;
|
||||
|
||||
private:
|
||||
EventGroupHandle_t m_wifiEventGroup = nullptr;
|
||||
|
||||
nvs_handle_t m_nvs;
|
||||
};
|
||||
#endif // __APPLICATION_H__
|
@ -1,5 +1,6 @@
|
||||
idf_component_register(SRCS
|
||||
Application.h Application.cpp
|
||||
MqttClient.h MqttClient.cpp
|
||||
INCLUDE_DIRS .
|
||||
REQUIRES mqtt json LedController
|
||||
REQUIRES esp_wifi nvs_flash mqtt json LedController
|
||||
)
|
||||
|
Reference in New Issue
Block a user