From 4daa74bbfb01bb56fb90b2104ae8c5adee1edb62 Mon Sep 17 00:00:00 2001 From: anderl1969 Date: Sun, 29 Mar 2026 15:54:54 +0200 Subject: [PATCH] feat: NFC-only mode based on working hw detection for hx711 --- src/lang.cpp | 7 ++ src/lang.h | 3 + src/main.cpp | 21 ++++-- src/scale.cpp | 181 +++++++++++++++++++++++++++++++++++++++++--------- src/scale.h | 1 + 5 files changed, 176 insertions(+), 37 deletions(-) diff --git a/src/lang.cpp b/src/lang.cpp index 2536f7e..1afe9e6 100644 --- a/src/lang.cpp +++ b/src/lang.cpp @@ -78,6 +78,8 @@ static const char EN_WIFI_RECONN[] = "WiFi reconnecting"; static const char EN_UPDATE[] = "Update"; static const char EN_DOWNLOAD[] = "Download"; +static const char EN_NOSCALE_PROMPT[] = "ready..."; + // ===================================================================== // German strings // ===================================================================== @@ -152,6 +154,8 @@ static const char DE_WIFI_RECONN[] = "WiFi Neuverbindung"; static const char DE_UPDATE[] = "Update"; static const char DE_DOWNLOAD[] = "Download"; +static const char DE_NOSCALE_PROMPT[] = "bereit..."; + // ===================================================================== // String table: [StringID][Lang] // ===================================================================== @@ -233,6 +237,9 @@ static const char* const stringTable[STR_COUNT][LANG_COUNT] = { // OTA { EN_UPDATE, DE_UPDATE }, { EN_DOWNLOAD, DE_DOWNLOAD }, + + // No-Scale mode + { EN_NOSCALE_PROMPT, DE_NOSCALE_PROMPT }, }; const char* tr(StringID id) { diff --git a/src/lang.h b/src/lang.h index 930350c..97fd60a 100644 --- a/src/lang.h +++ b/src/lang.h @@ -90,6 +90,9 @@ enum StringID : uint8_t { STR_UPDATE, STR_DOWNLOAD, + // No-Scale mode + STR_NOSCALE_PROMPT, + STR_COUNT // must be last }; diff --git a/src/main.cpp b/src/main.cpp index 24f5f75..a5806b0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -61,7 +61,9 @@ void setup() { // Scale start_scale(touchSensorConnected); - scaleTareRequest = true; + if (scaleConnected){ + scaleTareRequest = true; + } // WDT initialisieren mit 10 Sekunden Timeout bool panic = true; // Wenn true, löst ein WDT-Timeout einen System-Panik aus @@ -72,6 +74,11 @@ void setup() { // Aktuellen Task (loopTask) zum Watchdog hinzufügen esp_task_wdt_add(NULL); + + if ( !scaleConnected ) { + // Clear Display after Boot + oledDisplayText(tr(STR_NOSCALE_PROMPT)); + } } @@ -121,11 +128,14 @@ void loop() { oledShowConnectionError(tr(STR_API_CONN_LOST), WiFi.localIP().toString()); oledSetPriority(DISPLAY_PRIORITY_WARNING, 3000); mainTaskWasPaused = true; + } else if ( !scaleConnected ){ + // everything fine again: without scale manual clearing of the error msg is needed + oledDisplayText(tr(STR_NOSCALE_PROMPT)); } } - // Überprüfe den Status des Touch Sensors - if (touchSensorConnected && digitalRead(TTP223_PIN) == HIGH && currentMillis - lastButtonPress > debounceDelay) + // Überprüfe den Status des Touch Sensors (nur wenn Waage vorhanden) + if (scaleConnected && touchSensorConnected && digitalRead(TTP223_PIN) == HIGH && currentMillis - lastButtonPress > debounceDelay) { lastButtonPress = currentMillis; scaleTareRequest = true; @@ -160,7 +170,7 @@ void loop() { } // If scale is not calibrated, only show a warning - if (!scaleCalibrated) + if (scaleConnected && !scaleCalibrated) { // Do not show the warning if the calibratin process is onging if(!scaleCalibrationActive){ @@ -168,7 +178,8 @@ void loop() { vTaskDelay(pdMS_TO_TICKS(1000)); } } - else + + if (scaleConnected && scaleCalibrated) { // Ausgabe der Waage auf Display // Block weight display during NFC write operations and higher-priority display messages diff --git a/src/scale.cpp b/src/scale.cpp index f3d1d8d..efb0a7a 100644 --- a/src/scale.cpp +++ b/src/scale.cpp @@ -35,10 +35,22 @@ uint8_t scale_tare_counter = 0; bool scaleTareRequest = false; uint8_t pauseMainTask = 0; bool scaleCalibrated; +bool scaleConnected; bool autoTare = true; bool scaleCalibrationActive = false; volatile bool scaleCalibrationRequest = false; +/** + * Non blocking wait function + */ +void friendlyWait(uint16_t ticks) { + for (uint16_t i = 0; i < ticks; i++) { + yield(); + vTaskDelay(pdMS_TO_TICKS(1)); + esp_task_wdt_reset(); + } +} + // ##### Weight stabilization functions ##### /** @@ -224,6 +236,102 @@ void scale_loop(void * parameter) { } } +long readHX711Raw() { + long value = 0; + + for (int i = 0; i < 24; i++) { + digitalWrite(LOADCELL_SCK_PIN, HIGH); + delayMicroseconds(1); + + value = (value << 1) | digitalRead(LOADCELL_DOUT_PIN); + + digitalWrite(LOADCELL_SCK_PIN, LOW); + delayMicroseconds(1); + } + + // Gain = 128 (1 extra pulse) + digitalWrite(LOADCELL_SCK_PIN, HIGH); + delayMicroseconds(1); + digitalWrite(LOADCELL_SCK_PIN, LOW); + + return value; +} + +bool deepSearchScale() { + // without pull-up resistor (10k) on LOADCELL_DOUT_PIN the hx711 lib functions + // are not reliable in detecting a missing chip + // these tests work even without pull-up resistor + const uint32_t timeout = 1000; + const int samples = 10; + + pinMode(LOADCELL_DOUT_PIN, INPUT); + pinMode(LOADCELL_SCK_PIN, OUTPUT); + digitalWrite(LOADCELL_SCK_PIN, LOW); + + //delay(500); + friendlyWait(500); + + // 1. HX711 vorhanden? + uint32_t start = millis(); + while (digitalRead(LOADCELL_DOUT_PIN) == HIGH) { + if (millis() - start > timeout) { + Serial.println("HX711 not found (DOUT stays HIGH)."); + return false; + } + } + + Serial.println("HX711 seems to answer..."); + + // 2. Mehrere Werte lesen + long values[samples]; + + for (int i = 0; i < samples; i++) { + // warten bis bereit + uint32_t t = millis(); + while (digitalRead(LOADCELL_DOUT_PIN) == HIGH) { + if (millis() - t > timeout) { + Serial.println("HX711 not found (Timeout during read)."); + return false; + } + } + + values[i] = readHX711Raw(); + delay(50); + } + + // 3. Analyse + int invalidCount = 0; + + for (int i = 0; i < samples; i++) { + if (values[i] == 0 || values[i] == 0xFFFFFF) { + invalidCount++; + } + } + + // 4. Entscheidungen + + // Kein Sensor + if (invalidCount > samples / 2) { + Serial.println("HX711 not ready (Loadcell missing)."); + return false; + } + + Serial.println("HX711 found."); + return true; +} + +bool scaleDetected(){ + // Step 1: use hx711 library functions (need pull-up resistor to recognize missing hx711) + if ( !(scale.wait_ready_timeout(1000))) { + Serial.println("HX711 not found (pull-up resistor installed)."); + return false; + } + + //Step 2: when pull-up resistor is missing, then deep search... + return deepSearchScale(); + +} + void start_scale(bool touchSensorConnected) { Serial.println("Prüfe Calibration Value"); float calibrationValue; @@ -253,39 +361,48 @@ void start_scale(bool touchSensorConnected) { scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); oledShowProgressBar(5, NUM_SETUP_STEPS, DISPLAY_BOOT_TEXT, tr(STR_SEARCHING_SCALE)); - for (uint16_t i = 0; i < 3000; i++) { - yield(); - vTaskDelay(pdMS_TO_TICKS(1)); - esp_task_wdt_reset(); + friendlyWait(3000); + // for (uint16_t i = 0; i < 3000; i++) { + // yield(); + // vTaskDelay(pdMS_TO_TICKS(1)); + // esp_task_wdt_reset(); + // } + + // while(!scale.is_ready()) { + // vTaskDelay(pdMS_TO_TICKS(5000)); + // } + + scaleConnected = scaleDetected(); + if ( scaleConnected ) { + scale.set_scale(calibrationValue); + //vTaskDelay(pdMS_TO_TICKS(5000)); + + // Initialize weight stabilization filter + resetWeightFilter(); + + // Display Gewicht + oledShowWeight(0); + + Serial.println("starte Scale Task"); + BaseType_t result = xTaskCreatePinnedToCore( + scale_loop, /* Function to implement the task */ + "ScaleLoop", /* Name of the task */ + 2048, /* Stack size in words */ + NULL, /* Task input parameter */ + scaleTaskPrio, /* Priority of the task */ + &ScaleTask, /* Task handle. */ + scaleTaskCore); /* Core where the task should run */ + + if (result != pdPASS) { + Serial.println("Fehler beim Erstellen des ScaleLoop-Tasks"); + } else { + Serial.println("ScaleLoop-Task erfolgreich erstellt"); + } } - - while(!scale.is_ready()) { - vTaskDelay(pdMS_TO_TICKS(5000)); - } - - scale.set_scale(calibrationValue); - //vTaskDelay(pdMS_TO_TICKS(5000)); - - // Initialize weight stabilization filter - resetWeightFilter(); - - // Display Gewicht - oledShowWeight(0); - - Serial.println("starte Scale Task"); - BaseType_t result = xTaskCreatePinnedToCore( - scale_loop, /* Function to implement the task */ - "ScaleLoop", /* Name of the task */ - 2048, /* Stack size in words */ - NULL, /* Task input parameter */ - scaleTaskPrio, /* Priority of the task */ - &ScaleTask, /* Task handle. */ - scaleTaskCore); /* Core where the task should run */ - - if (result != pdPASS) { - Serial.println("Fehler beim Erstellen des ScaleLoop-Tasks"); - } else { - Serial.println("ScaleLoop-Task erfolgreich erstellt"); + else { + Serial.println("Kann kein HX711 Board finden !"); // Sende Text "Kann kein..." an seriellen Monitor + oledDisplayText(tr(STR_HX711_NOT_FOUND)); + vTaskDelay(pdMS_TO_TICKS(2000)); } } diff --git a/src/scale.h b/src/scale.h index 3b14fc2..111c197 100644 --- a/src/scale.h +++ b/src/scale.h @@ -23,6 +23,7 @@ extern uint8_t scale_tare_counter; extern bool scaleTareRequest; extern uint8_t pauseMainTask; extern bool scaleCalibrated; +extern bool scaleConnected; extern bool autoTare; extern bool scaleCalibrationActive; extern volatile bool scaleCalibrationRequest;