100 lines
3.3 KiB
C++
100 lines
3.3 KiB
C++
#include "ConnectWiFi.h"
|
|
#include "Config.h"
|
|
|
|
extern bool g_bWiFiSetupExecuted;
|
|
extern bool g_bWiFiHasBeenConnected;
|
|
void setupPostWiFi(bool bBoot = false);
|
|
|
|
void WiFiEvent(WiFiEvent_t event) {
|
|
switch (event) {
|
|
case IP_EVENT_STA_GOT_IP:
|
|
DPRINTF("WiFi connected, IP: %s\n", WiFi.localIP().toString().c_str());
|
|
g_bWiFiHasBeenConnected = true;
|
|
ledcWrite(PIN_LED_WIFI, PWM_FULL * 19 / 20); // LED_OFF
|
|
if (!g_bWiFiSetupExecuted) setupPostWiFi(false);
|
|
break;
|
|
case WIFI_EVENT_STA_DISCONNECTED:
|
|
DPRINTLN("WiFi disconnected.");
|
|
ledcWrite(PIN_LED_WIFI, PWM_OFF); // LED_ON
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Function to compare current Wi-Fi credentials with the ones in config
|
|
void checkAndUpdateWiFiCredentials() {
|
|
// Check if the SSID or PW are different from the current Wi-Fi credentials
|
|
if (strncmp(BLE_SSID, config.ssid, sizeof(BLE_SSID)) ||
|
|
strncmp(BLE_PW, config.pw, sizeof(BLE_PW))) {
|
|
DPRINTF("BLE Credentials SSID(%s) PW(%S)\n", BLE_SSID, BLE_PW);
|
|
DPRINTF("Cfg Credentials SSID(%s) PW(%S)\n", config.ssid, config.pw);
|
|
DPRINTLN("Wi-Fi credentials changed! Saving new credentials...");
|
|
strncpy(config.ssid, BLE_SSID, sizeof(BLE_SSID));
|
|
strncpy(config.pw, BLE_PW, sizeof(BLE_PW));
|
|
// Save the new credentials
|
|
config.save();
|
|
|
|
// Restart ESP32 to apply the new credentials
|
|
ESP.restart();
|
|
}
|
|
}
|
|
|
|
IRAM_ATTR
|
|
void checkWiFi(unsigned long tickMillis) {
|
|
static unsigned long lastAttempt = 0;
|
|
static bool bConnecting = false;
|
|
|
|
wl_status_t status = WiFi.status();
|
|
|
|
// Connected
|
|
if (status == WL_CONNECTED) {
|
|
bConnecting = false;
|
|
g_bWiFiHasBeenConnected = true;
|
|
return; // Already connected, no need to proceed further
|
|
}
|
|
|
|
checkAndUpdateWiFiCredentials();
|
|
|
|
// Connecting
|
|
if (bConnecting) {
|
|
if (tickMillis - lastAttempt < 60000) {
|
|
// give 60 seconds for connection try
|
|
return;
|
|
}
|
|
|
|
// Connection Failure
|
|
DPRINTF("Connection timed out! Status: %d\n", status);
|
|
//WiFi.disconnect(false, true);
|
|
bConnecting = false;
|
|
g_bWiFiHasBeenConnected = false;
|
|
}
|
|
|
|
// Not Connected 1 - Reconnect
|
|
if (status == WL_DISCONNECTED && g_bWiFiHasBeenConnected) {
|
|
DPRINTLN("Attempting WiFi reconnection...");
|
|
WiFi.reconnect();
|
|
ledcWrite(PIN_LED_WIFI, PWM_FULL * 19 / 20); // Light Blink
|
|
lastAttempt = tickMillis;
|
|
bConnecting = true;
|
|
}
|
|
|
|
|
|
// Not Conneccted 2 - Connect
|
|
if (tickMillis - lastAttempt > 300000) { // Retry every 5 minutes
|
|
DPRINTF("Loop: Connecting to WiFi: SSID: '%s', PW: '%s'\n", config.ssid, config.pw);
|
|
WiFi.disconnect(); // Stop Wi-Fi connection attempt
|
|
// delay(50);
|
|
// WiFi.mode(WIFI_OFF);
|
|
// delay(100);
|
|
// WiFi.mode(WIFI_STA);
|
|
// delay(50);
|
|
wl_status_t ret = WiFi.begin(config.ssid, config.pw);
|
|
DPRINTF(" WiFi.Begin(%s,%s) returned %d\n", config.ssid, config.pw, ret);
|
|
lastAttempt = tickMillis;
|
|
|
|
ledcWrite(PIN_LED_WIFI, PWM_FULL / 10); // Light Blink
|
|
lastAttempt = tickMillis;
|
|
bConnecting = true;
|
|
}
|
|
} |