Use remote requests for controlling,

rather than refreshing the page
This commit is contained in:
Joe Tretter
2022-10-13 10:58:36 -05:00
parent b15c4544fa
commit b997490431

View File

@@ -51,7 +51,7 @@ const char* loginIndex =
"<table width='20%' bgcolor='A09F9F' align='center'>" "<table width='20%' bgcolor='A09F9F' align='center'>"
"<tr>" "<tr>"
"<td colspan=2>" "<td colspan=2>"
"<center><font size=4><b>ESP32 Login Page</b></font></center>" "<center><font size=4><b>BLIND-Controller Login Page</b></font></center>"
"<br>" "<br>"
"</td>" "</td>"
"<br>" "<br>"
@@ -134,6 +134,7 @@ const char* serverIndex =
const char* movePage = const char* movePage =
"<html>" "<html>"
"<head>" "<head>"
"<title>Blind-POC</title>"
"<style type='text/css'>" "<style type='text/css'>"
"button {" "button {"
"width: 25%;" "width: 25%;"
@@ -141,12 +142,19 @@ const char* movePage =
"font-size:20px;" "font-size:20px;"
"} " "} "
"</style>" "</style>"
"<script>"
"let rq=url=>{"
" let xhr=new XMLHttpRequest();"
" xhr.open('PUT',url);"
" xhr.send();"
"}"
"</script>"
"</head>" "</head>"
"<body>" "<body>"
"<a href='http://192.168.10.141/close'><button>CLOSE (Full)</button></a>" "<button onclick='rq(\"/close\")'>CLOSE (Full)</button>"
"<a href='http://192.168.10.141/open'><button>OPEN (Full)</button></a>" "<button onclick='rq(\"/open\")'>OPEN (Full)</button>"
"<a href='http://192.168.10.141/rev'><button>Close (A bit)</button></a>" "<button onclick='rq(\"/rev\")'>Close (A bit)</button>"
"<a href='http://192.168.10.141/fwd'><button>Open (A bit)</button></a>" "<button onclick='rq(\"/fwd\")'>Open (A bit)</button>"
"</body>" "</body>"
"</html>"; "</html>";
@@ -216,7 +224,7 @@ void setup(void) {
server.send(200, "text/html", movePage); server.send(200, "text/html", movePage);
}); });
server.on("/open", HTTP_GET, []() { server.on("/open", HTTP_PUT, []() {
server.sendHeader("Connection", "close"); server.sendHeader("Connection", "close");
Serial.println("Opening"); Serial.println("Opening");
digitalWrite(motor1Pin1, LOW); digitalWrite(motor1Pin1, LOW);
@@ -227,7 +235,7 @@ void setup(void) {
server.send(200, "text/html", movePage); server.send(200, "text/html", movePage);
}); });
server.on("/close", HTTP_GET, []() { server.on("/close", HTTP_PUT, []() {
server.sendHeader("Connection", "close"); server.sendHeader("Connection", "close");
Serial.println("Closing"); Serial.println("Closing");
digitalWrite(motor1Pin1, HIGH); digitalWrite(motor1Pin1, HIGH);
@@ -239,7 +247,7 @@ void setup(void) {
server.send(200, "text/html", movePage); server.send(200, "text/html", movePage);
}); });
server.on("/fwd", HTTP_GET, []() { server.on("/fwd", HTTP_PUT, []() {
server.sendHeader("Connection", "close"); server.sendHeader("Connection", "close");
// Move the DC motor forward at maximum speed // Move the DC motor forward at maximum speed
Serial.println("Moving Forward"); Serial.println("Moving Forward");
@@ -251,7 +259,7 @@ void setup(void) {
server.send(200, "text/html", movePage); server.send(200, "text/html", movePage);
}); });
server.on("/rev", HTTP_GET, []() { server.on("/rev", HTTP_PUT, []() {
server.sendHeader("Connection", "close"); server.sendHeader("Connection", "close");
Serial.println("Moving Backward"); Serial.println("Moving Backward");
digitalWrite(motor1Pin1, HIGH); digitalWrite(motor1Pin1, HIGH);