Hi
I am trying to implement a momentary pushbutton running on a website that runs a function on my ESP32 board when clicked. I am using code from randomnerdtutorials and would like to add a tare function. (there's many toggle a LED examples, but I am looking for something that works 'onclick' and doesn't involve a state change. I would be grateful for any help.
<p>
<td><input type="button" onclick="TARESCALE;" ></td>
<td>Tare</td>
</p>
The whole code minus the toggle handling is here, getWeight, Temp and tare are not yet implemented and just return a constant at the moment.
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com/esp8266-nodemcu-access-point-ap-web-server/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
//Button click
https://community.platformio.org/t/getting-the-state-of-a-button-from-a-webpage/15609/3
*********/
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
/*
// Import required libraries
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
*/
const char* ssid = "MYESP32AP";
const char* password = "1234";
// current temperature & weight, updated in loop()
float w = 0.0;
float t = 0.0;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time DHT was updated
// Updates DHT readings every 10 seconds
const long interval = 1000;
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html {
font-family: Arial;
display: inline-block;
margin: 0px auto;
text-align: center;
}
h2 { font-size: 3.0rem; }
p { font-size: 3.0rem; }
.units { font-size: 1.2rem; }
.dht-labels{
font-size: 1.5rem;
vertical-align:middle;
padding-bottom: 15px;
}
</style>
</head>
<body>
<h2>Scale Weight Server</h2>
<p>
<td><input type="button" onclick="TARESCALE;" ></td>
<td>Tare</td>
</p>
<p>
<span class="dht-labels">Temperature</span>
<span id="temperature">%TEMPERATURE%</span>
<sup class="units">°C</sup>
</p>
<p>
<span class="dht-labels">Weight</span>
<span id="weight">%WEIGHT%</span>
<sup class="units">kg</sup>
</p>
</body>
<script>
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("temperature").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/temperature", true);
xhttp.send();
}, 10000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("weight").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/weight", true);
xhttp.send();
}, 1000 ) ;
</script>
</html>)rawliteral";
// Replaces placeholder with DHT values
String processor(const String& var) {
//Serial.println(var);
if (var == "TEMPERATURE") {
return String(t);
}
else if (var == "WEIGHT") {
return String(w);
}
return String();
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
Serial1.begin(57600);
Serial.print("Setting AP (Access Point)…");
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
// Print ESP8266 Local IP Address
Serial.println(WiFi.localIP());
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/html", index_html, processor);
});
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/plain", String(t).c_str());
});
server.on("/weight", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/plain", String(w).c_str());
});
// Start server
server.begin();
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
//int32_t Tempe = getTemp();
float newT = (float)getTemp() / 1000;
if (isnan(newT)) {
Serial.println("Failed to read temperature!");
}
else {
t = newT;
Serial.println(t);
}
// Read Weight
//int32_t Gewicht = getWeight();
float newW = (float)getWeight() / 1000;
Serial.print("newW ");
Serial.println(newW);
// if weight read failed, don't change h value
if (isnan(newW)) {
Serial.println("Failed to read weight!");
}
else {
w = newW;
Serial.println(w);
}
}
}
int32_t getWeight() {
int32_t weight = 200;
return weight;
}
int32_t getTemp() {
int32_t tempera = 20000; //m degC
return tempera;
}
uint8_t tareScale() {
//StartTare
}