#include <WiFi.h>
#include <WebServer.h>
#include "HX711.h"
#include <Wire.h>
#define DT 4
#define SCK 5
HX711 scale;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
WebServer Server(80);
String createwebpage(float weightKg)
{
String html = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="5">
<style>
body{
background: white;
font-family: Arial;
padding: 20px;
}
.bar {
background: #ddd;
border-radius: 25px;
height: 30px;
width: 100%;
overflow: hidden;
margin-bottom: 20px;
}
.fill {
height: 100%;
background: green;
border-radius: 25px;
text-align: right;
padding-right: 10px;
line-height: 30px;
color: white;
}
</style>
</head>
<body>
<h2>ESP32 SENSOR DATA</h2>
<h3>Weight</h3>
<div class="bar">
<div class="fill" style="width:WEIGHT%;">
WEIGHT Kg
</div>
</div>
</body>
</html>
)rawliteral";
html.replace("WEIGHT", String(weightKg, 2));
return html;
}
// Handle Root
void handleRoot()
{
float raw = scale.get_units(10); // average of 10 readings
float weightKg = raw; // after calibration this becomes kg
Server.send(200, "text/html", createwebpage(weightKg));
}
void setup()
{
Serial.begin(115200);
scale.begin(DT, SCK);
Serial.println("Remove weight...");
delay(3000);
scale.tare(); // reset scale
// IMPORTANT: Set calibration factor (change after calibration)
scale.set_scale(420.0); // adjust this value for your load cell
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Server.on("/", handleRoot);
Server.begin();
}
void loop()
{
Server.handleClient();
}