DIY fluid mass flow meter

Das Topic sollte nur Waage1 lauten in der Konfiguration, oder nicht?
Ich habe mir den Code jetzt nicht angeschaut. :smiley:
Grüße,
Kai

Moin Kai,
dass läuft seit gestern.

honk0504 hatte mir den Code per Mail gesendet, und da waren Leerzeichen zuviel drin.
Aber der HX711 müsste auch mit Tasmota Firmware und Web gehen.

Hier mal mein aktueller Code.
neu:

  • MQTT Meldungen werden jetzt nicht sekündlich sondern „nur“ alle 5 Minuten gesendet.

offen:

  • Werte in einem Webfrontend anzeigen
  • Parameter (Kalkulationsfaktor, Periode fürs Versenden von MQTT, …) in Webfrontend anzeigen und editieren können
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266WebServer.h>
#include "HX711.h" 

//Wifi Settings
const char* ssid = "my_WLAN"; //Hier die SSID des WLAN eintragen.
const char* password = "*****"; //Hier zum WLAN passendes Passwort eintragen.

//MQTT Settings
const char* mqtt_server = "0.0.0.0"; //Hier IP-Adresse des MQTT Broker eintragen.
WiFiClient espClient;
PubSubClient client(espClient);
char msg_buff[50];

//Hardware Settings
#define DOUT  D2
#define CLK  D3
HX711 scale(DOUT, CLK);
//Change this calibration factor as per your load cell once it is found you many need to vary it in thousands
//float calibration_factor = -219.50; //-219.50 worked for my scale 
float calibration_factor = 28.20; //28.20 worked for my scale 
int switchPin = D5;
int switchValue = 0;

//Timer Settings
#define FIVEMIN (1000UL * 60 * 5)
unsigned long rolltime = millis() + FIVEMIN;

// Set web server port number to 80
ESP8266WebServer server(80);

const int led = 13;

void handleRoot() {
  digitalWrite(led, 1);
  char temp[400];
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf(temp, 400,

           "<html>\
  <head>\
    <meta http-equiv='refresh' content='5'/>\
    <title>ESP8266 Demo</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h1>Hello from ESP8266!</h1>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <img src=\"/test.svg\" />\
  </body>\
</html>",

           hr, min % 60, sec % 60
          );
  server.send(200, "text/html", temp);
  digitalWrite(led, 0);
}

void handleNotFound() {
  digitalWrite(led, 1);
  String message = "File Not Found

";
  message += "URI: ";
  message += server.uri();
  message += "
Method: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "
Arguments: ";
  message += server.args();
  message += "
";

  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "
";
  }

  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

//=============================================================================================
//                         SETUP
//=============================================================================================
void setup() {
  delay(100);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(switchPin, INPUT);
  Serial.begin(115200);
  scale.begin(DOUT,CLK); //esp specific statment 
  Serial.println("HX711 Kalibrierung");
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  scale_setup();

  
}
void scale_setup() {
  client.subscribe("HX711cal"); 
  Serial.println("HX711 Kalibrierung");
  Serial.println("Bitte entfernen Sie jegliche Gewichte von der Waage!");
  Serial.println("Sobald das Auslesen beginnt legen Sie bitte ein bekanntes Gewicht auf die Waage.");
  Serial.println("Drücken Sie a,s,d,f um den Kalibrierungsfaktor um 10,100,1000,10000 zu erhöhen.");
  Serial.println("Drücken Sie y,x,c,v um den Kalibrierungsfaktor um 10,100,1000,10000 zu verringern.");
  Serial.println("Drücken Sie t für TARA");
  scale.tare(); //Reset the scale to 0
  Serial.println("1. TARA erledigt! Warten Sie bitte 5 weitere Sekunden.");
  delay(5000);
  scale.tare();
  Serial.println("2. TARA erledigt! Warten Sie bitte 2 weitere Sekunden.");
  delay(2000);
  scale.tare();
  Serial.println("3. TARA erledigt!");
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
}

void setup_wifi(){
  delay(10);
  Serial.println();
  Serial.print("Verbindungsaufbau zu ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi verbunden.");
  Serial.println("IP-Adresse: ");
  Serial.println(WiFi.localIP());
  Serial.print("Wifi Signalstärke (RSSI): ");
  Serial.println(WiFi.RSSI()*-1);
}

void reconnect() {
  while(!client.connected()) {
    Serial.print("Aufbau der MQTT Verbindung ...");
    if (client.connect("ESP8266Client")) {
      Serial.println(" verbunden");
      client.subscribe("HX711cal");
    } else {
      Serial.print("fehlgeschlagen, rc=");
      Serial.print(client.state());
      Serial.println(" erneuter Versuch in 5 Sekunden.");
      delay(5000);
      
    }
  }
}

void callback(char* topic, byte* payload, unsigned int length) {
   int i = 0;
  Serial.println("Meldung empfangen:  topic: " + String(topic));
  Serial.println("Länge: " + String(length,DEC));
  for(i=0; i<length; i++) {
    msg_buff[i] = payload[i];
  }
  msg_buff[i] = '\0';
  
  String msgString = String(msg_buff);
  
  Serial.println("Payload: " + msgString);
  if (msgString == "tare"){    // if there is a "1" published to any topic (#) on the broker then:
    digitalWrite(LED_BUILTIN, LOW);     // set pin to the opposite state 
    Serial.println("Switching LED");
    scale.tare(5);
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else if (msgString == "+0.1")
     calibration_factor += 0.1;
  else if(msgString == "-0.1")
      calibration_factor -= 0.1;
  else if(msgString == "+1")
      calibration_factor += 1;  
  else if(msgString == "-1")
      calibration_factor -= 1;  
  else if(msgString == "+10")
      calibration_factor += 10;  
  else if(msgString == "-10")
      calibration_factor -= 10;
  else if(msgString == "+100")
      calibration_factor += 100;  
  else if(msgString == "-100")
      calibration_factor -= 100;  
}

//=============================================================================================
//                         LOOP
//=============================================================================================
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  float weight = scale.get_units(15);
  float rounded = round(weight) ;
  long rssi = (WiFi.RSSI()*-1);
  //Serial.print("signal strength (RSSI):");
  //Serial.println(rssi);

  scale.set_scale(calibration_factor); //Adjust to this calibration factor
  switchValue = digitalRead(switchPin);
  Serial.print("ausgelesener Wert: ");
  Serial.print(weight);
  Serial.print(" g |||"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person
  Serial.print(" gerundeter Wert: ");
  Serial.print(rounded);
  Serial.print(" g |||"); //Change this to kg and re-adjust the calibration factor if you follow SI units like a sane person
  Serial.print(" Kalibrierungsfaktor: ");
  Serial.print(calibration_factor);
  Serial.print(" ||| RSSI: ");
  Serial.print(rssi);
  Serial.println();
  Serial.println(switchValue);

  if((long)(millis() - rolltime) >= 0) {
   //  Do your five minute roll stuff here
   client.publish("/Waage1/Scale1",String(rounded).c_str(),true);
   client.publish("/Waage1/Debug",String(weight).c_str(),true);
   client.publish("/Waage1/HX711calfactor",String(calibration_factor).c_str(),true);
   client.publish("/Waage1/RSSI",String(rssi).c_str(),true);
   Serial.print("=== MQTT Daten wurden erfolgreich verschickt (Zeitfenster: alle 5 Minuten). ===");
   rolltime += FIVEMIN;
   }  

  if(switchValue == 0){
    digitalWrite(LED_BUILTIN, LOW);
    scale.tare(5);
    digitalWrite(LED_BUILTIN, HIGH);
  }

  if(Serial.available())
  {
    char temp = Serial.read();
    if(temp == '+' || temp == 'a')
      calibration_factor += 0.1;
    else if(temp == '-' || temp == 'y')
      calibration_factor -= 0.1;
    else if(temp == 's')
      calibration_factor += 1;  
    else if(temp == 'x')
      calibration_factor -= 1;  
    else if(temp == 'd')
      calibration_factor += 10;  
    else if(temp == 'c')
      calibration_factor -= 10;
    else if(temp == 'f')
      calibration_factor += 100;  
    else if(temp == 'v')
      calibration_factor -= 100;
    else if(temp == 't')
      scale.tare(5); //Reset the scale to zero    
    else if(switchValue == '0')
      scale.tare(5);  //Reset the scale to zero
  }
}
//=============================================================================================