Fixes issue that scale not calibrated message was not shown

There was no warning any more if the scale is not calibrated. This change fixes that.
This commit is contained in:
Jan Philipp Ecker
2025-08-08 16:16:39 +02:00
parent a7c99d3f26
commit 5509d98969
5 changed files with 85 additions and 86 deletions

View File

@@ -16,7 +16,6 @@ const uint8_t LOADCELL_DOUT_PIN = 16; //16;
const uint8_t LOADCELL_SCK_PIN = 17; //17; const uint8_t LOADCELL_SCK_PIN = 17; //17;
const uint8_t calVal_eepromAdress = 0; const uint8_t calVal_eepromAdress = 0;
const uint16_t SCALE_LEVEL_WEIGHT = 500; const uint16_t SCALE_LEVEL_WEIGHT = 500;
uint16_t defaultScaleCalibrationValue = 430;
// ***** HX711 // ***** HX711
// ***** TTP223 (Touch Sensor) // ***** TTP223 (Touch Sensor)

View File

@@ -22,6 +22,7 @@
#define NVS_NAMESPACE_SCALE "scale" #define NVS_NAMESPACE_SCALE "scale"
#define NVS_KEY_CALIBRATION "cal_value" #define NVS_KEY_CALIBRATION "cal_value"
#define NVS_KEY_AUTOTARE "auto_tare" #define NVS_KEY_AUTOTARE "auto_tare"
#define SCALE_DEFAULT_CALIBRATION_VALUE 430.0f;
#define BAMBU_USERNAME "bblp" #define BAMBU_USERNAME "bblp"

View File

@@ -156,94 +156,93 @@ void loop() {
} }
} }
// Wenn Waage nicht Kalibriert // If scale is not calibrated, only show a warning
if (scaleCalibrated == 3) if (!scaleCalibrated)
{ {
oledShowMessage("Scale not calibrated!"); // Do not show the warning if the calibratin process is onging
vTaskDelay(5000 / portTICK_PERIOD_MS); if(!scaleCalibrationActive){
yield(); oledShowMessage("Scale not calibrated");
esp_task_wdt_reset(); vTaskDelay(1000 / portTICK_PERIOD_MS);
return;
}
// Ausgabe der Waage auf Display
if(pauseMainTask == 0)
{
if (mainTaskWasPaused || (weight != lastWeight && nfcReaderState == NFC_IDLE && (!bambuCredentials.autosend_enable || autoSetToBambuSpoolId == 0)))
{
(weight < 2) ? ((weight < -2) ? oledShowMessage("!! -0") : oledShowWeight(0)) : oledShowWeight(weight);
} }
mainTaskWasPaused = false; }else{
} // Ausgabe der Waage auf Display
else if(pauseMainTask == 0)
{
mainTaskWasPaused = true;
}
// Wenn Timer abgelaufen und nicht gerade ein RFID-Tag geschrieben wird
if (currentMillis - lastWeightReadTime >= weightReadInterval && nfcReaderState < NFC_WRITING)
{
lastWeightReadTime = currentMillis;
// Prüfen ob die Waage korrekt genullt ist
// Abweichung von 2g ignorieren
if (autoTare && (weight > 2 && weight < 7) || weight < -2)
{ {
scale_tare_counter++; if (mainTaskWasPaused || (weight != lastWeight && nfcReaderState == NFC_IDLE && (!bambuCredentials.autosend_enable || autoSetToBambuSpoolId == 0)))
{
(weight < 2) ? ((weight < -2) ? oledShowMessage("!! -0") : oledShowWeight(0)) : oledShowWeight(weight);
}
mainTaskWasPaused = false;
} }
else else
{ {
scale_tare_counter = 0; mainTaskWasPaused = true;
} }
// Prüfen ob das Gewicht gleich bleibt und dann senden
if (abs(weight - lastWeight) <= 2 && weight > 5) // Wenn Timer abgelaufen und nicht gerade ein RFID-Tag geschrieben wird
if (currentMillis - lastWeightReadTime >= weightReadInterval && nfcReaderState < NFC_WRITING)
{ {
weigthCouterToApi++; lastWeightReadTime = currentMillis;
}
else // Prüfen ob die Waage korrekt genullt ist
// Abweichung von 2g ignorieren
if (autoTare && (weight > 2 && weight < 7) || weight < -2)
{
scale_tare_counter++;
}
else
{
scale_tare_counter = 0;
}
// Prüfen ob das Gewicht gleich bleibt und dann senden
if (abs(weight - lastWeight) <= 2 && weight > 5)
{
weigthCouterToApi++;
}
else
{
weigthCouterToApi = 0;
weightSend = 0;
}
}
// reset weight counter after writing tag
// TBD: what exactly is the logic behind this?
if (currentMillis - lastWeightReadTime >= weightReadInterval && nfcReaderState != NFC_IDLE && nfcReaderState != NFC_READ_SUCCESS)
{ {
weigthCouterToApi = 0; weigthCouterToApi = 0;
weightSend = 0;
} }
}
lastWeight = weight;
// reset weight counter after writing tag // Wenn ein Tag mit SM id erkannte wurde und der Waage Counter anspricht an SM Senden
// TBD: what exactly is the logic behind this? if (activeSpoolId != "" && weigthCouterToApi > 3 && weightSend == 0 && nfcReaderState == NFC_READ_SUCCESS && tagProcessed == false && spoolmanApiState == API_IDLE) {
if (currentMillis - lastWeightReadTime >= weightReadInterval && nfcReaderState != NFC_IDLE && nfcReaderState != NFC_READ_SUCCESS) // set the current tag as processed to prevent it beeing processed again
{ tagProcessed = true;
weigthCouterToApi = 0;
}
lastWeight = weight;
// Wenn ein Tag mit SM id erkannte wurde und der Waage Counter anspricht an SM Senden if (updateSpoolWeight(activeSpoolId, weight))
if (activeSpoolId != "" && weigthCouterToApi > 3 && weightSend == 0 && nfcReaderState == NFC_READ_SUCCESS && tagProcessed == false && spoolmanApiState == API_IDLE) { {
// set the current tag as processed to prevent it beeing processed again weightSend = 1;
tagProcessed = true;
}
if (updateSpoolWeight(activeSpoolId, weight)) else
{ {
weightSend = 1; oledShowIcon("failed");
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
} }
else
{
oledShowIcon("failed");
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}
if(sendOctoUpdate && spoolmanApiState == API_IDLE){ if(sendOctoUpdate && spoolmanApiState == API_IDLE){
autoSetToBambuSpoolId = activeSpoolId.toInt(); autoSetToBambuSpoolId = activeSpoolId.toInt();
if(octoEnabled) if(octoEnabled)
{ {
updateSpoolOcto(autoSetToBambuSpoolId); updateSpoolOcto(autoSetToBambuSpoolId);
}
sendOctoUpdate = false;
} }
sendOctoUpdate = false;
} }
esp_task_wdt_reset(); esp_task_wdt_reset();

View File

@@ -17,8 +17,9 @@ uint8_t weigthCouterToApi = 0;
uint8_t scale_tare_counter = 0; uint8_t scale_tare_counter = 0;
bool scaleTareRequest = false; bool scaleTareRequest = false;
uint8_t pauseMainTask = 0; uint8_t pauseMainTask = 0;
uint8_t scaleCalibrated = 1; bool scaleCalibrated;
bool autoTare = true; bool autoTare = true;
bool scaleCalibrationActive = false;
// ##### Funktionen für Waage ##### // ##### Funktionen für Waage #####
uint8_t setAutoTare(bool autoTareValue) { uint8_t setAutoTare(bool autoTareValue) {
@@ -88,7 +89,13 @@ void start_scale(bool touchSensorConnected) {
// NVS lesen // NVS lesen
Preferences preferences; Preferences preferences;
preferences.begin(NVS_NAMESPACE_SCALE, true); // true = readonly preferences.begin(NVS_NAMESPACE_SCALE, true); // true = readonly
calibrationValue = preferences.getFloat(NVS_KEY_CALIBRATION, defaultScaleCalibrationValue); if(preferences.isKey(NVS_KEY_CALIBRATION)){
calibrationValue = preferences.getFloat(NVS_KEY_CALIBRATION);
scaleCalibrated = true;
}else{
calibrationValue = SCALE_DEFAULT_CALIBRATION_VALUE;
scaleCalibrated = false;
}
// auto Tare // auto Tare
// Wenn Touch Sensor verbunden, dann autoTare auf false setzen // Wenn Touch Sensor verbunden, dann autoTare auf false setzen
@@ -103,18 +110,6 @@ void start_scale(bool touchSensorConnected) {
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
if (isnan(calibrationValue) || calibrationValue < 1) {
calibrationValue = defaultScaleCalibrationValue;
scaleCalibrated = 0;
oledShowMessage("Scale not calibrated!");
for (uint16_t i = 0; i < 50000; i++) {
yield();
vTaskDelay(pdMS_TO_TICKS(1));
esp_task_wdt_reset();
}
}
oledShowProgressBar(6, 7, DISPLAY_BOOT_TEXT, "Tare scale"); oledShowProgressBar(6, 7, DISPLAY_BOOT_TEXT, "Tare scale");
for (uint16_t i = 0; i < 2000; i++) { for (uint16_t i = 0; i < 2000; i++) {
yield(); yield();
@@ -152,6 +147,8 @@ uint8_t calibrate_scale() {
uint8_t returnState = 0; uint8_t returnState = 0;
float newCalibrationValue; float newCalibrationValue;
scaleCalibrationActive = true;
vTaskSuspend(RfidReaderTask); vTaskSuspend(RfidReaderTask);
vTaskSuspend(ScaleTask); vTaskSuspend(ScaleTask);
@@ -228,6 +225,7 @@ uint8_t calibrate_scale() {
esp_task_wdt_reset(); esp_task_wdt_reset();
} }
scaleCalibrated = true;
returnState = 1; returnState = 1;
} }
else else
@@ -262,6 +260,7 @@ uint8_t calibrate_scale() {
vTaskResume(ScaleTask); vTaskResume(ScaleTask);
pauseBambuMqttTask = false; pauseBambuMqttTask = false;
pauseMainTask = 0; pauseMainTask = 0;
scaleCalibrationActive = false;
return returnState; return returnState;
} }

View File

@@ -15,8 +15,9 @@ extern uint8_t weigthCouterToApi;
extern uint8_t scale_tare_counter; extern uint8_t scale_tare_counter;
extern uint8_t scaleTareRequest; extern uint8_t scaleTareRequest;
extern uint8_t pauseMainTask; extern uint8_t pauseMainTask;
extern uint8_t scaleCalibrated; extern bool scaleCalibrated;
extern bool autoTare; extern bool autoTare;
extern bool scaleCalibrationActive;
extern TaskHandle_t ScaleTask; extern TaskHandle_t ScaleTask;