mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-03 09:49:32 +02:00
Change Serial logging to flexible logging (#690)
* Add Logging class Add Logging class which inherits from Print class, to be able to route logging to USB Serial or to memory for display in the webpage. Adds a log webpage only visible when DEBUG_VIA_WEB is defined.
This commit is contained in:
parent
b2aa3dc75b
commit
c713d0a94e
36 changed files with 1020 additions and 798 deletions
|
@ -72,28 +72,28 @@ void wifi_monitor() {
|
|||
// Increase the current check interval if it's not at the maximum
|
||||
if (current_check_interval + STEP_WIFI_CHECK_INTERVAL <= MAX_STEP_WIFI_CHECK_INTERVAL)
|
||||
current_check_interval += STEP_WIFI_CHECK_INTERVAL;
|
||||
#ifdef DEBUG_VIA_USB
|
||||
Serial.println("Wi-Fi not connected, attempting to reconnect...");
|
||||
#ifdef DEBUG_LOG
|
||||
logging.println("Wi-Fi not connected, attempting to reconnect...");
|
||||
#endif
|
||||
// Try WiFi.reconnect() if it was successfully connected at least once
|
||||
if (hasConnectedBefore) {
|
||||
lastReconnectAttempt = millis(); // Reset reconnection attempt timer
|
||||
#ifdef DEBUG_VIA_USB
|
||||
Serial.println("Wi-Fi reconnect attempt...");
|
||||
#ifdef DEBUG_LOG
|
||||
logging.println("Wi-Fi reconnect attempt...");
|
||||
#endif
|
||||
if (WiFi.reconnect()) {
|
||||
#ifdef DEBUG_VIA_USB
|
||||
Serial.println("Wi-Fi reconnect attempt sucess...");
|
||||
#ifdef DEBUG_LOG
|
||||
logging.println("Wi-Fi reconnect attempt sucess...");
|
||||
#endif
|
||||
reconnectAttempts = 0; // Reset the attempt counter on successful reconnect
|
||||
} else {
|
||||
#ifdef DEBUG_VIA_USB
|
||||
Serial.println("Wi-Fi reconnect attempt error...");
|
||||
#ifdef DEBUG_LOG
|
||||
logging.println("Wi-Fi reconnect attempt error...");
|
||||
#endif
|
||||
reconnectAttempts++;
|
||||
if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
|
||||
#ifdef DEBUG_VIA_USB
|
||||
Serial.println("Failed to reconnect multiple times, forcing a full connection attempt...");
|
||||
#ifdef DEBUG_LOG
|
||||
logging.println("Failed to reconnect multiple times, forcing a full connection attempt...");
|
||||
#endif
|
||||
FullReconnectToWiFi();
|
||||
}
|
||||
|
@ -101,8 +101,8 @@ void wifi_monitor() {
|
|||
} else {
|
||||
// If no previous connection, force a full connection attempt
|
||||
if (currentMillis - lastReconnectAttempt > current_full_reconnect_interval) {
|
||||
#ifdef DEBUG_VIA_USB
|
||||
Serial.println("No previous OK connection, force a full connection attempt...");
|
||||
#ifdef DEBUG_LOG
|
||||
logging.println("No previous OK connection, force a full connection attempt...");
|
||||
#endif
|
||||
FullReconnectToWiFi();
|
||||
}
|
||||
|
@ -127,13 +127,13 @@ static void FullReconnectToWiFi() {
|
|||
static void connectToWiFi() {
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
lastReconnectAttempt = millis(); // Reset the reconnect attempt timer
|
||||
#ifdef DEBUG_VIA_USB
|
||||
Serial.println("Connecting to Wi-Fi...");
|
||||
#ifdef DEBUG_LOG
|
||||
logging.println("Connecting to Wi-Fi...");
|
||||
#endif
|
||||
WiFi.begin(ssid.c_str(), password.c_str(), wifi_channel);
|
||||
} else {
|
||||
#ifdef DEBUG_VIA_USB
|
||||
Serial.println("Wi-Fi already connected.");
|
||||
#ifdef DEBUG_LOG
|
||||
logging.println("Wi-Fi already connected.");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@ -143,10 +143,11 @@ static void onWifiConnect(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|||
clear_event(EVENT_WIFI_DISCONNECT);
|
||||
set_event(EVENT_WIFI_CONNECT, 0);
|
||||
connected_once = true;
|
||||
#ifdef DEBUG_VIA_USB
|
||||
Serial.println("Wi-Fi connected.");
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
#ifdef DEBUG_LOG
|
||||
logging.print("Wi-Fi connected. RSSI: ");
|
||||
logging.print(-WiFi.RSSI());
|
||||
logging.print(" dBm, IP address: ");
|
||||
logging.println(WiFi.localIP().toString());
|
||||
#endif
|
||||
hasConnectedBefore = true; // Mark as successfully connected at least once
|
||||
reconnectAttempts = 0; // Reset the attempt counter
|
||||
|
@ -159,10 +160,10 @@ static void onWifiConnect(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|||
static void onWifiGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
//clear disconnects events if we got a IP
|
||||
clear_event(EVENT_WIFI_DISCONNECT);
|
||||
#ifdef DEBUG_VIA_USB
|
||||
Serial.println("Wi-Fi Got IP.");
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
#ifdef DEBUG_LOG
|
||||
logging.print("Wi-Fi Got IP. ");
|
||||
logging.print("IP address: ");
|
||||
logging.println(WiFi.localIP().toString());
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -170,8 +171,8 @@ static void onWifiGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
|
|||
static void onWifiDisconnect(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
if (connected_once)
|
||||
set_event(EVENT_WIFI_DISCONNECT, 0);
|
||||
#ifdef DEBUG_VIA_USB
|
||||
Serial.println("Wi-Fi disconnected.");
|
||||
#ifdef DEBUG_LOG
|
||||
logging.println("Wi-Fi disconnected.");
|
||||
#endif
|
||||
//we dont do anything here, the reconnect will be handled by the monitor
|
||||
//too many events received when the connection is lost
|
||||
|
@ -188,8 +189,8 @@ void init_mDNS() {
|
|||
|
||||
// Initialize mDNS .local resolution
|
||||
if (!MDNS.begin(mdnsHost)) {
|
||||
#ifdef DEBUG_VIA_USB
|
||||
Serial.println("Error setting up MDNS responder!");
|
||||
#ifdef DEBUG_LOG
|
||||
logging.println("Error setting up MDNS responder!");
|
||||
#endif
|
||||
} else {
|
||||
// Advertise via bonjour the service so we can auto discover these battery emulators on the local network.
|
||||
|
@ -200,16 +201,16 @@ void init_mDNS() {
|
|||
|
||||
#ifdef WIFIAP
|
||||
void init_WiFi_AP() {
|
||||
#ifdef DEBUG_VIA_USB
|
||||
Serial.println("Creating Access Point: " + String(ssidAP));
|
||||
Serial.println("With password: " + String(passwordAP));
|
||||
#ifdef DEBUG_LOG
|
||||
logging.println("Creating Access Point: " + String(ssidAP));
|
||||
logging.println("With password: " + String(passwordAP));
|
||||
#endif
|
||||
WiFi.softAP(ssidAP, passwordAP);
|
||||
IPAddress IP = WiFi.softAPIP();
|
||||
#ifdef DEBUG_VIA_USB
|
||||
Serial.println("Access Point created.");
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(IP);
|
||||
#ifdef DEBUG_LOG
|
||||
logging.println("Access Point created.");
|
||||
logging.print("IP address: ");
|
||||
logging.println(IP);
|
||||
#endif
|
||||
}
|
||||
#endif // WIFIAP
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue