WiFi credential saving on setup
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#include "BLE.hpp"
|
||||
#include "NimBLEDevice.h"
|
||||
#include "WiFi.hpp"
|
||||
#include "nvs_flash.h"
|
||||
#include "defines.h"
|
||||
|
||||
std::atomic<bool> flag_scan_requested{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);
|
||||
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;
|
||||
|
||||
@@ -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))
|
||||
|
||||
19
src/main.cpp
19
src/main.cpp
@@ -19,13 +19,13 @@ extern "C" void app_main() {
|
||||
bmWiFi.init();
|
||||
|
||||
nvs_handle_t WiFiHandle;
|
||||
nvs_open("WiFiCreds", NVS_READWRITE, &WiFiHandle);
|
||||
if (nvs_open(nvsWiFi, NVS_READWRITE, &WiFiHandle) == ESP_OK) {
|
||||
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;
|
||||
WiFiPrefsError |= nvs_get_str(WiFiHandle, "PW", NULL, &pwSize);
|
||||
WiFiPrefsError |= nvs_get_str(WiFiHandle, passTag, NULL, &pwSize);
|
||||
uint8_t authMode;
|
||||
WiFiPrefsError |= nvs_get_u8(WiFiHandle, "AuthMode", &authMode);
|
||||
WiFiPrefsError |= nvs_get_u8(WiFiHandle, authTag, &authMode);
|
||||
|
||||
if (WiFiPrefsError == ESP_ERR_NVS_NOT_FOUND) {
|
||||
// Make the RGB LED a certain color (Blue?)
|
||||
@@ -33,9 +33,9 @@ extern "C" void app_main() {
|
||||
initialSetup();
|
||||
} else if (WiFiPrefsError == ESP_OK) {
|
||||
char ssid[ssidSize];
|
||||
nvs_get_str(WiFiHandle, "SSID", ssid, &ssidSize);
|
||||
nvs_get_str(WiFiHandle, ssidTag, ssid, &ssidSize);
|
||||
char pw[pwSize];
|
||||
nvs_get_str(WiFiHandle, "PW", 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)) {
|
||||
@@ -48,6 +48,13 @@ extern "C" void app_main() {
|
||||
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
|
||||
while (1) {
|
||||
|
||||
Reference in New Issue
Block a user