Waage mit HX711 messung nach Stromausfall

Hallo liebe Gemeinde,

habe für mein Wohnmobil mit Propangasflasche folgende Messung des Gewichtes der Gasflasche vor:
Messung soll erfolgen wenn Plusspannung an ist oder angelegt wird. Das funktioniert auch perfekt und genau. Zum Einsatz kommen 4 Sensoren aus der Bucht die am ADC angeschlossen sind.
Mein Problem:
nach reset oder Stromausfall, startet der ESP32 neu und die Kalibrierung startet ebenso, setzt aber voraus das die neue Kalibrierung bei 0 Gewicht startet, das ist aber nicht gegeben, da die Gasflasche ist nach wie vor unverändert auf dem Sockel mit den vier Sensoren hängt.
Ich möchte aber, das der Messvorgang da weitergeht, wo er aufgehört hat, also ohne neue Kalibrierung.
Geht das?

Hänge mal mein Skript mit dran

Viele Grüsse

Gerd

WoMo-PubSubClientMQTTClient-tut-mithx711-broker-tut-3.ino (6.38 KB)

Setze Deinen Code bitte direkt ins Forum. Benutze dazu Codetags (</>-Button oben links im Forumseditor oder [code] davor und [/code] dahinter oder gehe in der IDE auf Bearbeiten - Für Forum kopieren und füge es hier ein.
Dann ist er auch auf mobilen Geräten besser lesbar.
Das kannst Du auch noch nachträglich ändern.

Gruß Tommy

Hallo
schreib das letzte Messergebnis ins EEPROM

Ich glaube, das letzte Messergebnis interessiert ihn nicht. Eher sollte der Kalibrierwert ins EEPROM. Und die Kalibrierung aus dem Setup raus.

Gruß Tommy

Es steht schon drin:

  calibrationValue = 23.4; // uncomment this if you want to set the calibration value in the sketch
#if defined(ESP8266)|| defined(ESP32)
  //EEPROM.begin(512); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom
#endif

Hier mal der gesamte Code, wie gewünscht,

es läuft ja der ESP32, der hat kein EEPROM, anstelle dessen soll die Preference.h Lib eingesetzt werden, das hat auch geklappt, den aktuellen Wert zu speichern, Aber nach erneutem einlesen des Wertes (nach Reset), in welche Variable muss der geschrieben werden?
Und den CalibrationValue, wie verarbeite ich den nach restart weiter, irgendwie erschliessen sich mir die unterschiedlichen Variablen noch nicht.?

vg ..... Gerd

#include <WiFi.h>
#include <PubSubClient.h>
#include "credentials.h"
#include <HX711_ADC.h>
#include <EEPROM.h>
#include <U8g2lib.h>
#include <Wire.h>
#include <Preferences.h>

#include <Adafruit_Sensor.h>

Preferences preferences;

const int HX711_dout = 4; //mcu > HX711 dout pin
const int HX711_sck = 5; //mcu > HX711 sck pin
HX711_ADC LoadCell(HX711_dout, HX711_sck);
const int calVal_eepromAdress = 0;
long t;
char x = 154;
char tmp[10];

///////////////////////////////////////////////////////Variablen setzen///////////
int val = 0; // Buildin Hallsensor testen
int Gewicht = 0;
int _update = 0;
int test = 0;

const char* ssid = networkSSID;
const char* password = networkPASSWORD;
const char* mqttServer = mqttSERVER;
const char* mqttUsername = mqttUSERNAME;
const char* mqttPassword = thingKEY;
const char* mqttDeviceId = thingID;

char subTopic[] = "ledControl";     //payload[0] will control/set LED
char pubTopic[] = "ledState";       //payload[0] will have ledState value
//char temperatureTopic[] = "Temperature";
//char humidityTopic[] = "Humidity";
char valTopic[] = "HallSensor2";
char GewichtTopic[] = "Waage";


//////////////////////////////////////////////////Server setzen/////////////
WiFiClient wifiClient;
PubSubClient client(wifiClient);
long lastMsg = 0;
char msg[50];
int value = 0;
int ledState = 0;
int loop_1 = 1;

//DHT_Unified dht(DHTPIN, DHTTYPE);
////////////////////////////////////////Server setzen ende////////////
////////////////////////////////////////SETUP//////////////////////
void setup_wifi() 
{
  delay(10);
  
  Serial.println();
  Serial.print("Connecting to wifi ");
  Serial.println(ssid);

//pref begin//////////////////////////////////////pref begin
preferences.begin ("az", false);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
//////////////////////////////////////////////////VOID CALLBACK//////////////////////
void callback(char* topic, byte* payload, unsigned int length) 
{
  Serial.print("topic: ");
  Serial.print(topic);
  Serial.print(" message: ");
  for (int i = 0; i < length; i++) 
  {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  
}
////////////////////////////////////////////////////////VOID RECONNECT BROKER //////////////////
void reconnect() 
{
  while (!client.connected()) 
  {
    Serial.print("broker...");
 
    if (client.connect(mqttDeviceId, mqttUsername, mqttPassword)) 
    {
      Serial.println("connected.");
      client.subscribe(subTopic);
    } else 
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(2000);
    }
  }
}
//////////////////////////////////////////////////VOID SETUP ///////////////////////////////
void setup() 
{
//  pinMode(LED_PIN, OUTPUT);     
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqttServer, 1883);
  client.setCallback(callback);
  Serial.println("Starting...");
  LoadCell.begin();

    float calibrationValue; // calibration value (see example file "Calibration.ino")
  calibrationValue = 23.4; // uncomment this if you want to set the calibration value in the sketch
#if defined(ESP8266)|| defined(ESP32)
  //EEPROM.begin(512); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom
#endif
  //EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom
  long stabilizingtime = 2000; // Stabilisierung verbessern-preciscion right after power-up can be improved by adding a few seconds of stabilizing time
  boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
  LoadCell.start(stabilizingtime, _tare);
  if (LoadCell.getTareTimeoutFlag()) {
    Serial.println("Timeout, check MCU>HX711 wiring and pin designations");
    while (1);
  }
  else {
    LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
    Serial.println("Startup is complete");
  }
}
///////////////////////////////////////////////////VOID LOOP///////////////////////////
void loop() 
{

  //////////////////
    static boolean newDataReady = 0;
  const int serialPrintInterval = 0; //increase value to slow down serial print activity
  // check for new data/start next conversion:
  if (LoadCell.update()) newDataReady = true;
  // get smoothed value from the dataset:
  if (newDataReady) {
    if (millis() > t + serialPrintInterval) {
      float i = LoadCell.getData();
//      Serial.print("Load_cell output val: ");
//      Serial.println(i);
      newDataReady = 0;
      t = millis();
    }
  }
  // receive command from serial terminal, send 't' to initiate tare operation:
  if (Serial.available() > 0) {
    float i;
    char inByte = Serial.read();
    if (inByte == 't') LoadCell.tareNoDelay();
  }
  // check if last tare operation is complete:
  if (LoadCell.getTareStatus() == true) {
    Serial.println("Tare complete");
  }
  ///////////////////////
  if (!client.connected()) 
  {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 2000) // Geschwindigkeit der Übertragung
  {
    lastMsg = now;
    char payLoad[1];
    itoa(ledState, payLoad, 10);
    client.publish(pubTopic, payLoad);
//////////////////////////////////////Daten aus Sensoren////////////////////
val = hallRead();
  // print the results to the serial monitor
  Serial.print(F("HallSensor2: "));
  Serial.print(val); 
  Serial.println(F("  V/T"));
  client.publish("ClientWaage/valTopic", String(val).c_str());
  delay(1000);

Gewicht = LoadCell.getData();
//int Gewicht = 700;
//if (Gewicht != Gewicht) { //tut auch nicht
  if (Gewicht >= 700) { //tut
//if (Gewicht <(Gewicht-20)||Gewicht >(Gewicht+20)) { //tut nicht
  Serial.println("passt");
}


_update = LoadCell.update();
  // print the results to the Waegesensor
  Serial.print(F("Wägesensor: "));
  Serial.print(Gewicht); 
  Serial.println(F("  gramm"));
  client.publish("ClientWaage/GewichtTopic", String(Gewicht).c_str());
  client.publish("ClientWaage/LoadCell_update", String(_update).c_str());
  delay(10000);
  }
}

hallo paulpaulson,
so könnte ich mir das Vorstellen, letzten Wert alle xx Minuten in den Flash Speicher mittels preferences wegschreiben, aber dann? wohin schreib ich den zurück? welche Variable setze ich mit diesem Wert?

vg Gerd

gschrode54:
so könnte ich mir das Vorstellen, letzten Wert alle xx Minuten in den Flash Speicher mittels preferences wegschreiben, aber dann? wohin schreib ich den zurück? welche Variable setze ich mit diesem Wert?

Nein. Tommy56 hat doch schon ganz zu Anfang geschrieben, was genommen werden muss.
Wenn Du den aktuellen, also den unter Belastung, Wert wegschreiben willst, ist das Murks. Rein logisch macht das keinen Sinn.

Du hast einen Referenzwert - der muss weggeschrieben sein.
Der aktuelle Wert, verglichen mit dem Referenzwert ergibt das, was auf den Sensoren einwirkt.

Fällt der Saft aus und Du kommst aus dem off/reset wieder zurück, hast Du den Referenzwert, lädst den und ermittels den aktuellen Wert der auf die Sensoren einwirkt und hast das tatsächliche Gewicht.

sorry, aber was ist den mein Referenzwert?

gschrode54:
sorry, aber was ist den mein Referenzwert?

Ab Zeile 127:

    float calibrationValue; // calibration value (see example file "Calibration.ino")
  calibrationValue = 23.4; // uncomment this if you want to set the calibration value in the sketch
#if defined(ESP8266)|| defined(ESP32)
  //EEPROM.begin(512); // uncomment this if you use ESP8266/ESP32 and want to fetch the calibration value from eeprom
#endif
  //EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom
  long stabilizingtime = 2000; // Stabilisierung verbessern-preciscion right after power-up can be improved by adding a few seconds of stabilizing time
  boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
  LoadCell.start(stabilizingtime, _tare);

Den Inhalt solltest Du eigentlich kennen.

Wieso kennen? Das ist doch nur kopiert.

Gruß Tommy

Tommy56:
.. nur kopiert.

Verräter :wink:

ok, danke.

@my_xy_projekt: eher Realist :wink:

Gruß Tommy

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.