Compare commits

..

No commits in common. "testing" and "main" have entirely different histories.

3 changed files with 62 additions and 77 deletions

View File

@ -13,9 +13,6 @@
#include "esp_task_wdt.h"
#include "commonFS.h"
bool mainTaskWasPaused = 0;
uint8_t scaleTareCounter = 0;
// ##### SETUP #####
void setup() {
Serial.begin(115200);
@ -48,7 +45,22 @@ void setup() {
// NFC Reader
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
@ -56,6 +68,9 @@ void setup() {
// Aktuellen Task (loopTask) zum Watchdog hinzufügen
esp_task_wdt_add(NULL);
// Optional: Andere Tasks zum Watchdog hinzufügen, falls nötig
// esp_task_wdt_add(task_handle);
}
@ -132,18 +147,10 @@ void loop() {
}
// Ausgabe der Waage auf Display
if(pauseMainTask == 0)
{
if (mainTaskWasPaused || (weight != lastWeight && hasReadRfidTag == 0 && (!autoSendToBambu || autoSetToBambuSpoolId == 0)))
if (pauseMainTask == 0 && weight != lastWeight && hasReadRfidTag == 0 && (!autoSendToBambu || autoSetToBambuSpoolId == 0))
{
(weight < 2) ? ((weight < -2) ? oledShowMessage("!! -0") : oledShowWeight(0)) : oledShowWeight(weight);
}
mainTaskWasPaused = false;
}
else
{
mainTaskWasPaused = true;
}
// Wenn Timer abgelaufen und nicht gerade ein RFID-Tag geschrieben wird
@ -154,20 +161,11 @@ void loop() {
// Prüfen ob die Waage korrekt genullt ist
if ((weight > 0 && weight < 5) || weight < 0)
{
if(scaleTareCounter < 5)
{
scaleTareCounter++;
scale_tare_counter++;
}
else
{
scaleTareRequest = true;
scaleTareCounter = 0;
}
}
else
{
scaleTareCounter = 0;
scale_tare_counter = 0;
}
// Prüfen ob das Gewicht gleich bleibt und dann senden
@ -212,5 +210,6 @@ void loop() {
}
}
yield();
esp_task_wdt_reset();
}

View File

@ -14,7 +14,7 @@ TaskHandle_t ScaleTask;
int16_t weight = 0;
uint8_t weigthCouterToApi = 0;
bool scaleTareRequest = false;
uint8_t scale_tare_counter = 0;
uint8_t pauseMainTask = 0;
uint8_t scaleCalibrated = 1;
@ -34,16 +34,14 @@ void scale_loop(void * parameter) {
Serial.println("++++++++++++++++++++++++++++++");
Serial.println("Scale Loop started");
Serial.println("++++++++++++++++++++++++++++++");
for(;;) {
if (scale.is_ready())
{
// Waage nochmal Taren, wenn zu lange Abweichung
if (scaleTareRequest == true)
if (scale_tare_counter >= 5)
{
Serial.println("Re-Tare scale");
scale.tare();
scaleTareRequest = false;
scale_tare_counter = 0;
}
weight = round(scale.get_units());
@ -53,13 +51,13 @@ void scale_loop(void * parameter) {
}
}
void start_scale() {
uint8_t start_scale() {
Serial.println("Prüfe Calibration Value");
float calibrationValue;
long calibrationValue;
// NVS lesen
preferences.begin(NVS_NAMESPACE, true); // true = readonly
calibrationValue = preferences.getFloat(NVS_KEY_CALIBRATION, defaultScaleCalibrationValue);
calibrationValue = preferences.getLong(NVS_KEY_CALIBRATION, defaultScaleCalibrationValue);
preferences.end();
Serial.print("Read Scale Calibration Value ");
@ -70,13 +68,6 @@ void start_scale() {
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();
}
}
oledShowMessage("Scale Tare Please remove all");
@ -110,21 +101,21 @@ void start_scale() {
} else {
Serial.println("ScaleLoop-Task erfolgreich erstellt");
}
return (scaleCalibrated == 1) ? 1 : 3;
}
uint8_t calibrate_scale() {
uint8_t returnState = 0;
float newCalibrationValue;
vTaskSuspend(RfidReaderTask);
vTaskSuspend(ScaleTask);
long newCalibrationValue;
//vTaskSuspend(RfidReaderTask);
vTaskDelete(RfidReaderTask);
vTaskDelete(ScaleTask);
pauseBambuMqttTask = true;
pauseMainTask = 1;
if (scale.wait_ready_timeout(1000))
{
scale.set_scale();
oledShowMessage("Step 1 empty Scale");
@ -146,7 +137,7 @@ uint8_t calibrate_scale() {
esp_task_wdt_reset();
}
float newCalibrationValue = scale.get_units(10);
long newCalibrationValue = scale.get_units(10);
Serial.print("Result: ");
Serial.println(newCalibrationValue);
@ -159,43 +150,35 @@ uint8_t calibrate_scale() {
// Speichern mit NVS
preferences.begin(NVS_NAMESPACE, false); // false = readwrite
preferences.putFloat(NVS_KEY_CALIBRATION, newCalibrationValue);
preferences.putLong(NVS_KEY_CALIBRATION, newCalibrationValue);
preferences.end();
// Verifizieren
preferences.begin(NVS_NAMESPACE, true);
float verifyValue = preferences.getFloat(NVS_KEY_CALIBRATION, 0);
long verifyValue = preferences.getLong(NVS_KEY_CALIBRATION, 0);
preferences.end();
Serial.print("Verified stored value: ");
Serial.println(verifyValue);
Serial.println("End calibration, remove weight");
Serial.println("End calibration, revome weight");
oledShowMessage("Remove weight");
scale.set_scale(newCalibrationValue);
for (uint16_t i = 0; i < 2000; i++) {
yield();
vTaskDelay(pdMS_TO_TICKS(1));
esp_task_wdt_reset();
}
oledShowMessage("Scale calibrated");
// For some reason it is not possible to re-tare the scale here, it will result in a wdt timeout. Instead let the scale loop do the taring
//scale.tare();
scaleTareRequest = true;
oledShowMessage("Calibration done");
for (uint16_t i = 0; i < 2000; i++) {
yield();
vTaskDelay(pdMS_TO_TICKS(1));
esp_task_wdt_reset();
}
returnState = 1;
}
else
{
{
@ -208,7 +191,7 @@ uint8_t calibrate_scale() {
vTaskDelay(pdMS_TO_TICKS(1));
esp_task_wdt_reset();
}
returnState = 0;
return 0;
}
}
}
@ -223,13 +206,16 @@ uint8_t calibrate_scale() {
vTaskDelay(pdMS_TO_TICKS(1));
esp_task_wdt_reset();
}
returnState = 0;
return 0;
}
vTaskResume(RfidReaderTask);
vTaskResume(ScaleTask);
oledShowMessage("Scale Ready");
Serial.println("restart Scale Task");
start_scale();
pauseBambuMqttTask = false;
pauseMainTask = 0;
return returnState;
return 1;
}

View File

@ -12,7 +12,7 @@ uint8_t tareScale();
extern HX711 scale;
extern int16_t weight;
extern uint8_t weigthCouterToApi;
extern uint8_t scaleTareRequest;
extern uint8_t scale_tare_counter;
extern uint8_t pauseMainTask;
extern uint8_t scaleCalibrated;