From 3e07f817289c4e56ea76976e9969273b4db2ff55 Mon Sep 17 00:00:00 2001 From: pulipakaa24 Date: Thu, 18 Dec 2025 15:01:25 -0600 Subject: [PATCH] WiFi credential saving on setup --- include/BLE.cpp | 18 +++++++++++++++- include/defines.h | 5 +++++ src/main.cpp | 55 ++++++++++++++++++++++++++--------------------- 3 files changed, 53 insertions(+), 25 deletions(-) diff --git a/include/BLE.cpp b/include/BLE.cpp index bac7ab3..9bd5c40 100644 --- a/include/BLE.cpp +++ b/include/BLE.cpp @@ -1,6 +1,8 @@ #include "BLE.hpp" #include "NimBLEDevice.h" #include "WiFi.hpp" +#include "nvs_flash.h" +#include "defines.h" std::atomic flag_scan_requested{false}; std::atomic credsGiven{false}; @@ -106,7 +108,21 @@ bool BLEtick(NimBLEAdvertising* pAdvertising) { else wifiConnect = bmWiFi.attemptConnect(tmpSSID.c_str(), tmpPASS.c_str(), tmpAUTH); if (!wifiConnect) return false; - // save wifi credentials here + nvs_handle_t WiFiHandle; + esp_err_t err = nvs_open(nvsWiFi, NVS_READWRITE, &WiFiHandle); + if (err != ESP_OK) { + printf("ERROR Saving Credentials\n"); + return false; + } + else { + err = nvs_set_str(WiFiHandle, ssidTag, tmpSSID.c_str()); + if (err == ESP_OK) err = nvs_set_str(WiFiHandle, passTag, tmpPASS.c_str()); + if (err == ESP_OK) err = nvs_set_str(WiFiHandle, unameTag, tmpUNAME.c_str()); + if (err == ESP_OK) err = nvs_set_u8(WiFiHandle, authTag, (uint8_t)tmpAUTH); + if (err == ESP_OK) nvs_commit(WiFiHandle); + nvs_close(WiFiHandle); + } + // Authenticate with server here } return false; diff --git a/include/defines.h b/include/defines.h index e7ecb80..d9494a2 100644 --- a/include/defines.h +++ b/include/defines.h @@ -7,6 +7,11 @@ #define ccwMax 10 #define cwMax 0 +#define nvsWiFi "WiFiCreds" +#define ssidTag "SSID" +#define passTag "PW" +#define authTag "AuthMode" +#define unameTag "UNAME" #define getMovingCW(port) ((movingCW & (1 << port)) >> port) #define setMovingCW(port) (movingCW |= (1 << port)) diff --git a/src/main.cpp b/src/main.cpp index c6fc3f5..469a652 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,34 +19,41 @@ extern "C" void app_main() { bmWiFi.init(); nvs_handle_t WiFiHandle; - nvs_open("WiFiCreds", NVS_READWRITE, &WiFiHandle); - size_t ssidSize; - esp_err_t WiFiPrefsError = nvs_get_str(WiFiHandle, "SSID", NULL, &ssidSize); - size_t pwSize; - WiFiPrefsError |= nvs_get_str(WiFiHandle, "PW", NULL, &pwSize); - uint8_t authMode; - WiFiPrefsError |= nvs_get_u8(WiFiHandle, "AuthMode", &authMode); + if (nvs_open(nvsWiFi, NVS_READWRITE, &WiFiHandle) == ESP_OK) { + size_t ssidSize; + esp_err_t WiFiPrefsError = nvs_get_str(WiFiHandle, ssidTag, NULL, &ssidSize); + size_t pwSize; + WiFiPrefsError |= nvs_get_str(WiFiHandle, passTag, NULL, &pwSize); + uint8_t authMode; + WiFiPrefsError |= nvs_get_u8(WiFiHandle, authTag, &authMode); - if (WiFiPrefsError == ESP_ERR_NVS_NOT_FOUND) { - // Make the RGB LED a certain color (Blue?) - nvs_close(WiFiHandle); - initialSetup(); - } else if (WiFiPrefsError == ESP_OK) { - char ssid[ssidSize]; - nvs_get_str(WiFiHandle, "SSID", ssid, &ssidSize); - char pw[pwSize]; - nvs_get_str(WiFiHandle, "PW", pw, &pwSize); - nvs_close(WiFiHandle); - // TODO: add enterprise support - if (!bmWiFi.attemptConnect(ssid, pw, (wifi_auth_mode_t)authMode)) { + if (WiFiPrefsError == ESP_ERR_NVS_NOT_FOUND) { + // Make the RGB LED a certain color (Blue?) + nvs_close(WiFiHandle); + initialSetup(); + } else if (WiFiPrefsError == ESP_OK) { + char ssid[ssidSize]; + nvs_get_str(WiFiHandle, ssidTag, ssid, &ssidSize); + char pw[pwSize]; + nvs_get_str(WiFiHandle, passTag, pw, &pwSize); + nvs_close(WiFiHandle); + // TODO: add enterprise support + if (!bmWiFi.attemptConnect(ssid, pw, (wifi_auth_mode_t)authMode)) { + // Make RGB LED certain color (Blue?) + initialSetup(); + } + } else { // Make RGB LED certain color (Blue?) + nvs_close(WiFiHandle); + printf("Program error in Wifi Connection\n"); initialSetup(); } - } else { - // Make RGB LED certain color (Blue?) - nvs_close(WiFiHandle); - printf("Program error in Wifi Connection\n"); - initialSetup(); + } + else { + printf("ERROR: Couldn't open wifi NVS segment\nProgram stopped.\n"); + while (1) { + vTaskDelay(pdMS_TO_TICKS(500)); + } } // Main loop