This commit is contained in:
Tyrel Souza 2022-11-01 01:08:29 -04:00
parent fb216b16fb
commit 9f4f42bab5
2 changed files with 61 additions and 76 deletions

View File

@ -18,19 +18,14 @@
<h1 class="display-4">Water Heater Control</h1>
<br />
<h2 id="currentSeconds" class="display-4">Number of Seconds: 5</h2>
<input type="range" id="seconds" name="seconds" min="1" max="9" onchange="setSeconds(this.value)">
<input type="range" id="temperature" name="temperature" min="90" max="120" onchange="setTemperature(this.value)">
<br />
<button type="button" class="btn btn-success btn-lg" id="toSlider">Set to [??]</button>
<br />
<br />
<button type="button" class="btn btn-primary btn-lg" id="down">Down</button>
<button type="button" class="btn btn-danger btn-lg" id="stop">Stop</button>
<button type="button" class="btn btn-primary btn-lg" id="up">Up</button>
<br />
<br />
<br />
<h2 id="currentAngle" class="display-4">Current Action: STOPPED</h2>
<button type="button" class="btn btn-danger btn-sm" id="initialize">Initialize to 90</button>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script
@ -40,61 +35,51 @@
></script>
<script>
var seconds = 5;
var currentTimeout = null;
var temperature = 90;
var timer = null;
const DOWN_ANGLE = 150;
const STOP_ANGLE = 160;
const UP_ANGLE = 170;
function setSeconds(to) {
seconds = to;
$("#currentSeconds").text("Number of Seconds: " + seconds);
function setTemperature(temp) {
temperature = temp;
$("#toSlider").text("Set Temperature: " + temperature)
console.log("Set Temperature: " + temperature);
}
function action(newTemperature) {
$("#toSlider").prop("disabled", true);
$("#temperature").prop("disabled", true);
console.log(newTemperature);
setTimeout(() => {
$("#toSlider").prop("disabled", false);
$("#temperature").prop("disabled", false);
function action(angle, direction) {
console.log(angle, direction)
if (currentTimeout !== null) {
console.log(currentTimeout)
clearTimeout(currentTimeout);
}
}, 3000);
$.post("/angle", {
angle: angle,
let rotations = newTemperature - 90; //base at 0;
$.post("/setTemperature", {
newTemperature,
rotations,
});
$("#currentAngle").text("Current Action: " + direction);
if (angle == STOP_ANGLE){
$("#down").prop("disabled", false)
$("#up").prop("disabled", false)
} else {
$("#down").prop("disabled", true)
$("#up").prop("disabled", true)
currentTimeout = setTimeout(() => {
action(STOP_ANGLE, "STOPPED");
}, seconds * 1000);
}
}
$(document).ready(() => {
$("#down").on("click", () => {
action(DOWN_ANGLE, "decreasing")
$("#to90").on("click", () => {
action(90);
setTemperature(90);
document.getElementById("temperature").value = temperature;
});
$("#stop").on("click", () => {
action(STOP_ANGLE, "STOPPED")
});
$("#up").on("click", () => {
action(UP_ANGLE, "increasing")
$("#toSlider").on("click", () => {
action(temperature);
});
$("#initialize").on("click", () => {
$.post("/initialize");
})
action(STOP_ANGLE, "STOPPED")
setSeconds(seconds);
document.getElementById("seconds").value = seconds;
document.getElementById("temperature").value = temperature;
setTemperature(temperature);
});
</script>

View File

@ -25,8 +25,10 @@
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AccelStepper.h>
#include <string>
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
bool initializing = false;
// ULN2003 Motor Driver Pins
#define IN1 5
@ -39,15 +41,15 @@ AccelStepper stepper(AccelStepper::HALF4WIRE, IN1, IN3, IN2, IN4);
const char *ssid = "TARDIS"; // replace with your SSID
const char *password = "EvangelionUnit-01"; // replace with your Password
const uint8_t servoPin = D1; // replace with servo pin
// Create Server instance
AsyncWebServer server(80);
void setup()
{
// Attach Servo, start SPIFFS and Connect to WiFi
Serial.begin(115200);
stepper.setMaxSpeed(2000);
stepper.setAcceleration(500);
stepper.setMaxSpeed(3000);
stepper.setAcceleration(2500);
// set target position
stepper.moveTo(0);
@ -74,26 +76,19 @@ void setup()
request->send(SPIFFS, "/index.html", "text/html");
});
/*server.on("/angle", HTTP_POST, [](AsyncWebServerRequest *request) {
String angle = request->arg("angle");
Serial.println("Current Position: " + angle + "°");
servo.write(angle.toInt());
request->send(200);
});*/
server.on("/stop", HTTP_POST, [](AsyncWebServerRequest *request) {
Serial.println("stopping");
stepper.stop();
request->send(200);
});
server.on("/cw", HTTP_POST, [](AsyncWebServerRequest *request) {
Serial.println("cw");
stepper.moveTo(2 * stepsPerRevolution);
server.on("/setTemperature", HTTP_POST, [](AsyncWebServerRequest *request) {
int rotations = request->arg("rotations").toInt();
double multiplier = 0.2; // TODO: find me out
stepper.moveTo((multiplier * rotations) * stepsPerRevolution);
request->send(200);
});
server.on("/ccw", HTTP_POST, [](AsyncWebServerRequest *request) {
Serial.println("cw");
stepper.moveTo(- 2 * stepsPerRevolution);
//rotate a looot back to 90 F, as we cant go lower, so we over rotate then mark that as 0 POSITION
server.on("/initialize", HTTP_POST, [](AsyncWebServerRequest *request) {
initializing = true;
stepper.moveTo(-10 * stepsPerRevolution);
request->send(200);
});
@ -103,13 +98,18 @@ void setup()
// Begin Server
server.begin();
}
void loop()
{
stepper.run();
if (stepper.distanceToGo() == 0 && initializing){
initializing = false;
stepper.stop();
stepper.setCurrentPosition(0);
}
Serial.print("Current Position: ");
Serial.println(stepper.currentPosition());
stepper.run();
}