From 5863865b37c8806f6110544b7db25e09541396d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=BCbbe=20Onken?= Date: Mon, 20 Nov 2023 22:26:36 +0100 Subject: [PATCH] Fixes #354 - Delay sending of stats until batt/temp/hum have been read at least once --- src/Globals.cpp | 5 +++-- src/Globals.h | 1 + src/MQTTManager.cpp | 2 +- src/PeripheryManager.cpp | 11 +++++++++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Globals.cpp b/src/Globals.cpp index 17510d2b..c7cd70c1 100644 --- a/src/Globals.cpp +++ b/src/Globals.cpp @@ -356,8 +356,8 @@ uint16_t MAX_BATTERY = 665; float TEMP_OFFSET; #else float TEMP_OFFSET = -9; -uint8_t BATTERY_PERCENT; -uint16_t BATTERY_RAW; +uint8_t BATTERY_PERCENT = 0; +uint16_t BATTERY_RAW = 0; #endif float HUM_OFFSET; uint16_t LDR_RAW; @@ -392,6 +392,7 @@ bool BLOCK_NAVIGATION = false; bool UPDATE_CHECK = false; float GAMMA = 0; bool SENSOR_READING = true; +bool SENSORS_STABLE = false; bool DFPLAYER_ACTIVE = false; bool ROTATE_SCREEN = false; uint8_t TIME_MODE = 1; diff --git a/src/Globals.h b/src/Globals.h index 43b4a4bd..57b91066 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -117,6 +117,7 @@ void saveSettings(); extern bool BLOCK_NAVIGATION; extern bool UPDATE_CHECK; extern bool SENSOR_READING; +extern bool SENSORS_STABLE; extern bool ROTATE_SCREEN; extern long STATS_INTERVAL; extern uint8_t TIME_MODE; diff --git a/src/MQTTManager.cpp b/src/MQTTManager.cpp index a0700e3c..db24106e 100644 --- a/src/MQTTManager.cpp +++ b/src/MQTTManager.cpp @@ -699,7 +699,7 @@ void MQTTManager_::tick() { mqtt.loop(); unsigned long currentMillis_Stats = millis(); - if (currentMillis_Stats - previousMillis_Stats >= STATS_INTERVAL) + if ((currentMillis_Stats - previousMillis_Stats >= STATS_INTERVAL) && (SENSORS_STABLE)) { previousMillis_Stats = currentMillis_Stats; sendStats(); diff --git a/src/PeripheryManager.cpp b/src/PeripheryManager.cpp index da89987a..ed0bed6f 100644 --- a/src/PeripheryManager.cpp +++ b/src/PeripheryManager.cpp @@ -440,8 +440,15 @@ void PeripheryManager_::tick() previousMillis_BatTempHum = currentMillis_BatTempHum; #ifndef awtrix2_upgrade uint16_t ADCVALUE = analogRead(BATTERY_PIN); - BATTERY_PERCENT = max(min((int)map(ADCVALUE, MIN_BATTERY, MAX_BATTERY, 0, 100), 100), 0); - BATTERY_RAW = ADCVALUE; + // Discard values that are totally out of range, especially the first value read after a reboot. + // Meaningful values for a Ulanzi are in the range 400..700 + if ((ADCVALUE > 100) && (ADCVALUE < 1000)) { + BATTERY_PERCENT = max(min((int)map(ADCVALUE, MIN_BATTERY, MAX_BATTERY, 0, 100), 100), 0); + BATTERY_RAW = ADCVALUE; + SENSORS_STABLE = true; + } +#else + SENSORS_STABLE = true; #endif if (SENSOR_READING) {