pre-commit fixes

This commit is contained in:
amarofarinha 2024-09-18 03:25:08 +01:00
parent ae475c3b24
commit 3a5e39171f
3 changed files with 43 additions and 50 deletions

View file

@ -37,16 +37,16 @@ IPAddress gateway(192, 168, 10, 1);
IPAddress subnet(255, 255, 255, 0); IPAddress subnet(255, 255, 255, 0);
#endif #endif
#ifdef WEBSERVER #ifdef WEBSERVER
const char* http_username = "admin"; // username to webserver authentication; const char* http_username = "admin"; // username to webserver authentication;
const char* http_password = "admin"; // password to webserver authentication; const char* http_password = "admin"; // password to webserver authentication;
// MQTT // MQTT
#ifdef MQTT #ifdef MQTT
const char* mqtt_user = "REDACTED"; // Set NULL for no username const char* mqtt_user = "REDACTED"; // Set NULL for no username
const char* mqtt_password = "REDACTED"; // Set NULL for no password const char* mqtt_password = "REDACTED"; // Set NULL for no password
#endif // USE_MQTT #endif // USE_MQTT
#endif // WEBSERVER #endif // WEBSERVER
#endif // WIFI #endif // WIFI
/* Charger settings (Optional, when using generator charging) */ /* Charger settings (Optional, when using generator charging) */
volatile float CHARGER_SET_HV = 384; // Reasonably appropriate 4.0v per cell charging of a 96s pack volatile float CHARGER_SET_HV = 384; // Reasonably appropriate 4.0v per cell charging of a 96s pack

View file

@ -2,13 +2,13 @@
#include "../../include.h" #include "../../include.h"
#include "../utils/events.h" #include "../utils/events.h"
// Configuration Parameters // Configuration Parameters
static const uint16_t WIFI_CHECK_INTERVAL = 5000; // 5 seconds static const uint16_t WIFI_CHECK_INTERVAL = 5000; // 5 seconds
static const uint16_t INIT_WIFI_FULL_RECONNECT_INTERVAL = 10000; // 10 seconds static const uint16_t INIT_WIFI_FULL_RECONNECT_INTERVAL = 10000; // 10 seconds
static const uint16_t MAX_WIFI_FULL_RECONNECT_INTERVAL = 60000; // 60 seconds static const uint16_t MAX_WIFI_FULL_RECONNECT_INTERVAL = 60000; // 60 seconds
static const uint16_t STEP_WIFI_FULL_RECONNECT_INTERVAL = 5000; // 5 seconds static const uint16_t STEP_WIFI_FULL_RECONNECT_INTERVAL = 5000; // 5 seconds
static const uint16_t MAX_RECONNECT_ATTEMPTS = 3; // Maximum number of reconnect attempts before forcing a full connection static const uint16_t MAX_RECONNECT_ATTEMPTS =
3; // Maximum number of reconnect attempts before forcing a full connection
// State variables // State variables
static unsigned long lastReconnectAttempt = 0; static unsigned long lastReconnectAttempt = 0;
@ -35,8 +35,8 @@ void init_WiFi() {
WiFi.setAutoReconnect(true); WiFi.setAutoReconnect(true);
#ifdef WIFICONFIG #ifdef WIFICONFIG
// Set static IP // Set static IP
WiFi.config(local_IP, gateway, subnet); WiFi.config(local_IP, gateway, subnet);
#endif #endif
// Initialize Wi-Fi event handlers // Initialize Wi-Fi event handlers
@ -46,43 +46,41 @@ void init_WiFi() {
// Start Wi-Fi connection // Start Wi-Fi connection
connectToWiFi(); connectToWiFi();
} }
// Task to monitor Wi-Fi status and handle reconnections // Task to monitor Wi-Fi status and handle reconnections
void wifi_monitor() { void wifi_monitor() {
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
// Check if it's time to monitor the Wi-Fi status // Check if it's time to monitor the Wi-Fi status
if (currentMillis - lastWiFiCheck > WIFI_CHECK_INTERVAL) { if (currentMillis - lastWiFiCheck > WIFI_CHECK_INTERVAL) {
lastWiFiCheck = currentMillis; lastWiFiCheck = currentMillis;
wl_status_t status = WiFi.status(); wl_status_t status = WiFi.status();
if (status != WL_CONNECTED ) { if (status != WL_CONNECTED) {
Serial.println("Wi-Fi not connected, attempting to reconnect..."); Serial.println("Wi-Fi not connected, attempting to reconnect...");
// Try WiFi.reconnect() if it was successfully connected at least once // Try WiFi.reconnect() if it was successfully connected at least once
if (hasConnectedBefore) { if (hasConnectedBefore) {
if (WiFi.reconnect()) { if (WiFi.reconnect()) {
Serial.println("Wi-Fi reconnect attempt..."); Serial.println("Wi-Fi reconnect attempt...");
reconnectAttempts = 0; // Reset the attempt counter on successful reconnect reconnectAttempts = 0; // Reset the attempt counter on successful reconnect
} else {
reconnectAttempts++;
if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
Serial.println("Failed to reconnect multiple times, forcing a full connection attempt...");
FullReconnectToWiFi();
}
}
} else { } else {
// If no previous connection, force a full connection attempt reconnectAttempts++;
if (currentMillis - lastReconnectAttempt > current_full_reconnect_interval) { if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
Serial.println("No previous OK connection, force a full connection attempt..."); Serial.println("Failed to reconnect multiple times, forcing a full connection attempt...");
FullReconnectToWiFi(); FullReconnectToWiFi();
} }
} }
} else {
// If no previous connection, force a full connection attempt
if (currentMillis - lastReconnectAttempt > current_full_reconnect_interval) {
Serial.println("No previous OK connection, force a full connection attempt...");
FullReconnectToWiFi();
}
} }
} }
}
} }
// Function to force a full reconnect to Wi-Fi // Function to force a full reconnect to Wi-Fi
@ -102,7 +100,7 @@ static void FullReconnectToWiFi() {
static void connectToWiFi() { static void connectToWiFi() {
if (WiFi.status() != WL_CONNECTED) { if (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to Wi-Fi..."); Serial.println("Connecting to Wi-Fi...");
WiFi.begin( ssid.c_str(), password.c_str(),wifi_channel); WiFi.begin(ssid.c_str(), password.c_str(), wifi_channel);
} else { } else {
Serial.println("Wi-Fi already connected."); Serial.println("Wi-Fi already connected.");
} }
@ -118,7 +116,6 @@ static void onWifiConnect(WiFiEvent_t event, WiFiEventInfo_t info) {
reconnectAttempts = 0; // Reset the attempt counter reconnectAttempts = 0; // Reset the attempt counter
current_full_reconnect_interval = INIT_WIFI_FULL_RECONNECT_INTERVAL; // Reset the full reconnect interval current_full_reconnect_interval = INIT_WIFI_FULL_RECONNECT_INTERVAL; // Reset the full reconnect interval
clear_event(EVENT_WIFI_CONNECT); clear_event(EVENT_WIFI_CONNECT);
} }
// Event handler for Wi-Fi Got IP // Event handler for Wi-Fi Got IP

View file

@ -2,8 +2,8 @@
#define WIFI_H #define WIFI_H
#include <WiFi.h> #include <WiFi.h>
#include "../../include.h"
#include <string> #include <string>
#include "../../include.h"
extern std::string ssid; extern std::string ssid;
extern std::string password; extern std::string password;
@ -11,7 +11,6 @@ extern const uint8_t wifi_channel;
extern const char* ssidAP; extern const char* ssidAP;
extern const char* passwordAP; extern const char* passwordAP;
void init_WiFi(); void init_WiFi();
void wifi_monitor(); void wifi_monitor();
static void connectToWiFi(); static void connectToWiFi();
@ -20,8 +19,6 @@ static void onWifiConnect(WiFiEvent_t event, WiFiEventInfo_t info);
static void onWifiDisconnect(WiFiEvent_t event, WiFiEventInfo_t info); static void onWifiDisconnect(WiFiEvent_t event, WiFiEventInfo_t info);
static void onWifiGotIP(WiFiEvent_t event, WiFiEventInfo_t info); static void onWifiGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
#ifdef WIFIAP #ifdef WIFIAP
void init_WiFi_AP(); void init_WiFi_AP();
#endif // WIFIAP #endif // WIFIAP
@ -29,7 +26,6 @@ void init_WiFi_AP();
#ifdef MDNSRESPONDER #ifdef MDNSRESPONDER
// Initialise mDNS // Initialise mDNS
void init_mDNS(); void init_mDNS();
#endif // MDNSRESPONDER #endif // MDNSRESPONDER
#endif #endif