Gaswaage mit dem ESP32 Diplay & web

Hallo zusammen,

ich komm nicht weiter und benötige eure Hilfe.
Ich habe folgenden Sketch erstellt und mich in eine Sackgasse manövriert.
Gedankekarusel und komm nicht mehr raus.
Es funktioniert das wiegen und ausgeben am Display jedoch bei der Erweiterung bei der Webvariante hänge ich fest.

Hier mein sketch:

#include <Wire.h>
#include <HX711_ADC.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#include <WiFi.h>
#include <ESPAsyncWebServer.h>

const char* ssid = "at_home"; // CHANGE IT
const char* password = "Geheim"; // CHANGE IT

AsyncWebServer server(80);

#define OLED_RESET -1 // we don't have a reset, but the constructor expects it
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
HX711_ADC LoadCell(33, 25);

byte interruptPin=12;
volatile bool taraRequest = false;

void setup()   {                
  pinMode(interruptPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(interruptPin), taraEvent, RISING);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x64)
  display.clearDisplay();
  display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(10,4);
  display.println("Wait");
  display.display();
  LoadCell.begin();
  LoadCell.start(2000); 
  LoadCell.setCalFactor(22.85); 
  Serial.begin(115200);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Print the ESP32's IP address
  Serial.print("ESP32 Web Server's IP address: ");
  Serial.println(WiFi.localIP());

  });

  // Start the server
  server.begin();
}


void loop() {
  float weightAsFloat = 0.0;
  static unsigned long t = 0;
  
  LoadCell.update();
  
  if (millis() > t + 250) {
    weightAsFloat = LoadCell.getData();
    displayWeight(weightAsFloat);
    t = millis();  
  }
  if(taraRequest){
    doTara();
    taraRequest = false;
  }
}

void displayWeight(float weight){
  int weightAsInt = int(weight+0.5);
  char weightAsString[6] = {0}; // sign (-) + 4 digits + Null character = 6
  dtostrf(weightAsInt,5,0,weightAsString);
  display.clearDisplay();
  display.setCursor(0,4);
  display.println(weightAsString);
  display.display();
}
  // Define a route to serve the HTML page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
    Serial.println("ESP32 Web Server: New request received:");  // for debugging
    Serial.println("GET /");        // for debugging
    request->  server.send(200, "text/html", SendHTML(weightAsString)); 
    
void doTara(){
  LoadCell.tareNoDelay();
  display.clearDisplay();
  display.setCursor(10,4);
  display.println("Wait");
  display.display();
  while(LoadCell.getTareStatus()== false){
    LoadCell.update();
    delay(50);
  }
}

// IRAM_ATTR void taraEvent(){ for ESP32 / ESP8266
void taraEvent(){
  taraRequest = true;
}

Müßte das nicht in setup() stehen?

Es muss jedenfalls in einer Funktion stehen (, die auch irgendwo mal aufgerufen wird).

(Zeile 51)
Das ist auch aus Versehen da reingerutscht.

Vermutlich übriggeblieben, fehlt am Ende von server.on :wink:

@jora1: Ich habe Dein Programm mal so korrigiert, daß im Browser "47" steht, wenn auch auf dem Display "47" angezeigt wird:

Programm
#include "zugangsdaten.h"
#include <Wire.h>
#include <HX711_ADC.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

const char* ssid = STA_SSID; // CHANGE IT
const char* password = STA_PASSWORD; // CHANGE IT

AsyncWebServer server(80);

#define OLED_RESET -1 // we don't have a reset, but the constructor expects it
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
HX711_ADC LoadCell(33, 25);

byte interruptPin = 12;
volatile bool taraRequest = false;
char weightAsString[6] = {0}; // sign (-) + 4 digits + Null character = 6

// IRAM_ATTR void taraEvent(){ for ESP32 / ESP8266
void taraEvent()  // damit der Compiler glücklich ist an dieser Stelle
{
  taraRequest = true;
}

void setup()   {
  pinMode(interruptPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(interruptPin), taraEvent, RISING);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x64)
  display.clearDisplay();
  display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(10, 4);
  display.println("Wait");
  display.display();
  LoadCell.begin();
  LoadCell.start(2000);
  LoadCell.setCalFactor(22.85);
  Serial.begin(115200);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Print the ESP32's IP address
  Serial.print("ESP32 Web Server's IP address: ");
  Serial.println(WiFi.localIP());

  // Define a route to serve the HTML page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
    Serial.println("ESP32 Web Server: New request received:");  // for debugging
    Serial.println("GET /");        // for debugging
    request->  send(200, "text/html", weightAsString);
  });

  // Start the server
  server.begin();
}


void loop() {
  float weightAsFloat = 0.0;
  static unsigned long t = 0;

  LoadCell.update();

  if (millis() > t + 250) {
    weightAsFloat = LoadCell.getData();
    weightAsFloat = analogRead(33) * 0.1;  // <-- agmue: weil ich kein HX711 habe
    displayWeight(weightAsFloat);
    t = millis();
  }
  if (taraRequest) {
    doTara();
    taraRequest = false;
  }
}

void displayWeight(float weight) {
  int weightAsInt = int(weight + 0.5);
  dtostrf(weightAsInt, 5, 0, weightAsString);
  display.clearDisplay();
  display.setCursor(0, 4);
  display.println(weightAsString);
  display.display();
}

void doTara() {
  LoadCell.tareNoDelay();
  display.clearDisplay();
  display.setCursor(10, 4);
  display.println("Wait");
  display.display();
  while (LoadCell.getTareStatus() == false) {
    LoadCell.update();
    delay(50);
  }
}

server.on wird aktiv, wenn der Browser Daten anfordert.

ToDo: Automatischer Seitenupdate mit richtigem HTML.

1 Like

Hallo agmue,

super vielen Dank funktioniert prima.
Was meinst du mit?

"ToDo: Automatischer Seitenupdate mit richtigem HTML"

bei HTML füge ich das ja so ein:

Meine selbstaktualisierende Webseite

Diese Webseite aktualisiert sich alle fünf Sekunden selbst!

Bei der Arduino IDE weiß ich das leider nicht. :confused:

In weightAsString steht Text drin, kein HTML, auch wenn Du das dem Browser mit "text/html" vorgaukelst. Ein Browser möchte aber lieber HTML.

Der ESP32 hat ein Dateisystem, das sich bestens zur Ablage von HTML-, CSS- und JPG-Dateien (usw.) eignet. Neue Meßwerte schickt man dann beispielsweise mittels JSON. Wie das geht, zeigt Fips auf seinen Seiten. Die Beispiele zur Temperaturanzeige kannst Du für Dich anpassen.

Eventuell hilfreich: Anleitung: Einführung zu fipsok.de

Vielen Dank nochmals,

hier der fertige Code

#include <Wire.h>
#include <HX711_ADC.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>

const char* ssid = "Geheim"; // CHANGE IT
const char* password = "Geheim"; // CHANGE IT

AsyncWebServer server(80);

#define OLED_RESET -1 // we don't have a reset, but the constructor expects it
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
HX711_ADC LoadCell(33, 25);

byte interruptPin = 12;
volatile bool taraRequest = false;
char weightAsString[6] = {0}; // sign (-) + 4 digits + Null character = 6

// IRAM_ATTR void taraEvent(){ for ESP32 / ESP8266
void taraEvent()  // damit der Compiler glücklich ist an dieser Stelle
{
  taraRequest = true;
}

void setup()   {
  pinMode(interruptPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(interruptPin), taraEvent, RISING);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x64)
  display.clearDisplay();
  display.setTextSize(4);
  display.setTextColor(WHITE);
  display.setCursor(10, 4);
  display.println("Wait");
  display.display();
  LoadCell.begin();
  LoadCell.start(2000);
  LoadCell.setCalFactor(22.85);
  Serial.begin(115200);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  // Print the ESP32's IP address
  Serial.print("ESP32 Web Server's IP address: ");
  Serial.println(WiFi.localIP());

  // Define a route to serve the HTML page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
 String html = "<html><head>";
  html += "<meta http-equiv='content-type' content='text/html; charset=utf-8'>";
  html += "<meta http-equiv='refresh' content='0.5'>";  // Aktualisiert die Seite alle 0.5 Sekunden
  html += "<style>";
  html += "body { font-family: Arial, sans-serif; }";
  html += "h1 { font-size: 48px; }";
  html += "p, span { font-size: 36px; }";
  html += "@media (max-width: 600px) {";
  html += "  h1 { font-size: 36px; }";
  html += "  p, span { font-size: 24px; }";
  html += "}";
  html += "</style>";
  html += "</head><body>";
  html += "<h1>Womo Gaswaage</h1>";
  html += "<p>Die aktuelle Restmenge beträgt:</p>";
  html += "<p><span><h1>" + String(weightAsString) + "  Gramm</h1></span></p>";  // Messwert in einer neuen Zeile
  html += "</body></html>";


    request-> send(200, "text/html", html);

    Serial.println("ESP32 Web Server: New request received:");  // for debugging
    Serial.println("GET /");        // for debugging
    request->  send(200, "text/html", html);
  });

  // Start the server
  server.begin();
}


void loop() {
  float weightAsFloat = 0.0;
  static unsigned long t = 0;

  LoadCell.update();

   if (millis() > t + 250) {
    weightAsFloat = LoadCell.getData();
    displayWeight(weightAsFloat);
    t = millis();  
  }
  if (taraRequest) {
    doTara();
    taraRequest = false;
  }
}

void displayWeight(float weight) {
  int weightAsInt = int(weight + 0.5);
  dtostrf(weightAsInt, 5, 0, weightAsString);
  display.clearDisplay();
  display.setCursor(0, 4);
  display.println(weightAsString);
  display.display();
}

void doTara() {
  LoadCell.tareNoDelay();
  display.clearDisplay();
  display.setCursor(10, 4);
  display.println("Wait");
  display.display();
  while (LoadCell.getTareStatus() == false) {
    LoadCell.update();
    delay(50);
  }
}

grüße Joachim

Danke für Deine Rückmeldung :slightly_smiling_face:

Wenn Dein WoMo keinen Router eingebaut hat, müßtest Du auf AP umsteigen.

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