#include "HX711.h"
#include <WiFi.h>
#include <WebServer.h>
const int LOADCELL_DOUT_PIN = 19;
const int LOADCELL_SCK_PIN = 18;
const char* ssid = "MOVISTAR_PLUS_B389"; // Enter SSID here
const char* password = "8BA8FAAAD65DE17863EE"; //Enter Password here
HX711 balanza;
WebServer server(80);
void setup()
{
Serial.begin(115200);
balanza.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
delay(250);
balanza.set_scale(397);
balanza.tare(20); // Hacer 20 lecturas, el promedio es la tara
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{ delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected..!");
Serial.print("Got IP: "); Serial.println(WiFi.localIP());
server.on("/", handle_OnConnect);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop()
{ server.handleClient();
}
void handle_OnConnect()
{ if (balanza.is_ready())
{ float reading = balanza.get_units(10); // Peso sin tara y corregido
Serial.println(reading );
server.send(200, "text/html", SendHTML(reading));
}
else
Serial.println("HX711 no encontrado.");
}
void handle_NotFound(){
server.send(404, "text/plain", "No hay respuesta");
}
String SendHTML(float peso)
{ String ptr = "<!DOCTYPE html>";
ptr +="<html>";
ptr +="<head>";
ptr +="<title>ESP32 Balanza Web</title>";
ptr +="<img src='https://www.prometec.net/wp-content/uploads/2018/11/logo-prometec.jpg'>";
ptr +="<meta http-equiv='refresh' content='0.3' >";
ptr +="<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
ptr +="<link href='https://fonts.googleapis.com/css?family=Open+Sans:300,400,600' rel='stylesheet'>";
ptr +="<style>";
ptr +="html { font-family: 'Open Sans', sans-serif; display: block; margin: 0px auto; text-align: center;color: #444444;}";
ptr +="body{margin: 0px;} ";
ptr +="h1 {margin: 50px auto 30px;} ";
ptr +=".side-by-side{display: table-cell;vertical-align: middle;position: relative;}";
ptr +=".text{font-weight: 600;font-size: 19px;width: 200px;}";
ptr +=".reading{font-weight: 300;font-size: 50px;padding-right: 25px;}";
ptr +=".temperature .reading{color: #F29C1F;}";
ptr +=".superscript{font-size: 17px;font-weight: 600;position: absolute;top: 10px;}";
ptr +=".data{padding: 10px;}";
ptr +=".container{display: table;margin: 0 auto;}";
ptr +=".icon{width:65px}";
ptr +="</style>";
ptr +="</head>";
ptr +="<body>";
ptr +="<h1>ESP32 Balanza Web</h1>";
ptr +="<div class='container'>";
ptr +="<div class='data temperature'>";
ptr +="<div class='side-by-side icon'>";
ptr +="<svg enable-background='new 0 0 19.438 54.003'height=54.003px id=Layer_1 version=1.1 viewBox='0 0 19.438 54.003'width=19.438px x=0px xml:space=preserve xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink y=0px><g><path d='M11.976,8.82v-2h4.084V6.063C16.06,2.715,13.345,0,9.996,0H9.313C5.965,0,3.252,2.715,3.252,6.063v30.982";
ptr +="C1.261,38.825,0,41.403,0,44.286c0,5.367,4.351,9.718,9.719,9.718c5.368,0,9.719-4.351,9.719-9.718";
ptr +="c0-2.943-1.312-5.574-3.378-7.355V18.436h-3.914v-2h3.914v-2.808h-4.084v-2h4.084V8.82H11.976z M15.302,44.833";
ptr +="c0,3.083-2.5,5.583-5.583,5.583s-5.583-2.5-5.583-5.583c0-2.279,1.368-4.236,3.326-5.104V24.257C7.462,23.01,8.472,22,9.719,22";
ptr +="s2.257,1.01,2.257,2.257V39.73C13.934,40.597,15.302,42.554,15.302,44.833z'fill=#F29C21 /></g></svg>";
ptr +="</div>";
//ptr +="<div class='side-by-side text'>Peso</div>";
ptr +="<div class='side-by-side reading'>";
ptr +=peso;
ptr += " gr.";
ptr +="</div>";
//ptr +="<span class='superscript'>m</span></div>";
ptr +="</div>";
ptr +="</div>";
ptr +="</body>";
ptr +="</html>";
return ptr;
}