WiFi credential saving on setup

This commit is contained in:
2025-12-18 15:01:25 -06:00
parent 857526a358
commit 3e07f81728
3 changed files with 53 additions and 25 deletions

View File

@@ -1,6 +1,8 @@
#include "BLE.hpp" #include "BLE.hpp"
#include "NimBLEDevice.h" #include "NimBLEDevice.h"
#include "WiFi.hpp" #include "WiFi.hpp"
#include "nvs_flash.h"
#include "defines.h"
std::atomic<bool> flag_scan_requested{false}; std::atomic<bool> flag_scan_requested{false};
std::atomic<bool> credsGiven{false}; std::atomic<bool> credsGiven{false};
@@ -106,7 +108,21 @@ bool BLEtick(NimBLEAdvertising* pAdvertising) {
else wifiConnect = bmWiFi.attemptConnect(tmpSSID.c_str(), tmpPASS.c_str(), tmpAUTH); else wifiConnect = bmWiFi.attemptConnect(tmpSSID.c_str(), tmpPASS.c_str(), tmpAUTH);
if (!wifiConnect) return false; 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 // Authenticate with server here
} }
return false; return false;

View File

@@ -7,6 +7,11 @@
#define ccwMax 10 #define ccwMax 10
#define cwMax 0 #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 getMovingCW(port) ((movingCW & (1 << port)) >> port)
#define setMovingCW(port) (movingCW |= (1 << port)) #define setMovingCW(port) (movingCW |= (1 << port))

View File

@@ -19,34 +19,41 @@ extern "C" void app_main() {
bmWiFi.init(); bmWiFi.init();
nvs_handle_t WiFiHandle; nvs_handle_t WiFiHandle;
nvs_open("WiFiCreds", NVS_READWRITE, &WiFiHandle); if (nvs_open(nvsWiFi, NVS_READWRITE, &WiFiHandle) == ESP_OK) {
size_t ssidSize; size_t ssidSize;
esp_err_t WiFiPrefsError = nvs_get_str(WiFiHandle, "SSID", NULL, &ssidSize); esp_err_t WiFiPrefsError = nvs_get_str(WiFiHandle, ssidTag, NULL, &ssidSize);
size_t pwSize; size_t pwSize;
WiFiPrefsError |= nvs_get_str(WiFiHandle, "PW", NULL, &pwSize); WiFiPrefsError |= nvs_get_str(WiFiHandle, passTag, NULL, &pwSize);
uint8_t authMode; uint8_t authMode;
WiFiPrefsError |= nvs_get_u8(WiFiHandle, "AuthMode", &authMode); WiFiPrefsError |= nvs_get_u8(WiFiHandle, authTag, &authMode);
if (WiFiPrefsError == ESP_ERR_NVS_NOT_FOUND) { if (WiFiPrefsError == ESP_ERR_NVS_NOT_FOUND) {
// Make the RGB LED a certain color (Blue?) // Make the RGB LED a certain color (Blue?)
nvs_close(WiFiHandle); nvs_close(WiFiHandle);
initialSetup(); initialSetup();
} else if (WiFiPrefsError == ESP_OK) { } else if (WiFiPrefsError == ESP_OK) {
char ssid[ssidSize]; char ssid[ssidSize];
nvs_get_str(WiFiHandle, "SSID", ssid, &ssidSize); nvs_get_str(WiFiHandle, ssidTag, ssid, &ssidSize);
char pw[pwSize]; char pw[pwSize];
nvs_get_str(WiFiHandle, "PW", pw, &pwSize); nvs_get_str(WiFiHandle, passTag, pw, &pwSize);
nvs_close(WiFiHandle); nvs_close(WiFiHandle);
// TODO: add enterprise support // TODO: add enterprise support
if (!bmWiFi.attemptConnect(ssid, pw, (wifi_auth_mode_t)authMode)) { if (!bmWiFi.attemptConnect(ssid, pw, (wifi_auth_mode_t)authMode)) {
// Make RGB LED certain color (Blue?)
initialSetup();
}
} else {
// Make RGB LED certain color (Blue?) // Make RGB LED certain color (Blue?)
nvs_close(WiFiHandle);
printf("Program error in Wifi Connection\n");
initialSetup(); initialSetup();
} }
} else { }
// Make RGB LED certain color (Blue?) else {
nvs_close(WiFiHandle); printf("ERROR: Couldn't open wifi NVS segment\nProgram stopped.\n");
printf("Program error in Wifi Connection\n"); while (1) {
initialSetup(); vTaskDelay(pdMS_TO_TICKS(500));
}
} }
// Main loop // Main loop