je-menvol/prog/prog.ino
2025-04-17 11:55:33 +02:00

223 lines
6.5 KiB
C++

#include <ESP32Servo.h>
#include "Adafruit_VL53L0X.h"
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
static const int servoPin = 13;
Servo servo1;
const int SDL = 22;
const int SDA2 = 21; // useless in the code
const int minHeight = 50;
const int maxHeight = 445;
int minServo = 0;
int maxServo = 180;
int lastAngle = -1;
float curHeight = 50;
float targetHeight = 250;
int equilibrium = 0;
int error = 0;
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
// Replace with your network credentials
const char* ssid = "ESP_ping";
const char* password = "pong";
const char* PARAM_INPUT_1 = "output";
const char* PARAM_INPUT_2 = "state";
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<title>ESP 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;}
h2 {font-size: 3.0rem;}
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 {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>ESP Web Server</h2>
%BUTTONPLACEHOLDER%
<script>
function sendSliderValue(element) {
var slider1 = document.getElementById("slider1");
var xhr = new XMLHttpRequest();
xhr.open("GET", "/update?output="+slider1.id+"&state=" + slider1.value, 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 += "<div class=\"slidecontainer\"><input type=\"range\" min=\"20\" max=\"450\" value=\"250\" class=\"slider\" id=\"slider1\" onchange=\"sendSliderValue(this.value)\"\"></div>";
buttons += "<div class=\"slidecontainer\"><input type=\"range\" min=\"20\" max=\"450\" value=\"250\" class=\"slider\" id=\"slider2\" onchange=\"sendSliderValue(this.value)\"\"></div>";
buttons += "<div class=\"slidecontainer\"><input type=\"range\" min=\"20\" max=\"450\" value=\"250\" class=\"slider\" id=\"slider3\" onchange=\"sendSliderValue(this.value)\"\"></div>";
buttons += "<div class=\"slidecontainer\"><input type=\"range\" min=\"20\" max=\"450\" value=\"250\" class=\"slider\" id=\"slider4\" onchange=\"sendSliderValue(this.value)\"\"></div>";
buttons += "<div class=\"slidecontainer\"><input type=\"range\" min=\"20\" max=\"450\" value=\"250\" class=\"slider\" id=\"slider5\" onchange=\"sendSliderValue(this.value)\"\"></div>";
return buttons;
}
return String();
}
auto mesureHeight(){
VL53L0X_RangingMeasurementData_t measure;
lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!
if (measure.RangeStatus != 4) { // phase failures have incorrect data
float height = measure.RangeMilliMeter;
if (height < 1000){
curHeight = height;
return height;
} else {
Serial.println("Incoherent measurement. Returning previous one.");
return curHeight;
}
}
else {
Serial.println(" out of range ");
return curHeight;
}
}
int requestedValue = 250;
void wifisetup() {
Serial.print("Setting AP (Access Point)…");
WiFi.softAP(ssid);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
// 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(PARAM_INPUT_1) && request->hasParam(PARAM_INPUT_2)) {
inputMessage1 = request->getParam(PARAM_INPUT_1)->value();
inputMessage2 = request->getParam(PARAM_INPUT_2)->value();
requestedValue = 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 setup() {
Serial.begin(115200); // Starts the serial communication
while (! Serial) {
delay(1);
}
wifisetup();
servo1.attach(servoPin);
Serial.println("Adafruit VL53L0X test");
if (!lox.begin()) {
Serial.println(F("Failed to boot VL53L0X"));
while(1);
}
Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
Serial.println("Setuping");
servo1.write(minServo);
delay(5000);
float h = mesureHeight();
if (h < maxHeight) {
Serial.println("CHANGE FAN SPEED");
while (1);
}
Serial.println("Setuping for downwards");
int angle = minServo;
while (h > maxHeight) {
++angle;
servo1.write(angle);
delay(100);
h = mesureHeight();
}
angle -= 2;
servo1.write(angle);
equilibrium = angle;
Serial.println("Done setuping have funning");
}
auto heightRatio(float height){
return max(min(1.0f, (height - minHeight) / (maxHeight-minHeight)), 0.0f);
}
int servoPosition(float ratio){
return (ratio * maxServo) + ((1-ratio) * minServo);
}
void loop() {
float h = mesureHeight();
Serial.print("Measure = ");
Serial.println(h);
int angle = 0;
if (abs(h - requestedValue) > 5.0f) {
Serial.println("HEYEYEYEYEYEYEY");
angle = equilibrium + ((h - requestedValue) * 1 / (7));
} else if (abs(h - requestedValue) > 2.0f) {
angle = equilibrium + ((h - requestedValue) * 1 / (7));
} else {
error = 0;
angle = equilibrium;
}
error += requestedValue - h;
Serial.print("angle = ");
Serial.print(angle);
Serial.print("; equilibrium = ");
Serial.println(equilibrium);
servo1.write(min(max(angle, minServo), maxServo));
delay(1);
}