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;
}
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.