From 70a448d828fb2bf25890b9ecb6cc5013c2034901 Mon Sep 17 00:00:00 2001 From: sieja Date: Sat, 22 Feb 2025 18:25:06 +0100 Subject: [PATCH] Ekran baterii --- .../TempHumLoggerHomeKit.ino | 126 +++++++++++++----- 1 file changed, 90 insertions(+), 36 deletions(-) diff --git a/ESP32/TempHumLoggerHomeKit/TempHumLoggerHomeKit.ino b/ESP32/TempHumLoggerHomeKit/TempHumLoggerHomeKit.ino index 03dfefe..0f55852 100644 --- a/ESP32/TempHumLoggerHomeKit/TempHumLoggerHomeKit.ino +++ b/ESP32/TempHumLoggerHomeKit/TempHumLoggerHomeKit.ino @@ -9,7 +9,7 @@ #include "SD.h" #include "SPI.h" -#define Version "0.11" +#define Version "0.24" #define WIRE Wire #define DHTTYPE DHT22 #define DHTPIN 4 @@ -49,6 +49,13 @@ int adcVoltValue; float measuredVoltage = 0; float batteryVoltage = 0; +float currentBatteryVoltage = 0; +float lastBatteryVoltage = 0; +int battLifeMins = 998; +int estHours = 0; +int estMins = 0; + +int batteryPercent = 0; int batteryBarWidth = 0; char dateString[21]; @@ -79,7 +86,11 @@ void setup() { display.setCursor(32, 16); display.println("RTC: DS3231"); display.setCursor(0, 24); - display.println("Battery: 18h"); + display.println("Battery: "); + display.setCursor(55, 24); + display.println(int(floor(float(battLifeMins)/60))); + display.setCursor(70, 24); + display.println("h"); display.display(); delay(500); dht.begin(); @@ -136,6 +147,10 @@ void setup() { display.display(); delay(500); + adcVoltValue = analogRead(VOLT_IN); // Odczyt wartości ADC z GPIO36 + measuredVoltage = (adcVoltValue * adcMaxVoltage) / adcResolution; + batteryVoltage = (measuredVoltage * voltageDividerFactor)+0.1; + currentBatteryVoltage = batteryVoltage; //setup END //###################### } @@ -317,19 +332,55 @@ void loop() { if (menuL0 == 8 ) { display.setCursor(0, 0); - display.print("MENU TECH"); + display.println(dateString); + display.setCursor(0, 8); + display.print("Battery"); - display.setCursor(70,0); + display.setCursor(80,8); display.print("V:"); - display.setCursor(85, 0); + display.setCursor(95, 8); display.print(batteryVoltage); - display.drawRect(0, 9, 124, 7, SSD1306_WHITE); - display.fillRect(124, 10, 5, 5, SSD1306_WHITE); - batteryBarWidth = (((batteryVoltage-3)/1.2)*122); + + + display.setCursor(0, 24); + if (currentBatteryVoltage >= 4.20 ){ + display.print("FlC"); + } else if (lastBatteryVoltage < currentBatteryVoltage && currentBatteryVoltage >= 2.0 ){ + display.print("Chr"); + } else if (lastBatteryVoltage > currentBatteryVoltage && currentBatteryVoltage >= 2.0 ) { + display.print("Dsg"); + } else { + display.print("---"); + } + batteryPercent = voltageToPercentage(currentBatteryVoltage); + display.setCursor(21, 24); + display.print("B:"); + display.setCursor(33, 24); + display.print(batteryPercent); + display.setCursor(52, 24); + display.print("%"); + + display.setCursor(60, 24); + display.print("Est:"); + estHours = int(floor( ((float(batteryPercent)/100) * battLifeMins) /60) ); + display.setCursor(85, 24); + display.print(leadZero(estHours)); + display.setCursor(99, 24); + display.print("h"); + estMins = ((float(batteryPercent)/100) * battLifeMins) - (estHours * 60); + display.setCursor(107, 24); + display.print(leadZero(estMins)); + display.setCursor(120, 24); + display.print("m"); + + + display.drawRect(0,16, 124, 6, SSD1306_WHITE); + display.fillRect(124, 17, 5, 4, SSD1306_WHITE); + batteryBarWidth = int(((float(batteryPercent)/100)*122)); if (batteryBarWidth >122){ batteryBarWidth = 122; } - display.fillRect(1, 10, batteryBarWidth, 5, SSD1306_WHITE); + display.fillRect(1, 17, batteryBarWidth, 5, SSD1306_WHITE); if (batteryVoltage < 2 ){ display.setCursor(60,8); display.print("xxx"); @@ -396,19 +447,23 @@ void loop() { // Log napiećia docelowoe procent baterii // kiedy zaden przycisk nie jest wcisniety + if (secs % 15 == 0) { + lastBatteryVoltage = currentBatteryVoltage; + currentBatteryVoltage = batteryVoltage; + delay(500); + + } if (mins % 1 == 0 && secs == 0) { File file = SD.open("/GeneralLog.txt", FILE_APPEND); if (file) { display.fillRect(0, 0, 124, 64, SSD1306_WHITE); file.print(dateString); - file.print(" Battery V: "); - file.println(formatNumber(batteryVoltage, 2)); + file.print(" Battery %: "); + file.println(batteryPercent); file.close(); } } - - - + //zapis na SD: if (mins % 1 == 0 && secs == 0) { File file = SD.open("/TempHumLog.txt", FILE_APPEND); @@ -449,28 +504,27 @@ String leadZero(float number) { -// -//float interpolate(float x, float x0, float x1, float y0, float y1) { -// return y0 + (x - x0) * (y1 - y0) / (x1 - x0); -//} -// -//// Funkcja przeliczająca zmierzone napięcie na procent naładowania -//float voltageToPercentage(float voltage) { -// // Jeśli napięcie jest poniżej minimalnego punktu – bateria uznajemy za rozładowaną -// if (voltage <= voltagePoints[0]) -// return percentPoints[0]; -// // Jeśli napięcie przekracza najwyższy punkt – bateria jest pełna -// if (voltage >= voltagePoints[numPoints - 1]) -// return percentPoints[numPoints - 1]; -// -// // Szukamy przedziału, w którym mieści się zmierzone napięcie, i stosujemy interpolację -// for (int i = 0; i < numPoints - 1; i++) { -// if (voltage >= voltagePoints[i] && voltage < voltagePoints[i+1]) { -// return interpolate(voltage, voltagePoints[i], voltagePoints[i+1], percentPoints[i], percentPoints[i+1]); -// } -// } -// return 0; -//} +float voltageToPercentage(float voltage) { + // Reprezentatywne punkty (wolt, procent) + const int nPoints = 9; + // Napięcia – uporządkowane malejąco + float vPoints[nPoints] = {4.06, 4.00, 3.95, 3.90, 3.85, 3.80, 3.70, 3.45, 2.75}; + // Odpowiednie poziomy naładowania + float pPoints[nPoints] = {100, 95, 90, 80, 70, 60, 50, 10, 0}; + if (voltage >= vPoints[0]) + return pPoints[0]; + if (voltage <= vPoints[nPoints - 1]) + return pPoints[nPoints - 1]; + for (int i = 0; i < nPoints - 1; i++) { + if (voltage <= vPoints[i] && voltage > vPoints[i + 1]) { + // Obliczamy współczynnik interpolacji + float fraction = (voltage - vPoints[i + 1]) / (vPoints[i] - vPoints[i + 1]); + // Interpolacja liniowa między punktami + return pPoints[i + 1] + fraction * (pPoints[i] - pPoints[i + 1]); + } + } + return 0; // Domyślnie – choć powinno się tu już nie dojść +} //Main feature: