From 37717392d00fab468ab536865f14bbbb5b6fe548 Mon Sep 17 00:00:00 2001 From: Manuel Weiser Date: Sun, 23 Feb 2025 16:44:43 +0100 Subject: [PATCH] feat: implement scale calibration checks and update start_scale function to return calibration status --- src/main.cpp | 45 ++++++++++++++++++++++++++++++--------------- src/scale.cpp | 10 ++++++++-- src/scale.h | 3 ++- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index cf9a766..31f047f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -58,7 +58,22 @@ void setup() { startNfc(); - start_scale(); + uint8_t scaleCalibrated = start_scale(); + if (scaleCalibrated == 3) { + oledShowMessage("Scale not calibrated!"); + for (uint16_t i = 0; i < 50000; i++) { + yield(); + vTaskDelay(pdMS_TO_TICKS(1)); + esp_task_wdt_reset(); + } + } else if (scaleCalibrated == 0) { + oledShowMessage("HX711 not found"); + for (uint16_t i = 0; i < 50000; i++) { + yield(); + vTaskDelay(pdMS_TO_TICKS(1)); + esp_task_wdt_reset(); + } + } // WDT initialisieren mit 10 Sekunden Timeout bool panic = true; // Wenn true, löst ein WDT-Timeout einen System-Panik aus @@ -84,33 +99,33 @@ uint8_t wifiErrorCounter = 0; // ##### PROGRAM START ##### void loop() { - - /* - // Überprüfe den WLAN-Status - if (WiFi.status() != WL_CONNECTED) { - wifiErrorCounter++; - wifiOn = false; - } else { - wifiErrorCounter = 0; - wifiOn = true; - } - if (wifiErrorCounter > 20) ESP.restart(); - */ - unsigned long currentMillis = millis(); // Send AMS Data min every Minute - if (currentMillis - lastAmsSendTime >= amsSendInterval) { + if (currentMillis - lastAmsSendTime >= amsSendInterval) + { lastAmsSendTime = currentMillis; sendAmsData(nullptr); } + // Wenn Waage nicht Kalibriert + if (scaleCalibrated == 3) + { + oledShowMessage("Scale not calibrated!"); + vTaskDelay(5000 / portTICK_PERIOD_MS); + yield(); + esp_task_wdt_reset(); + + return; + } + // Ausgabe der Waage auf Display if (pauseMainTask == 0 && weight != lastWeight && hasReadRfidTag == 0) { (weight < 0) ? oledShowMessage("!! -1") : oledShowWeight(weight); } + // Wenn Timer abgelaufen und nicht gerade ein RFID-Tag geschrieben wird if (currentMillis - lastWeightReadTime >= weightReadInterval && hasReadRfidTag < 3) { diff --git a/src/scale.cpp b/src/scale.cpp index dd461a1..040559a 100644 --- a/src/scale.cpp +++ b/src/scale.cpp @@ -16,6 +16,7 @@ int16_t weight = 0; uint8_t weigthCouterToApi = 0; uint8_t scale_tare_counter = 0; uint8_t pauseMainTask = 0; +uint8_t scaleCalibrated = 1; Preferences preferences; const char* NVS_NAMESPACE = "scale"; @@ -50,7 +51,7 @@ void scale_loop(void * parameter) { } } -void start_scale() { +uint8_t start_scale() { Serial.println("Prüfe Calibration Value"); long calibrationValue; @@ -64,7 +65,10 @@ void start_scale() { scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); - if (isnan(calibrationValue) || calibrationValue < 1) calibrationValue = defaultScaleCalibrationValue; + if (isnan(calibrationValue) || calibrationValue < 1) { + calibrationValue = defaultScaleCalibrationValue; + scaleCalibrated = 0; + } oledShowMessage("Scale Tare Please remove all"); for (uint16_t i = 0; i < 2000; i++) { @@ -97,6 +101,8 @@ void start_scale() { } else { Serial.println("ScaleLoop-Task erfolgreich erstellt"); } + + return (scaleCalibrated == 1) ? 1 : 3; } uint8_t calibrate_scale() { diff --git a/src/scale.h b/src/scale.h index 314143e..e721571 100644 --- a/src/scale.h +++ b/src/scale.h @@ -5,7 +5,7 @@ #include "HX711.h" -void start_scale(); +uint8_t start_scale(); uint8_t calibrate_scale(); uint8_t tareScale(); @@ -14,6 +14,7 @@ extern int16_t weight; extern uint8_t weigthCouterToApi; extern uint8_t scale_tare_counter; extern uint8_t pauseMainTask; +extern uint8_t scaleCalibrated; extern TaskHandle_t ScaleTask;