feat: NFC-only mode based on working hw detection for hx711
This commit is contained in:
1 parent
4b74bc8988
commit
4daa74bbfb
5 files changed
+176
-37
No files matched your search
@@ -78,6 +78,8 @@ static const char EN_WIFI_RECONN[] = "WiFi reconnecting";
|
|||||||
static const char EN_UPDATE[] = "Update";
|
static const char EN_UPDATE[] = "Update";
|
||||||
static const char EN_DOWNLOAD[] = "Download";
|
static const char EN_DOWNLOAD[] = "Download";
|
||||||
|
|
||||||
|
static const char EN_NOSCALE_PROMPT[] = "ready...";
|
||||||
|
|
||||||
// =====================================================================
|
// =====================================================================
|
||||||
// German strings
|
// German strings
|
||||||
// =====================================================================
|
// =====================================================================
|
||||||
@@ -152,6 +154,8 @@ static const char DE_WIFI_RECONN[] = "WiFi Neuverbindung";
|
|||||||
static const char DE_UPDATE[] = "Update";
|
static const char DE_UPDATE[] = "Update";
|
||||||
static const char DE_DOWNLOAD[] = "Download";
|
static const char DE_DOWNLOAD[] = "Download";
|
||||||
|
|
||||||
|
static const char DE_NOSCALE_PROMPT[] = "bereit...";
|
||||||
|
|
||||||
// =====================================================================
|
// =====================================================================
|
||||||
// String table: [StringID][Lang]
|
// String table: [StringID][Lang]
|
||||||
// =====================================================================
|
// =====================================================================
|
||||||
@@ -233,6 +237,9 @@ static const char* const stringTable[STR_COUNT][LANG_COUNT] = {
|
|||||||
// OTA
|
// OTA
|
||||||
{ EN_UPDATE, DE_UPDATE },
|
{ EN_UPDATE, DE_UPDATE },
|
||||||
{ EN_DOWNLOAD, DE_DOWNLOAD },
|
{ EN_DOWNLOAD, DE_DOWNLOAD },
|
||||||
|
|
||||||
|
// No-Scale mode
|
||||||
|
{ EN_NOSCALE_PROMPT, DE_NOSCALE_PROMPT },
|
||||||
};
|
};
|
||||||
|
|
||||||
const char* tr(StringID id) {
|
const char* tr(StringID id) {
|
||||||
|
|||||||
@@ -90,6 +90,9 @@ enum StringID : uint8_t {
|
|||||||
STR_UPDATE,
|
STR_UPDATE,
|
||||||
STR_DOWNLOAD,
|
STR_DOWNLOAD,
|
||||||
|
|
||||||
|
// No-Scale mode
|
||||||
|
STR_NOSCALE_PROMPT,
|
||||||
|
|
||||||
STR_COUNT // must be last
|
STR_COUNT // must be last
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+16
-5
@@ -61,7 +61,9 @@ void setup() {
|
|||||||
|
|
||||||
// Scale
|
// Scale
|
||||||
start_scale(touchSensorConnected);
|
start_scale(touchSensorConnected);
|
||||||
scaleTareRequest = true;
|
if (scaleConnected){
|
||||||
|
scaleTareRequest = true;
|
||||||
|
}
|
||||||
|
|
||||||
// WDT initialisieren mit 10 Sekunden Timeout
|
// WDT initialisieren mit 10 Sekunden Timeout
|
||||||
bool panic = true; // Wenn true, löst ein WDT-Timeout einen System-Panik aus
|
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
|
// Aktuellen Task (loopTask) zum Watchdog hinzufügen
|
||||||
esp_task_wdt_add(NULL);
|
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());
|
oledShowConnectionError(tr(STR_API_CONN_LOST), WiFi.localIP().toString());
|
||||||
oledSetPriority(DISPLAY_PRIORITY_WARNING, 3000);
|
oledSetPriority(DISPLAY_PRIORITY_WARNING, 3000);
|
||||||
mainTaskWasPaused = true;
|
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
|
// Überprüfe den Status des Touch Sensors (nur wenn Waage vorhanden)
|
||||||
if (touchSensorConnected && digitalRead(TTP223_PIN) == HIGH && currentMillis - lastButtonPress > debounceDelay)
|
if (scaleConnected && touchSensorConnected && digitalRead(TTP223_PIN) == HIGH && currentMillis - lastButtonPress > debounceDelay)
|
||||||
{
|
{
|
||||||
lastButtonPress = currentMillis;
|
lastButtonPress = currentMillis;
|
||||||
scaleTareRequest = true;
|
scaleTareRequest = true;
|
||||||
@@ -160,7 +170,7 @@ void loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If scale is not calibrated, only show a warning
|
// 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
|
// Do not show the warning if the calibratin process is onging
|
||||||
if(!scaleCalibrationActive){
|
if(!scaleCalibrationActive){
|
||||||
@@ -168,7 +178,8 @@ void loop() {
|
|||||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
if (scaleConnected && scaleCalibrated)
|
||||||
{
|
{
|
||||||
// Ausgabe der Waage auf Display
|
// Ausgabe der Waage auf Display
|
||||||
// Block weight display during NFC write operations and higher-priority display messages
|
// Block weight display during NFC write operations and higher-priority display messages
|
||||||
|
|||||||
+149
-32
@@ -35,10 +35,22 @@ uint8_t scale_tare_counter = 0;
|
|||||||
bool scaleTareRequest = false;
|
bool scaleTareRequest = false;
|
||||||
uint8_t pauseMainTask = 0;
|
uint8_t pauseMainTask = 0;
|
||||||
bool scaleCalibrated;
|
bool scaleCalibrated;
|
||||||
|
bool scaleConnected;
|
||||||
bool autoTare = true;
|
bool autoTare = true;
|
||||||
bool scaleCalibrationActive = false;
|
bool scaleCalibrationActive = false;
|
||||||
volatile bool scaleCalibrationRequest = 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 #####
|
// ##### 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) {
|
void start_scale(bool touchSensorConnected) {
|
||||||
Serial.println("Prüfe Calibration Value");
|
Serial.println("Prüfe Calibration Value");
|
||||||
float calibrationValue;
|
float calibrationValue;
|
||||||
@@ -253,39 +361,48 @@ void start_scale(bool touchSensorConnected) {
|
|||||||
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
|
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
|
||||||
|
|
||||||
oledShowProgressBar(5, NUM_SETUP_STEPS, DISPLAY_BOOT_TEXT, tr(STR_SEARCHING_SCALE));
|
oledShowProgressBar(5, NUM_SETUP_STEPS, DISPLAY_BOOT_TEXT, tr(STR_SEARCHING_SCALE));
|
||||||
for (uint16_t i = 0; i < 3000; i++) {
|
friendlyWait(3000);
|
||||||
yield();
|
// for (uint16_t i = 0; i < 3000; i++) {
|
||||||
vTaskDelay(pdMS_TO_TICKS(1));
|
// yield();
|
||||||
esp_task_wdt_reset();
|
// 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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
while(!scale.is_ready()) {
|
Serial.println("Kann kein HX711 Board finden !"); // Sende Text "Kann kein..." an seriellen Monitor
|
||||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
oledDisplayText(tr(STR_HX711_NOT_FOUND));
|
||||||
}
|
vTaskDelay(pdMS_TO_TICKS(2000));
|
||||||
|
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ extern uint8_t scale_tare_counter;
|
|||||||
extern bool scaleTareRequest;
|
extern bool scaleTareRequest;
|
||||||
extern uint8_t pauseMainTask;
|
extern uint8_t pauseMainTask;
|
||||||
extern bool scaleCalibrated;
|
extern bool scaleCalibrated;
|
||||||
|
extern bool scaleConnected;
|
||||||
extern bool autoTare;
|
extern bool autoTare;
|
||||||
extern bool scaleCalibrationActive;
|
extern bool scaleCalibrationActive;
|
||||||
extern volatile bool scaleCalibrationRequest;
|
extern volatile bool scaleCalibrationRequest;
|
||||||
|
|||||||
Reference in new issue
Block a user