ESP32 scale with display and web

jora74
Posts: 1
Joined: Mon Nov 04, 2024 10:31 am

ESP32 scale with display and web

Postby jora74 » Mon Nov 04, 2024 10:40 am

Hello everyone,

I'm stuck and need your help.
I created the following sketch and maneuvered myself into a dead end.
Thoughts are spinning and I can't get out of it.
The weighing and output on the display works, but I'm stuck with the extension for the web version.

Here is my 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;
}

Thank you for your help

lbernstone
Posts: 828
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 scale with display and web

Postby lbernstone » Tue Nov 05, 2024 3:50 pm

You haven't included sendHTML, which presumably is what doesn't work. I would recommend you move the handler to a separate function, rather than trying to handle this as a lambda function, and don't pass String objects back and forth. That tends to leak memory, and is the explicit purpose of pointers.

Who is online

Users browsing this forum: Baidu [Spider] and 60 guests