diff --git a/prog/prog.ino b/prog/prog.ino index 8766946..b4254f2 100644 --- a/prog/prog.ino +++ b/prog/prog.ino @@ -10,17 +10,85 @@ const int SDL = 22; const int SDA2 = 21; // useless in the code const int minHeight = 50; -const int maxHeight = 455; +const int maxHeight = 445; -int minServo = 30; -int maxServo = 60; +int minServo = 0; +int maxServo = 180; int lastAngle = -1; float curHeight = 50; float targetHeight = 250; -float equilibrium = 0; +int equilibrium = 0; +int error = 0; + +#include +#include +#include + +// 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( + + + ESP Web Server + + + + + +

ESP Web Server

+ %BUTTONPLACEHOLDER% + + + +)rawliteral"; + +// Replaces placeholder with button section in your web page +String processor(const String& var){ + //Serial.println(var); + if(var == "BUTTONPLACEHOLDER"){ + String buttons = ""; + buttons += "
"; + buttons += "
"; + buttons += "
"; + buttons += "
"; + buttons += "
"; + + + return buttons; + } + return String(); +} auto mesureHeight(){ VL53L0X_RangingMeasurementData_t measure; @@ -41,11 +109,54 @@ auto mesureHeight(){ } } +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 /update?output=&state= + server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) { + String inputMessage1; + String inputMessage2; + // GET input1 value on /update?output=&state= + 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"); @@ -57,7 +168,7 @@ void setup() { Serial.println("Setuping"); servo1.write(minServo); - delay(2000); + delay(5000); float h = mesureHeight(); if (h < maxHeight) { Serial.println("CHANGE FAN SPEED"); @@ -91,9 +202,22 @@ void loop() { float h = mesureHeight(); Serial.print("Measure = "); Serial.println(h); - if (abs(h - targetHeight) > 1.0f) { - servo1.write(equilibrium + 5); + 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); } \ No newline at end of file