2025-11-28 18:26:32 -06:00
|
|
|
#include <driver/gptimer.h>
|
|
|
|
|
#include "pwm.h"
|
|
|
|
|
#include "defines.h"
|
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
#include "NimBLEDevice.h"
|
|
|
|
|
#include "BLE.hpp"
|
|
|
|
|
#include "WiFi.hpp"
|
2025-11-29 16:24:56 -06:00
|
|
|
#include "setup.hpp"
|
2025-11-28 18:26:32 -06:00
|
|
|
|
|
|
|
|
extern "C" void app_main() {
|
2025-11-29 16:24:56 -06:00
|
|
|
esp_err_t ret = nvs_flash_init(); // change to secure init logic soon!!
|
2025-11-28 18:26:32 -06:00
|
|
|
// 2. If NVS is full or corrupt (common after flashing new code), erase and retry
|
|
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2025-11-29 16:24:56 -06:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
|
ret = nvs_flash_init();
|
2025-11-28 18:26:32 -06:00
|
|
|
}
|
|
|
|
|
ESP_ERROR_CHECK(ret);
|
|
|
|
|
|
2025-11-29 16:24:56 -06:00
|
|
|
bmWiFi.init();
|
|
|
|
|
|
|
|
|
|
nvs_handle_t WiFiHandle;
|
2025-12-18 15:01:25 -06:00
|
|
|
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);
|
2025-11-28 18:26:32 -06:00
|
|
|
|
2025-12-18 15:01:25 -06:00
|
|
|
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 {
|
2025-11-29 16:24:56 -06:00
|
|
|
// Make RGB LED certain color (Blue?)
|
2025-12-18 15:01:25 -06:00
|
|
|
nvs_close(WiFiHandle);
|
|
|
|
|
printf("Program error in Wifi Connection\n");
|
2025-11-29 16:24:56 -06:00
|
|
|
initialSetup();
|
|
|
|
|
}
|
2025-12-18 15:01:25 -06:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
printf("ERROR: Couldn't open wifi NVS segment\nProgram stopped.\n");
|
|
|
|
|
while (1) {
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(500));
|
|
|
|
|
}
|
2025-11-29 16:24:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Main loop
|
2025-11-28 18:26:32 -06:00
|
|
|
while (1) {
|
2025-11-29 16:24:56 -06:00
|
|
|
// Your main application logic here
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(100));
|
2025-11-28 18:26:32 -06:00
|
|
|
}
|
|
|
|
|
}
|