Skip to content
Snippets Groups Projects
Commit a880285f authored by aliceg3's avatar aliceg3
Browse files

push code

parent 03e8d87e
No related branches found
No related tags found
No related merge requests found
// Importing necessary libraries
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
// Setting network credentials
const char* ssid = "Connectify-ESP32 Boot";
const char* password = "temp_pass1234";
const char* input_parameter1 = "output";
const char* input_parameter2 = "state";
// Creating a AsyncWebServer object
AsyncWebServer server(80);
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<title>ESP32 WEB SERVER</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<style>
html {font-family: Arial; display: inline-block; text-align: center;}
p {font-size: 3.0rem;}
body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}
.switch {position: relative; display: inline-block; width: 120px; height: 68px}
.switch input {display: none}
.slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 6px}
.slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 3px}
input:checked+.slider {background-color: #b30000}
input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
</style>
</head>
<body>
<h2>ESP32 WEB SERVER</h2>
%BUTTONPLACEHOLDER%
<script>function toggleCheckbox(element) {
var xhr = new XMLHttpRequest();
if(element.checked){ xhr.open("GET", "/update?output="+element.id+"&state=1", true); }
else { xhr.open("GET", "/update?output="+element.id+"&state=0", true); }
xhr.send();
}
</script>
</body>
</html>
)rawliteral";
// Replaces placeholder with button section in your web page
String processor(const String& var){
//Serial.println(var);
if(var == "BUTTONPLACEHOLDER"){
String buttons = "";
buttons += "<h4>Output - GPIO 2</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"2\" " + outputState(2) + "><span class=\"slider\"></span></label>";
return buttons;
}
return String();
}
String outputState(int output){
if(digitalRead(output)){
return "checked";
}
else {
return "";
}
}
void setup(){
// Serial port for debugging purposes
Serial.begin(115200);
pinMode(2,OUTPUT);
digitalWrite(2, LOW);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi");
}
// Print ESP 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);
});
// Send a GET request to <ESP_IP>/update?output=<inputMessage1>&state=<inputMessage2>
server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage1;
String inputMessage2;
// GET input1 value on <ESP_IP>/update?output=<inputMessage1>&state=<inputMessage2>
if (request->hasParam(input_parameter1) && request->hasParam(input_parameter2)) {
inputMessage1 = request->getParam(input_parameter1)->value();
inputMessage2 = request->getParam(input_parameter2)->value();
digitalWrite(inputMessage1.toInt(), inputMessage2.toInt());
}
else {
inputMessage1 = "No message sent";
inputMessage2 = "No message sent";
}
Serial.print("GPIO: ");
Serial.print(inputMessage1);
Serial.print(" - Set to: ");
Serial.println(inputMessage2);
request->send(200, "text/plain", "OK");
});
// Start server
server.begin();
}
void loop() {
}
\ No newline at end of file
...@@ -8,25 +8,34 @@ ...@@ -8,25 +8,34 @@
#include <WiFi.h> #include <WiFi.h>
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <AsyncTCP.h> #include <AsyncTCP.h>
#include <Stepper.h>
// Replace with your network credentials // Replace with your network credentials
const char* ssid = "Connectify-ESP32 Boot"; const char* ssid = "Connectify-ESP32 Boot";
const char* password = "temp_pass1234"; const char* password = "temp_pass1234";
const char* input_parameter1 = "output";
const char* input_parameter2 = "state";
#define PRESSUREPIN1 34 #define PRESSUREPIN1 34
#define PRESSUREPIN2 35 #define PRESSUREPIN2 35
#define motorIN1 #define motorIN1 33
#define motorIN2 #define motorIN2 32
#define motorIN3 #define motorIN3 26
#define motorIN4 #define motorIN4 27
#define PRESSURE1GOAL 1000
#define PRESSURE2GOAL 2034
#define PRESSUREGOAL1 357 Stepper motor(200, motorIN1, motorIN2, motorIN3, motorIN4);
#define PRESSUREGOAL2 2034
// current temperature & humidity, updated in loop() // current temperature & humidity, updated in loop()
int pressure1 = 0.0; int pressure1 = 0.0;
int pressure2 = 0.0; int pressure2 = 0.0;
float motorSpeed = 0.0; float motor1Speed = 0.0;
String pressure1status = "NOT YET...";
String pressure2status = "NOT YET...";
String motor1status = "Not running";
// Create AsyncWebServer object on port 80 // Create AsyncWebServer object on port 80
AsyncWebServer server(80); AsyncWebServer server(80);
...@@ -53,33 +62,59 @@ const char index_html[] PROGMEM = R"rawliteral( ...@@ -53,33 +62,59 @@ const char index_html[] PROGMEM = R"rawliteral(
h2 { font-size: 3.0rem; } h2 { font-size: 3.0rem; }
p { font-size: 3.0rem; } p { font-size: 3.0rem; }
.units { font-size: 1.2rem; } .units { font-size: 1.2rem; }
.dht-labels{ .sensorlabel{
font-size: 1.5rem; font-size: 1.5rem;
vertical-align:middle; vertical-align:center;
padding-bottom: 15px; padding-bottom: 15px;
} }
.statuslabel { font-size: 1.3rem; }
button2 {background-color: #555555;}
body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}
.switch {position: relative; display: inline-block; width: 120px; height: 68px}
.switch input {display: none}
.slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 6px}
.slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 3px}
input:checked+.slider {background-color: #b30000}
input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
</style> </style>
</head> </head>
<body> <body>
<h2>Remotely Adjustable Boot</h2> <h2>Remotely Adjustable Boot</h2>
<p> <p>
<i class="fas fa-thermometer-half" style="color:#059e8a;"></i> <i class="fas fa-thermometer-half" style="color:#4a32a8;"></i>
<span class="dht-labels">Pressure LEFT</span> <span class="sensorlabel">Pressure LEFT</span>
<br>
<span id="pressure1">%PRESSURE1%</span> <span id="pressure1">%PRESSURE1%</span>
<sup class="units">mmHg</sup> <sup class="units">mmHg</sup>
</p> </p>
<p> <p>
<i class="fas fa-thermometer-half" style="color:#059e8a;"></i> <span class="sensorlabel">Status:</span>
<span class="dht-labels">Pressure RIGHT</span> <span id="pressure1status" class="statuslabel">%PRESSURE1STATUS%</span>
</p>
<p>
<i class="fas fa-thermometer-half" style="color:#4a32a8;"></i>
<span class="sensorlabel">Pressure RIGHT</span>
<span id="pressure2">%PRESSURE2%</span> <span id="pressure2">%PRESSURE2%</span>
<sup class="units">mmHg</sup> <sup class="units">mmHg</sup>
</p> </p>
<p> <p>
<i class="fas fa-tint" style="color:#00add6;"></i> <span class="sensorlabel">Status:</span>
<span class="dht-labels">Motor Speed</span> <span id="pressure2status" class="statuslabel">%PRESSURE2STATUS%</span>
<span id="motorspeed">%MOTORSPEED%</span>
<sup class="units">%</sup>
</p> </p>
<p>
<i class="fas fa-wrench" style="color:#4a32a8;"></i>
<span class="sensorlabel">Motor Status</span>
<span id="motor1status">%MOTOR1STATUS%</span>
<sup class="units">torque</sup>
</p>
%BUTTONPLACEHOLDER%
<script>function toggleCheckbox(element) {
var xhr = new XMLHttpRequest();
if(element.checked){ xhr.open("GET", "/update?output="+element.id+"&state=1", true); }
else { xhr.open("GET", "/update?output="+element.id+"&state=0", true); }
xhr.send();
}
</script>
</body> </body>
<script> <script>
setInterval(function ( ) { setInterval(function ( ) {
...@@ -93,6 +128,17 @@ setInterval(function ( ) { ...@@ -93,6 +128,17 @@ setInterval(function ( ) {
xhttp.send(); xhttp.send();
}, 0.1 ) ; }, 0.1 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("pressure1status").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/pressure1status", true);
xhttp.send();
}, 0.1 ) ;
setInterval(function ( ) { setInterval(function ( ) {
var xhttp = new XMLHttpRequest(); var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() { xhttp.onreadystatechange = function() {
...@@ -104,31 +150,62 @@ setInterval(function ( ) { ...@@ -104,31 +150,62 @@ setInterval(function ( ) {
xhttp.send(); xhttp.send();
}, 0.1 ) ; }, 0.1 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("pressure2status").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/pressure2status", true);
xhttp.send();
}, 0.1 ) ;
setInterval(function ( ) { setInterval(function ( ) {
var xhttp = new XMLHttpRequest(); var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() { xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) { if (this.readyState == 4 && this.status == 200) {
document.getElementById("motorspeed").innerHTML = this.responseText; document.getElementById("motor1status").innerHTML = this.responseText;
} }
}; };
xhttp.open("GET", "/motorspeed", true); xhttp.open("GET", "/motor1status", true);
xhttp.send(); xhttp.send();
}, 0.1 ) ; }, 0.1 ) ;
</script> </script>
</html>)rawliteral"; </html>)rawliteral";
String outputState(int output){
if(digitalRead(output)){
return "checked";
}
else {
return "";
}
}
// Replaces placeholder with DHT values // Replaces placeholder with DHT values
String processor(const String& var){ String processor(const String& var){
//Serial.println(var); //Serial.println(var);
if(var == "PRESSURE1"){ if(var == "PRESSURE1"){
return String(pressure1); return String(pressure1);
} }
else if (var == "PRESSURE1STATUS"){
return String(pressure1status);
}
else if(var == "PRESSURE2"){ else if(var == "PRESSURE2"){
return String(pressure2); return String(pressure2);
} }
else if(var == "MOTORSPEED"){ else if (var == "PRESSURE2STATUS"){
return String(motorSpeed); return String(pressure2status);
}
else if(var == "MOTOR1STATUS"){
return String(motor1status);
}
else if(var == "BUTTONPLACEHOLDER"){
String buttons = "";
buttons += "<h4>Output - GPIO 2</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"2\" " + outputState(2) + "><span class=\"slider\"></span></label>";
return buttons;
} }
return String(); return String();
} }
...@@ -155,18 +232,46 @@ void setup(){ ...@@ -155,18 +232,46 @@ void setup(){
server.on("/pressure1", HTTP_GET, [](AsyncWebServerRequest *request){ server.on("/pressure1", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", String(pressure1).c_str()); request->send_P(200, "text/plain", String(pressure1).c_str());
}); });
server.on("/pressure1status", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", String(pressure1status).c_str());
});
server.on("/pressure2", HTTP_GET, [](AsyncWebServerRequest *request){ server.on("/pressure2", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", String(pressure2).c_str()); request->send_P(200, "text/plain", String(pressure2).c_str());
}); });
server.on("/motorspeed", HTTP_GET, [](AsyncWebServerRequest *request){ server.on("/pressure2status", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", String(motorSpeed).c_str()); request->send_P(200, "text/plain", String(pressure2status).c_str());
});
server.on("/motor1status", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", String(motor1status).c_str());
});
server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage1;
String inputMessage2;
// GET input1 value on <ESP_IP>/update?output=<inputMessage1>&state=<inputMessage2>
if (request->hasParam(input_parameter1) && request->hasParam(input_parameter2)) {
inputMessage1 = request->getParam(input_parameter1)->value();
inputMessage2 = request->getParam(input_parameter2)->value();
digitalWrite(inputMessage1.toInt(), inputMessage2.toInt());
}
else {
inputMessage1 = "No message sent";
inputMessage2 = "No message sent";
}
Serial.print("GPIO: ");
Serial.print(inputMessage1);
Serial.print(" - Set to: ");
Serial.println(inputMessage2);
request->send(200, "text/plain", "OK");
}); });
// Start server // Start server
server.begin(); server.begin();
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
} }
void loop(){ void loop(){
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) { if (currentMillis - previousMillis >= interval) {
...@@ -187,45 +292,10 @@ void loop(){ ...@@ -187,45 +292,10 @@ void loop(){
intervals2[aModulus2] += 1; intervals2[aModulus2] += 1;
intervalValues2[aModulus2] += int(a2 % 600); intervalValues2[aModulus2] += int(a2 % 600);
Serial.println("Reading #" + String(counter) + ": " + String(a1) + ", " + String(aModulus1));
Serial.println("Reading #" + String(counter) + ": " + String(a2) + ", " + String(aModulus2));
delay(10); delay(10);
} }
Serial.println("Intervals array #1: [");
for (int counter = 0; counter < 7; counter++) {
Serial.print(intervals1[counter]);
Serial.print(",");
}
Serial.print("]");
Serial.println("Interval Values array #1: ");
for (int counter = 0; counter < 7; counter++) {
Serial.print(intervalValues1[counter]);
Serial.print(",");
}
Serial.print("]");
Serial.println("**********");
Serial.println("Intervals array #2: [");
for (int counter = 0; counter < 7; counter++) {
Serial.print(intervals2[counter]);
Serial.print(",");
}
Serial.print("]");
Serial.println("Interval Values array #2: ");
for (int counter = 0; counter < 7; counter++) {
Serial.print(intervalValues2[counter]);
Serial.print(",");
}
Serial.print("]");
Serial.println("**********");
int maxIntervalSize1 = intervals1[0]; int maxIntervalSize1 = intervals1[0];
int maxIntervalIndex1 = 0; int maxIntervalIndex1 = 0;
for (int index = 1; index < 7; index ++) { for (int index = 1; index < 7; index ++) {
...@@ -234,7 +304,6 @@ void loop(){ ...@@ -234,7 +304,6 @@ void loop(){
maxIntervalSize1 = intervals1[index]; maxIntervalSize1 = intervals1[index];
} }
} }
Serial.println("maxIntervalIndex #1: " + maxIntervalIndex1);
int maxIntervalSize2 = intervals2[0]; int maxIntervalSize2 = intervals2[0];
int maxIntervalIndex2 = 0; int maxIntervalIndex2 = 0;
for (int index = 1; index < 7; index ++) { for (int index = 1; index < 7; index ++) {
...@@ -243,20 +312,22 @@ void loop(){ ...@@ -243,20 +312,22 @@ void loop(){
maxIntervalSize2 = intervals2[index]; maxIntervalSize2 = intervals2[index];
} }
} }
Serial.println("maxIntervalIndex #2: " + maxIntervalIndex2);
pressure1 = (intervalValues1[maxIntervalIndex1]/maxIntervalSize1)+ maxIntervalIndex1*600; pressure1 = (intervalValues1[maxIntervalIndex1]/maxIntervalSize1)+ maxIntervalIndex1*600;
pressure2 = (intervalValues2[maxIntervalIndex2]/maxIntervalSize2)+ maxIntervalIndex2*600; pressure2 = (intervalValues2[maxIntervalIndex2]/maxIntervalSize2)+ maxIntervalIndex2*600;
Serial.println("adcValue #1: " + pressure1);
Serial.println("adcValue #2: " + pressure2); if (((float)abs(pressure1 - PRESSURE1GOAL)/(float)PRESSURE1GOAL) <= 0.15){
Serial.println("******************************"); pressure1status = "LEFT GOAL REACHED";
float newMotorSpeed = 0;
if (isnan(newMotorSpeed)) {
Serial.println("Failed to read from DHT sensor!");
} }
else { else {
motorSpeed = newMotorSpeed; pressure1status = "not yet...";
Serial.println(motorSpeed); }
if (((float)abs(pressure2 - PRESSURE2GOAL)/(float)PRESSURE2GOAL) <= 0.15){
pressure2status = "RIGHT GOAL REACHED";
} }
else {
pressure2status = "not yet...";
}
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment